May 26, 2020
Estimated Post Reading Time ~

ResourceResolver Object in AEM 6.1/6.0 Sling Services

In this article, we’ll discuss eight related questions around ResourceResolver Object in AEM 6.0 or 6.1 Sling services that most developers working on this technology will also have.

As we know, getAdministrativeResourceResolver() method has been deprecated from the ResourceResolverFactory interface in AEM6 and above versions. This brings up many questions in the minds of stakeholders, especially developers, involved in AEM projects. I’ve discussed some of those questions and provided answers to them.

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 to get a ResourceResolver object.
If you know the credentials of a user and want that user credential in your service, then go with the getResourceResolver() 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());
}

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 the Felix Console of your AEM instance.

Q2) What configuration do I need to make for AEM6.0?
For AEM6.0, the configuration steps are –
Go to Felix Console configuration tab i.e. http://<host>:<port>/system/console/configMgr
Search for User Mapping as shown in figure-

Click on this service to edit its configuration.
Here you have to add one entry and the syntax of the entry is –
“Bundle symbolic Name”:”SubServieName”=” User Name”

1. “Bundle symbolic Name”:- Here you have to add the bundle symbolic name where you are creating service.
2. “SubServiceName”:- This is the name you provided as a value of ResourceResolverFactory.SUBSERVICE property i.e. “testService”.
3. “User Name”:- This is the user name for ex. “admin”So in my case this configuration field value becomes “com.blog.blog-bundles:testService=admin”
After adding this entry, your configuration looks like

Now everything is set for AEM6.0

Q3) Will these configurations work for AEM6.1?
No.

Q4) What extra configuration do I need for AEM6.1?
If you are working with AEM6.1, then you have to complete all the steps explained for AEM6.0. Then, to make these configurations working for AEM6.1, you have to ensure that the “jcr:PrimaryType” of your user is “rep:SystemUser” i.e if you are trying to use “admin” user. Then it will not work as its “jcr:PrimaryType” value is “rep:User”
Q5) What do you mean by “jcr:PrimaryType” as “rep:SystemUser”?
It means that the user is just not a repository user. It must be a system user.

Q6) How to create a System User in AEM6.0?
To create a System User, follow these steps-
Go to CRX Explorer http://<host>:<port>/crx/explorer/index.jsp
Log in as Administrator i.e. using “admin:admin”
Click on “User Administration”, you will see a screen just like this

Here you will see the option of “Create System User.” Click on this button.
Add a value for User Id field for ex. “testUser”
Click on the green button.
Close the window.
Go to CRXDE Lite, your “testUser” will be created under /home/system directory. If you are not able to find it, then search for “testUser” in the home screen. You will get the location.

Q7) How to use this user?
In your “Apache Sling Service User Mapper Service,” the configuration changes your entry from –
com.blog.blog-bundles:testService=admin to com.blog.blog-bundles:testService=testUser

Q8) Should I do this configuration at the “Apache Sling Service User Mapper Service” configuration or should I create an “Apache Sling Service User Mapper Service Amendment” service configuration?
You can do it both ways. However, as “Apache Sling Service User Mapper Service” is a service factory in AEM6.1 so as to best practices you should create another service configuration by clicking on the plus button in front of the “Apache Sling Service User Mapper Service Amendment”.

When you click on the + button, one new configuration will be created at the end. Click on that service and do this configuration there. Your code will work in the same manner as before.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.