Author: fmeschbe
Date: Mon Apr 6 12:01:10 2009
New Revision: 762299
URL: http://svn.apache.org/viewvc?rev=762299&view=rev
Log:
SLING-909 adapt JcrResourceResolver2 to the now consistent definition
of the ResourceResolver.resolve methods and fix the unit tests
Modified:
incubator/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.java
incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2Test.java
Modified:
incubator/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.java?rev=762299&r1=762298&r2=762299&view=diff
==============================================================================
---
incubator/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.java
(original)
+++
incubator/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.java
Mon Apr 6 12:01:10 2009
@@ -99,12 +99,17 @@
// ---------- resolving resources
+ public Resource resolve(String absPath) {
+ return resolve(null, absPath);
+ }
+
public Resource resolve(HttpServletRequest request) {
// throws NPE if request is null as required
return resolve(request, request.getPathInfo());
}
public Resource resolve(HttpServletRequest request, String absPath) {
+
// make sure abspath is not null and is absolute
if (absPath == null) {
absPath = "/";
@@ -112,15 +117,129 @@
absPath = "/" + absPath;
}
- return resolveInternal(request, absPath, true);
- }
+ // check for special namespace prefix treatment
+ absPath = unmangleNamespaces(absPath);
- public Resource resolve(String absPath) {
- if (absPath == null) {
- throw new NullPointerException("absPath");
+ // Assume http://localhost:80 if request is null
+ String[] realPathList = { absPath };
+ String requestPath;
+ if (request != null) {
+ requestPath = getMapPath(request.getScheme(),
+ request.getServerName(), request.getServerPort(), absPath);
+ } else {
+ requestPath = getMapPath("http", "localhost", 80, absPath);
}
- return resolveInternal(null, absPath, false);
+ log.debug("resolve: Resolving request path {}", requestPath);
+
+ // loop while finding internal or external redirect into the
+ // content out of the virtual host mapping tree
+ // the counter is to ensure we are not caught in an endless loop here
+ // TODO: might do better to be able to log the loop and help the user
+ for (int i = 0; i < 100; i++) {
+
+ String[] mappedPath = null;
+ for (MapEntry mapEntry : resourceMapper.getResolveMaps()) {
+ mappedPath = mapEntry.replace(requestPath);
+ if (mappedPath != null) {
+ log.debug(
+ "resolve: MapEntry {} matches, mapped path is {}",
+ mapEntry, mappedPath);
+
+ if (mapEntry.isInternal()) {
+ // internal redirect
+ log.debug("resolve: Redirecting internally");
+ break;
+ }
+
+ // external redirect
+ log.debug("resolve: Returning external redirect");
+ return new RedirectResource(this, absPath, mappedPath[0]);
+ }
+ }
+
+ // if there is no virtual host based path mapping, abort
+ // and use the original realPath
+ if (mappedPath == null) {
+ log.debug(
+ "resolve: Request path {} does not match any MapEntry",
+ requestPath);
+ break;
+ }
+
+ // if the mapped path is not an URL, use this path to continue
+ if (!mappedPath[0].contains("://")) {
+ log.debug("resolve: Mapped path is for resource tree");
+ realPathList = mappedPath;
+ break;
+ }
+
+ // otherwise the mapped path is an URI and we have to try to
+ // resolve that URI now, using the URI's path as the real path
+ try {
+ URI uri = new URI(mappedPath[0]);
+ requestPath = getMapPath(uri.getScheme(), uri.getHost(),
+ uri.getPort(), uri.getPath());
+ realPathList = new String[] { uri.getPath() };
+
+ log.debug(
+ "resolve: Mapped path is an URL, using new request path
{}",
+ requestPath);
+ } catch (URISyntaxException use) {
+ // TODO: log and fail
+ throw new ResourceNotFoundException(absPath);
+ }
+ }
+
+ // now we have the real path resolved from virtual host mapping
+ // this path may be absolute or relative, in which case we try
+ // to resolve it against the search path
+
+ Resource res = null;
+ for (int i = 0; res == null && i < realPathList.length; i++) {
+ String realPath = realPathList[i];
+
+ // first check whether the requested resource is a StarResource
+ if (StarResource.appliesTo(realPath)) {
+
+ log.debug("resolve: Mapped path {} is a Star Resource",
+ realPath);
+ res = new StarResource(this, ensureAbsPath(realPath),
+ factory.getJcrResourceTypeProvider());
+
+ } else
+
+ if (realPath.startsWith("/")) {
+
+ // let's check it with a direct access first
+ log.debug("resolve: Try absolute mapped path {}", realPath);
+ res = resolveInternal(realPath);
+
+ } else {
+
+ String[] searchPath = getSearchPath();
+ for (int spi = 0; res == null && spi < searchPath.length;
spi++) {
+ log.debug(
+ "resolve: Try relative mapped path with search path
entry {}",
+ searchPath[spi]);
+ res = resolveInternal(searchPath[spi] + realPath);
+ }
+
+ }
+
+ }
+
+ // if no resource has been found, use a NonExistingResource
+ if (res == null) {
+ log.debug(
+ "resolve: Path {} does not resolve, returning
NonExistingResource at {}",
+ absPath, realPathList[0]);
+ res = new NonExistingResource(this,
ensureAbsPath(realPathList[0]));
+ } else {
+ log.debug("resolve: Path {} resolves to Resource {}", absPath,
res);
+ }
+
+ return res;
}
// calls map(HttpServletRequest, String) as map(null, resourcePath)
@@ -405,139 +524,6 @@
return rootProvider.getSession();
}
- // expect absPath to be non-null and absolute
- public Resource resolveInternal(HttpServletRequest request, String absPath,
- boolean requireResource) {
-
- // check for special namespace prefix treatment
- absPath = unmangleNamespaces(absPath);
-
- // Assume http://localhost:80 if request is null
- String[] realPathList = { absPath };
- String requestPath;
- if (request != null) {
- requestPath = getMapPath(request.getScheme(),
- request.getServerName(), request.getServerPort(), absPath);
- } else {
- requestPath = getMapPath("http", "localhost", 80, absPath);
- }
-
- log.debug("resolve: Resolving request path {}", requestPath);
-
- // loop while finding internal or external redirect into the
- // content out of the virtual host mapping tree
- // the counter is to ensure we are not caught in an endless loop here
- // TODO: might do better to be able to log the loop and help the user
- for (int i = 0; i < 100; i++) {
-
- String[] mappedPath = null;
- for (MapEntry mapEntry : resourceMapper.getResolveMaps()) {
- mappedPath = mapEntry.replace(requestPath);
- if (mappedPath != null) {
- log.debug(
- "resolve: MapEntry {} matches, mapped path is {}",
- mapEntry, mappedPath);
-
- if (mapEntry.isInternal()) {
- // internal redirect
- log.debug("resolve: Redirecting internally");
- break;
- }
-
- // external redirect
- log.debug("resolve: Returning external redirect");
- return new RedirectResource(this, absPath, mappedPath[0]);
- }
- }
-
- // if there is no virtual host based path mapping, abort
- // and use the original realPath
- if (mappedPath == null) {
- log.debug(
- "resolve: Request path {} does not match any MapEntry",
- requestPath);
- break;
- }
-
- // if the mapped path is not an URL, use this path to continue
- if (!mappedPath[0].contains("://")) {
- log.debug("resolve: Mapped path is for resource tree");
- realPathList = mappedPath;
- break;
- }
-
- // otherwise the mapped path is an URI and we have to try to
- // resolve that URI now, using the URI's path as the real path
- try {
- URI uri = new URI(mappedPath[0]);
- requestPath = getMapPath(uri.getScheme(), uri.getHost(),
- uri.getPort(), uri.getPath());
- realPathList = new String[] { uri.getPath() };
-
- log.debug(
- "resolve: Mapped path is an URL, using new request path
{}",
- requestPath);
- } catch (URISyntaxException use) {
- // TODO: log and fail
- throw new ResourceNotFoundException(absPath);
- }
- }
-
- // now we have the real path resolved from virtual host mapping
- // this path may be absolute or relative, in which case we try
- // to resolve it against the search path
-
- Resource res = null;
- for (int i = 0; res == null && i < realPathList.length; i++) {
- String realPath = realPathList[i];
-
- // first check whether the requested resource is a StarResource
- if (StarResource.appliesTo(realPath)) {
-
- log.debug("resolve: Mapped path {} is a Star Resource",
- realPath);
- res = new StarResource(this, ensureAbsPath(realPath),
- factory.getJcrResourceTypeProvider());
-
- } else
-
- if (realPath.startsWith("/")) {
-
- // let's check it with a direct access first
- log.debug("resolve: Try absolute mapped path {}", realPath);
- res = resolveInternal(realPath);
-
- } else {
-
- String[] searchPath = getSearchPath();
- for (int spi = 0; res == null && spi < searchPath.length;
spi++) {
- log.debug(
- "resolve: Try relative mapped path with search path
entry {}",
- searchPath[spi]);
- res = resolveInternal(searchPath[spi] + realPath);
- }
-
- }
-
- }
-
- if (res == null) {
- if (requireResource) {
- log.debug(
- "resolve: Path {} does not resolve, returning
NonExistingResource at {}",
- absPath, realPathList[0]);
- res = new NonExistingResource(this,
- ensureAbsPath(realPathList[0]));
- } else {
- log.debug("resolve: No Resource for {}", absPath);
- }
- } else {
- log.debug("resolve: Path {} resolves to Resource {}", absPath,
res);
- }
-
- return res;
- }
-
/**
* Returns a string used for matching map entries against the given request
* or URI parts.
Modified:
incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2Test.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2Test.java?rev=762299&r1=762298&r2=762299&view=diff
==============================================================================
---
incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2Test.java
(original)
+++
incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2Test.java
Mon Apr 6 12:01:10 2009
@@ -150,20 +150,29 @@
public void testBasicAPIAssumptions() throws Exception {
- final String no_resource_path = "/no_resource/at/this/location";
- try {
- resResolver.resolve((String) null);
- fail("Expected NullPointerException trying to resolve null path");
- } catch (NullPointerException npe) {
- // expected
- }
-
- assertNull("Expecting no resource for relative path",
- resResolver.resolve("relPath/relPath"));
+ // null resource is accessing /, which exists of course
+ final Resource res00 = resResolver.resolve((String) null);
+ assertNotNull(res00);
+ assertEquals("Null path is expected to return root", "/",
+ res00.getPath());
+
+ // relative paths are treated as if absolute
+ final String path01 = "relPath/relPath";
+ final Resource res01 = resResolver.resolve(path01);
+ assertNotNull(res01);
+ assertEquals("Expecting absolute path for relative path", "/" + path01,
+ res01.getPath());
+ assertTrue("Resource must be NonExistingResource",
+ res01 instanceof NonExistingResource);
- assertNull("Expecting null if resource cannot be found",
- resResolver.resolve(no_resource_path));
+ final String no_resource_path = "/no_resource/at/this/location";
+ final Resource res02 = resResolver.resolve(no_resource_path);
+ assertNotNull(res02);
+ assertEquals("Expecting absolute path for relative path",
+ no_resource_path, res02.getPath());
+ assertTrue("Resource must be NonExistingResource",
+ res01 instanceof NonExistingResource);
try {
resResolver.resolve((HttpServletRequest) null);