Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

AEM 6.4 - Not able to access resource with below resourceResolver

Avatar

Level 2

This issue is related to get a resource with the help of system user ("someuser") having all permissions given to that user in AEM 6.4. below is the code snippet:

In configuration, my system user is defined as "someuser" against the bundle. But, for below code I am doing resourceResolver.get UserId() giving me //sling-scripting. It should give me "someuser".

ResourceResolverFactory resourceResolverFactory = sling.getService(ResourceResolverFactory.class);
    ResourceResolver systemResourceResolver = resourceResolver;
    if (!(resourceResolverFactory == null)) {
        Map<String, Object> authenticationInfo = new HashMap<>();
        authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, "bundlevalidation");
        systemResourceResolver = resourceResolverFactory.getServiceResourceResolver(authenticationInfo);
    }

    String pagePath = slingRequest.getRequestPathInfo().getSuffix();
    Resource contentBundleResource = null;
    if (pagePath != null) {
        Resource res = systemResourceResolver.getResource(validPagePath);
    }

I debugged all variables above defined, only res is coming as null.

This code is working fine in AEM 6.1. But creating issue in 6.4.

What code change do I need to make for getting that resource.

I studied below link but didn't get much information:

AEM 6.4 unable to access content path via resourceresolver/session

Also, I have this code written in JSP file.

Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

OK, I understand your case.

The "problem" is the case, that the JSP does not run in the context of your bundle, but in the context of the sling-scripting engine. Thus every call of

authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, "bundlevalidation");

systemResourceResolver = resourceResolverFactory.getServiceResourceResolver(authenticationInfo);

will lookup the subservice for the sling scripting engine bundle, but not yours. For this bundle only the "sling-scripting" user is defined.

What you can do to make your code working:

  • Either you define a new usermapping for the sling-scripting-engine bundle, and configure a mapping for the subservice "bundlevalidation" and map it to "someuser".
  • Or you move that code completely into a service running inside your own custom bundle; if you have defined the usermapping there already, it work should work as well.

I would prefer the second option, because all this sounds too much of business logic, which can be much easier unittested when the code lives inside a bundle instead of a JSP.

View solution in original post

5 Replies

Avatar

Employee Advisor

It's not clear for me, where you get the "resourceResolver" from. And on which of the 2 resourceResolvers you have in the above code your call this method at all. Can you please elaborate?

"sling-scripting" is the system user, which is used to resolve the rendering scripts. JSPs are running inside the JSP scripting engine; if you open a new ServiceResolver with the code above, you'll get a resource resolver owned by sling-scripting. This is not a bug but a change introduced with (IIRC) AEM 6.2.

And to be honest, I don't think that opening a new resource Resolver is code which belongs into a JSP. That's something I would put into a service and a bundle.

Avatar

Level 2

Mistakenly I have written resourceResolver, I was talking about systemResourceResolver.

In configuration, my system user is defined as "someuser" against the bundle. But, for above code when I am doing systemResourceResolver.get UserId() giving me //sling-scripting. It should give me "someuser".

systemResourceResolver.get UserId()

actual Result: "sling-scripting"

Expected Result: "someuser"

Avatar

Correct answer by
Employee Advisor

OK, I understand your case.

The "problem" is the case, that the JSP does not run in the context of your bundle, but in the context of the sling-scripting engine. Thus every call of

authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, "bundlevalidation");

systemResourceResolver = resourceResolverFactory.getServiceResourceResolver(authenticationInfo);

will lookup the subservice for the sling scripting engine bundle, but not yours. For this bundle only the "sling-scripting" user is defined.

What you can do to make your code working:

  • Either you define a new usermapping for the sling-scripting-engine bundle, and configure a mapping for the subservice "bundlevalidation" and map it to "someuser".
  • Or you move that code completely into a service running inside your own custom bundle; if you have defined the usermapping there already, it work should work as well.

I would prefer the second option, because all this sounds too much of business logic, which can be much easier unittested when the code lives inside a bundle instead of a JSP.

Avatar

Level 2

Thanks a lot for giving your time and reply again. It resolved my issue, great help....

Avatar

Level 10

Try to write this code in Java as shown here -- Adobe Experience Manager Help | Querying Adobe Experience Manager 6.4 JCR data

You can see the video that shows this use working.