incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r592017 - in /incubator/sling/trunk/microsling/microsling-core/src: main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java test/java/org/apache/sling/microsling/resource/MicroslingResourceResolverTest.java
Date Mon, 05 Nov 2007 14:34:37 GMT
Author: fmeschbe
Date: Mon Nov  5 06:34:36 2007
New Revision: 592017

URL: http://svn.apache.org/viewvc?rev=592017&view=rev
Log:
SLING-88 Allow resolution of relative paths in resolveRelativeSegments and optimize getResource(Resource,
String)
method to try to get the resource relative to the base resource

Modified:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/resource/MicroslingResourceResolverTest.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=592017&r1=592016&r2=592017&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 Nov  5 06:34:36 2007
@@ -92,10 +92,38 @@
     public Resource getResource(Resource base, String path)
             throws SlingException {
 
-        if (!path.startsWith("/")) {
-            path = base.getURI() + "/" + path;
+        // special case of absolute paths
+        if (path.startsWith("/")) {
+            return getResource(path);
         }
 
+        // resolve relative path segments now
+        path = resolveRelativeSegments(path);
+        if (path != null) {
+            if (path.length() == 0) {
+                // return the base resource
+                return base;
+            } else if (base.getRawData() instanceof Node) {
+                try {
+                    Node baseNode = (Node) base.getRawData();
+                    if (baseNode.hasNode(path)) {
+                        return new JcrNodeResource(baseNode.getNode(path));
+                    }
+
+                    log.error("getResource: There is no node at {} below {}",
+                        path, base.getURI());
+                    return null;
+                } catch (RepositoryException re) {
+                    log.error(
+                        "getResource: Problem accessing relative resource at "
+                            + path, re);
+                    return null;
+                }
+            }
+        }
+
+        // try (again) with absolute resource path
+        path = base.getURI() + "/" + path;
         return getResource(path);
     }
 
@@ -256,15 +284,19 @@
      */
     protected String resolveRelativeSegments(String path) {
 
-        // require non-empty absolute path !
-        if (path.length() == 0 || path.charAt(0) != '/') {
-            log.error("resolveRelativeSegments: Path '{}' must be absolute", path);
-            return null;
+        // don't care for empty paths
+        if (path.length() == 0) {
+            log.error("resolveRelativeSegments: Not modifying empty path");
+            return path;
         }
 
         // prepare the path buffer with trailing slash (simplifies impl)
-        char[] buf = new char[path.length() + 1];
-        path.getChars(0, path.length(), buf, 0);
+        int absOffset = (path.charAt(0) == '/') ? 0 : 1;
+        char[] buf= new char[path.length() + 1 + absOffset];
+        if (absOffset == 1) {
+            buf[0] = '/';
+        }
+        path.getChars(0, path.length(), buf, absOffset);
         buf[buf.length - 1] = '/';
 
         int lastSlash = 0; // last slash in path
@@ -311,15 +343,15 @@
             }
         }
 
+        String resolved;
         if (bufPos == 0 && numDots == 0) {
-            log.debug("resolveRelativeSegments: Resolving '{}' to '/'", path);
-            return "/";
-        } else if (bufPos == path.length()) {
-            log.debug("resolveRelativeSegments: No resolution for '{}' needed", path);
-            return path;
+            resolved = (absOffset == 0) ? "/" : "";
+        } else if ((bufPos - absOffset) == path.length()) {
+            resolved = path;
+        } else {
+            resolved = new String(buf, absOffset, bufPos-absOffset);
         }
 
-        String resolved = new String(buf, 0, bufPos);
         log.debug("resolveRelativeSegments: Resolving '{}' to '{}'", path, resolved);
         return resolved;
     }

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/resource/MicroslingResourceResolverTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/resource/MicroslingResourceResolverTest.java?rev=592017&r1=592016&r2=592017&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/resource/MicroslingResourceResolverTest.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/resource/MicroslingResourceResolverTest.java
Mon Nov  5 06:34:36 2007
@@ -118,15 +118,27 @@
         assertEquals("/az/...", resolver.resolveRelativeSegments("/az/..."));
         assertEquals("/az/bz/...", resolver.resolveRelativeSegments("/az/bz/..."));
 
-        assertNull(resolver.resolveRelativeSegments("a/b/c"));
-        assertNull(resolver.resolveRelativeSegments("az/bz/cz"));
-        assertNull(resolver.resolveRelativeSegments(""));
-
         try {
             resolver.resolveRelativeSegments(null);
             fail("Resolving null expects NullPointerException");
         } catch (NullPointerException npe) {
             // expected
         }
+    }
+
+    public void testResolveRelativeSegmentsRelative() {
+        assertEquals("a/b", resolver.resolveRelativeSegments("a/b"));
+        assertEquals("a", resolver.resolveRelativeSegments("a/b/.."));
+
+        assertEquals("b", resolver.resolveRelativeSegments("a/../b"));
+        assertEquals("a/c", resolver.resolveRelativeSegments("a/b/../c"));
+        assertEquals("c", resolver.resolveRelativeSegments("a/b/../../c"));
+        assertEquals("", resolver.resolveRelativeSegments("a/b/../.."));
+        assertEquals("a/c/d", resolver.resolveRelativeSegments("a/b/../c/d"));
+        assertNull(resolver.resolveRelativeSegments("a/b/../../../c"));
+
+        assertEquals("a/b/c", resolver.resolveRelativeSegments("a/b/c"));
+        assertEquals("az/bz/cz", resolver.resolveRelativeSegments("az/bz/cz"));
+        assertEquals("", resolver.resolveRelativeSegments(""));
     }
 }



Mime
View raw message