incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r726787 - in /incubator/sling/trunk/jcr/resource/src: main/java/org/apache/sling/jcr/resource/internal/ main/java/org/apache/sling/jcr/resource/internal/helper/ test/java/org/apache/sling/jcr/resource/internal/
Date Mon, 15 Dec 2008 19:57:29 GMT
Author: fmeschbe
Date: Mon Dec 15 11:57:29 2008
New Revision: 726787

URL: http://svn.apache.org/viewvc?rev=726787&view=rev
Log:
SLING-778 Consolidate duplicate reverse (internal-to-external) mappings and
enable check for scheme/host/port prefix of mappings to select the best
mappings. In addition order the reverse mappings to check for best match

Modified:
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.java
    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/JcrResourceResolver2.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.java?rev=726787&r1=726786&r2=726787&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.java
(original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.java
Mon Dec 15 11:57:29 2008
@@ -181,15 +181,41 @@
             
         }
         
+        // cut off scheme and host, if the same as requested
+        String schemehostport;
+        if (request != null) {
+            StringBuilder sb = new StringBuilder();
+            sb.append(request.getScheme()).append("://");
+            sb.append(request.getServerName());
+            if (request.getServerPort() > 0) {
+                sb.append(':').append(request.getServerPort());
+            }
+            sb.append("/");
+            schemehostport = sb.toString();
+        } else {
+            schemehostport = null;
+        }
+
         for (MapEntry mapEntry : resourceMapper.getMapMaps()) {
             String[] mappedPaths = mapEntry.replace(mappedPath);
-            if (mappedPaths != null && mappedPaths.length > 0) {
-                log.debug(
-                    "resolve: MapEntry {} matches, mapped path is {}",
-                    mapEntry, mappedPaths);
+            if (mappedPaths != null) {
 
                 mappedPath = mappedPaths[0];
                 mappedPathIsUrl = !mapEntry.isInternal();
+
+                if (mappedPathIsUrl && schemehostport != null) {
+                    for (String candidate : mappedPaths) {
+                        if (candidate.startsWith(schemehostport)) {
+                            mappedPath = candidate.substring(schemehostport.length() - 1);
+                            break;
+                        }
+                    }
+                }
+
+                log.debug(
+                    "resolve: MapEntry {} matches, mapped path is {}",
+                    mapEntry, mappedPath);
+
                 break;
             }
         }
@@ -204,30 +230,13 @@
             mappedPath = mappedPath.concat(resolutionPathInfo);
         }
         
+        // mangle the namespaces
+        mappedPath = mangleNamespaces(mappedPath);
+
         if (mappedPathIsUrl) {
-            
-            // cut off scheme and host, if the same as requested
-            if (request != null) {
-                StringBuilder sb = new StringBuilder();
-                sb.append(request.getScheme()).append("://");
-                sb.append(request.getServerName());
-                if (request.getServerPort() > 0) {
-                    sb.append(':').append(request.getServerPort());
-                }
-                sb.append("/");
-                
-                if (mappedPath.startsWith(sb.toString())) {
-                    mappedPath = mappedPath.substring(sb.length()-1);
-                }
-            }
-            
-            // TODO: probably need to mangle name spaces
             return mappedPath;
         }
 
-        // mangle the namespaces
-        mappedPath = mangleNamespaces(mappedPath);
-
         // prepend servlet context path if we have a request
         if (request != null && request.getContextPath() != null
             && request.getContextPath().length() > 0) {

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=726787&r1=726786&r2=726787&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
Mon Dec 15 11:57:29 2008
@@ -20,6 +20,7 @@
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -27,6 +28,9 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.TreeSet;
 import java.util.Map.Entry;
 
 import javax.jcr.RepositoryException;
@@ -66,7 +70,7 @@
 
     private List<MapEntry> resolveMaps;
 
-    private List<MapEntry> mapMaps;
+    private Collection<MapEntry> mapMaps;
 
     private Set<String> namespaces;
 
@@ -114,7 +118,7 @@
         try {
 
             List<MapEntry> newResolveMaps = new ArrayList<MapEntry>();
-            List<MapEntry> newMapMaps = new ArrayList<MapEntry>();
+            SortedMap<String, MapEntry> newMapMaps = new TreeMap<String, MapEntry>();
 
             // load the /etc/map entries into the maps
             loadResolverMap(resolver, newResolveMaps, newMapMaps);
@@ -123,11 +127,11 @@
             loadVanityPaths(resolver, newResolveMaps);
             loadConfiguration(factory, newResolveMaps);
 
-            // load the configuration into the mapper map 
+            // load the configuration into the mapper map
             loadMapConfiguration(factory, newMapMaps);
 
             this.resolveMaps = newResolveMaps;
-            this.mapMaps = newMapMaps;
+            this.mapMaps = new TreeSet<MapEntry>(newMapMaps.values());
 
         } finally {
 
@@ -185,7 +189,7 @@
         return resolveMaps;
     }
 
-    public List<MapEntry> getMapMaps() {
+    public Collection<MapEntry> getMapMaps() {
         return mapMaps;
     }
 
@@ -246,7 +250,8 @@
     // ---------- internal
 
     private void loadResolverMap(JcrResourceResolver2 resolver,
-            List<MapEntry> resolveEntries, List<MapEntry> mapEntries) {
+            Collection<MapEntry> resolveEntries,
+            Map<String, MapEntry> mapEntries) {
         // the standard map configuration
         Resource res = resolver.getResource(MAP_ROOT);
         if (res != null) {
@@ -255,13 +260,13 @@
     }
 
     private void gather(JcrResourceResolver2 resolver,
-            List<MapEntry> resolveEntries, List<MapEntry> mapEntries,
-            Resource parent, String parentPath) {
+            Collection<MapEntry> resolveEntries,
+            Map<String, MapEntry> mapEntries, Resource parent, String parentPath) {
         // scheme list
         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;
@@ -281,7 +286,10 @@
             List<MapEntry> childMapEntries = MapEntry.createMapEntry(childPath,
                 child, trailingSlash);
             if (childMapEntries != null) {
-                mapEntries.addAll(childMapEntries);
+                for (MapEntry mapEntry : childMapEntries) {
+                    addMapEntry(mapEntries, mapEntry.getPattern(),
+                        mapEntry.getRedirect()[0], mapEntry.getStatus());
+                }
             }
 
             // add trailing slash to child path to append the child
@@ -327,7 +335,8 @@
                 }
 
                 // 1. entry with exact match
-                entries.add(new MapEntry(url + "$", redirect + ".html", status, false));
+                entries.add(new MapEntry(url + "$", redirect + ".html", status,
+                    false));
 
                 // 2. entry with match supporting selectors and extension
                 entries.add(new MapEntry(url + "(\\..*)", redirect + "$1",
@@ -379,7 +388,7 @@
     }
 
     private void loadMapConfiguration(JcrResourceResolverFactoryImpl factory,
-            List<MapEntry> entries) {
+            Map<String, MapEntry> entries) {
         // URL Mappings
         Mapping[] mappings = factory.getMappings();
         if (mappings != null) {
@@ -389,7 +398,7 @@
                     String url = mapping.getTo();
                     String alias = mapping.getFrom();
                     if (!url.equals(alias)) {
-                        entries.add(new MapEntry(alias, url, -1, false));
+                        addMapEntry(entries, alias, url, -1);
                     }
                 }
             }
@@ -405,9 +414,25 @@
                     // this regular expression must match the whole URL !!
                     String path = "^" + intPath + "$";
                     String url = extPath;
-                    entries.add(new MapEntry(path, url, -1, false));
+                    addMapEntry(entries, path, url, -1);
                 }
             }
         }
     }
+
+    private void addMapEntry(Map<String, MapEntry> entries, String path,
+            String url, int status) {
+        MapEntry entry = entries.get(path);
+        if (entry == null) {
+            entry = new MapEntry(path, url, status, false);
+        } else {
+            String[] redir = entry.getRedirect();
+            String[] newRedir = new String[redir.length + 1];
+            System.arraycopy(redir, 0, newRedir, 0, redir.length);
+            newRedir[redir.length] = url;
+            entry = new MapEntry(entry.getPattern(), newRedir,
+                entry.getStatus(), false);
+        }
+        entries.put(path, entry);
+    }
 }

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=726787&r1=726786&r2=726787&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
Mon Dec 15 11:57:29 2008
@@ -22,6 +22,7 @@
 import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Comparator;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -37,7 +38,7 @@
  * 
  * @see http://cwiki.apache.org/SLING/flexible-resource-resolution.html
  */
-public class MapEntry {
+public class MapEntry implements Comparable<MapEntry> {
 
     private static final Pattern[] PATH_TO_URL_MATCH = {
         Pattern.compile("http/([^/]+)\\.80(/.*)?$"),
@@ -174,6 +175,10 @@
         return null;
     }
 
+    public String getPattern() {
+        return urlPattern.toString();
+    }
+    
     public String[] getRedirect() {
         return redirect;
     }
@@ -186,6 +191,28 @@
         return status;
     }
 
+    // ---------- Comparable
+
+    public int compareTo(MapEntry m) {
+        if (this == m) {
+            return 0;
+        }
+
+        int tlen = urlPattern.toString().length();
+        int mlen = m.urlPattern.toString().length();
+        if (tlen < mlen) {
+            return 1;
+        } else if (tlen > mlen) {
+            return -1;
+        }
+
+        // lentghs are equal, but the entries are not
+        // so order m after this
+        return 1;
+    }
+
+    // ---------- Object overwrite
+    
     @Override
     public String toString() {
         StringBuilder buf = new StringBuilder();

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=726787&r1=726786&r2=726787&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
Mon Dec 15 11:57:29 2008
@@ -249,23 +249,8 @@
     }
 
     public void testResolveResourceExternalRedirect() throws Exception {
-        HttpServletRequest request = new ResourceResolverTestRequest(rootPath) {
-            @Override
-            public String getScheme() {
-                return "https";
-            }
-
-            @Override
-            public String getServerName() {
-                return "localhost";
-            }
-
-            @Override
-            public int getServerPort() {
-                return -1;
-            }
-        };
-
+        HttpServletRequest request = new ResourceResolverTestRequest("https",
+            null, -1, rootPath);
         Node localhost443 = mapRoot.getNode("map/https/localhost.443");
         localhost443.setProperty(JcrResourceResolver2.PROP_REDIRECT_EXTERNAL,
             "http://localhost");
@@ -283,23 +268,8 @@
     }
 
     public void testResolveResourceInternalRedirectUrl() throws Exception {
-        HttpServletRequest request = new ResourceResolverTestRequest(rootPath) {
-            @Override
-            public String getScheme() {
-                return "https";
-            }
-
-            @Override
-            public String getServerName() {
-                return "localhost";
-            }
-
-            @Override
-            public int getServerPort() {
-                return -1;
-            }
-        };
-
+        HttpServletRequest request = new ResourceResolverTestRequest("https",
+            null, -1, rootPath);
         Node localhost443 = mapRoot.getNode("map/https/localhost.443");
         localhost443.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
             "http://localhost");
@@ -318,23 +288,8 @@
     }
 
     public void testResolveResourceInternalRedirectPath() throws Exception {
-        HttpServletRequest request = new ResourceResolverTestRequest(rootPath) {
-            @Override
-            public String getScheme() {
-                return "https";
-            }
-
-            @Override
-            public String getServerName() {
-                return "localhost";
-            }
-
-            @Override
-            public int getServerPort() {
-                return -1;
-            }
-        };
-
+        HttpServletRequest request = new ResourceResolverTestRequest("https",
+            null, -1, rootPath);
         Node localhost443 = mapRoot.getNode("map/https/localhost.443");
         Node toContent = localhost443.addNode("_playground_designground_",
             "sling:Mapping");
@@ -360,23 +315,8 @@
     }
 
     public void testResolveVirtualHostHttp80() throws Exception {
-        HttpServletRequest request = new ResourceResolverTestRequest(rootPath) {
-            @Override
-            public String getScheme() {
-                return "http";
-            }
-
-            @Override
-            public String getServerName() {
-                return "virtual.host.com";
-            }
-
-            @Override
-            public int getServerPort() {
-                return -1;
-            }
-        };
-
+        HttpServletRequest request = new ResourceResolverTestRequest(null,
+            "virtual.host.com", -1, rootPath);
         Node virtualhost80 = mapRoot.getNode("map/http").addNode(
             "virtual.host.com.80", "sling:Mapping");
         virtualhost80.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
@@ -405,70 +345,212 @@
         assertEquals("/playground/en.html", mapped11);
     }
 
+    public void testResolveVirtualHostHttp80Multiple() throws Exception {
+
+        final String de = "de";
+        final String en = "en";
+        final String hostDE = de + ".host.com";
+        final String hostEN = en + ".host.com";
+        final String contentDE = "/content/" + de;
+        final String contentEN = "/content/" + en;
+
+        Node virtualhost80 = mapRoot.getNode("map/http").addNode(
+            hostDE + ".80", "sling:Mapping");
+        virtualhost80.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
+            contentDE);
+        virtualhost80 = mapRoot.getNode("map/http").addNode(hostEN + ".80",
+            "sling:Mapping");
+        virtualhost80.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
+            contentEN);
+        session.save();
+
+        Thread.sleep(1000L);
+
+        // de content mapping
+
+        final HttpServletRequest requestDE = new ResourceResolverTestRequest(
+            null, hostDE, -1, rootPath);
+        final Resource resDE0 = resResolver.resolve(requestDE,
+            "/playground.html");
+        assertNotNull(resDE0);
+        assertEquals(contentDE + "/playground.html", resDE0.getPath());
+
+        final Resource resDE1 = resResolver.resolve(requestDE,
+            "/playground/index.html");
+        assertNotNull(resDE1);
+        assertEquals(contentDE + "/playground/index.html", resDE1.getPath());
+
+        final String mappedDE00 = resResolver.map(resDE0.getPath());
+        assertEquals("http://" + hostDE + "/playground.html", mappedDE00);
+        final String mappedDE01 = resResolver.map(requestDE, resDE0.getPath());
+        assertEquals("/playground.html", mappedDE01);
+
+        final String mappedDE10 = resResolver.map(resDE1.getPath());
+        assertEquals("http://" + hostDE + "/playground/index.html", mappedDE10);
+        final String mappedDE11 = resResolver.map(requestDE, resDE1.getPath());
+        assertEquals("/playground/index.html", mappedDE11);
+
+        // en content mapping
+
+        final HttpServletRequest requestEN = new ResourceResolverTestRequest(
+            null, hostEN, -1, rootPath);
+        final Resource resEN0 = resResolver.resolve(requestEN,
+            "/playground.html");
+        assertNotNull(resEN0);
+        assertEquals(contentEN + "/playground.html", resEN0.getPath());
+
+        final Resource resEN1 = resResolver.resolve(requestEN,
+            "/playground/index.html");
+        assertNotNull(resEN1);
+        assertEquals(contentEN + "/playground/index.html", resEN1.getPath());
+
+        final String mappedEN00 = resResolver.map(resEN0.getPath());
+        assertEquals("http://" + hostEN + "/playground.html", mappedEN00);
+        final String mappedEN01 = resResolver.map(requestEN, resEN0.getPath());
+        assertEquals("/playground.html", mappedEN01);
+
+        final String mappedEN10 = resResolver.map(resEN1.getPath());
+        assertEquals("http://" + hostEN + "/playground/index.html", mappedEN10);
+        final String mappedEN11 = resResolver.map(requestEN, resEN1.getPath());
+        assertEquals("/playground/index.html", mappedEN11);
+    }
+
+    public void testResolveVirtualHostHttp80MultipleRoot() throws Exception {
+
+        final String de = "de";
+        final String en = "en";
+        final String fr = "fr";
+        final String hostDE = de + ".host.com";
+        final String hostEN = en + ".host.com";
+        final String hostFR = fr + ".host.com";
+
+        Node virtualhost80 = mapRoot.getNode("map/http").addNode(
+            hostDE + ".80", "sling:Mapping");
+        virtualhost80.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
+            "/");
+        virtualhost80 = mapRoot.getNode("map/http").addNode(hostEN + ".80",
+            "sling:Mapping");
+        virtualhost80.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
+            "/");
+        session.save();
+
+        Thread.sleep(1000L);
+
+        // de content mapping
+
+        final HttpServletRequest requestDE = new ResourceResolverTestRequest(
+            null, hostDE, -1, rootPath);
+        final Resource resDE0 = resResolver.resolve(requestDE,
+            "/playground.html");
+        assertNotNull(resDE0);
+        assertEquals("/playground.html", resDE0.getPath());
+
+        final Resource resDE1 = resResolver.resolve(requestDE,
+            "/playground/index.html");
+        assertNotNull(resDE1);
+        assertEquals("/playground/index.html", resDE1.getPath());
+
+        final String mappedDE00 = resResolver.map(resDE0.getPath());
+        assertEquals("http://" + hostDE + "/playground.html", mappedDE00);
+        final String mappedDE01 = resResolver.map(requestDE, resDE0.getPath());
+        assertEquals("/playground.html", mappedDE01);
+
+        final String mappedDE10 = resResolver.map(resDE1.getPath());
+        assertEquals("http://" + hostDE + "/playground/index.html", mappedDE10);
+        final String mappedDE11 = resResolver.map(requestDE, resDE1.getPath());
+        assertEquals("/playground/index.html", mappedDE11);
+
+        // en content mapping
+
+        final HttpServletRequest requestEN = new ResourceResolverTestRequest(
+            null, hostEN, -1, rootPath);
+        final Resource resEN0 = resResolver.resolve(requestEN,
+            "/playground.html");
+        assertNotNull(resEN0);
+        assertEquals("/playground.html", resEN0.getPath());
+
+        final Resource resEN1 = resResolver.resolve(requestEN,
+            "/playground/index.html");
+        assertNotNull(resEN1);
+        assertEquals("/playground/index.html", resEN1.getPath());
+
+        // here we get back the hostDE, since this is the first configured
+        // and we have no request information to map the correct of the
+        // duplicate entries !
+        final String mappedEN00 = resResolver.map(resEN0.getPath());
+        assertEquals("http://" + hostDE + "/playground.html", mappedEN00);
+
+        // here we expect the path without scheme/host/port since we have
+        // the request and can select the right mapping
+        final String mappedEN01 = resResolver.map(requestEN, resEN0.getPath());
+        assertEquals("/playground.html", mappedEN01);
+
+        // here we get back the hostDE, since this is the first configured
+        // and we have no request information to map the correct of the
+        // duplicate entries !
+        final String mappedEN10 = resResolver.map(resEN1.getPath());
+        assertEquals("http://" + hostDE + "/playground/index.html", mappedEN10);
+
+        // here we expect the path without scheme/host/port since we have
+        // the request and can select the right mapping
+        final String mappedEN11 = resResolver.map(requestEN, resEN1.getPath());
+        assertEquals("/playground/index.html", mappedEN11);
+
+        final HttpServletRequest requestFR = new ResourceResolverTestRequest(
+            null, hostFR, -1, rootPath);
+        final Resource resFR1 = resResolver.resolve(requestFR,
+            "/playground/index.html");
+        assertNotNull(resFR1);
+        assertEquals("/playground/index.html", resFR1.getPath());
+
+        // here we get back the hostDE, since this is the first configured
+        // and we have no request information to map the correct of the
+        // duplicate entries !
+        final String mappedFR10 = resResolver.map(resFR1.getPath());
+        assertEquals("http://" + hostDE + "/playground/index.html", mappedFR10);
+
+        // here we get back the hostDE, since this is the first configured
+        // and we have request information which does not map any of the
+        // configured duplicate entries !
+        final String mappedFR11 = resResolver.map(requestFR, resFR1.getPath());
+        assertEquals("http://" + hostDE + "/playground/index.html", mappedFR11);
+    }
+
     public void testResolveVirtualHostHttp8080() throws Exception {
-        HttpServletRequest request = new ResourceResolverTestRequest(rootPath) {
-            @Override
-            public String getScheme() {
-                return "http";
-            }
-            
-            @Override
-            public String getServerName() {
-                return "virtual.host.com";
-            }
-            
-            @Override
-            public int getServerPort() {
-                return 8080;
-            }
-        };
-        
+        HttpServletRequest request = new ResourceResolverTestRequest(null,
+            "virtual.host.com", 8080, rootPath);
         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");
+            "/playground/en.html");
         assertNotNull(res1);
         assertEquals("/content/virtual/playground/en.html", res1.getPath());
-        
+
         final String mapped00 = resResolver.map(res0.getPath());
         assertEquals("http://virtual.host.com:8080/playground.html", mapped00);
         final String mapped01 = resResolver.map(request, res0.getPath());
         assertEquals("/playground.html", mapped01);
-        
+
         final String mapped10 = resResolver.map(res1.getPath());
-        assertEquals("http://virtual.host.com:8080/playground/en.html", mapped10);
+        assertEquals("http://virtual.host.com:8080/playground/en.html",
+            mapped10);
         final String mapped11 = resResolver.map(request, res1.getPath());
         assertEquals("/playground/en.html", mapped11);
     }
-    
-    public void testResolveVirtualHostHttp8080Root() throws Exception {
-        HttpServletRequest request = new ResourceResolverTestRequest(rootPath) {
-            @Override
-            public String getScheme() {
-                return "http";
-            }
-
-            @Override
-            public String getServerName() {
-                return "virtual.host.com";
-            }
-
-            @Override
-            public int getServerPort() {
-                return 8080;
-            }
-        };
 
+    public void testResolveVirtualHostHttp8080Root() throws Exception {
+        HttpServletRequest request = new ResourceResolverTestRequest(null,
+            "virtual.host.com", 8080, rootPath);
         Node virtualhost80 = mapRoot.getNode("map/http").addNode(
             "virtual.host.com.8080", "sling:Mapping");
         virtualhost80.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
@@ -490,31 +572,17 @@
         assertEquals("http://virtual.host.com:8080/playground.html", mapped00);
         final String mapped01 = resResolver.map(request, res0.getPath());
         assertEquals("/playground.html", mapped01);
-        
+
         final String mapped10 = resResolver.map(res1.getPath());
-        assertEquals("http://virtual.host.com:8080/playground/en.html", mapped10);
+        assertEquals("http://virtual.host.com:8080/playground/en.html",
+            mapped10);
         final String mapped11 = resResolver.map(request, res1.getPath());
         assertEquals("/playground/en.html", mapped11);
     }
 
     public void testResolveVirtualHostHttps443() throws Exception {
-        HttpServletRequest request = new ResourceResolverTestRequest(rootPath) {
-            @Override
-            public String getScheme() {
-                return "https";
-            }
-
-            @Override
-            public String getServerName() {
-                return "virtual.host.com";
-            }
-
-            @Override
-            public int getServerPort() {
-                return -1;
-            }
-        };
-
+        HttpServletRequest request = new ResourceResolverTestRequest("https",
+            "virtual.host.com", -1, rootPath);
         Node virtualhost443 = mapRoot.getNode("map/https").addNode(
             "virtual.host.com.443", "sling:Mapping");
         virtualhost443.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
@@ -536,7 +604,7 @@
         assertEquals("https://virtual.host.com/playground.html", mapped00);
         final String mapped01 = resResolver.map(request, res0.getPath());
         assertEquals("/playground.html", mapped01);
-        
+
         final String mapped10 = resResolver.map(res1.getPath());
         assertEquals("https://virtual.host.com/playground/en.html", mapped10);
         final String mapped11 = resResolver.map(request, res1.getPath());
@@ -544,23 +612,8 @@
     }
 
     public void testResolveVirtualHostHttps4443() throws Exception {
-        HttpServletRequest request = new ResourceResolverTestRequest(rootPath) {
-            @Override
-            public String getScheme() {
-                return "https";
-            }
-
-            @Override
-            public String getServerName() {
-                return "virtual.host.com";
-            }
-
-            @Override
-            public int getServerPort() {
-                return 4443;
-            }
-        };
-
+        HttpServletRequest request = new ResourceResolverTestRequest("https",
+            "virtual.host.com", 4443, rootPath);
         Node virtualhost4443 = mapRoot.getNode("map/https").addNode(
             "virtual.host.com.4443", "sling:Mapping");
         virtualhost4443.setProperty(
@@ -582,9 +635,10 @@
         assertEquals("https://virtual.host.com:4443/playground.html", mapped00);
         final String mapped01 = resResolver.map(request, res0.getPath());
         assertEquals("/playground.html", mapped01);
-        
+
         final String mapped10 = resResolver.map(res1.getPath());
-        assertEquals("https://virtual.host.com:4443/playground/en.html", mapped10);
+        assertEquals("https://virtual.host.com:4443/playground/en.html",
+            mapped10);
         final String mapped11 = resResolver.map(request, res1.getPath());
         assertEquals("/playground/en.html", mapped11);
     }
@@ -757,10 +811,10 @@
         String path = rootNode.getPath();
         String mapped = resResolver.map(path);
         assertEquals(path, mapped);
-        
+
         Node child = rootNode.addNode("child");
         session.save();
-        
+
         // absolute path, expect rootPath segment to be
         // cut off the mapped path because we map the rootPath
         // onto root
@@ -768,15 +822,15 @@
         mapped = resResolver.map(child.getPath());
         assertEquals(path, mapped);
     }
-    
+
     public void testMapExtension() throws Exception {
         String path = rootNode.getPath();
         String mapped = resResolver.map(path);
         assertEquals(path, mapped);
-        
+
         Node child = rootNode.addNode("child");
         session.save();
-        
+
         // absolute path, expect rootPath segment to be
         // cut off the mapped path because we map the rootPath
         // onto root
@@ -785,15 +839,15 @@
         mapped = resResolver.map(child.getPath() + selExt);
         assertEquals(path, mapped);
     }
-    
+
     public void testMapSelectorsExtension() throws Exception {
         String path = rootNode.getPath();
         String mapped = resResolver.map(path);
         assertEquals(path, mapped);
-        
+
         Node child = rootNode.addNode("child");
         session.save();
-        
+
         // absolute path, expect rootPath segment to be
         // cut off the mapped path because we map the rootPath
         // onto root
@@ -802,7 +856,7 @@
         mapped = resResolver.map(child.getPath() + selExt);
         assertEquals(path, mapped);
     }
-    
+
     public void testMapExtensionSuffix() throws Exception {
         String path = rootNode.getPath();
         String mapped = resResolver.map(path);
@@ -821,87 +875,87 @@
     }
 
     public void testAlias() throws Exception {
-        
+
         Node child = rootNode.addNode("child");
         child.setProperty(JcrResourceResolver2.PROP_ALIAS, "kind");
         session.save();
-        
+
         // expect kind due to alias and no parent due to mapping
         // the rootPath onto root
         String path = "/kind";
         String mapped = resResolver.map(child.getPath());
         assertEquals(path, mapped);
-        
+
         Resource res = resResolver.resolve(null, path);
         Node resNode = res.adaptTo(Node.class);
         assertNotNull(resNode);
-        
+
         assertEquals(child.getPath(), resNode.getPath());
     }
-    
+
     public void testAliasExtension() throws Exception {
-        
+
         final String selExt = ".html";
-        
+
         Node child = rootNode.addNode("child");
         child.setProperty(JcrResourceResolver2.PROP_ALIAS, "kind");
         session.save();
-        
+
         // expect kind due to alias and no parent due to mapping
         // the rootPath onto root
         String path = "/kind" + selExt;
         String mapped = resResolver.map(child.getPath() + selExt);
         assertEquals(path, mapped);
-        
+
         Resource res = resResolver.resolve(null, path);
         Node resNode = res.adaptTo(Node.class);
         assertNotNull(resNode);
-        
+
         assertEquals(child.getPath(), resNode.getPath());
     }
-    
+
     public void testAliasSelectorsExtension() throws Exception {
-        
+
         final String selExt = ".sel1.sel2.html";
-        
+
         Node child = rootNode.addNode("child");
         child.setProperty(JcrResourceResolver2.PROP_ALIAS, "kind");
         session.save();
-        
+
         // expect kind due to alias and no parent due to mapping
         // the rootPath onto root
         String path = "/kind" + selExt;
         String mapped = resResolver.map(child.getPath() + selExt);
         assertEquals(path, mapped);
-        
+
         Resource res = resResolver.resolve(null, path);
         Node resNode = res.adaptTo(Node.class);
         assertNotNull(resNode);
-        
+
         assertEquals(child.getPath(), resNode.getPath());
     }
-    
+
     public void testAliasExtensionSuffix() throws Exception {
-        
+
         final String selExt = ".html/some/suffx.pdf";
-        
+
         Node child = rootNode.addNode("child");
         child.setProperty(JcrResourceResolver2.PROP_ALIAS, "kind");
         session.save();
-        
+
         // expect kind due to alias and no parent due to mapping
         // the rootPath onto root
         String path = "/kind" + selExt;
         String mapped = resResolver.map(child.getPath() + selExt);
         assertEquals(path, mapped);
-        
+
         Resource res = resResolver.resolve(null, path);
         Node resNode = res.adaptTo(Node.class);
         assertNotNull(resNode);
-        
+
         assertEquals(child.getPath(), resNode.getPath());
     }
-    
+
     // ---------- internal
 
     private void testStarResourceHelper(final String path, final String method) {
@@ -913,18 +967,37 @@
         assertEquals(StarResource.DEFAULT_RESOURCE_TYPE, res.getResourceType());
     }
 
-    private static class ResourceResolverTestRequest implements
+    private static final class ResourceResolverTestRequest implements
             HttpServletRequest {
 
         private final String pathInfo;
 
         private final String method;
 
+        private final String scheme;
+
+        private final String host;
+
+        private final int port;
+
         ResourceResolverTestRequest(String pathInfo) {
             this(pathInfo, null);
         }
 
         ResourceResolverTestRequest(String pathInfo, String httpMethod) {
+            this(null, null, -1, pathInfo, httpMethod);
+        }
+
+        ResourceResolverTestRequest(String scheme, String host, int port,
+                String pathInfo) {
+            this(scheme, host, port, pathInfo, null);
+        }
+
+        ResourceResolverTestRequest(String scheme, String host, int port,
+                String pathInfo, String httpMethod) {
+            this.scheme = (scheme == null) ? "http" : scheme;
+            this.host = (host == null) ? "localhost" : host;
+            this.port = port;
             this.pathInfo = pathInfo;
             this.method = httpMethod;
         }
@@ -1022,15 +1095,15 @@
         }
 
         public String getScheme() {
-            return "http";
+            return scheme;
         }
 
         public String getServerName() {
-            return "localhost";
+            return host;
         }
 
         public int getServerPort() {
-            return -1;
+            return port;
         }
 
         public boolean isSecure() {



Mime
View raw message