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) A 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();
    }
}

No comments:

Post a Comment