April 3, 2020
Estimated Post Reading Time ~

Ways to access JCR repository in CQ5

Well to be honest, there are multiple ways you can access CRX repository but all depends upon how you want to access the repository and from where do you want to access the repository.

Typical use cases are :

1) Access the repository within OSGIfied environment.

2) Access the repository using External applications hosted in Java/.Net or any other language which supports REST.

3) Access the repository using WebDAV Client/ RMI.

Example codes and explanation for each scenario is listed below:

1) In the first use case, i have described how you can fetch the data from repository using both Sling as well JCR API’s. However, the question arises when should one use JCR API over Sling and vice versa. The answer to be honest is worth pondering over but as far as i know the basic usage of Sling API’s is to fetch the data from the repository and use it for purposes like generating files pdf/xml for client needs but if you are committing the data from external systems into the repository JCR API’s should be used. The reason for this is that sling api lack a bit of flexibility when it comes to committing the data while JCR API adds lot of additional lines of code to maintain if the task is limited to consuming the data.

@Component
@Service
public class JCRRepositoryAccess {
 @Reference
 private ResourceResolverFactory resourceResolverFactory;
 private ResourceResolver resourceResolver;
 @Reference
 private SlingRepository slingRepository;
 private Session session;
 protected void exampleMethod() {
  try {
   session = slingRepository.loginAdministrative(null); // JCR API 
   resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null); // Sling API
  } catch (RepositoryException e) {
   // TODO Auto-generated catch block  
   e.printStackTrace();
  } catch (LoginException e) {
   // TODO Auto-generated catch block  
   e.printStackTrace();
  } finally {
   if (session != null || session.isLive()) {
    session.logout();
   }
  }
 }
}

2) The below code shows how can connect to the repository from an external application using Java.
    String remoteURLLocation = "http://localhost:4502/crx/server";
  Repository repository;
  Session session = null;
  try {
   repository = JcrUtils.getRepository(remoteURLLocation);
   if (null != repository) {
    SimpleCredentials credential = new SimpleCredentials("your_username", "your_password".toCharArray());
    session = repository.login(credential, "crx.default");
    // You are now connected to Repository. You can use JCR API for accessing the nodes and whatever else you want to do.   
   }
  } catch (RepositoryException e) {
   e.printStackTrace();
  }
If you want to access the Repository from a .Net client, you can go to this tutorial on Adobe Website
3) Access the repository using webdav client.
There is a nice tool NetDrive for windows to access the repository using webdav.
RMI is achieved as
Repository repository = JcrUtils.getRepository(“rmi://host:port/crx”);


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.