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/02/26 08:07:17 UTC

svn commit: r631105 - in /incubator/sling/trunk/sling/core/src: main/java/org/apache/sling/core/impl/request/SlingRequestPathInfo.java test/java/org/apache/sling/core/impl/request/SlingRequestPathInfoTest.java

Author: fmeschbe
Date: Mon Feb 25 23:07:13 2008
New Revision: 631105

URL: http://svn.apache.org/viewvc?rev=631105&view=rev
Log:
SLING-250 simplify RequestPathInfo parsing as the trailer of an URL not used
for resource resolution always either is empty or starts with a dot because
resource resolution splits the request URL at dots and not at slashes.
(see SLING-230)
Fixed testcases removing those, which assume cutting at slashes.

Modified:
    incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/SlingRequestPathInfo.java
    incubator/sling/trunk/sling/core/src/test/java/org/apache/sling/core/impl/request/SlingRequestPathInfoTest.java

Modified: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/SlingRequestPathInfo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/SlingRequestPathInfo.java?rev=631105&r1=631104&r2=631105&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/SlingRequestPathInfo.java (original)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/SlingRequestPathInfo.java Mon Feb 25 23:07:13 2008
@@ -23,12 +23,12 @@
 /**
  * microsling request URI parser that provides SlingRequestPathInfo for the
  * current request, based on the path of the Resource. The values provided by
- * this depend on the Resource.getPath() value, as the ResourceResolver might use
- * all or only part of the request URI path to locate the resource (see also
+ * this depend on the Resource.getPath() value, as the ResourceResolver might
+ * use all or only part of the request URI path to locate the resource (see also
  * SLING-60 ). What we're after is the remainder of the path, the part that was
  * not used to locate the Resource, and we split that part in different
  * subparts: selectors, extension and suffix.
- *
+ * 
  * @see SlingRequestPathInfoTest for a number of examples.
  */
 public class SlingRequestPathInfo implements RequestPathInfo {
@@ -59,95 +59,52 @@
         }
 
         resourcePath = r.getResourceMetadata().getResolutionPath();
-        if (resourcePath != null && !"/".equals(resourcePath)
+        if (resourcePath != null
             && pathToParse.length() >= resourcePath.length()) {
             pathToParse = pathToParse.substring(resourcePath.length());
         }
 
-        if (pathToParse.startsWith("/.")) {
-            final int lastDot = pathToParse.lastIndexOf('.');
-            
-            if (1==lastDot) {
-            	selectorString = null;
-                selectors = NO_SELECTORS;
-            } else {
-            	String tmpSel = pathToParse.substring(2, lastDot);
-                selectors = tmpSel.split("\\.");
-                selectorString = (selectors.length > 0) ? tmpSel : null;
-            	
-            	//selectorString = pathToParse.substring(firstDot, lastDot - firstDot);
-            	
-            }
-            
-            final int lastSlash = pathToParse.lastIndexOf('/');
-            if (lastSlash == 0) {
-            	suffix = "";
-            	extension = pathToParse.substring(lastDot + 1);
-            } else {
-            	final int secondSlash = pathToParse.indexOf("/", 1);
-            	suffix = pathToParse.substring(secondSlash);
-            	String prefix = pathToParse.substring(0, secondSlash);
-            	final int extensionDot = prefix.lastIndexOf(".");
-            	extension = prefix.substring(extensionDot + 1);
-            }
-        } else if (pathToParse.startsWith("/")) {
-            // resolution path is up in the hierarchy
-            // from request path: split the remainder
-            // in suffix and extension only
+        // separate selectors/ext from the suffix
+        int firstSlash = pathToParse.indexOf('/');
+        String pathToSplit;
+        if (firstSlash < 0) {
+            pathToSplit = pathToParse;
+            suffix = null;
+        } else {
+            pathToSplit = pathToParse.substring(0, firstSlash);
+            suffix = pathToParse.substring(firstSlash);
+        }
+
+        int lastDot = pathToSplit.lastIndexOf('.');
+
+        if (lastDot <= 1) {
+
+            // no selectors if only extension exists or selectors is empty
             selectorString = null;
             selectors = NO_SELECTORS;
-            final int lastDot = pathToParse.lastIndexOf('.');
-            final int lastSlash = pathToParse.lastIndexOf('/');
-            if(lastDot >= 0 && lastDot > lastSlash) {
-                suffix = pathToParse.substring(0, lastDot).substring(1);
-                extension = pathToParse.substring(lastDot + 1);
-            } else {
-                extension = null;
-                suffix = pathToParse;
-            }
 
         } else {
 
-            // separate selectors/ext from the suffix
-            int firstSlash = pathToParse.indexOf('/');
-            String pathToSplit;
-            if (firstSlash < 0) {
-                pathToSplit = pathToParse;
-                suffix = null;
-            } else {
-                pathToSplit = pathToParse.substring(0, firstSlash);
-                suffix = pathToParse.substring(firstSlash);
-            }
-
-
-            int lastDot = pathToSplit.lastIndexOf('.');
-
-            if (lastDot <= 1) {
-
-                // no selectors if only extension exists or selectors is empty
-                selectorString = null;
-                selectors = NO_SELECTORS;
-
-            } else {
-
-                // no selectors if splitting would give an empty array
-                String tmpSel = pathToSplit.substring(1, lastDot);
-                selectors = tmpSel.split("\\.");
-                selectorString = (selectors.length > 0) ? tmpSel : null;
-
-            }
-
-            // extension only if lastDot is not trailing
-            extension = (lastDot + 1 < pathToSplit.length())
-                    ? pathToSplit.substring(lastDot + 1)
-                    : null;
+            // no selectors if splitting would give an empty array
+            String tmpSel = pathToSplit.substring(1, lastDot);
+            selectors = tmpSel.split("\\.");
+            selectorString = (selectors.length > 0) ? tmpSel : null;
+
         }
+
+        // extension only if lastDot is not trailing
+        extension = (lastDot + 1 < pathToSplit.length())
+                ? pathToSplit.substring(lastDot + 1)
+                : null;
     }
 
-    private SlingRequestPathInfo(String resourcePath, String selectorString, String extension, String suffix) {
+    private SlingRequestPathInfo(String resourcePath, String selectorString,
+            String extension, String suffix) {
         this.resourcePath = resourcePath;
         this.selectorString = selectorString;
-        this.selectors = (selectorString != null) ? selectorString.split("\\.") : NO_SELECTORS;
+        this.selectors = (selectorString != null)
+                ? selectorString.split("\\.")
+                : NO_SELECTORS;
         this.extension = extension;
         this.suffix = suffix;
     }

Modified: incubator/sling/trunk/sling/core/src/test/java/org/apache/sling/core/impl/request/SlingRequestPathInfoTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/test/java/org/apache/sling/core/impl/request/SlingRequestPathInfoTest.java?rev=631105&r1=631104&r2=631105&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/test/java/org/apache/sling/core/impl/request/SlingRequestPathInfoTest.java (original)
+++ incubator/sling/trunk/sling/core/src/test/java/org/apache/sling/core/impl/request/SlingRequestPathInfoTest.java Mon Feb 25 23:07:13 2008
@@ -26,16 +26,6 @@
 /** Test the SlingRequestPathInfo class */
 public class SlingRequestPathInfoTest extends TestCase {
 
-    public void testSimplePath() {
-        RequestPathInfo p = new SlingRequestPathInfo(
-            new MockResource("/"), "/some/path.print.a4.html/some/suffix");
-        assertEquals("/", p.getResourcePath());
-        assertNull(p.getSelectorString());
-        assertEquals(0, p.getSelectors().length);
-        assertNull(p.getExtension());
-        assertEquals("/some/path.print.a4.html/some/suffix", p.getSuffix());
-    }
-
     public void testNullResource() {
         try {
             new SlingRequestPathInfo(null, "dontcare");
@@ -105,16 +95,6 @@
         assertEquals("/suffix", p.getSuffix());
     }
 
-    public void testSimpleSuffix() {
-        RequestPathInfo p = new SlingRequestPathInfo(
-            new MockResource("/"), "/some/path.print.a4.html/some/suffix");
-        assertEquals("/", p.getResourcePath());
-        assertNull(p.getSelectorString());
-        assertEquals(0, p.getSelectors().length);
-        assertNull(p.getExtension());
-        assertEquals("/some/path.print.a4.html/some/suffix", p.getSuffix());
-    }
-
     public void testAllOptions() {
         RequestPathInfo p = new SlingRequestPathInfo(
             new MockResource("/some/path"), "/some/path.print.a4.html/some/suffix");
@@ -139,13 +119,23 @@
 
     public void testPathOnly() {
         RequestPathInfo p = new SlingRequestPathInfo(new MockResource(
-            "/some/path/here"), "/some/path/here");
+        "/some/path/here"), "/some/path/here");
         assertEquals("/some/path/here", p.getResourcePath());
         assertNull(p.getSelectorString());
         assertEquals(0, p.getSelectors().length);
         assertNull(p.getExtension());
         assertNull(p.getSuffix());
     }
+    
+    public void testPathWithExtensionOnly() {
+        RequestPathInfo p = new SlingRequestPathInfo(new MockResource(
+            "/some/path/here.html"), "/some/path/here.html");
+        assertEquals("/some/path/here.html", p.getResourcePath());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
+        assertNull(p.getSuffix());
+    }
 
     public void testPathAndExtensionOnly() {
         RequestPathInfo p = new SlingRequestPathInfo(new MockResource(
@@ -189,16 +179,6 @@
         assertEquals("/some/suffix", p.getSuffix());
     }
 
-    public void testPartialResolutionA() {
-        RequestPathInfo p = new SlingRequestPathInfo(new MockResource(
-            "/some"), "/some/path.print.a4.html/some/suffix");
-        assertEquals("/some", p.getResourcePath());
-        assertNull(p.getSelectorString());
-        assertEquals(0, p.getSelectors().length);
-        assertNull(p.getExtension());
-        assertEquals("/path.print.a4.html/some/suffix", p.getSuffix());
-    }
-
     public void testPartialResolutionB() {
         RequestPathInfo p = new SlingRequestPathInfo(new MockResource(
             "/some/path"), "/some/path.print.a4.html/some/suffix");
@@ -232,40 +212,6 @@
         assertEquals("/some/suffix", p.getSuffix());
     }
 
-    public void testPartialResolutionE() {
-        RequestPathInfo p = new SlingRequestPathInfo(new MockResource(
-            "/some/path.print.a4.html"), "/some/path.print.a4.html/some/suffix");
-        assertEquals("/some/path.print.a4.html", p.getResourcePath());
-        assertNull(p.getSelectorString());
-        assertEquals(0, p.getSelectors().length);
-        assertNull(p.getExtension());
-        assertEquals("/some/suffix", p.getSuffix());
-    }
-    
-    public void testJIRA_SLING_173_a() {
-        RequestPathInfo p = 
-            new SlingRequestPathInfo(
-                    new MockResource("/ujax-tests"), 
-                    "/ujax-tests/12005879509741.json"
-            );
-        assertEquals("/ujax-tests", p.getResourcePath());
-        assertNull(p.getSelectorString());
-        assertEquals("json",p.getExtension());
-        assertEquals("12005879509741",p.getSuffix());
-    }
-
-    public void testJIRA_SLING_173_b() {
-        RequestPathInfo p = 
-            new SlingRequestPathInfo(
-                    new MockResource("/ujax-tests"), 
-                    "/ujax-tests/12005879509741.json."
-            );
-        assertEquals("/ujax-tests", p.getResourcePath());
-        assertNull(p.getSelectorString());
-        assertEquals("",p.getExtension());
-        assertEquals("12005879509741.json",p.getSuffix());
-    }
-
     public void testJIRA_250_a() {
     	RequestPathInfo p = 
             new SlingRequestPathInfo(
@@ -285,7 +231,7 @@
             );
         assertEquals("/", p.getResourcePath());
         assertEquals("json", p.getExtension());
-        assertEquals("", p.getSuffix());
+        assertNull(p.getSuffix());
         assertEquals("Selector string must not be null", "1", p.getSelectorString());
     }
     
@@ -309,7 +255,7 @@
             );
         assertEquals("/", p.getResourcePath());
         assertEquals("json", p.getExtension());
-        assertEquals("", p.getSuffix());
+        assertNull(p.getSuffix());
         assertNull(p.getSelectorString());
     }