You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2007/12/10 21:52:47 UTC

svn commit: r603042 - in /incubator/sling/trunk/microsling/microsling-core/src: main/java/org/apache/sling/microsling/resource/ test/java/org/apache/sling/microsling/integration/ test/java/org/apache/sling/microsling/resource/

Author: bdelacretaz
Date: Mon Dec 10 12:52:36 2007
New Revision: 603042

URL: http://svn.apache.org/viewvc?rev=603042&view=rev
Log:
SLING-117 - do not go up the path when resolving Resources for GET or HEAD requests

Added:
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/GetWithSuffixTest.java   (with props)
Modified:
    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/resource/ResourcePathIterator.java
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/SyntheticResourceProvider.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/resource/ResourcePathIteratorTest.java

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=603042&r1=603041&r2=603042&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 Mon Dec 10 12:52:36 2007
@@ -102,18 +102,22 @@
     /**
      * Resolves the Resource from the request
      */
-    public Resource resolve(ServletRequest request) throws SlingException {
+    public Resource resolve(ServletRequest servletRequest) throws SlingException {
         Resource result = null;
         String path = null;
         
         // look for a real JCR Resource
-        String pathInfo = SlingRequestPaths.getPathInfo((HttpServletRequest)request);
+        final HttpServletRequest request = (HttpServletRequest)servletRequest;
+        String pathInfo = SlingRequestPaths.getPathInfo(request);
         try {
             Session session = getSession();
-            final ResourcePathIterator it = new ResourcePathIterator(pathInfo);
+            final ResourcePathIterator it = new ResourcePathIterator(pathInfo,request.getMethod());
+            String resolutionPath = "";
             while (it.hasNext() && result == null) {
-                result = getResource(session, it.next());
+                resolutionPath = it.next();
+                result = getResource(session, resolutionPath);
             }
+            
         } catch (RepositoryException re) {
             throw new SlingException("RepositoryException for path=" + path, re);
         }

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/ResourcePathIterator.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/ResourcePathIterator.java?rev=603042&r1=603041&r2=603042&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/ResourcePathIterator.java (original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/ResourcePathIterator.java Mon Dec 10 12:52:36 2007
@@ -39,13 +39,21 @@
  *      /some
  *    </li>
  *  </ol>
+ *  
+ *  The above rules are not valid for GET or HEAD requests, where
+ *  we do not go up the path: for those requests, the path is
+ *  only split at dots that follow the last slash
  */
 class ResourcePathIterator implements Iterator<String> {
 
     private String nextPath;
+    private final String httpMethod;
+    private final int lastSlashPos;
     
-    ResourcePathIterator(String path) {
+    ResourcePathIterator(String path,String httpMethod) {
         nextPath = path;
+        this.httpMethod = httpMethod;
+        lastSlashPos = (path == null ? -1 : path.lastIndexOf('/'));
     }
     
     public boolean hasNext() {
@@ -54,7 +62,19 @@
 
     public String next() {
         final String result = nextPath;
-        final int pos = Math.max(result.lastIndexOf('.'),result.lastIndexOf('/'));
+        
+        int pos = -1;
+        if("GET".equals(httpMethod) || "HEAD".equals(httpMethod)) {
+            // SLING-117: for GET and POST, do not go up the path to resolve resources,
+            // only split at dots that follow the last slash
+            pos = result.lastIndexOf('.');
+            if(pos < lastSlashPos) {
+                pos = -1;
+            }
+        } else {
+            pos = Math.max(result.lastIndexOf('.'),result.lastIndexOf('/'));
+        }
+        
         if(pos < 0) {
             nextPath = null;
         } else {

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/SyntheticResourceProvider.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/SyntheticResourceProvider.java?rev=603042&r1=603041&r2=603042&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/SyntheticResourceProvider.java (original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/SyntheticResourceProvider.java Mon Dec 10 12:52:36 2007
@@ -56,7 +56,9 @@
     Resource getSyntheticResource(String pathInfo) {
         Resource result = null;
         
-        final ResourcePathIterator it = new ResourcePathIterator(pathInfo);
+        // for synthetic resources, the ResourcePathIterator must go up the path
+        // like for POST methods, even if the actual method is a GET
+        final ResourcePathIterator it = new ResourcePathIterator(pathInfo,"POST");
         while (it.hasNext() && result == null) {
             final String path = it.next();
             for(Pattern p : pathPattern) {

Added: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/GetWithSuffixTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/GetWithSuffixTest.java?rev=603042&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/GetWithSuffixTest.java (added)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/GetWithSuffixTest.java Mon Dec 10 12:52:36 2007
@@ -0,0 +1,80 @@
+/*
+ * 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.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletResponse;
+
+/** GET requests with a suffix should fail with a 404, otherwise
+ *  we get a lot of extra possible URLs which point to the same
+ *  content.
+ */
+public class GetWithSuffixTest extends RenderingTestBase {
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        // set test values
+        testText = "This is a test " + System.currentTimeMillis();
+
+        // create the test node, 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>();
+        props.put("text", testText);
+        displayUrl = 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);
+    }
+    
+    public void testWithExactUrl() throws IOException {
+        final String toDelete = uploadTestScript("rendering-test.esp","html.esp");
+        try {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
+            assertTrue("Content includes ESP marker",content.contains("ESP template"));
+            assertTrue("Content contains formatted test text",content.contains("<p>" + testText + "</p>"));
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+
+    public void testWithExtraPathA() throws IOException {
+        final String toDelete = uploadTestScript("rendering-test.esp","html.esp");
+        try {
+            assertHttpStatus(displayUrl + "/extra.html", HttpServletResponse.SC_NOT_FOUND);
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+    
+    /** behavior seems slightly different if using GET.esp vs. html.esp for the
+     *  script name, verify that both give a 404
+     */
+    public void testWithExtraPathB() throws IOException {
+        final String toDelete = uploadTestScript("rendering-test.esp","GET.esp");
+        try {
+            assertHttpStatus(displayUrl + "/extra/more.a4.html", HttpServletResponse.SC_NOT_FOUND);
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+}

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

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

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/resource/ResourcePathIteratorTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/resource/ResourcePathIteratorTest.java?rev=603042&r1=603041&r2=603042&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/resource/ResourcePathIteratorTest.java (original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/resource/ResourcePathIteratorTest.java Mon Dec 10 12:52:36 2007
@@ -24,43 +24,46 @@
 
 public class ResourcePathIteratorTest extends TestCase {
     
+    public final static String GET = "GET";
+    public final static String POST = "POST";
+    
     public void testNullInput() {
-        final Iterator<String> it = new ResourcePathIterator(null);
+        final Iterator<String> it = new ResourcePathIterator(null,POST);
         assertFalse(it.hasNext());
     }
     
     public void testEmptyInput() {
-        final Iterator<String> it = new ResourcePathIterator("");
-        assertFalse(it.hasNext());
+        final Iterator<String> it = new ResourcePathIterator("",POST);
+        assertFalse("done iterating", it.hasNext());
     }
     
     public void testNoSeparators() {
-        final Iterator<String> it = new ResourcePathIterator("MickeyMouseWasHere");
+        final Iterator<String> it = new ResourcePathIterator("MickeyMouseWasHere",POST);
         assertTrue(it.hasNext());
         assertEquals("MickeyMouseWasHere",it.next());
-        assertFalse(it.hasNext());
+        assertFalse("done iterating", it.hasNext());
     }
     
     public void testSlashOnly() {
-        final Iterator<String> it = new ResourcePathIterator("/MickeyMouseWasHere/ok");
+        final Iterator<String> it = new ResourcePathIterator("/MickeyMouseWasHere/ok",POST);
         assertTrue(it.hasNext());
         assertEquals("/MickeyMouseWasHere/ok",it.next());
         assertTrue(it.hasNext());
         assertEquals("/MickeyMouseWasHere",it.next());
-        assertFalse(it.hasNext());
+        assertFalse("done iterating", it.hasNext());
     }
     
     public void testDotOnly() {
-        final Iterator<String> it = new ResourcePathIterator("MickeyMouseWasHere.ok");
+        final Iterator<String> it = new ResourcePathIterator("MickeyMouseWasHere.ok",POST);
         assertTrue(it.hasNext());
         assertEquals("MickeyMouseWasHere.ok",it.next());
         assertTrue(it.hasNext());
         assertEquals("MickeyMouseWasHere",it.next());
-        assertFalse(it.hasNext());
+        assertFalse("done iterating", it.hasNext());
     }
     
     public void testRealisticPath() {
-        final Iterator<String> it = new ResourcePathIterator("/some/stuff.print.a4/more");
+        final Iterator<String> it = new ResourcePathIterator("/some/stuff.print.a4/more",POST);
         assertTrue(it.hasNext());
         assertEquals("/some/stuff.print.a4/more",it.next());
         assertTrue(it.hasNext());
@@ -71,17 +74,53 @@
         assertEquals("/some/stuff",it.next());
         assertTrue(it.hasNext());
         assertEquals("/some",it.next());
-        assertFalse(it.hasNext());
+        assertFalse("done iterating", it.hasNext());
+    }
+    
+    public void testGetA() {
+        final Iterator<String> it = new ResourcePathIterator("/some/stuff/more.a4.html",GET);
+        assertTrue(it.hasNext());
+        assertEquals("/some/stuff/more.a4.html",it.next());
+        assertTrue(it.hasNext());
+        assertEquals("/some/stuff/more.a4",it.next());
+        assertTrue(it.hasNext());
+        assertEquals("/some/stuff/more",it.next());
+        assertFalse("done iterating", it.hasNext());
+    }
+    
+    public void testGetB() {
+        final Iterator<String> it = new ResourcePathIterator("/some/stuff/more.html",GET);
+        assertTrue(it.hasNext());
+        assertEquals("/some/stuff/more.html",it.next());
+        assertTrue(it.hasNext());
+        assertEquals("/some/stuff/more",it.next());
+        assertFalse("done iterating", it.hasNext());
+    }
+    
+    public void testGetC() {
+        final Iterator<String> it = new ResourcePathIterator("/some/stuff/more",GET);
+        assertTrue(it.hasNext());
+        assertEquals("/some/stuff/more",it.next());
+        assertFalse("done iterating", it.hasNext());
+    }
+    
+    public void testGetD() {
+        final Iterator<String> it = new ResourcePathIterator("/some/stuff.print/more.html",GET);
+        assertTrue(it.hasNext());
+        assertEquals("/some/stuff.print/more.html",it.next());
+        assertTrue(it.hasNext());
+        assertEquals("/some/stuff.print/more",it.next());
+        assertFalse("done iterating", it.hasNext());
     }
     
     public void testRelativePath() {
-        final Iterator<String> it = new ResourcePathIterator("some/stuff.print");
+        final Iterator<String> it = new ResourcePathIterator("some/stuff.print",POST);
         assertTrue(it.hasNext());
         assertEquals("some/stuff.print",it.next());
         assertTrue(it.hasNext());
         assertEquals("some/stuff",it.next());
         assertTrue(it.hasNext());
         assertEquals("some",it.next());
-        assertFalse(it.hasNext());
+        assertFalse("done iterating", it.hasNext());
     }
 }