AEM CQ - Retrieve Groups and Users from CRX using RMI

Goal


Create a sample java RMI client to connect to CQ (AEM 56) and access the groups and users in CRX repository. For using JCR Remoting or DavEx check this post

Update: There are issue connecting to AEM 6 (Oak) using RMI

Download


Download the necessary jars (JCR, SL4J etc.) from Adobe repo - http://repo.adobe.com/nexus/content/groups/public/ and add them to classpath. You'll need the following jars

.m2\repository\javax\jcr\jcr\2.0\jcr-2.0.jar
.m2\repository\org\apache\jackrabbit\jackrabbit-jcr-rmi\2.6.2\jackrabbit-jcr-rmi-2.6.2.jar
.m2\repository\org\slf4j\slf4j-api\1.5.8\slf4j-api-1.5.8.jar
.m2\repository\org\slf4j\slf4j-simple\1.5.2\slf4j-simple-1.5.2.jar

Setup


1) Access the CQ configuration console http://localhost:4502/system/console/configMgr in a browser and find the bundle Apache Sling JCR Repository RMI Registrar

2) Click on Apache Sling JCR Repository RMI Registrar and change the port number from 1099 to 1199 to start RMI bundle

3) Run the following client

package apps;

import org.apache.jackrabbit.rmi.client.ClientNode;
import org.apache.jackrabbit.rmi.client.ClientRepositoryFactory;

import javax.jcr.NodeIterator;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;

public class AemGroupsUsers {
    public static void main(String[] args) throws Exception {
        String crxApplicationName = "virtual-crx";
        String repoUrl = "//localhost:1199/" + crxApplicationName;
        String workspace = "crx.default";
        String username = "admin";

        char[] password = "admin".toCharArray();

        ClientRepositoryFactory factory = new ClientRepositoryFactory();
        Repository repository = factory.getRepository(repoUrl);

        Session session = repository.login(new SimpleCredentials(username, password), workspace);
        QueryManager qm = session.getWorkspace().getQueryManager();

        String stmt = "//element(*,rep:Group) order by @rep:principalName";
        Query q = qm.createQuery(stmt, Query.XPATH);

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

        System.out.println("Groups Query : " + stmt);

        while (results.hasNext()) {
            node = (ClientNode) results.next();
            System.out.print(node.getName() + ",");
        }

        stmt = "//element(*,rep:User) order by @rep:principalName";
        q = qm.createQuery(stmt, Query.XPATH);

        results = q.execute().getNodes();

        System.out.println("\n\nUsers Query : " + stmt);

        while (results.hasNext()) {
            node = (ClientNode) results.next();
            System.out.print(node.getName() + ",");
        }

        session.logout();
    }
}

No comments:

Post a Comment