Expand my Community achievements bar.

Difference between getResourceResolver(param) and getServiceResourceResolver(param)??

Avatar

Level 9

Hello All,

What are the occasions we can use getResourceResolver(param) and getServiceResourceResolver(paramMap) methods??

We are trying to get resource resolver using below code but getting null object.

Map  param = new HashMap ();             

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

ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(param);

-Kishore

6 Replies

Avatar

Level 10

If you are using this code:

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);

and getting null - means you have not setup Sling Mapping properly. See this AEM Article that walks you through how to successfully use this code; 

 

https://helpx.adobe.com/experience-manager/using/querying-experience-manager-sling.html

Avatar

Administrator

Hi 

I would recommend you to have a look at this reference article:- 

Excellent Read:- http://www.accunitysoft.com/resourceresolver-object-in-aem6-16-0-sling-services/

Q1). How to get ResourceResolver object in Sling services in AEM6.0 and above versions?
If you are working with AEM6 or AEM6.1 then you have two options-

  1. If you know the credentials of an User and want that user credential in your service then go withgetResourceResolver() method. 

Map<String, Object> param = new HashMap<String, Object>();            
param.put(ResourceResolverFactory.USER,"admin");
param.put(ResourceResolverFactory.PASSWORD,"admin".toCharArray());
try {
      resourceResolver = resourceResolverFactory.getResourceResolver(param);
}catch (Exception e){
      System.out.println(e.getMessage());
}

 

2.If you don’t have user credentials and want to access ResourceResolver object then you have to use
getServiceResourceResolver() method. as shown below –

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE,"testService");
try {
    resourceResolver = resourceResolverFactory.getServiceResourceResolver(param);
}catch (Exception e){
    System.out.println(e.getMessage());
}

 

Note : – In this case you have to add a configuration in your Felix Console to your AEM instance.

 

Link 2:- https://aemtreasury.wordpress.com/2015/07/02/using-resourceresolver-in-service/

//USING RESOURCERESOLVER IN SERVICE

The Major part is the param which we give to getServiceResourceResolver got have proper privilege, Otherwise I got Null ResourceResolver object.

//

How I can use the proper privilege?

I’ve done this by configrating “Apache Sling Service User Mapper Service” via OSGI configuration(http://localhost:4502/system/console/configMgr)

[img]https://aemtreasury.files.wordpress.com/2015/07/sling-service-mapping.png[/img]

Create new config in Service mappings section by following the rule:

Provides mappings from service name to user names. Each entry is of the form ‘serviceName [ “:” subServiceName ] “=” userName’ where serviceName and subServiceName identify the service and userName defines the name of the user to provide to the service. Invalid entries are logged and ignored. (user.mapping)

In my case, com.dale.cq.services.tryservice:properPrivilege=properUser   (properUser should be the user given proper read/write privilege , I use the author user for testing).

Note: “com.dale.cq.services.tryservice” used here is  NOT a package ID, it is a BundleId which can be gotten from http://localhost:4502/system/console/bundles
There are 2 way to simplify :

1. Simplify config it to com.dale.cq.services.tryservice=properUser (in my case is  com.dale.cq.services.tryservice=author)

or

2. Just use resolver = resolverFactory.getServiceResourceResolver(null); and config Default User as a properuser with right privilege (I use the author user for testing)

 

Reference Read:- http://stackoverflow.com/questions/31350548/resourceresolverfactory-getserviceresourceresolver-throw...

 

I hope this will help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Level 9

Do we need to make changes to get the resource resolver object on publisher instance??

We don't see any issue on author but not able to get node information using resource resolver in publisher instance.

Please help.

Thanks,

Kishore

Avatar

Level 10

Do we need to make changes to get the resource resolver object on publisher instance??

Setup everything on Pub as you did not author. Define  a system user and use sling mapping on pub. Do everything the same as you did on author and it will work. 

Avatar

Administrator

Try to maintain one to one mapping in publish instance as your have in author instance.

As mentioned, "Define system user and use sling mapping on pub."

~kautuk



Kautuk Sahni

Avatar

Level 9

I understand about the system user but what exactly mean by sling mapping??

Thanks,

Kishore