Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

AEM get resource resolver in jsp using serviceuser.

Avatar

Employee

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.

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

9 Replies

Avatar

Level 3

Hi @SateeshRe 

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

Avatar

Correct answer by
Community Advisor

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

Avatar

Community Advisor

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.

Avatar

Community Advisor

@SateeshRe , 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

Avatar

Level 2

@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.

Avatar

Community Advisor

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-u...

 

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.

Avatar

Level 2

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?

Avatar

Community Advisor

You need to move you business logic from jap to service but if you still want to do in jsp then you need to whitelist all bundles and may be you require a code refracting as well. Please see below docs to whitelist all bundles https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-6-4-whitelist-of-bundl...

Avatar

Administrator

@SateeshRe 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