Return-Path: Delivered-To: apmail-incubator-sling-commits-archive@locus.apache.org Received: (qmail 19083 invoked from network); 3 Dec 2008 14:23:56 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 3 Dec 2008 14:23:56 -0000 Received: (qmail 45551 invoked by uid 500); 3 Dec 2008 14:24:06 -0000 Delivered-To: apmail-incubator-sling-commits-archive@incubator.apache.org Received: (qmail 45522 invoked by uid 500); 3 Dec 2008 14:24:06 -0000 Mailing-List: contact sling-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: sling-dev@incubator.apache.org Delivered-To: mailing list sling-commits@incubator.apache.org Received: (qmail 45504 invoked by uid 99); 3 Dec 2008 14:24:06 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Dec 2008 06:24:06 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Dec 2008 14:22:46 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id B302A23888A6; Wed, 3 Dec 2008 06:23:03 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r722899 - /incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.java Date: Wed, 03 Dec 2008 14:23:03 -0000 To: sling-commits@incubator.apache.org From: fmeschbe@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081203142303.B302A23888A6@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: fmeschbe Date: Wed Dec 3 06:23:02 2008 New Revision: 722899 URL: http://svn.apache.org/viewvc?rev=722899&view=rev Log: SLING-752 Compliance with modified JavaDoc: - throw NPE if path to resolve(String) is null - fail resolution for relative paths Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver2.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=722899&r1=722898&r2=722899&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 Wed Dec 3 06:23:02 2008 @@ -112,141 +112,28 @@ // ---------- resolving resources - public Resource resolve(HttpServletRequest request, String absPath) { + public Resource resolve(HttpServletRequest request) { + // throws NPE if request is null as required + return resolve(request, request.getPathInfo()); + } - // Assume root if absPath is null + public Resource resolve(HttpServletRequest request, String absPath) { + // make sure abspath is not null and is absolute if (absPath == null) { absPath = "/"; + } else if (!absPath.startsWith("/")) { + absPath = "/" + absPath; } - // 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 : maps) { - 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"); - 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) { - log.debug("resolve: Resource {} does not exist", realPathList[0]); - res = new NonExistingResource(this, ensureAbsPath(realPathList[0])); - } else { - log.debug("resolve: Found resource {}", res); - } - - return res; + return resolveInternal(request, absPath, true); } - - public Resource resolve(HttpServletRequest request) { - return resolve(request, request.getPathInfo()); - } - + public Resource resolve(String absPath) { - return resolve(null, absPath); + if (absPath == null) { + throw new NullPointerException("absPath"); + } + + return resolveInternal(null, absPath, false); } // trivial implementation not taking into account any mappings in @@ -420,6 +307,139 @@ return namespaces; } + // 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 : maps) { + 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"); + 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.