Expand my Community achievements bar.

How can I call an author api by java code

Avatar

Level 4

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? 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

16 Replies

Avatar

Level 8

Hi @Johann_Lu 

 

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

Avatar

Level 4

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

Avatar

Level 8

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

Avatar

Level 4

Or may be you can tell me that how can I get the resources refferenced by the page? I have tried several apis like 'ReferenceProvider' which didn't work.

Avatar

Community Advisor

@Johann_Lu : Have you tried invoking this from your backend ie. java?
The request to your backend system would already have the session details coming through cookie from original backend request.
Please try and see if it already works as-is. If not, please identify (i.e login-token) what is needed for reference.json to work in author and retrieve the same from original request's cookie.
thanks.

Avatar

Level 4

Yes, I know this method, I am just looking for a more convenient way to call this api. In fact, I just need all resources referenced by the page,like this api '/libs/wcm/core/content/reference.json' do.

Avatar

Community Advisor

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

 

 

Avatar

Level 4

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

Avatar

Administrator

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



Kautuk Sahni

Avatar

Level 4

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

Avatar

Level 1

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

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