Sunday, 17 June 2018

Content Copier Servlet to perform JCR copy operation

This post is a part-2 of this post. In my previous post i have created a servlet invoker utility component and I will use that tool to invoke the Content copier servlet which is demonstrated below.

This servlet performs the JCR workspace copy operation. To demonstrate a particular use case i have worked on, there was a bunch of pages in a folder of type (sling:OrderedFolder) in CQ5 inside /content and more sub-pages inside the sub folder of type (sling:OrderedFolder) in it, and the ask was to migrate the pages from folder structure (nested) in CQ5 to AEM page structure.

To achieve the same I have written a servlet which accepts the source(path of the parent source folder) and destination (path of the parent destination page) paths as arguments and perform the workspace copy operation.

The primary method used to perform the operation is
Workspace.copy(String srcAbsPath, String destAbsPath)
This method copies the subgraph rooted at, and including, the node at srcAbsPath to the new location at destAbsPath.

Servlet to achieve the functionality-
package com.foo.community.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.servlets.SlingSafeMethodsServlet;

import javax.jcr.Repository;
import javax.servlet.ServletException;
import java.io.IOException;
import javax.jcr.NodeIterator;

import javax.jcr.Session; 
import javax.jcr.Node; 
import javax.jcr.Workspace;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 


@SuppressWarnings("serial")
@SlingServlet(paths = "/system/utility/servletinvoker")
public class ContentCopierServlet extends SlingSafeMethodsServlet {

  private static final Logger log = LoggerFactory.getLogger(this.getClass());

  @Reference
  private Repository repository;
  @Override
  protected void doGet(final SlingHttpServletRequest req,
   final SlingHttpServletResponse resp) throws ServletException, IOException {

     String message="Initial servlet message";
     String requestPath1 = req.getParameter("path1");
     String requestPath2 = req.getParameter("path2");
     try {
 Session session= req.getResourceResolver().adaptTo(Session.class);
 copyNodes(requestPath1,requestPath2,session);
 message= "Content copied from: "+ requestPath1+ " to: "+ requestPath2; 
     } 
     catch (Exception e) {
 message="Error performing operation: " + e.getMessage();
 log.error("copyServlet main error", e);
 e.printStackTrace();
     }
     resp.getWriter().println(message);
    }
 
   private void copyNodes(String requestPath1, String requestPath2, Session session)
                                                              throws Exception{
 
    Workspace workspace = session.getWorkspace();
    String resrc = requestPath1.substring(1,requestPath1.length()); 
    String redes = requestPath2.substring(1,requestPath2.length()); 
  
    Node rootnode = session.getRootNode();
    Node srcnodepath = rootnode.getNode(resrc);
    Node destnode = rootnode.getNode(redes);
  
    if (srcnodepath.hasNodes()) {
 NodeIterator worknodes = srcnodepath.getNodes();
 while (worknodes.hasNext()) {
    Node childnde = worknodes.nextNode();
    
    // formation of destination path and its relative path variables
    String[] parts = childnde.getPath().split("/");
    String destrelativepath = parts[parts.length-1];
    String destpath = destnode.getPath() + "/" + destrelativepath;
    
    if (destnode.hasNode(destrelativepath)) {
    
  String prtype= childnde.getProperty("jcr:primaryType").getString();
  log.info("primary type is: "+prtype);
  int flag=0;
  if(prtype.equals("sling:OrderedFolder")){
   copyNodes(childnde.getPath(),destpath,session);
   flag=1;
  }     
     
  if(!destpath.contains("jcr:content") && flag==0){
   destnode.getNode(destrelativepath).remove();
   session.save();
   log.info("src path is: "+childnde.getPath());
   log.info("dest path is: "+destpath);
   workspace.copy(childnde.getPath(), destpath);
  }
    }else{
  String srpath= childnde.getPath();
  workspace.copy(srpath, destpath);  
           }
 }
       }
     } 
}


Before beginning the operation, make sure to have the destination page structure(skeleton) ready for this to work.
All you have to do is to create the destination pages corresponding to folders of your source.
If your source contains the sub-folders, you have to create a nested page structure in your destination also. 
The screenshot of CRX before and after the operation is shown below:


The Servlet is registered as path,  so you can make an ajax call from your utility component and pass the source and destination paths as an argument.
Also, mostly the utilities like these are performed on author instance so you can find the proper path for the servlet to be registered at.

So, this was my use case, but if you have similar such requirement, you know how to do JCR workspace copy operation.

Wednesday, 13 June 2018

Servlet Invoker Utility Component

This post will be a beginning of various posts in which I will create a Servlet Invoker utility component and through this component invoke the servlet for various operations in AEM by passing parameters from our component as an argument to the servlet.

Often times, we create a utility in AEM in form of Services, Servlets etc. and call them by various means using Sling Models, WCMUSEPojo etc. which is not consistent though it achieves your functionality, but here through a common component we can invoke our Servlets and pass parameters to it and achieve our functionality.

To begin with, create a component in AEM and it's dialog. The dialog can contains the field for which servlet to invoke and what parameters to pass to the Servlet .
It's html contains a simple Button on press of which our servlet will invoke.

For ex:
<sly data-sly-use.clientlib="/libs/granite/sightly/templates/clientlib.html">

   <h2>Welcome to Servlet Invoker Utility Component</h2>

   <strong>Source path:</strong> <span id="path1">${properties.path1}</span> <br>
   <strong>Destination path:</strong> <span id="path2">${properties.path2}</span>
   <br><br>

   <input id="submitButton" type="submit" title="Submit" value="Begin Operation">

   <div id="result">
       <h3 data-sly-test="${properties.path1 && properties.path1}">
                        Press Begin Operation button to invoke servlet</h3>
       <h3 data-sly-test="${!(properties.path1 && properties.path1)}">
                        Please author source and destination path</h3>
   </div>

<sly data-sly-call="${clientlib.js @ categories='servlet.invoke'}" />
It looks something like this:















Along with the html file of the component, create a clientlibs for the component with your categories say servlet.invoke and write a JS code which contains a logic to do an ajax call to your servlet on press of the Button. Also you can pass the parameters to your servlets through the same ajax call which you can get from your html.
For ex.
(function ($, document) {
    'use strict';
    $("#submitButton").click(function(){
        var r = confirm("This will invoke the servlet and begin operation.
                         Do you want to proceed? ");
        if (r == true) {
            $("#result").html('<h3>
                          Operation in progress...Please wait</h3>');
            $.ajax({
                    type : "GET",
                    url : '/system/utility/servletinvoker',

                    data : {
                        path1: $("#path1").html(), path2:$("#path2").html()
                    },
                    success : function(data) {

                        $("#result").html('<h3>
                                          '+data+'</h3>');
                    },
                    error : function(XMLHttpRequest, textStatus, errorThrown) {
                        $("#result").html('<h3>
                    Error calling utility servlet: '+errorThrown+'</h3>');
                    }
            });}});

})(Granite.$, document);
Notice the url of the servlet for ajax call, by this you can understand that the servlet is registered via path, Since most probably, you will be using this utility component in author instance, you can find out a path for the servlet to be registered in AEM.

Now, you are good to go with your simple component. In my next post, I'll create a servlet and show you how to invoke it through this component.