Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

AEM - Java code needs to access frontend resource ( in /etc.clientlibs)

Avatar

Level 7

Hi folks,

I have some Java code which needs to write an image to a pdf file.  The image is located in the code, in frontend/src/main/webpack/resources/images/blah.gif/jcr:content.

 

Eventually the build system puts the image at the following location:

/apps/xxx/clientlibs/resources/images/blah.gif/jcr:content

which the proxy servlet maps to

/etc.clientlibs/xxx/clientlibs/resources/images/blah.gif/jcr:content 

 

The Java code works fine on Author ( I assume because I am logged in), but not on the publishers.  Does the "anonymous" user have to have explicit read access permission at the path to the image at /apps/xxx/clientlibs/resources/images/blah.gif/jcr:content ?   

 

tnx 

Fiona

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @fionas76543059 -

 

To access the AEM system restricted paths in Java code, you will need service resource resolver. You can create a system user with appropriate permissions to the /apps/* path and use this to configure service for fetching the service resource resolver.

 

Some useful links for implementation - 

https://aem4beginner.blogspot.com/how-to-get-resourceresolver-from

http://www.aemcq5tutorials.com/tutorials/resourceresolver-from-resourceresolverfactory/

 

Thanks,

Fani

View solution in original post

8 Replies

Avatar

Community Advisor

@fionas76543059 ,

 

Yes. Please give read access to anonymous user to access the image resources on publish instance.

 

Thanks,

Aruna

Avatar

Correct answer by
Community Advisor

Hi @fionas76543059 -

 

To access the AEM system restricted paths in Java code, you will need service resource resolver. You can create a system user with appropriate permissions to the /apps/* path and use this to configure service for fetching the service resource resolver.

 

Some useful links for implementation - 

https://aem4beginner.blogspot.com/how-to-get-resourceresolver-from

http://www.aemcq5tutorials.com/tutorials/resourceresolver-from-resourceresolverfactory/

 

Thanks,

Fani

Avatar

Level 7

Thanks all !

 

I will go with the new  system user option for the Java code then as this seems to be the safest.

 

 

Avatar

Community Advisor

Hi @fionas76543059 

 

If you are accessing the resource using /etc.clientlibs/xxxx then there should not be any permission issue as anonymous user should be able to access it using the proxy servlet like the way it's accessing the other clientlib content.

 

Can you check which path the request is being made and then we can check if a system user is really required or if there are any other issue.

 

Thanks!

Avatar

Level 7

hi!

thanks for all the help!

 

I access lots of image in /etc.clientlibs from Javascript programs without problems.

This is from the Java code though and it reads the "data" property on the gif node jcr:content to write it to a PDF.

If I give "anonymous" read permission on /apps/xxxx/clientlibs/resources/images/blah.gif it does work.

When I tried to use /etc.clientlibs/xxx/clientlibs/resouces/images/blah.gif/jcr:content  it didn't work

I'm a bit puzzled!

 

thanks

Fiona

 

Avatar

Employee Advisor

Hi @fionas76543059 ,

 

Have you added allowProxy true on your clientlibs. If not that will be the issue.

 

https://experienceleague.adobe.com/docs/experience-manager-64/developing/introduction/clientlibs.htm...

 

 

Hope this helps!!
Thanks

Avatar

Level 6

@fionas76543059 You can add the allow Proxy on the client-lib. See the snapshot below for the reference.

kchaurasiya_0-1631706181670.png

 

if in case it does not work they create system user and use that system user to read the node values.

Thank you.

 

 

 

Avatar

Level 7

Thanks for all the help.

My  clientlib already has this allowProxy flag set and it works fine for all my Javascript code that also references the images there.

It is only in the Java when I try to read the image to write it into a PDF that I get this "message, javax.jcr.PathNotFoundException: Node with path /etc.clientlibs/xxxx/clientlibs/core/resources/images/hd_PF.gif/jcr:content does not exist."

 

The only thing that has worked for me is too enable "anonymous" to read /apps/xxxx/clientlibs/resources/images/hd_PF.gif/jcr:content.

 

But I don't know if this is a good thing to do.  It is like the Java code doesn't see the proxy servlet stuff