Search in sources :

Example 1 with ResourcePathIterator

use of org.apache.sling.resourceresolver.impl.helper.ResourcePathIterator in project sling by apache.

the class ResourceResolverImpl method resolveInternal.

/**
     * Internally resolves the absolute path. The will almost always contain
     * request selectors and an extension. Therefore this method uses the
     * {@link ResourcePathIterator} to cut off parts of the path to find the
     * actual resource.
     * <p>
     * This method operates in two steps:
     * <ol>
     * <li>Check the path directly
     * <li>Drill down the resource tree from the root down to the resource trying to get the child
     * as per the respective path segment or finding a child whose <code>sling:alias</code> property
     * is set to the respective name.
     * </ol>
     * <p>
     * If neither mechanism (direct access and drill down) resolves to a resource this method
     * returns <code>null</code>.
     *
     * @param absPath
     *            The absolute path of the resource to return.
     * @return The resource found or <code>null</code> if the resource could not
     *         be found. The
     *         {@link org.apache.sling.api.resource.ResourceMetadata#getResolutionPathInfo()
     *         resolution path info} field of the resource returned is set to
     *         the part of the <code>absPath</code> which has been cut off by
     *         the {@link ResourcePathIterator} to resolve the resource.
     */
private Resource resolveInternal(final String absPath, final Map<String, String> parameters) {
    Resource resource = null;
    String curPath = absPath;
    try {
        final ResourcePathIterator it = new ResourcePathIterator(absPath);
        while (it.hasNext() && resource == null) {
            curPath = it.next();
            resource = getAbsoluteResourceInternal(null, curPath, parameters, true);
        }
    } catch (final Exception ex) {
        throw new SlingException("Problem trying " + curPath + " for request path " + absPath, ex);
    }
    // uriPath = curPath + sling.resolutionPathInfo
    if (resource != null) {
        final String rpi = absPath.substring(curPath.length());
        resource.getResourceMetadata().setResolutionPath(absPath.substring(0, curPath.length()));
        resource.getResourceMetadata().setResolutionPathInfo(rpi);
        resource.getResourceMetadata().setParameterMap(parameters);
        logger.debug("resolveInternal: Found resource {} with path info {} for {}", new Object[] { resource, rpi, absPath });
    } else {
        String tokenizedPath = absPath;
        // no direct resource found, so we have to drill down into the
        // resource tree to find a match
        resource = getAbsoluteResourceInternal(null, "/", parameters, true);
        //SLING-5638
        if (resource == null) {
            resource = getAbsoluteResourceInternal(absPath, parameters, true);
            if (resource != null) {
                tokenizedPath = tokenizedPath.substring(resource.getPath().length());
            }
        }
        final StringBuilder resolutionPath = new StringBuilder();
        final StringTokenizer tokener = new StringTokenizer(tokenizedPath, "/");
        while (resource != null && tokener.hasMoreTokens()) {
            final String childNameRaw = tokener.nextToken();
            Resource nextResource = getChildInternal(resource, childNameRaw);
            if (nextResource != null) {
                resource = nextResource;
                resolutionPath.append("/").append(childNameRaw);
            } else {
                String childName = null;
                final ResourcePathIterator rpi = new ResourcePathIterator(childNameRaw);
                while (rpi.hasNext() && nextResource == null) {
                    childName = rpi.next();
                    nextResource = getChildInternal(resource, childName);
                }
                // switch the currentResource to the nextResource (may be
                // null)
                resource = nextResource;
                resolutionPath.append("/").append(childName);
                // with the extension cut off
                if (nextResource != null) {
                    break;
                }
            }
        }
        // uriPath = curPath + sling.resolutionPathInfo
        if (resource != null) {
            final String path = resolutionPath.toString();
            final String pathInfo = absPath.substring(path.length());
            resource.getResourceMetadata().setResolutionPath(path);
            resource.getResourceMetadata().setResolutionPathInfo(pathInfo);
            resource.getResourceMetadata().setParameterMap(parameters);
            logger.debug("resolveInternal: Found resource {} with path info {} for {}", new Object[] { resource, pathInfo, absPath });
        }
    }
    return resource;
}
Also used : StringTokenizer(java.util.StringTokenizer) StarResource(org.apache.sling.resourceresolver.impl.helper.StarResource) RedirectResource(org.apache.sling.resourceresolver.impl.helper.RedirectResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) SlingException(org.apache.sling.api.SlingException) ResourcePathIterator(org.apache.sling.resourceresolver.impl.helper.ResourcePathIterator) StringUtils.defaultString(org.apache.commons.lang3.StringUtils.defaultString) PersistenceException(org.apache.sling.api.resource.PersistenceException) SlingException(org.apache.sling.api.SlingException) URIException(org.apache.sling.resourceresolver.impl.helper.URIException) LoginException(org.apache.sling.api.resource.LoginException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException)

Aggregations

StringTokenizer (java.util.StringTokenizer)1 StringUtils.defaultString (org.apache.commons.lang3.StringUtils.defaultString)1 SlingException (org.apache.sling.api.SlingException)1 LoginException (org.apache.sling.api.resource.LoginException)1 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)1 RedirectResource (org.apache.sling.resourceresolver.impl.helper.RedirectResource)1 ResourcePathIterator (org.apache.sling.resourceresolver.impl.helper.ResourcePathIterator)1 StarResource (org.apache.sling.resourceresolver.impl.helper.StarResource)1 URIException (org.apache.sling.resourceresolver.impl.helper.URIException)1