Author: fmeschbe
Date: Mon Jan 26 13:48:45 2009
New Revision: 737694
URL: http://svn.apache.org/viewvc?rev=737694&view=rev
Log:
SLING-790 Enable namespace mangling of full URLs and fix the outgoing
regular expression (this matched "/content/jcr:" instead of "/jcr:")
plus extend unittests to also look for context path support
Modified:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.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=737694&r1=737693&r2=737694&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 Jan 26 13:48:45 2009
@@ -56,6 +56,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.sun.jndi.toolkit.url.Uri;
+
public class JcrResourceResolver2 extends SlingAdaptable implements
ResourceResolver {
@@ -69,7 +71,7 @@
private static final String MANGLE_NAMESPACE_OUT_PREFIX = "/";
- private static final String MANGLE_NAMESPACE_OUT = "/([^:]+):";
+ private static final String MANGLE_NAMESPACE_OUT = "/([^:/]+):";
public static final String PROP_REG_EXP = "sling:match";
@@ -206,6 +208,7 @@
for (String candidate : mappedPaths) {
if (candidate.startsWith(schemehostport)) {
mappedPath =
candidate.substring(schemehostport.length() - 1);
+ mappedPathIsUrl = false;
log.debug(
"map: Found host specific mapping {} resolving
to {}",
candidate, mappedPath);
@@ -236,24 +239,46 @@
// 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);
-
- return mappedPath;
- }
- // mangle the namespaces
- mappedPath = mangleNamespaces(mappedPath);
+ } else {
- // prepend servlet context path if we have a request
- if (request != null && request.getContextPath() != null
- && request.getContextPath().length() > 0) {
- mappedPath = request.getContextPath().concat(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) {
+ mappedPath = request.getContextPath().concat(mappedPath);
+ }
+
+ log.debug(
+ "map: Returning path {} (after mangling, inlc. context) for
{}",
+ mappedPath, resourcePath);
+
}
-
- log.debug(
- "map: Returning path {} (after mangling, inlc. context) for {}",
- mappedPath, resourcePath);
return mappedPath;
}
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=737694&r1=737693&r2=737694&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 Jan 26 13:48:45 2009
@@ -105,9 +105,15 @@
// ensure using JcrResourceResolver2
Field unrrField = resFac.getClass().getDeclaredField(
- "useNewResourceResolver");
+ "useNewResourceResolver");
unrrField.setAccessible(true);
unrrField.set(resFac, true);
+
+ // ensure namespace mangling
+ Field mangeNamespacePrefixesField = resFac.getClass().getDeclaredField(
+ "mangleNamespacePrefixes");
+ mangeNamespacePrefixesField.setAccessible(true);
+ mangeNamespacePrefixesField.set(resFac, true);
Field mapEntriesField =
resFac.getClass().getDeclaredField("mapEntries");
mapEntriesField.setAccessible(true);
@@ -827,7 +833,8 @@
final String mapHost = "virtual.host.com";
final String mapRootPath = "/content/virtual";
-
+ final String contextPath = "/context";
+
Node virtualhost80 = mapRoot.getNode("map/http").addNode(
mapHost + ".80", "sling:Mapping");
virtualhost80.setProperty(JcrResourceResolver2.PROP_REDIRECT_INTERNAL,
@@ -835,21 +842,69 @@
session.save();
Thread.sleep(1000L);
-
- final HttpServletRequest foreignRequest = new
ResourceResolverTestRequest(null,
- "foreign.host.com", -1, rootPath);
+
+ //---------------------------------------------------------------------
+ // tests expecting paths without context
+
final HttpServletRequest virtualRequest = new
ResourceResolverTestRequest(null,
mapHost, -1, rootPath);
-
+
// simple mapping - cut off prefix and add host
- final String path0 = "/sample";
- final String mapped0 = resResolver.map(foreignRequest, mapRootPath +
path0);
- assertEquals("http://" + mapHost + path0, mapped0);
+ final String pathv0 = "/sample";
+ final String mappedv0 = resResolver.map(virtualRequest, mapRootPath +
pathv0);
+ assertEquals("Expect unmangled path", pathv0, mappedv0);
// expected name mangling without host prefix
- final String path1 = "/sample/jcr:content";
- final String mapped1 = resResolver.map(virtualRequest, mapRootPath +
path1);
- assertEquals(path1, mapped1);
+ final String pathv1 = "/sample/jcr:content";
+ final String mangledv1 = "/sample/_jcr_content";
+ final String mappedv1 = resResolver.map(virtualRequest, mapRootPath +
pathv1);
+ assertEquals("Expect mangled path", mangledv1, mappedv1);
+
+
+ //---------------------------------------------------------------------
+ // tests expecting paths with context "/context"
+
+ ((ResourceResolverTestRequest)
virtualRequest).setContextPath(contextPath);
+
+ // simple mapping - cut off prefix and add host
+ final String pathvc0 = "/sample";
+ final String mappedvc0 = resResolver.map(virtualRequest, mapRootPath +
pathvc0);
+ assertEquals("Expect unmangled path", contextPath + pathv0, mappedvc0);
+
+ // expected name mangling without host prefix
+ final String pathvc1 = "/sample/jcr:content";
+ final String mangledvc1 = "/sample/_jcr_content";
+ final String mappedvc1 = resResolver.map(virtualRequest, mapRootPath +
pathvc1);
+ assertEquals("Expect mangled path", contextPath + mangledvc1,
mappedvc1);
+
+ //---------------------------------------------------------------------
+ // tests expecting absolute URLs without context
+
+ final HttpServletRequest foreignRequest = new
ResourceResolverTestRequest(null,
+ "foreign.host.com", -1, rootPath);
+
+ final String pathf0 = "/sample";
+ final String mappedf0 = resResolver.map(foreignRequest, mapRootPath +
pathf0);
+ assertEquals("Expect unmangled absolute URI", "http://" + mapHost +
pathf0, mappedf0);
+
+ final String pathf1 = "/sample/jcr:content";
+ final String mangledf1 = "/sample/_jcr_content";
+ final String mappedf1 = resResolver.map(foreignRequest, mapRootPath +
pathf1);
+ assertEquals("Expect mangled absolute URI", "http://" + mapHost +
mangledf1, mappedf1);
+
+ //---------------------------------------------------------------------
+ // tests expecting absolute URLs with context "/context"
+
+ ((ResourceResolverTestRequest)
foreignRequest).setContextPath(contextPath);
+
+ final String pathfc0 = "/sample";
+ final String mappedfc0 = resResolver.map(foreignRequest, mapRootPath +
pathfc0);
+ assertEquals("Expect unmangled absolute URI", "http://" + mapHost +
contextPath + pathfc0, mappedfc0);
+
+ final String pathfc1 = "/sample/jcr:content";
+ final String mangledfc1 = "/sample/_jcr_content";
+ final String mappedfc1 = resResolver.map(foreignRequest, mapRootPath +
pathfc1);
+ assertEquals("Expect mangled absolute URI", "http://" + mapHost +
contextPath + mangledfc1, mappedfc1);
}
public void testMapContext() throws Exception {
@@ -1024,6 +1079,8 @@
private final String host;
private final int port;
+
+ private String contextPath;
ResourceResolverTestRequest(String pathInfo) {
this(pathInfo, null);
@@ -1047,6 +1104,10 @@
this.method = httpMethod;
}
+ void setContextPath(String contextPath) {
+ this.contextPath = contextPath;
+ }
+
public String getPathInfo() {
return pathInfo;
}
@@ -1155,6 +1216,10 @@
return false;
}
+ public String getContextPath() {
+ return contextPath;
+ }
+
public void removeAttribute(String name) {
}
@@ -1168,10 +1233,6 @@
return null;
}
- public String getContextPath() {
- return null;
- }
-
public Cookie[] getCookies() {
return null;
}