incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r597442 - in /incubator/sling/trunk/microsling/microsling-core/src: main/java/org/apache/sling/microsling/ main/java/org/apache/sling/microsling/resource/ main/java/org/apache/sling/microsling/scripting/helpers/ main/java/org/apache/sling/m...
Date Thu, 22 Nov 2007 15:52:02 GMT
Author: bdelacretaz
Date: Thu Nov 22 07:52:01 2007
New Revision: 597442

URL: http://svn.apache.org/viewvc?rev=597442&view=rev
Log:
SLING-104 : infinite loop in sling.include fixed, using SlingRequestPaths utility. Also added
infinite loop detection, and integration test

Added:
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/IncludeTest.java
  (with props)
    incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/include-test.esp
Modified:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/MicroslingSlingHttpServletRequest.java
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptHelper.java
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingMainServlet.java
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/MicroslingHttpTestBase.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/MicroslingSlingHttpServletRequest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/MicroslingSlingHttpServletRequest.java?rev=597442&r1=597441&r2=597442&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/MicroslingSlingHttpServletRequest.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/MicroslingSlingHttpServletRequest.java
Thu Nov 22 07:52:01 2007
@@ -40,6 +40,7 @@
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.services.ServiceLocator;
+import org.apache.sling.api.wrappers.SlingRequestPaths;
 import org.apache.sling.microsling.request.helpers.MicroslingRequestPathInfo;
 import org.apache.sling.microsling.request.helpers.MicroslingRequestProgressTracker;
 import org.apache.sling.microsling.request.helpers.SlingRequestParameterMap;
@@ -75,8 +76,8 @@
         this.serviceLocator = serviceLocator;
         this.resourceResolver = new MicroslingResourceResolver(session);
         this.resource = resourceResolver.resolve(request);
-        this.requestPathInfo = new MicroslingRequestPathInfo(resource,
-            request.getPathInfo());
+        this.requestPathInfo = 
+            new MicroslingRequestPathInfo(resource, SlingRequestPaths.getPathInfo(request));
     }
 
     /*

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java?rev=597442&r1=597441&r2=597442&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java
Thu Nov 22 07:52:01 2007
@@ -44,6 +44,7 @@
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.wrappers.SlingRequestPaths;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -104,7 +105,7 @@
     public Resource resolve(ServletRequest request) throws SlingException {
         Resource result = null;
         String path = null;
-        String pathInfo = ((HttpServletRequest) request).getPathInfo();
+        String pathInfo = SlingRequestPaths.getPathInfo((HttpServletRequest)request);
         try {
             Session session = getSession();
             final ResourcePathIterator it = new ResourcePathIterator(pathInfo);

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptHelper.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptHelper.java?rev=597442&r1=597441&r2=597442&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptHelper.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptHelper.java
Thu Nov 22 07:52:01 2007
@@ -26,9 +26,13 @@
 import javax.servlet.ServletException;
 import javax.servlet.ServletResponse;
 
+import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;
+import org.apache.sling.api.wrappers.SlingRequestPaths;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /** Simple script helper providing access to the (wrapped) response, the
  * on-demand writer and a simple API for request inclusion. Instances of this
@@ -37,9 +41,20 @@
  */
 public class ScriptHelper {
 
+    private static final Logger log = LoggerFactory.getLogger(ScriptHelper.class);
+    
     private final SlingHttpServletRequest request;
 
     private final SlingHttpServletResponse response;
+    
+    public static final String INCLUDE_COUNTER = "ScriptHelper.include.counter";
+    public static final int MAX_INCLUDE_RECURSION_LEVEL = 20;
+    
+    public static class InfiniteIncludeLoopException extends SlingException {
+        InfiniteIncludeLoopException(String path) {
+            super("Infinite include loop (> " + MAX_INCLUDE_RECURSION_LEVEL + " levels)
for path '" + path + "'");
+        }
+    }
 
     public ScriptHelper(SlingHttpServletRequest request, SlingHttpServletResponse response)
{
         this.request = request;
@@ -54,7 +69,30 @@
         return response;
     }
 
-    public void include(String path) throws ServletException, IOException {
+    public void include(String path) throws ServletException, IOException, SlingException
{
+        
+        // detect infinite loops
+        Integer recursionLevel = (Integer)getRequest().getAttribute(INCLUDE_COUNTER);
+        if(recursionLevel == null) {
+            recursionLevel = new Integer(1);
+        } else if(recursionLevel.intValue() > MAX_INCLUDE_RECURSION_LEVEL) {
+            throw new InfiniteIncludeLoopException(path);
+        } else {
+            recursionLevel = new Integer(recursionLevel.intValue() + 1);
+        }
+        getRequest().setAttribute(INCLUDE_COUNTER, recursionLevel);
+
+        if(log.isDebugEnabled()) {
+            log.debug(
+                    "Including path '" 
+                    + path 
+                    + "' in request '" 
+                    + SlingRequestPaths.getPathInfo(getRequest())
+                    + "'"
+            );
+        }
+        
+        // include path
         RequestDispatcher dispatcher = getRequest().getRequestDispatcher(path);
         if (dispatcher != null) {
             dispatcher.include(getRequest(), getResponse());

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingMainServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingMainServlet.java?rev=597442&r1=597441&r2=597442&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingMainServlet.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingMainServlet.java
Thu Nov 22 07:52:01 2007
@@ -39,6 +39,7 @@
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.scripting.SlingScriptResolver;
+import org.apache.sling.api.wrappers.SlingRequestPaths;
 import org.apache.sling.microsling.MicroslingSlingHttpServletRequest;
 import org.apache.sling.microsling.MicroslingSlingHttpServletResponse;
 import org.apache.sling.microsling.contenttype.ResponseContentTypeResolverFilter;
@@ -157,10 +158,11 @@
         HttpServletResponse hRes = (HttpServletResponse) resp;
 
         // root redirect
-        if (hReq.getPathInfo() == null) {
-            hRes.sendRedirect(hReq.getRequestURI() + "/" + indexPage);
+        final String pathInfo = SlingRequestPaths.getPathInfo(hReq);
+        if (pathInfo == null) {
+            hRes.sendRedirect(SlingRequestPaths.getRequestURI(hReq) + "/" + indexPage);
             return;
-        } else if ("/".equals(hReq.getPathInfo())) {
+        } else if ("/".equals(pathInfo)) {
             hRes.sendRedirect(indexPage);
             return;
         }

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java?rev=597442&r1=597441&r2=597442&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
Thu Nov 22 07:52:01 2007
@@ -33,6 +33,7 @@
 import org.apache.sling.api.request.RequestParameter;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.servlets.SlingAllMethodsServlet;
+import org.apache.sling.api.wrappers.SlingRequestPaths;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -98,7 +99,7 @@
                 currentPath = currentNode.getPath();
                 s = currentNode.getSession();
             } else {
-                currentPath = request.getPathInfo();
+                currentPath = SlingRequestPaths.getPathInfo(request);
                 // TODO not very convenient way to get a Session...
                 final Resource root = request.getResourceResolver().getResource("/");
                 final Node rootNode = (Node)root.getRawData();
@@ -126,7 +127,7 @@
             if(currentNode!=null) {
                 pathToCreate = currentPath;
             } else {
-                pathToCreate = request.getPathInfo();
+                pathToCreate = SlingRequestPaths.getPathInfo(request);
             }
             if(!pathToCreate.endsWith("/")) {
                 pathToCreate += "/";
@@ -159,7 +160,10 @@
                 redirectPath += redirectExtension;
             }
             
-            final String redirectUrl = request.getContextPath() + request.getServletPath()
+ redirectPath; 
+            final String redirectUrl = 
+                SlingRequestPaths.getContextPath(request)
+                + SlingRequestPaths.getServletPath(request)
+                + redirectPath; 
             if(log.isDebugEnabled()) {
                 log.debug("Redirecting to " + redirectUrl);
             }

Added: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/IncludeTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/IncludeTest.java?rev=597442&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/IncludeTest.java
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/IncludeTest.java
Thu Nov 22 07:52:01 2007
@@ -0,0 +1,95 @@
+/*
+ * 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.microsling.integration;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletResponse;
+
+/** Test the {link ScriptHelper#include) functionality */
+ public class IncludeTest extends MicroslingHttpTestBase {
+
+    private String nodeUrlA;
+    private String testTextA;
+    private String nodeUrlB;
+    private String testTextB;
+    private String nodeUrlC;
+    private String scriptPath;
+    private String toDelete;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        
+        // Create the test nodes under a path that's specific to this class to
+        // allow collisions
+        final String url = HTTP_BASE_URL + "/" + getClass().getSimpleName() + "." + System.currentTimeMillis();
+        final Map<String,String> props = new HashMap<String,String>();
+        
+        // Create two test nodes and store their paths
+        testTextA = "Text A " + System.currentTimeMillis();
+        props.put("text", testTextA);
+        nodeUrlA = testClient.createNode(url, props);
+        
+        // Node B stores the path of A, so that the test script can
+        // include A when rendering B
+        testTextB = "Text B " + System.currentTimeMillis();
+        props.put("text", testTextB);
+        props.put("pathToInclude", new URL(nodeUrlA).getPath());
+        nodeUrlB = testClient.createNode(url, props);
+        
+        // Node C is used for the infinite loop detection test
+        props.remove("pathToInclude");
+        props.put("testInfiniteLoop","true");
+        nodeUrlC = testClient.createNode(url, props);
+
+        // the rendering script goes under /sling/scripts in the repository
+        scriptPath = "/sling/scripts/nt/unstructured";
+        testClient.mkdirs(WEBDAV_BASE_URL, scriptPath);
+        toDelete = uploadTestScript(scriptPath,"include-test.esp","html.esp");
+    }
+    
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        testClient.delete(toDelete);
+    }
+
+    public void testWithoutInclude() throws IOException {
+        final String content = getContent(nodeUrlA + ".html", CONTENT_TYPE_HTML);
+        assertTrue("Content includes ESP marker",content.contains("ESP template"));
+        assertTrue("Content contains formatted test text",content.contains("<p class=\"main\">"
+ testTextA + "</p>"));
+        assertFalse("Nothing has been included",content.contains("<p>Including"));
+    }
+
+    public void testWithInclude() throws IOException {
+        final String content = getContent(nodeUrlB + ".html", CONTENT_TYPE_HTML);
+        assertTrue("Content includes ESP marker",content.contains("ESP template"));
+        assertTrue("Content contains formatted test text",content.contains("<p class=\"main\">"
+ testTextB + "</p>"));
+        assertTrue("Include has been used",content.contains("<p>Including"));
+    }
+    
+    public void testInfiniteLoopDetection() throws IOException {
+        // Node C has a property that causes an infinite include loop,
+        // microsling must return an error 500 in this case
+        assertHttpStatus(nodeUrlC + ".html", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+    }
+
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/IncludeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/IncludeTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/MicroslingHttpTestBase.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/MicroslingHttpTestBase.java?rev=597442&r1=597441&r2=597442&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/MicroslingHttpTestBase.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/MicroslingHttpTestBase.java
Thu Nov 22 07:52:01 2007
@@ -17,6 +17,7 @@
 package org.apache.sling.microsling.integration;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 
@@ -104,6 +105,21 @@
             );
         }
         return get.getResponseBodyAsString();
+    }
+
+    /** upload rendering test script, and return its URL for future deletion */
+    protected String uploadTestScript(String scriptPath, String localFilename,String filenameOnServer)
throws IOException {
+        final String url = WEBDAV_BASE_URL + scriptPath + "/" + filenameOnServer;
+        final String testFile = "/integration-test/" + localFilename;
+        final InputStream data = getClass().getResourceAsStream(testFile);
+        try {
+            testClient.upload(url, data);
+        } finally {
+            if(data!=null) {
+                data.close();
+            }
+        }
+        return url;
     }
 
 }

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java?rev=597442&r1=597441&r2=597442&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java
Thu Nov 22 07:52:01 2007
@@ -17,29 +17,15 @@
 package org.apache.sling.microsling.integration;
 
 import java.io.IOException;
-import java.io.InputStream;
 
 /** Base class for rendering tests
  */
 class RenderingTestBase extends MicroslingHttpTestBase {
-
     protected String scriptPath;
     protected String testText;
     protected String displayUrl;
-
-    /** upload rendering test script, and return its URL for future deletion */
+    
     protected String uploadTestScript(String localFilename,String filenameOnServer) throws
IOException {
-        final String url = WEBDAV_BASE_URL + scriptPath + "/" + filenameOnServer;
-        final String testFile = "/integration-test/" + localFilename;
-        final InputStream data = getClass().getResourceAsStream(testFile);
-        try {
-            testClient.upload(url, data);
-        } finally {
-            if(data!=null) {
-                data.close();
-            }
-        }
-        return url;
+        return uploadTestScript(scriptPath, localFilename, filenameOnServer);
     }
-
 }

Added: incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/include-test.esp
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/include-test.esp?rev=597442&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/include-test.esp
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/include-test.esp
Thu Nov 22 07:52:01 2007
@@ -0,0 +1,27 @@
+<%-- used by IncludeTest --%>
+<html>
+	<body>
+		<h1>ESP template</h1>
+		<p class="main"><%= resource.item.text %></p>
+		
+		<%
+			if(resource.item.pathToInclude) {
+			  %>
+			  <p>pathToInclude = <%= resource.item.pathToInclude %></p>
+  		  	  <p>Including <%= resource.item.pathToInclude %></p>
+			  <%
+			  sling.include(resource.item.pathToInclude + ".html");
+			}
+		%>
+		
+		<%
+			if(resource.item.testInfiniteLoop) {
+			  %>
+			  <p>testInfiniteLoop = <%= resource.item.testInfiniteLoop %></p>
+			  <%
+			  // try to include the item itself, to cause an infinite loop
+			  sling.include(resource.getURI() + ".html");
+			}
+		%>
+	</body>
+</html>



Mime
View raw message