How to get Image/Dam reference from any CQ5 Page

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

You can also find references of  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 = 1L;  
      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();  
           }  
      }  
 }  
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s