AEM get resource resolver in jsp using serviceuser. | Community
Skip to main content
Adobe Employee
April 19, 2024
Solved

AEM get resource resolver in jsp using serviceuser.

  • April 19, 2024
  • 6 replies
  • 2966 views

Hi Team,

 

I have a requirement to get the resource using path. I tried to create a service user and provided required permissions but it is not working

This is my JSP code.

ResourceResolverFactory resolverFactory = sling.getService(ResourceResolverFactory.class);
ResourceResolver serviceResourceResolver = null;
Map<String,Object> serviceMap = new HashMap<>();
serviceMap.put(resolverFactory.SUBSERVICE,"test-service");
serviceResourceResolver = resolverFactory.getServiceResourceResolver(
serviceMap);

userManager = resourceResolver.adaptTo(UserManager.class);
serviceUserManager = serviceResourceResolver.adaptTo(UserManager.class);
Authorizable authorizable = serviceUserManager.getAuthorizable("test1-approvers");

log.info("sample is " + serviceResourceResolver.getResource("/content/xyz/en/light")); -- it s thrwoing a null pointer. I have provided required permissions.

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by EstebanBustamante

Hi, 

If a nullPointer exception is being thrown, it means that the "serviceResourceResolver" is null. This occurs because authentication with the provided user was unsuccessful. Please ensure that the system user is correctly mapped and has all the necessary permissions. To which bundle did you tie the system-user you are using? That maybe the error. You can refer to an example of how to create a system user in AEM at the following link: https://www.aemcq5tutorials.com/tutorials/create-system-user-in-aem/ Your code appears to be correct. You can view an example of how this works in the following file: /libs/dam/gui/coral/components/admin/contentrenderer/row/common/common.jsp

 

Additionally, depending on where and how this JSP is being invoked, you could use the Sling request object to retrieve the resourceResolver from it as well. You could give it a try to see if that works. It would be something like this:

Resource contentRes = slingRequest.getResourceResolver().resolve(contentPath);


Hope this helps

6 replies

RikVanB
April 19, 2024

Hi @sateeshre2 

Could you verify if you followed all steps mentioned in this User Guide? It would also be nice if you could provide us with some screenshots from your different configurations you did to make it work.

 

Greetings

Rik

EstebanBustamante
Community Advisor and Adobe Champion
EstebanBustamanteCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
April 19, 2024

Hi, 

If a nullPointer exception is being thrown, it means that the "serviceResourceResolver" is null. This occurs because authentication with the provided user was unsuccessful. Please ensure that the system user is correctly mapped and has all the necessary permissions. To which bundle did you tie the system-user you are using? That maybe the error. You can refer to an example of how to create a system user in AEM at the following link: https://www.aemcq5tutorials.com/tutorials/create-system-user-in-aem/ Your code appears to be correct. You can view an example of how this works in the following file: /libs/dam/gui/coral/components/admin/contentrenderer/row/common/common.jsp

 

Additionally, depending on where and how this JSP is being invoked, you could use the Sling request object to retrieve the resourceResolver from it as well. You could give it a try to see if that works. It would be something like this:

Resource contentRes = slingRequest.getResourceResolver().resolve(contentPath);


Hope this helps

Esteban Bustamante
DPrakashRaj
Community Advisor
Community Advisor
April 19, 2024

I don’t think it’s necessary to use service user at all times to get the resource unless you are doing any write operation in the repo and are only accessing the nodes inside /content folder. Normally /content folder has anonymous read permission so please revisit your design for this specific use case to use service user to only read the jcr nodes inside /content node.

Shashi_Mulugu
Community Advisor
Community Advisor
April 22, 2024

@sateeshre2 , Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community

April 26, 2024

@estebanbustamante I am having the same issue as this user. What bundle would I map a service user to for this to work correctly in a JSP? Maybe org.apache.sling.scripting.coreI looked at the code in /libs/dam/gui/coral/components/admin/contentrenderer/row/common/common.jsp (thanks for the reference) but I couldn't find the corresponding service user and mapping in the JCR.

DPrakashRaj
Community Advisor
Community Advisor
April 26, 2024

It should map to your core bundle symbolic name.

<bundleId>:<subServiceName>=<systemUser>

please also look for below notes on https://experienceleague.adobe.com/en/docs/experience-manager-65/content/security/security-service-users

 

Administrative sessions in JSPs

JSPs cannot use loginService(), because there is no associated service. However, administrative sessions in JSPs are usually a sign of a violation of the MVC paradigm.

This can be fixed in two ways:

  1. Restructuring the content in a way that allows manipulating it with the user session;
  2. Extracting the logic to a service that provides an API that can then be used by the JSP.

The first method is the preferred one.

April 26, 2024

I don't quite follow, this is in a JSP context created by a package install not a service in a bundle. Regardless, my code looks like this:

 

ResourceResolverFactory resolverFactory = sling.getService(ResourceResolverFactory.class); ResourceResolver resolver = null; try { resolver = resolverFactory.getServiceResourceResolver( Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object)"test-user")); log.info("resolver is null? {}", resolver == null); ModifiableValueMap modifiableValueMap = resolver.resolve(submitInfo.getFormContainerPath()).adaptTo(ModifiableValueMap.class); log.info("modifiable value map is null? {}", modifiableValueMap == null); modifiableValueMap.put("myProperty", propertyString); } finally { if (resolver != null) { resolver.close(); } }

 

And the log always says:

modifiable value map is null? true

 

Let's presume I can't reorganize the code, and I was instructed to do this in a JSP. The service user exists and has read/write permissions on the content path. How do I make this work?

kautuk_sahni
Community Manager
Community Manager
May 20, 2024

@sateeshre2 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni