Expand my Community achievements bar.

SOLVED

What is the difference between ResourceResolver and ResourceResolverFactory interface?

Avatar

Level 5

I am reading [https://sling.apache.org/apidocs/sling7/org/apache/sling/api/resource/ResourceResolverFactory.html] and found that ResourceResolverFactory is required to create ResourceResolver. But without using the ResourceResolverFactory we can get the resources using ResourceResolver only. So could you please explain me in what scenarios do we need to use ResourceResolverFactory?

1 Accepted Solution

Avatar

Correct answer by
Level 7

ResourceResolver is one of the powerful API interface provided by Sling. Its job is to return resource object based on the given input, which can be a path to the resource, query or parent resource to get its children. Resource resolver object can be accessed in any one of the following ways -

1. Syntax for getting ResourceResolver from ResourceResolverFactory:

ResourceResolver serviceResourceResolver = resourceResolverFactory.getServiceResourceResolver(authenticationInfo);

Resource someResource = serviceResourceResolver.getResource("/path/to/resource");

someResource.adaptTo(MyModel.class);

NOTE: We'll use ResourceResolverFactory in situations such as - working to get JCR session and while we don't have request and resource objects (that means, while not dealing with servlets).

2. Syntax for getting ResourceResolver from Request object:

ResourceResolver resourceResolver=request.getResourceResolver();

3. Syntax for getting ResourceResolver from Resource object:

ResourceResolver resourceResolver= resource.getResourceResolver();

NOTE: We can retrieve a ResourceResolver using a ResourceResolverFactory and a service-user mapping as shown in the links below.

http://www.aemcq5tutorials.com/tutorials/resourceresolver-from-resourceresolverfactory/

http://www.adobeaemclub.com/access-to-resourceresolver-in-osgi-services-aem-6-1/

https://aem6tutorials.wordpress.com/2016/02/10/how-to-get-resourceresolver-from-resourceresolverfact...

Regards,

TechAspect Solutions

View solution in original post

5 Replies

Avatar

Level 10

When working to get a JCR session - you typically inject a ResoureResolverFactory instance into your code to get a ResourceResolver object. See this code:

//Inject a Sling ResourceResolverFactory

@Reference

private ResourceResolverFactory resolverFactory;

     


public String getCustomerData(String filter) {

Customer cust = null;

List<Customer> custList = new ArrayList<Customer>();

Map<String, Object> param = new HashMap<String, Object>();

param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");

ResourceResolver resolver = null;

try {

          

    //Invoke the adaptTo method to create a Session used to create a QueryManager

    resolver = resolverFactory.getServiceResourceResolver(param);

    session = resolver.adaptTo(Session.class);

Avatar

Level 4

ResourceResolverFactory is like any other service that is available in OSGI framework. It's most frequent use is accessing resource resolver. You can see its detailed use in  your felix console, /system/console/configMgr. Search for "Apache Sling Resource Resolver Factory".

Now, coming to your second part of the question...

It depends on where you need to get the handle of resourceResolver. In Servlet OR In Java code(non servlet), In OSGI service or In WorkflowProcess class., In JSP .. etc.  The most straight forward way is to handle it in the servlet where we have request and resource objects available. So, its request.getResourceResolver() and resource.getResourceResolver(); In case of WorkflowProcess class OR in OSGI service,  you could inject a @Reference object for ResourceResolverFactory and get resourceResolver.

Finally, In case of JSP, if you have sling:defineObjects tag included, then resourceResolver becomes an implicit object and you could use it as is. I hope i have answered your question. Please let me know if you have any more questions or clarifications with respect to this question.

Avatar

Correct answer by
Level 7

ResourceResolver is one of the powerful API interface provided by Sling. Its job is to return resource object based on the given input, which can be a path to the resource, query or parent resource to get its children. Resource resolver object can be accessed in any one of the following ways -

1. Syntax for getting ResourceResolver from ResourceResolverFactory:

ResourceResolver serviceResourceResolver = resourceResolverFactory.getServiceResourceResolver(authenticationInfo);

Resource someResource = serviceResourceResolver.getResource("/path/to/resource");

someResource.adaptTo(MyModel.class);

NOTE: We'll use ResourceResolverFactory in situations such as - working to get JCR session and while we don't have request and resource objects (that means, while not dealing with servlets).

2. Syntax for getting ResourceResolver from Request object:

ResourceResolver resourceResolver=request.getResourceResolver();

3. Syntax for getting ResourceResolver from Resource object:

ResourceResolver resourceResolver= resource.getResourceResolver();

NOTE: We can retrieve a ResourceResolver using a ResourceResolverFactory and a service-user mapping as shown in the links below.

http://www.aemcq5tutorials.com/tutorials/resourceresolver-from-resourceresolverfactory/

http://www.adobeaemclub.com/access-to-resourceresolver-in-osgi-services-aem-6-1/

https://aem6tutorials.wordpress.com/2016/02/10/how-to-get-resourceresolver-from-resourceresolverfact...

Regards,

TechAspect Solutions

Avatar

Level 1

ResourceResolver Interface

  • Purpose: The ResourceResolver interface is used to resolve and access resources within the repository. It acts as your main tool for finding resources, adapting them to different types, and navigating the resource tree.

  • Functionality:

    • Resolving Resources: It maps HTTP requests or paths to resources stored in the repository.
    • Accessing Resource Properties: Allows for reading properties of resources.
    • Adapting Resources: Enables adapting resources to other types, like adapting a Resource to a ValueMap for property access or to a custom model class.
    • Querying: Supports querying the repository using SQL, XPath, or the Query Builder API.
  • Lifecycle: A ResourceResolver instance is typically obtained per request or session and must be closed after use to release any underlying resources and sessions.

ResourceResolverFactory Interface

  • Purpose: The ResourceResolverFactory is a factory interface used to create and obtain instances of ResourceResolver. It provides the methods to get a new ResourceResolver instance based on different types of authentication and access requirements.

  • Functionality:

    • Creating ResourceResolvers: Offers methods to create new ResourceResolver instances, such as getServiceResourceResolver for service users or getAdministrativeResourceResolver (deprecated and not recommended for use due to security concerns) for administrative access.
    • Parameterized Resolver Creation: Allows for the creation of ResourceResolver instances with specific attributes or requirements, using Map<String, Object> parameters to define the creation context.
  • Service User Mapping: In AEM, ResourceResolverFactory is often used in conjunction with service users, which are predefined users with specific permissions. This practice is part of AEM's security best practices, ensuring that code executes with only the permissions it requires.