org.apache.sling.commons.testing.sling
Class MockResourceResolver

java.lang.Object
  extended by org.apache.sling.commons.testing.sling.MockResourceResolver
All Implemented Interfaces:
Adaptable, ResourceResolver

public class MockResourceResolver
extends Object
implements ResourceResolver


Field Summary
 
Fields inherited from interface org.apache.sling.api.resource.ResourceResolver
REQUEST_ATTR_WORKSPACE_INFO, USER_IMPERSONATOR
 
Constructor Summary
MockResourceResolver()
           
 
Method Summary
<AdapterType>
AdapterType
adaptTo(Class<AdapterType> type)
          Adapts the adaptable to another type.
 void addChildren(Resource parent, Collection<Resource> children)
           
 void addResource(Resource resource)
           
 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 ResourceResolver.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 ResourceResolver.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 ResourceResolver.resolve(HttpServletRequest, String) such that when the path is given to the ResourceResolver.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 ResourceResolver.resolve(String) such that when the path is given to the ResourceResolver.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)
          Resolves the resource from the given HttpServletRequest.
 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.
 void setSearchPath(String... searchPath)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockResourceResolver

public MockResourceResolver()
Method Detail

addResource

public void addResource(Resource resource)

addChildren

public void addChildren(Resource parent,
                        Collection<Resource> children)

resolve

public Resource resolve(javax.servlet.http.HttpServletRequest request)
Description copied from interface: ResourceResolver
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 ResourceResolver.resolve(HttpServletRequest, String) where the absPath argument is the result of calling the getPathInfo() on the request object. If the ResourceResolver.REQUEST_ATTR_WORKSPACE_INFO attribute is set, the given workspace is used to resolve the resource.

Specified by:
resolve in interface ResourceResolver
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.

resolve

public Resource resolve(String absPath)
Description copied from interface: ResourceResolver
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 ResourceResolver.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 ResourceResolver.resolve(HttpServletRequest, String) method with null as the request argument.

Specified by:
resolve in interface ResourceResolver
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.

map

public String map(String resourcePath)
Description copied from interface: ResourceResolver
Returns a path mapped from the (resource) path applying the reverse mapping used by the ResourceResolver.resolve(String) such that when the path is given to the ResourceResolver.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 ResourceResolver.resolve(String) with the path returned may return null.

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

Specified by:
map in interface ResourceResolver
Parameters:
resourcePath - The path for which to return a mapped path.
Returns:
The mapped path.

getResource

public Resource getResource(String path)
Description copied from interface: ResourceResolver
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 ResourceResolver.resolve(String) method, this method does not apply any logic to the path, so the path is used as-is to fetch the content.

Specified by:
getResource in interface ResourceResolver
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.

getResource

public Resource getResource(Resource base,
                            String path)
Description copied from interface: ResourceResolver
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.

Specified by:
getResource in interface ResourceResolver
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.

getSearchPath

public String[] getSearchPath()
Description copied from interface: ResourceResolver
Returns the search path used by the ResourceResolver.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.

Specified by:
getSearchPath in interface ResourceResolver

listChildren

public Iterator<Resource> listChildren(Resource parent)
Description copied from interface: ResourceResolver
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.

Specified by:
listChildren in interface ResourceResolver
Parameters:
parent - The Resource whose children are requested.
Returns:
An Iterator of Resource objects.

findResources

public Iterator<Resource> findResources(String query,
                                        String language)
Description copied from interface: ResourceResolver
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.

Specified by:
findResources in interface ResourceResolver
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.

queryResources

public Iterator<Map<String,Object>> queryResources(String query,
                                                   String language)
Description copied from interface: ResourceResolver
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.

Specified by:
queryResources in interface ResourceResolver
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.

adaptTo

public <AdapterType> AdapterType adaptTo(Class<AdapterType> type)
Description copied from interface: Adaptable
Adapts the adaptable to another type.

Specified by:
adaptTo in interface Adaptable
Type Parameters:
AdapterType - The generic type to which this resource is adapted to
Parameters:
type - The Class object of the target type, such as Node.class
Returns:
The adapter target or null if the resource cannot adapt to the requested type

setSearchPath

public void setSearchPath(String... searchPath)

map

public String map(javax.servlet.http.HttpServletRequest request,
                  String resourcePath)
Description copied from interface: ResourceResolver
Returns an URL mapped from the (resource) path applying the reverse mapping used by the ResourceResolver.resolve(HttpServletRequest, String) such that when the path is given to the ResourceResolver.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 ResourceResolver.resolve(HttpServletRequest, String) with the path returned may return null.

This method is intended as the reverse operation of the ResourceResolver.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.

Specified by:
map in interface ResourceResolver
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.

resolve

public Resource resolve(javax.servlet.http.HttpServletRequest request,
                        String absPath)
Description copied from interface: ResourceResolver
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 ResourceResolver.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 ResourceResolver.REQUEST_ATTR_WORKSPACE_INFO attribute is set, the given workspace is used to resolve the resource.

Specified by:
resolve in interface ResourceResolver
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.

close

public void close()
Description copied from interface: ResourceResolver
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.

Specified by:
close in interface ResourceResolver

getUserID

public String getUserID()
Description copied from interface: ResourceResolver
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.

Specified by:
getUserID in interface ResourceResolver
Returns:
the user ID

isLive

public boolean isLive()
Description copied from interface: ResourceResolver
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.

Specified by:
isLive in interface ResourceResolver
Returns:
true if the resource resolver has not been closed yet. Once the resource resolver has been closed, this method returns false.

clone

public ResourceResolver clone(Map<String,Object> authenticationInfo)
                       throws LoginException
Description copied from interface: ResourceResolver
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);
 

Specified by:
clone in interface ResourceResolver
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.

getAttribute

public Object getAttribute(String name)
Description copied from interface: ResourceResolver
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).

Specified by:
getAttribute in interface ResourceResolver
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.

getAttributeNames

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

Specified by:
getAttributeNames in interface ResourceResolver
Returns:
An iterator of attribute names


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