Search code examples
jspaemsling

Sling: Find out if resource is an image


I have a completely set-up CQ5/AEM application and am supposed to generate a sitemap.xml. So far, so good.

I have a list of all the pages, but some of those pages are actually images. My question: How can I find out if a page is actually an image? Both have jcr:primaryType=cq:Page

public void getMoreChildren(HttpServletRequest request, JspWriter out, Page incomingChildPage) {
        Iterator<Page> childPageChildren = incomingChildPage.listChildren();
        while (childPageChildren.hasNext()) {
            Page childPage = childPageChildren.next();
            String pagePath = childPage.getPath();
            SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)request;

            ResourceResolver resourceResolver = slingRequest.getResourceResolver();
            Externalizer externalizer = resourceResolver.adaptTo(Externalizer.class);

            String externalUrl = externalizer.publishLink(resourceResolver,pagePath) + ".html";
            //do things with data so far
            getMoreChildren(request, out, childPage);
        }
}

All of this runs within a JSP and does what it's supposed to do so far, except that it treats images as pages and I want to ignore image files. What do I need to do?


Solution

  • Images stored in the DAM should have jcr:primaryType=dam:Asset. That said, you should be able to check these properties:

    jcr:content
        jcr:primaryType=cq:PageContent
        cq:template=/apps/yourApp/yourTemplate
    

    An image won't have a jcr:content/cq:template property, and it won't have a jcr:content/jcr:primaryType property that equals cq:PageContent.

    Those properties exist under the jcr:content node. Try hitting two sample urls--one for the image, and one for the regular page--but add ".infinity.json" to the end of the URL. That will show you the properties of each so you can find things to help you filter out the images.