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)

Below code shows how can connect to repository from 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”);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s