Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

What is the difference between ResourceResolver and ResourceResolverFactory interface?

RashidJorvee
Level 4
Level 4

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
Techaspect_Solu
Correct answer by
Level 7
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

0 Replies
smacdonald2008
Level 10
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);

Mallik-Vamaraju
Level 3
Level 3

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.

Techaspect_Solu
Correct answer by
Level 7
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