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 2011/01/20 15:31:25 UTC

svn commit: r1061315 - in /sling/trunk/bundles/jcr/resource/src: main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java

Author: fmeschbe
Date: Thu Jan 20 14:31:24 2011
New Revision: 1061315

URL: http://svn.apache.org/viewvc?rev=1061315&view=rev
Log:
SLING-1938 If converting a resource to an absolute URL try to make sure to use the same scheme as for the provided request if a mapping for the same scheme is available and if the request has been provided. Plus a unit test for this functionality.

Modified:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
    sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java?rev=1061315&r1=1061314&r2=1061315&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java (original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java Thu Jan 20 14:31:24 2011
@@ -540,17 +540,20 @@ public class JcrResourceResolver
 
 
         // cut off scheme and host, if the same as requested
-        String schemehostport;
+        final String schemehostport;
+        final String schemePrefix;
         if (request != null) {
             schemehostport = MapEntry.getURI(request.getScheme(),
                 request.getServerName(), request.getServerPort(), "/");
-
-            LOGGER.debug("map: Mapping path {} for {}", resourcePath,
-                schemehostport);
+            schemePrefix = request.getScheme().concat("://");
+            LOGGER.debug(
+                "map: Mapping path {} for {} (at least with scheme prefix {})",
+                new Object[] { resourcePath, schemehostport, schemePrefix });
 
         } else {
 
             schemehostport = null;
+            schemePrefix = null;
             LOGGER.debug("map: Mapping path {} for default", resourcePath);
 
         }
@@ -635,10 +638,12 @@ public class JcrResourceResolver
 
                 LOGGER.debug("map: Match for Entry {}", mapEntry);
 
-                mappedPath = mappedPaths[0];
                 mappedPathIsUrl = !mapEntry.isInternal();
 
                 if (mappedPathIsUrl && schemehostport != null) {
+
+                    mappedPath = null;
+
                     for (final String candidate : mappedPaths) {
                         if (candidate.startsWith(schemehostport)) {
                             mappedPath = candidate.substring(schemehostport.length() - 1);
@@ -647,8 +652,21 @@ public class JcrResourceResolver
                                 "map: Found host specific mapping {} resolving to {}",
                                 candidate, mappedPath);
                             break;
+                        } else if (candidate.startsWith(schemePrefix)
+                            && mappedPath == null) {
+                            mappedPath = candidate;
                         }
                     }
+
+                    if (mappedPath == null) {
+                        mappedPath = mappedPaths[0];
+                    }
+
+                } else {
+
+                    // we can only go with assumptions selecting the first entry
+                    mappedPath = mappedPaths[0];
+
                 }
 
                 LOGGER.debug(

Modified: sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java?rev=1061315&r1=1061314&r2=1061315&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java (original)
+++ sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java Thu Jan 20 14:31:24 2011
@@ -100,10 +100,10 @@ public class JcrResourceResolverTest ext
         // test mappings
         mapRoot = getSession().getRootNode().addNode("etc", "nt:folder");
         Node map = mapRoot.addNode("map", "sling:Mapping");
-        Node https = map.addNode("https", "sling:Mapping");
-        https.addNode("localhost.443", "sling:Mapping");
         Node http = map.addNode("http", "sling:Mapping");
         http.addNode("localhost.80", "sling:Mapping");
+        Node https = map.addNode("https", "sling:Mapping");
+        https.addNode("localhost.443", "sling:Mapping");
 
         session.save();
 
@@ -1085,6 +1085,88 @@ public class JcrResourceResolverTest ext
         assertEquals("/playground/en.html", mapped11);
     }
 
+    public void testResolveVirtualHostHttpVsHttps() throws Exception {
+
+        final String host0 = "www.host.com";
+        final String host1 = "secure.host.com";
+        final String content = "/content/page";
+
+        Node virtualhost80 = mapRoot.getNode("map/http").addNode(host0 + ".80",
+            "sling:Mapping");
+        virtualhost80.setProperty(JcrResourceResolver.PROP_REDIRECT_INTERNAL,
+            content);
+        Node virtualhost443 = mapRoot.getNode("map/https").addNode(
+            host0 + ".443", "sling:Mapping");
+        virtualhost443.setProperty(JcrResourceResolver.PROP_REDIRECT_INTERNAL,
+            content);
+        session.save();
+
+        Thread.sleep(1000L);
+
+        // HTTP request
+
+        final HttpServletRequest requestHttp0 = new ResourceResolverTestRequest(
+            null, host0, -1, rootPath);
+        final Resource resHttp0 = resResolver.resolve(requestHttp0, "/playground.html");
+        assertNotNull(resHttp0);
+        assertEquals(content + "/playground.html", resHttp0.getPath());
+
+        final Resource resHttp1 = resResolver.resolve(requestHttp0,
+        "/playground/index.html");
+        assertNotNull(resHttp1);
+        assertEquals(content + "/playground/index.html", resHttp1.getPath());
+
+        // HTTPS request
+        final HttpServletRequest requestHttps0 = new ResourceResolverTestRequest(
+            "https", host0, -1, rootPath);
+        final Resource resHttps0 = resResolver.resolve(requestHttps0, "/playground.html");
+        assertNotNull(resHttps0);
+        assertEquals(content + "/playground.html", resHttps0.getPath());
+
+        final Resource resHttps1 = resResolver.resolve(requestHttps0,
+            "/playground/index.html");
+        assertNotNull(resHttps1);
+        assertEquals(content + "/playground/index.html", resHttps1.getPath());
+
+        // HTTP Mapping
+
+        final String mappedHttp00 = resResolver.map(resHttp0.getPath());
+        assertEquals("http://" + host0 + "/playground.html", mappedHttp00);
+        final String mappedHttp01 = resResolver.map(requestHttp0, resHttp0.getPath());
+        assertEquals("/playground.html", mappedHttp01);
+
+        final String mappedHttp10 = resResolver.map(resHttp1.getPath());
+        assertEquals("http://" + host0 + "/playground/index.html", mappedHttp10);
+        final String mappedHttp11 = resResolver.map(requestHttp0, resHttp1.getPath());
+        assertEquals("/playground/index.html", mappedHttp11);
+
+        // HTTPS Mapping
+
+        final HttpServletRequest requestHttp1 = new ResourceResolverTestRequest(
+            null, host1, -1, rootPath);
+        final HttpServletRequest requestHttps1 = new ResourceResolverTestRequest(
+            "https", host1, -1, rootPath);
+
+        final String mappedHttps00 = resResolver.map(resHttps0.getPath());
+        assertEquals("http://" + host0 + "/playground.html", mappedHttps00);
+        final String mappedHttps01 = resResolver.map(requestHttps0, resHttps0.getPath());
+        assertEquals("/playground.html", mappedHttps01);
+        final String mappedHttps02 = resResolver.map(requestHttp1, resHttps0.getPath());
+        assertEquals("http://" + host0 + "/playground.html", mappedHttps02);
+        final String mappedHttps03 = resResolver.map(requestHttps1, resHttps0.getPath());
+        assertEquals("https://" + host0 + "/playground.html", mappedHttps03);
+
+        final String mappedHttps10 = resResolver.map(resHttps1.getPath());
+        assertEquals("http://" + host0 + "/playground/index.html", mappedHttps10);
+        final String mappedHttps11 = resResolver.map(requestHttps0, resHttps1.getPath());
+        assertEquals("/playground/index.html", mappedHttps11);
+        final String mappedHttps12 = resResolver.map(requestHttp1, resHttps1.getPath());
+        assertEquals("http://" + host0 + "/playground/index.html", mappedHttps12);
+        final String mappedHttps13 = resResolver.map(requestHttps1, resHttps1.getPath());
+        assertEquals("https://" + host0 + "/playground/index.html", mappedHttps13);
+
+    }
+
     public void testResolveResourceAlias() throws Exception {
         // define an alias for the rootPath
         String alias = "testAlias";