April 3, 2020
Estimated Post Reading Time ~

How to get Image/Dam reference from any CQ5 Page

If you are looking for all the image references in any CQ Page to maybe play with these assets. This can be done easily using the below servlet code. The path should be passed from the parameter.

You can also find references for components and dam images using parameter debug=layout. Click Here to see. 


 @SlingServlet(paths = "/bin/assetreference", metatype = true, methods = {
  "GET"
 })
 public class AssetReference extends AbstractPredicateServlet {
  private static final long serialVersionUID = 1 L;
  public void doGet(final SlingHttpServletRequest slingHTTPrequest, final SlingHttpServletResponse response) throws IOException {
   try {
    String pagePath = slingHTTPrequest.getParameter("path");
    ResourceResolver resourceResolver = slingHTTPrequest.getResourceResolver();
    Resource resource = resourceResolver.getResource(pagePath + "/" + JcrConstants.JCR_CONTENT);
    Node node = resource.adaptTo(Node.class);
    AssetReferenceSearch assetReference = new AssetReferenceSearch(node, "/content/dam", resourceResolver);
    for (Map.Entry < String, Asset > assetMap: assetReference.search().entrySet()) {
     String val = assetMap.getKey();
     Asset asset = assetMap.getValue();
     LoggerUtil.debugLog(this.getClass(), "Key and Value are {}{}", val, asset.getPath());
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.