Recently, I tried to get ResourceResolver directly from services rather than using request.getResourceResolver().
As API document said, I can use resolverFactory.getServiceResourceResolver() to get ResourceResolver object.
The main code I used list below:
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, “properPrivilege”);
resolver = resolverFactory.getServiceResourceResolver(param);
The Major part is the param which I 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)
Look at screen shoot below
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)