Skip to main content
Level 4
Jan 22, 2024 2:49 AM UTC

How can I call an author api by java code

I just want to call aem ootp api like '/libs/wcm/core/content/reference.json' by java code in author. Are there any convenient ways to do this? 

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

16 replies

Level 7
Jan 22, 2024 3:15 AM UTC

Hi @johann_lu 

 

You can call that API, similar to how would you call 3rd party API. 

Translate
Johann_LuAuthor
Level 4
Jan 22, 2024 3:32 AM UTC

But when I call author apis, it needs account and password, I just want some convenient ways to call these api.

Translate
Level 7
Jan 22, 2024 3:50 AM UTC

Hi @johann_lu 

Usually in publish environment, for the anonymous user: user has restricted permissions only. ie /content /bin.  In your case, you need to give read permission to the path "/libs/wcm/core/content/" for the anonymous user. Is this the right way?? Not sure.

Or else, you need to create some proxy servlet, that will internally call /libs/wcm/core/content/.  

Translate
Mahedi_Sabuj
Community Advisor
Community Advisor
Jan 22, 2024 9:01 AM UTC

You can use AssetReferenceSearch get the resources referenced by the page. It's constructor takes the node (content node of the page ) to search under, path where the assets are stored (can be /content/dam ) and Resource Resolver.

String Pagepath = "/content/we-retail/en"; //This would be your page path Resource r = resourceResolver.getResource(Pagepath + "/" + JcrConstants.JCR_CONTENT); Node n = r.adaptTo(Node.class); AssetReferenceSearch ref = new AssetReferenceSearch (n, DamConstants.MOUNTPOINT_ASSETS, resourceResolver); Map<String,Asset> allref = new HashMap<String,Asset>(); allref.putAll(ref.search()); for (Map.Entry<String, Asset> entry : allref.entrySet()) { String val = entry.getKey(); out.println(val+"<br>"); // Path of all Asset ref in page Asset asset = entry.getValue(); }

Reference: https://stackoverflow.com/questions/30892111/how-to-get-detail-of-all-digital-assets-used-in-single-webpage-in-aem 

 

 

Translate
Mahedi Sabuj
Johann_LuAuthor
Level 4
Jan 22, 2024 9:11 AM UTC

What about template and policy?

Translate
Johann_LuAuthor
Level 4
Jan 23, 2024 2:18 AM UTC

I have checked it before, it didn't work, the result is null

Translate
arunpatidar
Community Advisor
Community Advisor
Jan 23, 2024 8:30 AM UTC
kautuk_sahni
Level 9
Jan 23, 2024 11:02 AM UTC

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

Translate
Kautuk Sahni
Johann_LuAuthor
Level 4
Jan 24, 2024 1:12 AM UTC

What they said I have tried before, but those didn't work.

Translate
Level 2
May 14, 2024 7:33 AM UTC

I have the same requirement as yours, I stumbled upon this article it might help

https://kiransg.com/2023/05/16/sling-servlet-helpers/

Translate