org.apache.sling.api.resource
Interface ResourceResolver

All Superinterfaces:
Adaptable
All Known Implementing Classes:
MockResourceResolver

public interface ResourceResolver
extends Adaptable

The ResourceResolver defines the service API which may be used to resolve Resource objects. The resource resolver is available to the request processing servlet through the SlingHttpServletRequest.getResourceResolver() method.

The ResourceResolver is also an Adaptable to get adapters to other types. A JCR based resource resolver might support adapting to the JCR Session used by the resolver to access the JCR Repository.

A ResourceResolver is generally not thread safe! As a consequence, an application which uses the resolver, its returned resources and/or objects resulting from adapting either the resolver or a resource, must provide proper synchronization to ensure no more than one thread concurrently operates against a single resolver, resource or resulting objects.

Accessing Resources

This interface defines two kinds of methods to access resources: The resolve methods and the getResource methods. The difference lies in the algorithm applied to find the requested resource and in the behaviour in case a resource cannot be found:

Method Kind Access Algorithm Missing Resource
resolve Path is always assumed to be absolute. Uses elaborate resource resolution algorithm. This kind of method is intended to resolve request URLs to resources. Returns NonExistingResource
getResource Directly access resources with absolute path. For relative paths, the search path is applied. This method is intended to be used by request processing scripts to access further resources as required. Returns null

Lifecycle

A Resource Resolver has a life cycle which begins with the creation of the Resource Resolver using any of the factory methods and ends with calling the close() method. It is very important to call the close() method once the resource resolver is not used any more to ensure any system resources are properly clean up.

To check whether a Resource Resolver can still be used, the isLive() method can be called.

Resource Resolver Attributes

The authentication info properties provided to the ResourceResolverFactory.getResourceResolver(Map), ResourceResolverFactory.getAdministrativeResourceResolver(Map), or clone(Map) are available through the getAttributeNames() and getAttribute(String) methods with the exception of security sensitive properties like ResourceResolverFactory.PASSWORD which is not exposed.


Field Summary
static String REQUEST_ATTR_WORKSPACE_INFO
          A request attribute containing the workspace to use for resolve(HttpServletRequest) and resolve(HttpServletRequest, String) if not the default workspace should be used to resolve the resource.
static String USER_IMPERSONATOR
          The name of the resource resolver attribute which is set if the resource resolver has been impersonated as per the ResourceResolverFactory.USER_IMPERSONATION property.
 
Method Summary
 ResourceResolver clone(Map<String,Object> authenticationInfo)
          Returns a new ResourceResolver instance based on the given authenticationInfo map and the original authentication info used to create this instance.
 void close()
          Close this resource resolver.
 Iterator<Resource> findResources(String query, String language)
          Searches for resources using the given query formulated in the given language.
 Object getAttribute(String name)
          Returns the value of the given resource resolver attribute or null if the attribute is not set (or not visible as is the case of the ResourceResolverFactory.PASSWORD or other security sensitive attributes).
 Iterator<String> getAttributeNames()
          Returns an iterator of attribute names whose value can be retrieved calling the getAttribute(String) method.
 Resource getResource(Resource base, String path)
          Returns a Resource object for data located at the given path.
 Resource getResource(String path)
          Returns a Resource object for data located at the given path.
 String[] getSearchPath()
          Returns the search path used by the getResource(String) method to search for resources by relative path.
 String getUserID()
          Get the user ID, if any, associated with this resource resolver.
 boolean isLive()
          Returns true if this resource resolver has not been closed yet.
 Iterator<Resource> listChildren(Resource parent)
          Returns an Iterator of Resource objects loaded from the children of the given Resource.
 String map(javax.servlet.http.HttpServletRequest request, String resourcePath)
          Returns an URL mapped from the (resource) path applying the reverse mapping used by the resolve(HttpServletRequest, String) such that when the path is given to the resolve(HttpServletRequest, String) method the same resource is returned.
 String map(String resourcePath)
          Returns a path mapped from the (resource) path applying the reverse mapping used by the resolve(String) such that when the path is given to the resolve(String) method the same resource is returned.
 Iterator<Map<String,Object>> queryResources(String query, String language)
          Queries the storage using the given query formulated in the given language.
 Resource resolve(javax.servlet.http.HttpServletRequest request)
          Deprecated. as of 2.0.4, use resolve(HttpServletRequest, String) instead.
 Resource resolve(javax.servlet.http.HttpServletRequest request, String absPath)
          Resolves the resource from the given absPath optionally taking HttpServletRequest into account, such as the value of the Host request header.
 Resource resolve(String absPath)
          Resolves the resource from the given absolute path.
 
Methods inherited from interface org.apache.sling.api.adapter.Adaptable
adaptTo
 

Field Detail

REQUEST_ATTR_WORKSPACE_INFO

static final String REQUEST_ATTR_WORKSPACE_INFO
A request attribute containing the workspace to use for resolve(HttpServletRequest) and resolve(HttpServletRequest, String) if not the default workspace should be used to resolve the resource.

Since:
2.1

USER_IMPERSONATOR

static final String USER_IMPERSONATOR
The name of the resource resolver attribute which is set if the resource resolver has been impersonated as per the ResourceResolverFactory.USER_IMPERSONATION property. The value of this attribute is the name of the primary user provided to the resource resolver factory method.

Since:
2.1
See Also:
Constant Field Values
Method Detail

resolve

Resource resolve(javax.servlet.http.HttpServletRequest request,
                 String absPath)
Resolves the resource from the given absPath optionally taking HttpServletRequest into account, such as the value of the Host request header. Returns a NonExistingResource if the path cannot be resolved to an existing and accessible resource.

The difference between this method and the resolve(String) method is, that this method may take request properties like the scheme, the host header or request parameters into account to resolve the resource. If the REQUEST_ATTR_WORKSPACE_INFO attribute is set, the given workspace is used to resolve the resource.

Parameters:
request - The http servlet request object providing more hints at how to resolve the absPath. This parameter may be null in which case the implementation should use reasonable defaults.
absPath - The absolute path to be resolved to a resource. If this parameter is null, it is assumed to address the root of the resource tree. If the path is relative it is assumed relative to the root, that is a slash is prepended to the path before resolving it.
Returns:
The Resource addressed by the absPath or a NonExistingResource if no such resource can be resolved.
Throws:
SlingException - Or a subclass thereof may be thrown if an error occurrs trying to resolve the resource.
IllegalStateException - if this resource resolver has already been closed.
Since:
2.0.4

resolve

Resource resolve(String absPath)
Resolves the resource from the given absolute path. Returns a NonExistingResource if the path cannot be resolved to an existing and accessible resource.

This method is intended to apply the same algorithm to the absolute path as is used by the resolve(HttpServletRequest) method except for cases where the latter uses request property such as request headers or request parameters to resolve a resource.

It is ok for the implementation of this method to just call the resolve(HttpServletRequest, String) method with null as the request argument.

Parameters:
absPath - The absolute path to be resolved to a resource. If this parameter is null, it is assumed to address the root of the resource tree. If the path is relative it is assumed relative to the root, that is a slash is prepended to the path before resolving it.
Returns:
The Resource addressed by the absPath or a NonExistingResource if no such resource can be resolved.
Throws:
SlingException - Or a subclass thereof may be thrown if an error occurrs trying to resolve the resource.
IllegalStateException - if this resource resolver has already been closed.

resolve

@Deprecated
Resource resolve(javax.servlet.http.HttpServletRequest request)
Deprecated. as of 2.0.4, use resolve(HttpServletRequest, String) instead.

Resolves the resource from the given HttpServletRequest. Returns a NonExistingResource if the path cannot be resolved to an existing and accessible resource.

This method is deprecated as of API version 2.0.4 and should not be used anymore. Implementations are expected to implement this method calling the resolve(HttpServletRequest, String) where the absPath argument is the result of calling the getPathInfo() on the request object. If the REQUEST_ATTR_WORKSPACE_INFO attribute is set, the given workspace is used to resolve the resource.

Parameters:
request - The http servlet request object used to resolve the resource for. This must not be null.
Returns:
The Resource addressed by HttpServletRequest.getPathInfo() or a NonExistingResource if no such resource can be resolved.
Throws:
NullPointerException - If request is null .
SlingException - Or a subclass thereof may be thrown if an error occurrs trying to resolve the resource.
IllegalStateException - if this resource resolver has already been closed.

map

String map(String resourcePath)
Returns a path mapped from the (resource) path applying the reverse mapping used by the resolve(String) such that when the path is given to the resolve(String) method the same resource is returned.

Note, that technically the resourcePath need not refer to an existing resource. This method just applies the mappings and returns the resulting string. If the resourcePath does not address an existing resource roundtripping may of course not work and calling resolve(String) with the path returned may return null.

This method is intended as the reverse operation of the resolve(String) method.

Parameters:
resourcePath - The path for which to return a mapped path.
Returns:
The mapped path.
Throws:
IllegalStateException - if this resource resolver has already been closed.

map

String map(javax.servlet.http.HttpServletRequest request,
           String resourcePath)
Returns an URL mapped from the (resource) path applying the reverse mapping used by the resolve(HttpServletRequest, String) such that when the path is given to the resolve(HttpServletRequest, String) method the same resource is returned.

Note, that technically the resourcePath need not refer to an existing resource. This method just applies the mappings and returns the resulting string. If the resourcePath does not address an existing resource roundtripping may of course not work and calling resolve(HttpServletRequest, String) with the path returned may return null.

This method is intended as the reverse operation of the resolve(HttpServletRequest, String) method. As such the URL returned is expected to be an absolute URL including scheme, host, any servlet context path and the actual path used to resolve the resource.

Parameters:
request - The http servlet request object which may be used to apply more mapping functionality.
resourcePath - The path for which to return a mapped path.
Returns:
The mapped URL.
Throws:
IllegalStateException - if this resource resolver has already been closed.
Since:
2.0.4

getResource

Resource getResource(String path)
Returns a Resource object for data located at the given path.

This specification does not define the location for resources or the semantics for resource paths. For an implementation reading content from a Java Content Repository, the path could be a javax.jcr.Item path from which the resource object is loaded. In contrast to the resolve(String) method, this method does not apply any logic to the path, so the path is used as-is to fetch the content.

Parameters:
path - The absolute path to the resource object to be loaded. The path may contain relative path specifiers like . (current location) and .. (parent location), which are resolved by this method. If the path is relative, that is the first character is not a slash, implementations are expected to apply a search path algorithm to resolve the relative path to a resource.
Returns:
The Resource object loaded from the path or null if the path does not resolve to a resource.
Throws:
SlingException - If an error occurrs trying to load the resource object from the path.
IllegalStateException - if this resource resolver has already been closed.

getResource

Resource getResource(Resource base,
                     String path)
Returns a Resource object for data located at the given path.

This specification does not define the location for resources or the semantics for resource paths. For an implementation reading content from a Java Content Repository, the path could be a javax.jcr.Item path from which the resource object is loaded.

Parameters:
base - The base Resource against which a relative path argument given by path is resolved. This parameter may be null if the path is known to be absolute.
path - The path to the resource object to be loaded. If the path is relative, i.e. does not start with a slash (/), the resource relative to the given base resource is returned. The path may contain relative path specifiers like . (current location) and .. (parent location), which are resolved by this method.
Returns:
The Resource object loaded from the path or null if the path does not resolve to a resource.
Throws:
SlingException - If an error occurrs trying to load the resource object from the path or if base is null and path is relative.
IllegalStateException - if this resource resolver has already been closed.

getSearchPath

String[] getSearchPath()
Returns the search path used by the getResource(String) method to search for resources by relative path. If no search path is set an empty array is returned.

The returns array of Strings is a copy of the internal value, so modifications to this array have no influence on the operation of the ResourceResolver.

Each entry in the array is an absolute path terminated with a slash character. Thus to create an absolute path from a search path entry and a relative path, the search path entry and relative path may just be concatenated.

Throws:
IllegalStateException - if this resource resolver has already been closed.

listChildren

Iterator<Resource> listChildren(Resource parent)
Returns an Iterator of Resource objects loaded from the children of the given Resource.

This specification does not define what the term "child" means. This is left to the implementation to define. For example an implementation reading content from a Java Content Repository, the children could be the Resource objects loaded from child items of the Item of the given Resource.

Parameters:
parent - The Resource whose children are requested.
Returns:
An Iterator of Resource objects.
Throws:
NullPointerException - If parent is null.
SlingException - If any error occurs acquiring the child resource iterator.
IllegalStateException - if this resource resolver has already been closed.

findResources

Iterator<Resource> findResources(String query,
                                 String language)
Searches for resources using the given query formulated in the given language.

The semantic meaning of the query and language depend on the actual implementation and storage used for the resources. For JCR repository being used as storage, the query and lanuage parameters are used to create a JCR Query through the QueryManager. The result returned is then based on the NodeIterator provided by the query result.

Parameters:
query - The query string to use to find the resources.
language - The language in which the query is formulated.
Returns:
An Iterator of Resource objects matching the query.
Throws:
QuerySyntaxException - If the query is not syntactically correct according to the query language indicator of if the query language is not supported.
SlingException - If an error occurrs querying for the resources.
IllegalStateException - if this resource resolver has already been closed.

queryResources

Iterator<Map<String,Object>> queryResources(String query,
                                            String language)
Queries the storage using the given query formulated in the given language.

The semantic meaning of the query and language depend on the actual implementation and storage used for the resources. For JCR repository being used as storage, the query and lanuage parameters are used to create a JCR Query through the QueryManager. The result returned is then based on the RowIterator provided by the query result. The map returned for each row is indexed by the column name and the column value is the JCR Value object converted into the respective Java object, such as Boolean for a value of property type Boolean.

Parameters:
query - The query string to use to find the resources.
language - The language in which the query is formulated.
Returns:
An Iterator of Map instances providing access to the query result.
Throws:
QuerySyntaxException - If the query is not syntactically correct according to the query language indicator of if the query language is not supported.
SlingException - If an error occurrs querying for the resources.
IllegalStateException - if this resource resolver has already been closed.

clone

ResourceResolver clone(Map<String,Object> authenticationInfo)
                       throws LoginException
Returns a new ResourceResolver instance based on the given authenticationInfo map and the original authentication info used to create this instance.

The new resource resolver is created according to the following algorithm:

 Map<String, Object> newAuthenticationInfo = new HashMap(
     authenticationInfoOfThisInstance);
 newAuthenticationInfo.addAll(authenticationInfo);
 return resourceResolverFactory.getResourceResolver(newAuthenticationInfo);
 

Parameters:
authenticationInfo - The map or credential data to overlay the orignal credential data with for the creation of a new resource resolver. This may be null in which case the same credential data is used as was used to create this instance.
Returns:
A new ResourceResolver
Throws:
LoginException - If an error occurrs creating the new ResourceResolver with the provided credential data.
IllegalStateException - if this resource resolver has already been closed.
Since:
2.1

isLive

boolean isLive()
Returns true if this resource resolver has not been closed yet.

Unlike the other methods defined in this interface, this method will never throw an exception even after the resource resolver has been closed.

Returns:
true if the resource resolver has not been closed yet. Once the resource resolver has been closed, this method returns false.
Since:
2.1

close

void close()
Close this resource resolver. This method should be called by clients when the resource resolver is not used anymore. Once this method has been called, the resource resolver is considered unusable and will throw exceptions if still used.

Since:
2.1

getUserID

String getUserID()
Get the user ID, if any, associated with this resource resolver. The meaning of this identifier is an implementation detail defined by the underlying repository. This method may return null.

Returns:
the user ID
Throws:
IllegalStateException - if this resource resolver has already been closed.
Since:
2.1

getAttributeNames

Iterator<String> getAttributeNames()
Returns an iterator of attribute names whose value can be retrieved calling the getAttribute(String) method. This iterator will not include any attributes which are not accessible.

Returns:
An iterator of attribute names
Throws:
IllegalStateException - if this resource resolver has already been closed.

getAttribute

Object getAttribute(String name)
Returns the value of the given resource resolver attribute or null if the attribute is not set (or not visible as is the case of the ResourceResolverFactory.PASSWORD or other security sensitive attributes).

Parameters:
name - The name of the attribute to access
Returns:
The value of the attribute or null if the attribute is not set or not accessible.
Throws:
NullPointerException - if name is null.
IllegalStateException - if this resource resolver has already been closed.


Copyright © 2007-2011 The Apache Software Foundation. All Rights Reserved.