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;
}
Aggregations