AEM CQ - Retrieve Groups and Users from CRX using RMI


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 the necessary jars (JCR, SL4J etc.) from Adobe repo - and add them to classpath. You'll need the following jars



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);
            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);
            System.out.print(node.getName() + ",");


No comments:

Post a Comment