Tuesday, October 22, 2013

Remote access of JCR repository in CQ5 | AEM

Follow the below steps to access the CQ5 JCR repository (CRX) remotely.

  1. Construct the remote URL of your CQ instance.
    1. e.g. http://localhost:4502/crx/server
  2. Where localhost  is the host where CQ is running & 4502 is port where CQ is listening to the requests.
  3. Get the repository by using the JCRUtils class which is available in the jackrabbit-jcr-commons.jar library.
    1. repository = JcrUtils.getRepository("http://localhost:4502/crx/server");
  4. Create a SimpleCredential object by passing username, password entries.
    1. SimpleCredentials creds = new SimpleCredentials("admin", "admin".toCharArray());
  5. Invoke the login method of repository object by passing the credential object & the workspace name.
    1. Session session = repository.login(creds, "crx.default");

A code snippet for the above can be found below
public class RemoteAccess {
public static void main(String x[]){
String remoteURL = "http://localhost:4502/crx/server";
Repository repository;
Session session = null;
try {
repository = JcrUtils.getRepository(remoteURL);
if( null != repository ){
SimpleCredentials creds = new SimpleCredentials("admin", "admin".toCharArray());
session = repository.login(creds, "crx.default");
if(session.isLive()){
System.out.println("You are connected to the Remote CQ instance");
session.logout();
}
}
} catch (RepositoryException e) {
e.printStackTrace();
}
}
}

No comments:

Post a Comment