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/12/15 01:49:29 UTC

svn commit: r726590 - in /incubator/sling/trunk/jcr/resource/src: main/java/org/apache/sling/jcr/resource/internal/helper/ test/java/org/apache/sling/jcr/resource/internal/

Author: fmeschbe
Date: Sun Dec 14 16:49:29 2008
New Revision: 726590

URL: http://svn.apache.org/viewvc?rev=726590&view=rev
Log:
SLING-776 Add support to ensure trailing slashes on patterns and replacements
if the patterns are simple strings. In the case of regular expressions,
trailing slashes are not added to not drestroy patterns.

Modified:
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntries.java
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java
    incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2Test.java

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntries.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntries.java?rev=726590&r1=726589&r2=726590&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntries.java (original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntries.java Sun Dec 14 16:49:29 2008
@@ -261,27 +261,33 @@
         Iterator<Resource> children = ResourceUtil.listChildren(parent);
         while (children.hasNext()) {
             Resource child = children.next();
+            
             String name = resolver.getProperty(child,
                 JcrResourceResolver2.PROP_REG_EXP);
+            boolean trailingSlash = false;
             if (name == null) {
-                name = ResourceUtil.getName(child);
+                name = ResourceUtil.getName(child).concat("/");
+                trailingSlash = true;
             }
-            String childPath = parentPath + name;
+
+            String childPath = parentPath.concat(name);
 
             MapEntry childResolveEntry = MapEntry.createResolveEntry(childPath,
-                child);
+                child, trailingSlash);
             if (childResolveEntry != null) {
                 resolveEntries.add(childResolveEntry);
             }
 
             List<MapEntry> childMapEntries = MapEntry.createMapEntry(childPath,
-                child);
+                child, trailingSlash);
             if (childMapEntries != null) {
                 mapEntries.addAll(childMapEntries);
             }
 
             // add trailing slash to child path to append the child
-            childPath += "/";
+            if (!trailingSlash) {
+                childPath = childPath.concat("/");
+            }
 
             // gather the children of this entry
             gather(resolver, resolveEntries, mapEntries, child, childPath);
@@ -321,11 +327,11 @@
                 }
 
                 // 1. entry with exact match
-                entries.add(new MapEntry(url + "$", redirect + ".html", status));
+                entries.add(new MapEntry(url + "$", redirect + ".html", status, false));
 
                 // 2. entry with match supporting selectors and extension
                 entries.add(new MapEntry(url + "(\\..*)", redirect + "$1",
-                    status));
+                    status, false));
             }
         }
     }
@@ -342,7 +348,7 @@
                     // this regular expression must match the whole URL !!
                     String url = "^" + ANY_SCHEME_HOST + extPath + "$";
                     String redirect = intPath;
-                    entries.add(new MapEntry(url, redirect, -1));
+                    entries.add(new MapEntry(url, redirect, -1, false));
                 }
             }
         }
@@ -367,7 +373,7 @@
             }
             for (Entry<String, List<String>> entry : map.entrySet()) {
                 entries.add(new MapEntry(ANY_SCHEME_HOST + entry.getKey(),
-                    entry.getValue().toArray(new String[0]), -1));
+                    entry.getValue().toArray(new String[0]), -1, false));
             }
         }
     }
@@ -383,7 +389,7 @@
                     String url = mapping.getTo();
                     String alias = mapping.getFrom();
                     if (!url.equals(alias)) {
-                        entries.add(new MapEntry(alias, url, -1));
+                        entries.add(new MapEntry(alias, url, -1, false));
                     }
                 }
             }
@@ -399,7 +405,7 @@
                     // this regular expression must match the whole URL !!
                     String path = "^" + intPath + "$";
                     String url = extPath;
-                    entries.add(new MapEntry(path, url, -1));
+                    entries.add(new MapEntry(path, url, -1, false));
                 }
             }
         }

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java?rev=726590&r1=726589&r2=726590&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java (original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java Sun Dec 14 16:49:29 2008
@@ -53,6 +53,13 @@
 
     private final int status;
 
+    public static String appendSlash(String path) {
+        if (!path.endsWith("/")) {
+            path = path.concat("/");
+        }
+        return path;
+    }
+    
     public static URI toURI(String uriPath) {
         for (int i = 0; i < PATH_TO_URL_MATCH.length; i++) {
             Matcher m = PATH_TO_URL_MATCH[i].matcher(uriPath);
@@ -69,7 +76,7 @@
         return null;
     }
 
-    public static MapEntry createResolveEntry(String url, Resource resource) {
+    public static MapEntry createResolveEntry(String url, Resource resource, boolean trailingSlash) {
         ValueMap props = resource.adaptTo(ValueMap.class);
         if (props != null) {
             String redirect = props.get(
@@ -77,20 +84,20 @@
             if (redirect != null) {
                 int status = props.get(
                     JcrResourceResolver2.PROP_REDIRECT_EXTERNAL_STATUS, 302);
-                return new MapEntry(url, redirect, status);
+                return new MapEntry(url, redirect, status, trailingSlash);
             }
 
             String[] internalRedirect = props.get(
                 JcrResourceResolver2.PROP_REDIRECT_INTERNAL, String[].class);
             if (internalRedirect != null) {
-                return new MapEntry(url, internalRedirect, -1);
+                return new MapEntry(url, internalRedirect, -1, trailingSlash);
             }
         }
 
         return null;
     }
 
-    public static List<MapEntry> createMapEntry(String url, Resource resource) {
+    public static List<MapEntry> createMapEntry(String url, Resource resource, boolean trailingSlash) {
         ValueMap props = resource.adaptTo(ValueMap.class);
         if (props != null) {
             String redirect = props.get(
@@ -115,7 +122,7 @@
                     internalRedirect.length);
                 for (String redir : internalRedirect) {
                     if (!redir.contains("$")) {
-                        prepEntries.add(new MapEntry(redir, url, status));
+                        prepEntries.add(new MapEntry(redir, url, status, trailingSlash));
                     }
                 }
 
@@ -128,11 +135,21 @@
         return null;
     }
 
-    public MapEntry(String url, String redirect, int status) {
-        this(url, new String[] { redirect }, status);
+    public MapEntry(String url, String redirect, int status, boolean trailingSlash) {
+        this(url, new String[] { redirect }, status, trailingSlash);
     }
 
-    public MapEntry(String url, String[] redirect, int status) {
+    public MapEntry(String url, String[] redirect, int status, boolean trailingSlash) {
+        
+        // ensure trailing slashes on redirects if the url
+        // ends with a trailing slash
+        if (trailingSlash) {
+            url = appendSlash(url);
+            for (int i = 0; i < redirect.length; i++) {
+                redirect[i] = appendSlash(redirect[i]);
+            }
+        }
+        
         this.urlPattern = Pattern.compile(url);
         this.redirect = redirect;
         this.status = status;

Modified: incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2Test.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2Test.java?rev=726590&r1=726589&r2=726590&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2Test.java (original)
+++ incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2Test.java Sun Dec 14 16:49:29 2008
@@ -273,8 +273,6 @@
 
         Thread.sleep(1000L);
 
-        resResolver = resFac.getResourceResolver(session);
-
         Resource res = resResolver.resolve(request, rootPath);
         assertNotNull(res);
         assertEquals(rootPath, res.getPath());
@@ -306,7 +304,8 @@
         localhost443.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
             "http://localhost");
         session.save();
-        resResolver = resFac.getResourceResolver(session);
+
+        Thread.sleep(1000L);
 
         Resource res = resResolver.resolve(request, rootPath);
         assertNotNull(res);
@@ -347,8 +346,6 @@
 
         Thread.sleep(1000L);
 
-        resResolver = resFac.getResourceResolver(session);
-
         Resource res = resResolver.resolve(request, "/playground.html");
         assertNotNull(res);
         assertEquals("/content/playground.html", res.getPath());
@@ -410,6 +407,48 @@
             public String getScheme() {
                 return "http";
             }
+            
+            @Override
+            public String getServerName() {
+                return "virtual.host.com";
+            }
+            
+            @Override
+            public int getServerPort() {
+                return 8080;
+            }
+        };
+        
+        Node virtualhost80 = mapRoot.getNode("map/http").addNode(
+            "virtual.host.com.8080", "sling:Mapping");
+        virtualhost80.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
+            "/content/virtual");
+        session.save();
+        
+        Thread.sleep(1000L);
+        
+        final Resource res0 = resResolver.resolve(request, "/playground.html");
+        assertNotNull(res0);
+        assertEquals("/content/virtual/playground.html", res0.getPath());
+        
+        final Resource res1 = resResolver.resolve(request,
+        "/playground/en.html");
+        assertNotNull(res1);
+        assertEquals("/content/virtual/playground/en.html", res1.getPath());
+        
+        final String mapped0 = resResolver.map(request, res0.getPath());
+        assertEquals("http://virtual.host.com:8080/playground.html", mapped0);
+        
+        final String mapped1 = resResolver.map(request, res1.getPath());
+        assertEquals("http://virtual.host.com:8080/playground/en.html", mapped1);
+    }
+    
+    public void testResolveVirtualHostHttp8080Root() throws Exception {
+        HttpServletRequest request = new ResourceResolverTestRequest(rootPath) {
+            @Override
+            public String getScheme() {
+                return "http";
+            }
 
             @Override
             public String getServerName() {
@@ -425,19 +464,19 @@
         Node virtualhost80 = mapRoot.getNode("map/http").addNode(
             "virtual.host.com.8080", "sling:Mapping");
         virtualhost80.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
-            "/content/virtual");
+            "/");
         session.save();
 
         Thread.sleep(1000L);
 
         final Resource res0 = resResolver.resolve(request, "/playground.html");
         assertNotNull(res0);
-        assertEquals("/content/virtual/playground.html", res0.getPath());
+        assertEquals("/playground.html", res0.getPath());
 
         final Resource res1 = resResolver.resolve(request,
             "/playground/en.html");
         assertNotNull(res1);
-        assertEquals("/content/virtual/playground/en.html", res1.getPath());
+        assertEquals("/playground/en.html", res1.getPath());
 
         final String mapped0 = resResolver.map(request, res0.getPath());
         assertEquals("http://virtual.host.com:8080/playground.html", mapped0);