Package org.apache.sling.resourceresolver.impl.helper

# Class ResourceResolverControl

java.lang.Object
org.apache.sling.resourceresolver.impl.helper.ResourceResolverControl

public class ResourceResolverControl extends Object
This class takes a number of `AuthenticatedResourceProvider` objects and exposes it as one such object. Provider appropriate for the given operation is chosen basing on its `ResourceProviderInfo.getPath()` (more specific first) and service ranking. Like a resource resolver itself, this class is not thread safe.

## Constructor Summary

Constructors
Constructor
Description
`ResourceResolverControl(boolean isAdmin, Map<String,Object> authenticationInfo, ResourceProviderStorageProvider resourceProviderTracker)`
Create a new resource resolver context.

## Method Summary

Modifier and Type
Method
Description
`<AdapterType>
AdapterType`
`adaptTo(ResourceResolverContext context, Class<AdapterType> type)`
Returns the first non-null result of the adaptTo() method invoked on the providers.
`AuthenticatedResourceProvider`
`checkSourceAndDest(ResourceResolverContext context, String srcAbsPath, String destAbsPath)`
Check source and destination for operations
`void`
`clearAuthenticatedProviders()`
Clear all authenticated providers
`void`
`close()`
Close all dynamic resource providers.
`void`
`commit(ResourceResolverContext context)`
Commit changes on all modifiable ResourceProviders.
`org.apache.sling.api.resource.Resource`
`copy(ResourceResolverContext context, String srcAbsPath, String destAbsPath)`
Tries to find a resource provider accepting both paths and invokes `AuthenticatedResourceProvider.copy(String, String)` method on it.
`org.apache.sling.api.resource.Resource`
`create(ResourceResolverContext context, String path, Map<String,Object> properties)`
Create a resource.
`static final String`
`createDescendantPath(String path, String descendantName)`
Create a child path
`void`
`delete(ResourceResolverContext context, org.apache.sling.api.resource.Resource resource)`
Delete the resource.
`Iterator<org.apache.sling.api.resource.Resource>`
`findResources(ResourceResolverContext context, String query, String language)`
Queries all resource providers and combines the results.
`Object`
`getAttribute(ResourceResolverContext context, String name)`
Returns the first non-null result of the `AuthenticatedResourceProvider.getAttribute(String)` invocation on the providers.
`Collection<String>`
`getAttributeNames(ResourceResolverContext context)`
Returns the union of all attribute names.
`Map<String,Object>`
`getAuthenticationInfo()`
The authentication info
`org.apache.sling.api.resource.Resource`
`getParent(@NotNull ResourceResolverContext context, @NotNull String parentPath, @NotNull org.apache.sling.api.resource.Resource child)`
Returns parent from the most appropriate resource provider accepting the given children.
`String`
`getParentResourceType(org.apache.sling.api.resource.ResourceResolverFactory factory, org.apache.sling.api.resource.ResourceResolver resolver, String resourceType)`
Get the parent resource type
`static String`
`getProperty(org.apache.sling.api.resource.Resource res, String propName)`
Returns `getProperty(res, propName, String.class)`
`static <Type> Type`
`getProperty(org.apache.sling.api.resource.Resource res, String propName, Class<Type> type)`
Returns the value of the name property of the resource converted to the requested `type`.
`org.apache.sling.api.resource.Resource`
`getResource(ResourceResolverContext context, String path, org.apache.sling.api.resource.Resource parent, Map<String,String> parameters, boolean isResolve)`
Returns resource from the most appropriate resource provider.
`ResourceProviderStorage`
`getResourceProviderStorage()`
Get the provider storage
`String[]`
`getSupportedLanguages(ResourceResolverContext context)`
Return the union of query languages supported by the providers.
`boolean`
`hasChanges(ResourceResolverContext context)`
Check if any modifiable ResourceProvider has uncommited changes.
`boolean`
`isAdmin()`
Is this an admin resource resolver?
`boolean`
`isClosed()`
Is this already closed?
`boolean`
`isLive(@NotNull ResourceResolverContext context)`
Returns `true` if all providers are live.
`Iterator<org.apache.sling.api.resource.Resource>`
`listChildren(ResourceResolverContext context, org.apache.sling.api.resource.Resource parent)`
This method asks all matching resource providers for the children iterators, merges them, adds `SyntheticResource`s (see `getResource(ResourceResolverContext, String, Resource, Map, boolean)` for more details), filters out the duplicates and returns the resulting iterator.
`Iterator<org.apache.sling.api.resource.Resource>`
`listChildrenInternal(ResourceResolverContext context, Node<ResourceProviderHandler> node, org.apache.sling.api.resource.Resource parent, Iterator<org.apache.sling.api.resource.Resource> realChildren)`
Internal method
`org.apache.sling.api.resource.Resource`
`move(ResourceResolverContext context, String srcAbsPath, String destAbsPath)`
Tries to find a resource provider accepting both paths and invokes `AuthenticatedResourceProvider.move(String, String)` method on it.
`boolean`
`orderBefore(@NotNull ResourceResolverContext context, @NotNull org.apache.sling.api.resource.Resource parent, @NotNull String name, @Nullable String followingSiblingName)`
Order resources
`Iterator<Map<String,Object>>`
`queryResources(ResourceResolverContext context, String query, String language)`
Queries all resource providers and combines the results.
`void`
`refresh(@NotNull ResourceResolverContext context)`
Refreshes all refreshable providers as well as the resolver used for resource types.
`void`
`registerAuthenticatedProvider(@NotNull ResourceProviderHandler handler, @Nullable Object providerState)`
Register authetnticated provider
`void`
`revert(ResourceResolverContext context)`
Revert changes on all modifiable ResourceProviders.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ResourceResolverControl

public ResourceResolverControl(boolean isAdmin, Map<String,Object> authenticationInfo, ResourceProviderStorageProvider resourceProviderTracker)
Create a new resource resolver context.

Parameters:
  `isAdmin` - Admin resource resolver?
  `authenticationInfo` - The auth info
  `resourceProviderTracker` - Tracker for all providers

## Method Details

### isAdmin

public boolean isAdmin()
Is this an admin resource resolver?

Returns:
  `true` if it is an admin resource resolver

### getAuthenticationInfo

public Map<String,Object> getAuthenticationInfo()
The authentication info

Returns:
  The map with the auth info

### isClosed

public boolean isClosed()
Is this already closed?

Returns:
  `true` if it is closed.

### refresh

public void refresh(@NotNull @NotNull ResourceResolverContext context)
Refreshes all refreshable providers as well as the resolver used for resource types.

Parameters:
  `context` - The context

### isLive

public boolean isLive(@NotNull @NotNull ResourceResolverContext context)
Returns `true` if all providers are live.

Parameters:
  `context` - The context
Returns:
  `true` if all providers are live

### getParent

public org.apache.sling.api.resource.Resource getParent(@NotNull @NotNull ResourceResolverContext context, @NotNull @NotNull String parentPath, @NotNull @NotNull org.apache.sling.api.resource.Resource child)
Returns parent from the most appropriate resource provider accepting the given children. In some cases the `SyntheticResource` can be returned if no resource provider returns parent for this child. See `getResource(ResourceResolverContext, String, Resource, Map, boolean)` for more details

Parameters:
  `context` - The context
  `parentPath` - The parent path
  `child` - The child resource
Returns:
  The parent or `null`

### getResource

public org.apache.sling.api.resource.Resource getResource(ResourceResolverContext context, String path, org.apache.sling.api.resource.Resource parent, Map<String,String> parameters, boolean isResolve)
Returns resource from the most appropriate resource provider.

If there's no such provider and the path is a part of some resource provider path, then the `SyntheticResource` will be returned. For instance, if we have resource provider under `/libs/sling/servlet/default/GET.servlet` and no resource provider returns a resource for `/libs/sling/servlet/default`, then the `SyntheticResource` will be returned to provide a consistent resource tree.

The same behaviour occurs in `getParent(ResourceResolverContext, String, Resource)` and `listChildren(ResourceResolverContext, Resource)`.

Parameters:
  `context` - The context
  `path` - Resource path
  `parent` - Parent resource
  `parameters` - Additional parameters
  `isResolve` - Whether this is a resolve or get call
Returns:
  The resource or `null`

### listChildren

public Iterator<org.apache.sling.api.resource.Resource> listChildren(ResourceResolverContext context, org.apache.sling.api.resource.Resource parent)
This method asks all matching resource providers for the children iterators, merges them, adds `SyntheticResource`s (see `getResource(ResourceResolverContext, String, Resource, Map, boolean)` for more details), filters out the duplicates and returns the resulting iterator. All transformations are done lazily, during the `Iterator.hasNext()` invocation on the result.

Parameters:
  `context` - The context
  `parent` - The parent resource
Returns:
  Iterator

### listChildrenInternal

public Iterator<org.apache.sling.api.resource.Resource> listChildrenInternal(ResourceResolverContext context, Node<ResourceProviderHandler> node, org.apache.sling.api.resource.Resource parent, Iterator<org.apache.sling.api.resource.Resource> realChildren)
Internal method

Parameters:
  `context` - The context
  `node` - The node
  `parent` - The parent
  `realChildren` - The children
Returns:
  The children

### getAttributeNames

public Collection<String> getAttributeNames(ResourceResolverContext context)
Returns the union of all attribute names.

Parameters:
  `context` - The context
Returns:
  The attribute names

### getAttribute

public Object getAttribute(ResourceResolverContext context, String name)
Returns the first non-null result of the `AuthenticatedResourceProvider.getAttribute(String)` invocation on the providers.

Parameters:
  `context` - The context
  `name` - Attribute name
Returns:
  Attribute value or `null`

### create

public org.apache.sling.api.resource.Resource create(ResourceResolverContext context, String path, Map<String,Object> properties) throws org.apache.sling.api.resource.PersistenceException
Create a resource.

Parameters:
  `context` - The context
  `path` - The resource path
  `properties` - The resource properties
Returns:
  The new resource
Throws:
  `UnsupportedOperationException` - If creation is not allowed/possible
  `org.apache.sling.api.resource.PersistenceException` - If creation fails

### orderBefore

public boolean orderBefore(@NotNull @NotNull ResourceResolverContext context, @NotNull @NotNull org.apache.sling.api.resource.Resource parent, @NotNull @NotNull String name, @Nullable @Nullable String followingSiblingName) throws UnsupportedOperationException, org.apache.sling.api.resource.PersistenceException, IllegalArgumentException
Order resources

Parameters:
  `context` - The context
  `parent` - The parent
  `name` - Resource name
  `followingSiblingName` - Following sibling name
Returns:
  `true` if ordering succeeded
Throws:
  `UnsupportedOperationException` - If the operation is not supported#
  `org.apache.sling.api.resource.PersistenceException` - If ordering fails
  `IllegalArgumentException` - If input parameters are wrong
See Also:
  - `ResourceResolver.orderBefore(Resource, String, String)`

### createDescendantPath

public static final String createDescendantPath(String path, String descendantName)
Create a child path

Parameters:
  `path` - Path
  `descendantName` - Child name
Returns:
  Path

### delete

public void delete(ResourceResolverContext context, org.apache.sling.api.resource.Resource resource) throws org.apache.sling.api.resource.PersistenceException
Delete the resource. Iterate over all modifiable ResourceProviders giving each an opportunity to delete the resource if they are able.

Parameters:
  `context` - The context
  `resource` - The resource to delete
Throws:
  `NullPointerException` - if resource is null
  `UnsupportedOperationException` - If deletion is not allowed/possible
  `org.apache.sling.api.resource.PersistenceException` - If deletion fails

### revert

public void revert(ResourceResolverContext context)
Revert changes on all modifiable ResourceProviders.

Parameters:
  `context` - The context

### commit

public void commit(ResourceResolverContext context) throws org.apache.sling.api.resource.PersistenceException
Commit changes on all modifiable ResourceProviders.

Parameters:
  `context` - The context
Throws:
  `org.apache.sling.api.resource.PersistenceException` - If operation fails

### hasChanges

public boolean hasChanges(ResourceResolverContext context)
Check if any modifiable ResourceProvider has uncommited changes.

Parameters:
  `context` - The context
Returns:
  `true` if there are uncommited changes

### getSupportedLanguages

public String[] getSupportedLanguages(ResourceResolverContext context)
Return the union of query languages supported by the providers.

Parameters:
  `context` - The context
Returns:
  The supported query languages

### findResources

public Iterator<org.apache.sling.api.resource.Resource> findResources(ResourceResolverContext context, String query, String language)
Queries all resource providers and combines the results.

Parameters:
  `context` - The context
  `query` - The query
  `language` - The language
Returns:
  The result

### queryResources

public Iterator<Map<String,Object>> queryResources(ResourceResolverContext context, String query, String language)
Queries all resource providers and combines the results.

Parameters:
  `context` - The context
  `query` - The query
  `language` - The language
Returns:
  The result

### adaptTo

public <AdapterType> AdapterType adaptTo(ResourceResolverContext context, Class<AdapterType> type)
Returns the first non-null result of the adaptTo() method invoked on the providers.

Type Parameters:
  `AdapterType` - The type to adapt to
Parameters:
  `context` - The context
  `type` - The type to adapt to
Returns:
  The object or `null`

### checkSourceAndDest

public AuthenticatedResourceProvider checkSourceAndDest(ResourceResolverContext context, String srcAbsPath, String destAbsPath) throws org.apache.sling.api.resource.PersistenceException
Check source and destination for operations

Parameters:
  `context` - The context
  `srcAbsPath` - The source
  `destAbsPath` - The destination
Returns:
  The responsible provider or `null`
Throws:
  `org.apache.sling.api.resource.PersistenceException` - If something goes wrong

### copy

public org.apache.sling.api.resource.Resource copy(ResourceResolverContext context, String srcAbsPath, String destAbsPath) throws org.apache.sling.api.resource.PersistenceException
Tries to find a resource provider accepting both paths and invokes `AuthenticatedResourceProvider.copy(String, String)` method on it. Returns false if there's no such provider.

Parameters:
  `context` - The context
  `srcAbsPath` - The source
  `destAbsPath` - The destination
Returns:
  The copied resources
Throws:
  `org.apache.sling.api.resource.PersistenceException` - if operation fails

### move

public org.apache.sling.api.resource.Resource move(ResourceResolverContext context, String srcAbsPath, String destAbsPath) throws org.apache.sling.api.resource.PersistenceException
Tries to find a resource provider accepting both paths and invokes `AuthenticatedResourceProvider.move(String, String)` method on it. Returns false if there's no such provider.

Parameters:
  `context` - The context
  `srcAbsPath` - The source
  `destAbsPath` - The destination
Returns:
  The moved resource
Throws:
  `org.apache.sling.api.resource.PersistenceException` - if operation fails

### getResourceProviderStorage

public ResourceProviderStorage getResourceProviderStorage()
Get the provider storage

Returns:
  The provider storage

### close

public void close()
Close all dynamic resource providers.

### getParentResourceType

public String getParentResourceType(org.apache.sling.api.resource.ResourceResolverFactory factory, org.apache.sling.api.resource.ResourceResolver resolver, String resourceType)
Get the parent resource type

Parameters:
  `factory` - The factory
  `resolver` - The resolver
  `resourceType` - The type
Returns:
  The parent resource type
See Also:
  - `ResourceResolver.getParentResourceType(java.lang.String)`

### getProperty

public static String getProperty(org.apache.sling.api.resource.Resource res, String propName)
Returns `getProperty(res, propName, String.class)`

Parameters:
  `res` - The resource to access the property from
  `propName` - The name of the property to access
Returns:
  The property as a `String` or `null` if the property does not exist or cannot be converted into a `String`

### getProperty

public static <Type> Type getProperty(org.apache.sling.api.resource.Resource res, String propName, Class<Type> type)
Returns the value of the name property of the resource converted to the requested `type`.

If the resource itself does not have the property, the property is looked up in the `jcr:content` child node. This access is done through the same `ValueMap` as is used to access the property directly. This generally only works for JCR based `ValueMap` instances which provide access to relative path property names. This may not work in non JCR `ValueMap`, however in non JCR envs there is usually no "jcr:content" child node anyway

Type Parameters:
  `Type` - The type
Parameters:
  `res` - The resource to access the property from
  `propName` - The name of the property to access
  `type` - The type into which to convert the property
Returns:
  The property converted to the requested `type` or `null` if the property does not exist or cannot be converted into the requested `type`

### registerAuthenticatedProvider

public void registerAuthenticatedProvider(@NotNull @NotNull ResourceProviderHandler handler, @Nullable @Nullable Object providerState)
Register authetnticated provider

Parameters:
  `handler` - The handler
  `providerState` - The state

### clearAuthenticatedProviders

public void clearAuthenticatedProviders()
Clear all authenticated providers