AEM 6 SP2 - Accessing CRX Remotely using Jcr Remoting Based On Webdav (DavEx)

Goal


To access CRX remotely in a client java program, JCR Remoting (DavEx) or RMI can be used. This post is on using JCR Remoting (aka WebDav remoting, DavEx). For RMI check this post

More documentation:

http://wiki.apache.org/jackrabbit/RemoteAccess

https://docs.adobe.com/docs/en/crx/2-3/developing/accessing_the_crx.html

Solution


1) If maven is used, following are the dependencies

 <dependencies>
      <dependency>
            <groupId>javax.jcr</groupId>
            <artifactId>jcr</artifactId>
            <version>2.0</version>
      </dependency>  
      <dependency>
            <groupId>org.apache.jackrabbit</groupId>
            <artifactId>jackrabbit-jcr2dav</artifactId>
            <version>2.4.0</version>
      </dependency> 
      <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
      </dependency>
 </dependencies>

2) Without maven, to connect to CRX in a plain java standalone program, the following jars should be added to classpath. These jars can be downloaded from Adobe repo - Nexus

Assuming jars are available in user's .m2 folder, the following paths should be added to classpath

                      C:\Users\nalabotu\.m2\repository\org\apache\jackrabbit\jackrabbit-jcr-commons\2.7.0\jackrabbit-jcr-commons-2.7.0.jar
                      C:\Users\nalabotu\.m2\repository\javax\jcr\jcr\2.0\jcr-2.0.jar
                      C:\Users\nalabotu\.m2\repository\org\apache\jackrabbit\jackrabbit-jcr2dav\2.4.0\jackrabbit-jcr2dav-2.4.0.jar
                      C:\Users\nalabotu\.m2\repository\org\apache\jackrabbit\jackrabbit-jcr2spi\2.4.0\jackrabbit-jcr2spi-2.4.0.jar
                      C:\Users\nalabotu\.m2\repository\org\apache\jackrabbit\jackrabbit-spi\2.4.0\jackrabbit-spi-2.4.0.jar
                      C:\Users\nalabotu\.m2\repository\org\apache\jackrabbit\jackrabbit-spi2dav\2.4.0\jackrabbit-spi2dav-2.4.0.jar
                      C:\Users\nalabotu\.m2\repository\org\slf4j\slf4j-api\1.7.5\slf4j-api-1.7.5.jar
                      C:\Users\nalabotu\.m2\repository\org\apache\jackrabbit\jackrabbit-spi-commons\2.4.0\jackrabbit-spi-commons-2.4.0.jar
                      C:\Users\nalabotu\.m2\repository\org\apache\jackrabbit\jackrabbit-webdav\2.4.0\jackrabbit-webdav-2.4.0.jar
                      C:\Users\nalabotu\.m2\repository\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar
                      C:\Users\nalabotu\.m2\repository\commons-codec\commons-codec\1.6\commons-codec-1.6.jar
                      C:\Users\nalabotu\.m2\repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar
                      C:\Users\nalabotu\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar

3) Sample standalone program for connecting to CRX using DavEx and execute query, returning templates (type cq:Template)

package apps;

import org.apache.jackrabbit.commons.JcrUtils;

import javax.jcr.*;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;

public class DavExWebDavRemotingTest {
    public static void main(String[] args) throws Exception{
        String REPO = "http://localhost:4502/crx/server";
        String WORKSPACE = "crx.default";

        Repository repository = JcrUtils.getRepository(REPO);

        Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()), WORKSPACE);
        QueryManager qm = session.getWorkspace().getQueryManager();

        String stmt = "select * from cq:Template";
        Query q = qm.createQuery(stmt, Query.SQL);

        NodeIterator results = q.execute().getNodes();
        Node node = null;

        while(results.hasNext()){
            node = (Node)results.next();
            System.out.println(node.getPath());
        }

        session.logout();
    }
}

4) Sample script to generate random folders in AEM

public class CreateFoldersInAEM {
    private static String PARENT_PATH = "/content/dam/sandbox/experience-aem";

    private static int DEPTH = 2;

    public static void main(String[] args) throws Exception{
        String REPO = "http://localhost:4502/crx/server";
        String WORKSPACE = "crx.default";

        Repository repository = JcrUtils.getRepository(REPO);

        Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()), WORKSPACE);

        Node node = session.getNode(PARENT_PATH + "/");

        addNumericFoldersTwoLevelDeep(node, (DEPTH - 1));

        session.save();

        session.logout();
    }

    private static void addNumericFoldersTwoLevelDeep(Node node, int depth) throws Exception{
        if(depth < 0){
            return;
        }

        int maximum = 99999, minimum = 10000, count = 10;

        List<Integer> range = IntStream.range(minimum, maximum).boxed()
                                    .collect(Collectors.toCollection(ArrayList::new));

        Collections.shuffle(range);

        range.subList(0, count).forEach((randomNum) -> {
            try{
                Node subFolder = node.addNode(String.valueOf(randomNum), "sling:Folder");

                addNumericFoldersTwoLevelDeep(subFolder, (depth - 1));

                addAlphaNumericFoldersTwoLevelDeep(subFolder, (depth - 1));
            }catch(Exception e){
                throw new RuntimeException(e);
            }
        });
    }

    private static void addAlphaNumericFoldersTwoLevelDeep(Node node, int depth) throws Exception{
        if(depth < 0){
            return;
        }

        int count = 10;

        while(count -- > 0){
            Node subFolder = node.addNode(UUID.randomUUID().toString(), "sling:Folder");

            addAlphaNumericFoldersTwoLevelDeep(subFolder, (depth - 1));
        }
    }
}


No comments:

Post a Comment