You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/01/23 15:59:13 UTC

svn commit: r614554 - in /incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver: ScriptHelper.java impl/DefaultSlingScript.java impl/LazyRequestReader.java

Author: fmeschbe
Date: Wed Jan 23 06:59:01 2008
New Revision: 614554

URL: http://svn.apache.org/viewvc?rev=614554&view=rev
Log:
SLING-186 Fix exception handling and prevent IOException getting the
request reader for multipart/form-data POST requests.

Added:
    incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/LazyRequestReader.java
Modified:
    incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/ScriptHelper.java
    incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java

Modified: incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/ScriptHelper.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/ScriptHelper.java?rev=614554&r1=614553&r2=614554&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/ScriptHelper.java (original)
+++ incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/ScriptHelper.java Wed Jan 23 06:59:01 2008
@@ -28,6 +28,8 @@
 
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.SlingIOException;
+import org.apache.sling.api.SlingServletException;
 import org.apache.sling.api.request.RequestDispatcherOptions;
 import org.apache.sling.api.scripting.SlingScript;
 import org.apache.sling.api.scripting.SlingScriptHelper;
@@ -66,16 +68,33 @@
         return response;
     }
 
-    public void include(String path) throws ServletException, IOException {
+    /**
+     * @trows SlingIOException Wrapping a <code>IOException</code> thrown
+     *        while handling the include.
+     * @throws SlingServletException Wrapping a <code>ServletException</code>
+     *             thrown while handling the include.
+     */
+    public void include(String path) {
         include(path, null);
     }
 
-    public void include(String path, RequestDispatcherOptions options)
-            throws ServletException, IOException {
+    /**
+     * @trows SlingIOException Wrapping a <code>IOException</code> thrown
+     *        while handling the include.
+     * @throws SlingServletException Wrapping a <code>ServletException</code>
+     *             thrown while handling the include.
+     */
+    public void include(String path, RequestDispatcherOptions options) {
         // TODO: Implement for options !!
         RequestDispatcher dispatcher = getRequest().getRequestDispatcher(path);
         if (dispatcher != null) {
-            dispatcher.include(getRequest(), getResponse());
+            try {
+                dispatcher.include(getRequest(), getResponse());
+            } catch (IOException ioe) {
+                throw new SlingIOException(ioe);
+            } catch (ServletException se) {
+                throw new SlingServletException(se);
+            }
         }
     }
 

Modified: incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java?rev=614554&r1=614553&r2=614554&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java (original)
+++ incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java Wed Jan 23 06:59:01 2008
@@ -44,8 +44,10 @@
 
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.SlingIOException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.scripting.ScriptEvaluationException;
 import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.api.scripting.SlingScript;
 import org.apache.sling.api.scripting.SlingScriptHelper;
@@ -68,33 +70,44 @@
         return scriptResource;
     }
 
-    public void eval(SlingBindings props) throws IOException, ServletException {
+    /**
+     * @throws ScriptEvaluationException
+     */
+    public void eval(SlingBindings props) {
 
-        Bindings bindings = verifySlingBindings(props);
+        String scriptName = getScriptResource().getPath();
 
-        ScriptContext ctx = new SimpleScriptContext();
-        ctx.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
-        ctx.setReader(((SlingHttpServletRequest) bindings.get(REQUEST)).getReader());
-        ctx.setWriter((Writer) bindings.get(OUT));
-        ctx.setErrorWriter(new LogWriter((Logger) bindings.get(LOG)));
+        try {
+            Bindings bindings = verifySlingBindings(scriptName, props);
+            
+            ScriptContext ctx = new SimpleScriptContext();
+            ctx.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
+            ctx.setReader(new LazyRequestReader((SlingHttpServletRequest) bindings.get(REQUEST)));
+            ctx.setWriter((Writer) bindings.get(OUT));
+            ctx.setErrorWriter(new LogWriter((Logger) bindings.get(LOG)));
 
-        Reader reader = getScriptReader();
+            Reader reader = getScriptReader();
 
-        try {
             // evaluate the script
             scriptEngine.eval(reader, ctx);
 
             // optionall flush the output channel
             Object flushObject = bindings.get(SlingBindings.FLUSH);
-            if (flushObject instanceof Boolean && ((Boolean) flushObject).booleanValue()) {
+            if (flushObject instanceof Boolean
+                && ((Boolean) flushObject).booleanValue()) {
                 ctx.getWriter().flush();
             }
 
             // allways flush the error channel
             ctx.getErrorWriter().flush();
 
+        } catch (IOException ioe) {
+            throw new ScriptEvaluationException(scriptName, ioe.getMessage(),
+                ioe);
         } catch (ScriptException se) {
-            throw new ServletException(se.getMessage(), se);
+            Throwable cause = (se.getCause() == null) ? se : se.getCause();
+            throw new ScriptEvaluationException(scriptName, se.getMessage(),
+                cause);
         }
     }
 
@@ -123,26 +136,27 @@
         return new BufferedReader(new InputStreamReader(input, encoding));
     }
 
-    private Bindings verifySlingBindings(SlingBindings slingBindings)
-            throws IOException, ServletException {
+    private Bindings verifySlingBindings(String scriptName,
+            SlingBindings slingBindings) throws IOException {
+        
         Object requestObject = slingBindings.get(REQUEST);
         if (!(requestObject instanceof SlingHttpServletRequest)) {
-            throw fail(REQUEST, "Missing or wrong type");
+            throw fail(scriptName, REQUEST, "Missing or wrong type");
         }
 
         Object responseObject = slingBindings.get(RESPONSE);
         if (!(responseObject instanceof SlingHttpServletResponse)) {
-            throw fail(RESPONSE, "Missing or wrong type");
+            throw fail(scriptName, RESPONSE, "Missing or wrong type");
         }
 
         Object resourceObject = slingBindings.get(RESOURCE);
         if (resourceObject != null && !(resourceObject instanceof Resource)) {
-            throw fail(RESOURCE, "Wrong type");
+            throw fail(scriptName, RESOURCE, "Wrong type");
         }
 
         Object writerObject = slingBindings.get(OUT);
         if (writerObject != null && !(writerObject instanceof PrintWriter)) {
-            throw fail(OUT, "Wrong type");
+            throw fail(scriptName, OUT, "Wrong type");
         }
 
         Bindings bindings = new SimpleBindings();
@@ -160,30 +174,30 @@
             sling = (SlingScriptHelper) slingObject;
 
             if (sling.getRequest() != requestObject) {
-                throw fail(REQUEST,
+                throw fail(scriptName, REQUEST,
                     "Not the same as request field of SlingScriptHelper");
             }
 
             if (sling.getResponse() != responseObject) {
-                throw fail(RESPONSE,
+                throw fail(scriptName, RESPONSE,
                     "Not the same as response field of SlingScriptHelper");
             }
 
             if (resourceObject != null
                 && sling.getRequest().getResource() != resourceObject) {
-                throw fail(RESOURCE,
+                throw fail(scriptName, RESOURCE,
                     "Not the same as resource of the SlingScriptHelper request");
             }
 
             if (writerObject != null
                 && sling.getResponse().getWriter() != writerObject) {
-                throw fail(OUT,
+                throw fail(scriptName, OUT,
                     "Not the same as writer of the SlingScriptHelper response");
             }
 
         } else {
 
-            throw fail(SLING, "Wrong type");
+            throw fail(scriptName, SLING, "Wrong type");
 
         }
 
@@ -191,7 +205,7 @@
         if (logObject == null) {
             logObject = LoggerFactory.getLogger(getLoggerName());
         } else if (!(logObject instanceof Logger)) {
-            throw fail(LOG, "Wrong type");
+            throw fail(scriptName, LOG, "Wrong type");
         }
 
         // set base variables
@@ -212,8 +226,10 @@
         return bindings;
     }
 
-    private ServletException fail(String variableName, String message) {
-        return new ServletException(variableName + ": " + message);
+    private ScriptEvaluationException fail(String scriptName,
+            String variableName, String message) {
+        return new ScriptEvaluationException(scriptName, variableName + ": "
+            + message);
     }
 
     private String getLoggerName() {

Added: incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/LazyRequestReader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/LazyRequestReader.java?rev=614554&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/LazyRequestReader.java (added)
+++ incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/LazyRequestReader.java Wed Jan 23 06:59:01 2008
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.scripting.resolver.impl;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.nio.CharBuffer;
+
+import javax.servlet.ServletRequest;
+
+class LazyRequestReader extends Reader {
+
+    private final ServletRequest request;
+
+    private Reader delegatee;
+
+    LazyRequestReader(ServletRequest request) {
+        this.request = request;
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (delegatee != null) {
+            delegatee.close();
+        }
+    }
+
+    @Override
+    public void mark(int readAheadLimit) throws IOException {
+        getReader().mark(readAheadLimit);
+    }
+
+    @Override
+    public boolean markSupported() {
+        return (delegatee != null) ? delegatee.markSupported() : false;
+    }
+
+    @Override
+    public int read() throws IOException {
+        return getReader().read();
+    }
+
+    @Override
+    public int read(char[] cbuf, int off, int len) throws IOException {
+        return getReader().read(cbuf, off, len);
+    }
+
+    @Override
+    public int read(char[] cbuf) throws IOException {
+        return getReader().read(cbuf);
+    }
+
+    @Override
+    public int read(CharBuffer target) throws IOException {
+        return getReader().read(target);
+    }
+
+    @Override
+    public boolean ready() throws IOException {
+        return getReader().ready();
+    }
+
+    @Override
+    public void reset() throws IOException {
+        if (delegatee != null) {
+            delegatee.reset();
+        }
+    }
+
+    @Override
+    public long skip(long n) throws IOException {
+        return getReader().skip(n);
+    }
+
+    private Reader getReader() throws IOException {
+        if (delegatee == null) {
+            delegatee = request.getReader();
+        }
+        return delegatee;
+    }
+}