Wednesday, 1 January 2020

A servlet to Query Pages containing particular components.

If you want to get your list of pages having particular components authored in it inside your project content folder in the form of json response, then here's a servlet for you.

The servlet is registered at path - /bin/generate/pagesreport to which you need to pass your page folder path (default is /content) and components path (separated by comma) as a query-param and it will output the list of pages (its title, path, template and component resourceType) as a json response.

What this servlet is doing is - it is forming a Predicate Map of Query having-
- path (content path from the input you provide)
- node type (nt:unstructured) 
- property (sling:resourceType)
- property value (list of components path from the input you provide)
and using it in QueryBuilder API and fetching the results in form of page node paths.

Further it is processing the hits (page node paths) from the search result to form the json response which you can consume by dong an ajax call.

This is the servlet for it-
package com.aem.community.wf.core.servlets;

import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;

import javax.jcr.Session;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SlingServlet(paths = "/bin/generate/pagesreport")
public class PageQueryServlet extends SlingSafeMethodsServlet {

 private static final long serialVersionUID = 1L;
 private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
 @Reference
 private QueryBuilder builder;
 
 @Reference
 private ResourceResolverFactory resolverFactory;
 
 @SuppressWarnings("deprecation")
 @Override
 protected void doGet(SlingHttpServletRequest request, 
                 SlingHttpServletResponse response) throws IOException {
  
     String pageRootPath = request.getParameter("pageroot");   
     if(pageRootPath == null){
      pageRootPath = "/content";
     }
     String compType = request.getParameter("comptype");   
     if(compType == null){
      compType = "weretail/components/content/productgrid";
     }else{
      compType = compType.replace("/apps/", "");
      logger.info("Logging comp type is: "+compType);
     }
     long totalMatches = 0;
     
     response.setCharacterEncoding("UTF-8");
     response.setContentType("application/json");
     
     JSONObject json = new JSONObject();
     JSONArray jsonarray = new JSONArray();
     JSONObject tempJson;
     
     try { 
         ResourceResolver resolver = request.getResourceResolver();
         Session session = resolver.adaptTo(Session.class);
      
         Map map = new HashMap();             
         map.put("path", pageRootPath);
         map.put("type", "nt:unstructured");
         map.put("property", "sling:resourceType");
         
         String[] patharr = compType.split(",");
         for (int i=0; i<patharr.length; i++){
             map.put("property."+i+"_value", patharr[i]);
         }
         map.put("p.offset", "0");
         map.put("p.limit", "-1");
                           
         Query query = builder.createQuery(PredicateGroup.create(map), session);                    
                     
         SearchResult result = query.getResult();
         totalMatches = result.getTotalMatches();                             
         String hitpath;
         Resource pageResource;
         String pagepath;
         String jcrpagepath;
         List<String> pagesList = new ArrayList<String>();
         int pagecounter = 0;
         int compcounter = 1;
         
         for (Hit hit : result.getHits()) {
            hitpath = hit.getPath();   
            tempJson = new JSONObject();
            pagepath = hitpath.split("/jcr:content")[0];
            jcrpagepath = pagepath +"/jcr:content";
            pageResource = resolver.getResource(jcrpagepath); 
                
            if (pageResource != null && !pagesList.contains(pagepath)) { 
              pagecounter++;
              ValueMap valueMap = pageResource.getValueMap(); 
              tempJson.put("pagename", 
                            valueMap.get("jcr:title", String.class));
              tempJson.put("pagerestype", 
                            valueMap.get("sling:resourceType", String.class));
              tempJson.put("pagetemplate", 
                            valueMap.get("cq:template", String.class));
              tempJson.put("pagepath", pagepath);
              jsonarray.put(tempJson);
              pagesList.add(pagepath);
            }
         }
         json.put("totalcomponents", totalMatches);
         json.put("totalpages", pagecounter);
         json.put("pages", jsonarray);
         logger.info("Total" + totalMatches + "paths were found: \n");
         
     } catch (Exception e) {
      logger.error("Exception caught: "+e);
 }
     response.getWriter().print(json.toString());
    
    }
}

Below is the sample url to call this servlet by passing apps folder path-
http://localhost:4502/bin/generate/pagesreport?pageroot=/content&comptype=weretail/components/content/productgrid,weretail/components/content/text

And this is the screenshot of the response by hitting above url-

A servlet to Query Components inside your apps folder.

If you want to get your list of components inside your project apps folder in the form of json response, then here's a servlet for you.

The servlet is registered at path - /bin/generate/componentsreport  to which you need to pass your project folder path (default is /apps) as a query-param and it will output the list of components (their name and path) as a json response.

What this servlet is doing is - it is forming a Predicate Map of Query having the path (apps path from the input you provide) and node type (cq:Component) and using it in QueryBuilder API and fetching the results in form of component paths.
Further it is processing the hits (components path) from the search result to form the json response which you can consume by dong an ajax call.

This is the servlet for it-
package com.aem.community.wf.core.servlets;

import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;

import javax.jcr.Session;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@SlingServlet(paths = "/bin/generate/componentsreport")
public class ComponentQueryServlet extends SlingSafeMethodsServlet {

 private static final long serialVersionUID = 1L;
 private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
 @Reference
        private QueryBuilder builder;
 
 @Reference
        private ResourceResolverFactory resolverFactory;
 
 @SuppressWarnings("deprecation")
 @Override
        protected void doGet(SlingHttpServletRequest request, 
                SlingHttpServletResponse response) throws IOException {
        
  
     String compPath = request.getParameter("comppath");   
     if(compPath == null){
      compPath = "/apps";
     }
     long totalMatches = 0;
     
     response.setCharacterEncoding("UTF-8");
 response.setContentType("application/json");
     
     JSONObject json = new JSONObject();
     JSONArray jsonarray = new JSONArray();
     JSONObject tempJson;
     
     try { 
      ResourceResolver resolver = request.getResourceResolver();
      Session session = resolver.adaptTo(Session.class);
      
      Map map = new HashMap();             
         map.put("path", compPath);
         map.put("type", "cq:Component");
         map.put("p.offset", "0");
         map.put("p.limit", "-1");
                           
         Query query = builder.createQuery(PredicateGroup.create(map),
                                                                     session);                    
                     
         SearchResult result = query.getResult();
         totalMatches = result.getTotalMatches();                             
         String hitpath;
         Resource compResource;
         String compTitle;
         
         for (Hit hit : result.getHits()) {
                hitpath = hit.getPath();   
                tempJson = new JSONObject();
                
                compResource = resolver.getResource(hitpath); 
         if (compResource != null) { 
                    ValueMap valueMap = compResource.getValueMap(); 
                    compTitle = valueMap.get("jcr:title", String.class);
                    tempJson.put("componentname", compTitle);
                }
         tempJson.put("componentpath", hitpath);
         jsonarray.put(tempJson);
         }
         json.put("totalcomponents", totalMatches);
         json.put("components", jsonarray);
         logger.info("Total" + totalMatches + "paths were found: \n");
         
     } catch (Exception e) {
      logger.error("Exception caught: "+e);
 }
     response.getWriter().print(json.toString());
    
    }
}

Below is the sample url to call this servlet by passing apps folder path-
http://localhost:4502/bin/generate/componentsreport?comppath=/apps/weretail

And this is the screenshot of the response by hitting above url-



  

A servlet to output html markup template of your AEM page.

If you want to get the html markup template of your AEM page which doesn't have css or js and pure html with white-spaces stripped off, here's a servlet for you.

You can invoke this servlet in any of your aem page by using the page path with 'template' selector and it will output the plain html for you.
Ex: http://localhost:4502/content/we-train/en.template.html

In few of the projects, when aem is integrated with SPA framework, where the role of aem is just hosting pages with structure and content and the client-side rendering is done through external SPA framework, it is often required to deliver the html from aem to front-end SPA team. In those cases, this servlet can be utilized.

So below is the AEM servlet registered with default resourceType - sling/servlet/default, and selector as template, so it can be invoked on any page.
I have written the comments as well in the code for understanding what each piece is doing. 

package com.foo.community.core.servlets;

import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.engine.SlingRequestProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.contentsync.handler.util.RequestResponseFactory;
import com.day.cq.wcm.api.WCMMode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * A default resourceType servlet called with 'template' selector to output
 * html template without css and js
 */
@SlingServlet(methods = {"GET"},
resourceTypes = {"sling/servlet/default"},
selectors = {"template"})
public class TemplateServlet extends SlingSafeMethodsServlet {

    private static final long serialVersionUID = 1L;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    /** Service to create HTTP Servlet requests and responses */
    @Reference
    private RequestResponseFactory requestResponseFactory;
    
    /** Service to process requests through Sling */
    @Reference
    private SlingRequestProcessor requestProcessor;

    @Override
    protected void doGet(final SlingHttpServletRequest request,
            final SlingHttpServletResponse response) throws ServletException,
              IOException {
        
        String requestPath = request.getRequestPathInfo().getResourcePath() +
             "." + request.getRequestPathInfo().getExtension();
        logger.info("requestPath is : "+requestPath);
  
        /** Setup request */
        HttpServletRequest req = 
                        requestResponseFactory.createRequest("GET", requestPath);
        WCMMode.DISABLED.toRequest(req);

        /** Setup response */
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpServletResponse resp = requestResponseFactory.createResponse(out);

        /** Process request through Sling */
        requestProcessor.processRequest(req, resp, request.getResourceResolver());
        String htmlcontent = out.toString();
     
        /** Replacing js and css files extension in html */
        String content = htmlcontent.replace(".js", ".nojs");
        content = content.replace(".css", ".nocss");
  
        /** Setting up writer to write html template */
         PrintWriter writer = null;
        if (StringUtils.isNotBlank(content)) {
            content = content.replaceAll("(\r?\n+\t?\\s*)", "\n");
            content = content.replaceAll("\n", ""); 
            logger.info("content written is :\n"+content);
            response.setCharacterEncoding("UTF-8");  
            response.setContentType("text/html");  
            writer = response.getWriter();
            writer.write(content);
            writer.close();
        }
    }
}

Below is the screenshot of how any page looks like in aem author-



and by using template selector on the above page, you will get it's html output-