jspaemdam

how can I get the asset path of a given rendition from in Java file in AEM?


I want to display 'cq5dam.thumbnail.140.100.png' rendition of my image asset as a thumbnail in my html page. How can I get the asset path of one of my asset in DAM in a java/JSP for a given rendition


Solution

  • this function will do the trick

    public static String getImageAssetPath(SlingHttpServletRequest slingRequest,String actualDamPath,String renditionParam,String defaultPath) {
        try {
            if(StringUtils.isNotEmpty(actualPath)){
                  Resource resource = slingRequest.getResourceResolver().getResource(actualPath);
                  Asset asset = resource.adaptTo(Asset.class);
                  String imageAssetPath = asset.getRendition(renditionParam).getPath();
                  LOGGER.info("imageAssetPath for given rendition: " + imageAssetPath);
                  return imageAssetPath;
           }
    
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }
    
        return defaultPath;
    }
    

    put this function in a tag library so that the same can be used in your jsps

    ${mytaglib:getAssetPath(slingRequest,property.previewImage,'cq5dam.thumbnail.140.100.png','')}