Interface ResourceResolverHelper


  • @ProviderType
    public interface ResourceResolverHelper
    The ResourceResolverHelper service provides access to a ResourceResolver previously provided for the current thread.

    The implementation provides two contexts:

    • While handling a Servlet API request the default ResourceResolver provided is the one provided by the Sling Engine through the SlingHttpServletRequest
    • In a callstack opened by a call to the callWith(ResourceResolver, Callable) method
    • Method Detail

      • getResourceResolver

        ResourceResolver getResourceResolver()
        Returns the current ResourceResolver for the current thread or null if this method is called on a thread which is not handling a request or which is not in a call stack started with a call to the callWith(ResourceResolver, Callable) method.

        ResourceResolver instances must not be shared accross threads because they are not thread safe.

      • getResourceResolverAs

        <Type> Type getResourceResolverAs​(Class<Type> type)
        Returns the current ResourceResolver adapted to the selected type. If there is no curren ResourceResolver or if the ResourceResolver does not adapt to the desired type null is returned.

        This method is equivalent to calling getResourceResolver().adaptTo(type).

        Parameters:
        type - The type to which the current ResourceResolver should be adapted.
        Returns:
        The ResourceResolver adapted to the desired type or null
      • callWith

        <Type> Type callWith​(ResourceResolver resolver,
                             Callable<Type> callable)
                      throws Exception
        Calls the given callable under the provides ResourceResolver. Before calling the callable the resolver is set as the current ResourceResolver such that the getResourceResolver() method returns the given ResourceResolver.
        Parameters:
        resolver - The ResourceResolver to set as the current ResourceResolver before calling the callable.
        callable - The code to execute under the resolver.
        Returns:
        The result from calling the callable
        Throws:
        NullPointerException - if callable is null.
        Exception - The exception thrown by the callable is returned.