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-
- 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-throws-exception-in-aem-6-1/31394583#31394583
I hope this will help you.
Thanks and Regards
Kautuk Sahni