Author: fmeschbe
Date: Fri Dec 18 13:54:47 2009
New Revision: 892252

URL: http://svn.apache.org/viewvc?rev=892252&view=rev
Log:
SLING-1218 apply modified patch (modification is to include relevant classes 
from the http client library in the bundle), thanks Alex Klimetscheck for 
providing the patch.

Modified:
    sling/trunk/bundles/jcr/resource/pom.xml
    
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/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/pom.xml?rev=892252&r1=892251&r2=892252&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/pom.xml (original)
+++ sling/trunk/bundles/jcr/resource/pom.xml Fri Dec 18 13:54:47 2009
@@ -69,7 +69,8 @@
                         <!-- Include URL support from Jackrabbit -->
                         <Embed-Dependency>
                             
jackrabbit-classloader;inline="org/apache/jackrabbit/net/**|org/apache/jackrabbit/classloader/Util.*",
-                            
jackrabbit-jcr-commons;inline="org/apache/jackrabbit/util/ISO9075.*|org/apache/jackrabbit/name/QName.*|org/apache/jackrabbit/util/XMLChar.*|org/apache/jackrabbit/util/Text.*"
+                            
jackrabbit-jcr-commons;inline="org/apache/jackrabbit/util/ISO9075.*|org/apache/jackrabbit/name/QName.*|org/apache/jackrabbit/util/XMLChar.*|org/apache/jackrabbit/util/Text.*",
+                            
commons-httpclient;inline="org/apache/commons/httpclient/HttpClientError.*|org/apache/commons/httpclient/HttpException.*|org/apache/commons/httpclient/URI*|org/apache/commons/httpclient/util/EncodingUtil.*"
                         </Embed-Dependency>
 
                         <Sling-Namespaces>
@@ -141,17 +142,18 @@
             <version>1.0.0</version>
             <scope>provided</scope>
         </dependency>
-        
-        <!--
-            VirtualURL BidiMap, to be removed once JcrResourceResolver
-            is removed and completely replaced by JcrResourceResolver2
-        -->
         <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>commons-httpclient</groupId>
+            <artifactId>commons-httpclient</artifactId>
+            <version>3.1</version>
+            <scope>provided</scope>
+        </dependency>
 
         <!-- For the Console Plugin of the JcrResourceResolverFactoryImpl -->
         <dependency>
@@ -178,7 +180,7 @@
             <version>1.6.0</version>
             <scope>provided</scope>
         </dependency>
-
+        
         <!-- Testing -->
         <dependency>
             <groupId>org.apache.sling</groupId>

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=892252&r1=892251&r2=892252&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
 Fri Dec 18 13:54:47 2009
@@ -36,6 +36,7 @@
 import javax.jcr.query.RowIterator;
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.commons.httpclient.URIException;
 import org.apache.sling.adapter.SlingAdaptable;
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.resource.NonExistingResource;
@@ -375,51 +376,33 @@
             mappedPath = resourcePath;
         }
 
-        if (mappedPathIsUrl) {
-            // TODO: Consider mangling the path but not the scheme and
-            // esp. the host:port part
-
-            // [scheme:][//authority][path][?query][#fragment]
-            try {
-                URI uri = new URI(mappedPath);
-
-                // mangle the namespaces in the path
-                String path = mangleNamespaces(uri.getPath());
-
-                // prepend servlet context path if we have a request
-                if (request != null && request.getContextPath() != null
-                    && request.getContextPath().length() > 0) {
-                    path = request.getContextPath().concat(path);
-                }
-
-                // reconstruct the uri with the modified path
-                mappedPath = new URI(uri.getScheme(), uri.getAuthority(), path,
-                    uri.getQuery(), uri.getFragment()).toString();
-            } catch (URISyntaxException use) {
-                log.warn("map: Unable to mangle namespaces for " + mappedPath
-                    + " returning unmangled", use);
-            }
-
-            log.debug("map: Returning URL {} as mapping for path {}",
-                mappedPath, resourcePath);
-
-        } else {
+        // [scheme:][//authority][path][?query][#fragment]
+        try {
+            // use commons-httpclient's URI instead of java.net.URI, as it can
+            // actually accept *unescaped* URIs, such as the "mappedPath" and
+            // return them in proper escaped form, including the path, via 
toString()
+            org.apache.commons.httpclient.URI uri = new 
org.apache.commons.httpclient.URI(mappedPath, false);
 
-            // mangle the namespaces
-            mappedPath = mangleNamespaces(mappedPath);
+            // 1. mangle the namespaces in the path
+            String path = mangleNamespaces(uri.getPath());
 
-            // prepend servlet context path if we have a request
+            // 2. prepend servlet context path if we have a request
             if (request != null && request.getContextPath() != null
                 && request.getContextPath().length() > 0) {
-                mappedPath = request.getContextPath().concat(mappedPath);
+                path = request.getContextPath().concat(path);
             }
+            // update the path part of the URI
+            uri.setPath(path);
 
-            log.debug(
-                "map: Returning path {} (after mangling, incl. context) for 
{}",
-                mappedPath, resourcePath);
-
+            mappedPath = uri.toString();
+        } catch (URIException e) {
+            log.warn("map: Unable to mangle namespaces for " + mappedPath
+                    + " returning unmangled", e);
         }
 
+        log.debug("map: Returning URL {} as mapping for path {}",
+            mappedPath, resourcePath);
+        
         // reappend fragment and/or query
         if (fragmentQuery != null) {
             mappedPath = mappedPath.concat(fragmentQuery);

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=892252&r1=892251&r2=892252&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
 Fri Dec 18 13:54:47 2009
@@ -903,7 +903,57 @@
         mapped = resResolver.map(child.getPath());
         assertEquals(path, mapped);
     }
+    
+    public void testMapURLEscaping() throws Exception {
 
+        final String mapHostInternal = "internal.host.com";
+        final String mapRootInternal = "/content/internal";
+        
+        Node internalRedirect = mapRoot.getNode("map/http").addNode(
+            mapHostInternal + ".80", "sling:Mapping");
+        
internalRedirect.setProperty(JcrResourceResolver.PROP_REDIRECT_INTERNAL,
+            mapRootInternal);
+        session.save();
+
+        Thread.sleep(1000L);
+
+        final String path = "/sample with spaces";
+        final String escapedPath = "/sample%20with%20spaces";
+        
+        // 
---------------------------------------------------------------------
+        // internal redirect
+
+        // a) test map(String)
+        // => return full URL, escaped
+        String mapped = resResolver.map(mapRootInternal + path);
+        assertEquals("http://"; + mapHostInternal + escapedPath, mapped);
+
+        // b) test map(HttpServletRequest, String) with "localhost"
+        // => return full URL, escaped
+        mapped = resResolver.map(new ResourceResolverTestRequest(rootPath), 
mapRootInternal + path);
+        assertEquals("http://"; + mapHostInternal + escapedPath, mapped);
+
+        // c) test map(HttpServletRequest, String) with "internal.host.com"
+        // => only return path, escaped, because request host/port matches 
(cut off host part)
+        mapped = resResolver.map(new ResourceResolverTestRequest(null, 
mapHostInternal, -1, rootPath), mapRootInternal + path);
+        assertEquals(escapedPath, mapped);
+        
+        // 
---------------------------------------------------------------------
+        // no mapping config
+        // => return only escaped path
+        
+        final String unmappedRoot = "/unmappedRoot";
+
+        // a) test map(String)
+        mapped = resResolver.map(unmappedRoot + path);
+        assertEquals(unmappedRoot + escapedPath, mapped);
+
+        // b) test map(HttpServletRequest, String)
+        mapped = resResolver.map(new ResourceResolverTestRequest(rootPath), 
unmappedRoot + path);
+        assertEquals(unmappedRoot + escapedPath, mapped);
+
+    }
+    
     public void testMapNamespaceMangling() throws Exception {
 
         final String mapHost = "virtual.host.com";


Reply via email to