Article summary

Summary
Discusses how to programmatically manipulate the Adobe Experience Manager JCR using the JCR API. This article manipulates the JCR by using an external Java console application.
Digital Marketing Solution(s) Adobe Experience Manager (Adobe CQ)
Audience
Developer (intermediate)
Required Skills
Java
Tested On Adobe Experience Manager 5.5, 5.6, 6.x

Introduction

You can programmatically modify nodes and properties located within the Adobe Experience Manager repository, which is part of the Adobe Marketing Cloud. To access the Experience Manager JCR repository, you use the Java Content Repository (JCR) API. You can use the Java JCR API to perform create, replace, update, and delete (CRUD) operations on content located within the Experience Manager repository. For more information about the Java JCR API, see JCR API.

This development article creates a Java class that modifies nodes and properties within the Experience Manager JCR. The Java class connects to a local Experience Manager instance  and creates nodes and stores data values to node properties. You store data by manipulating node properties.

Note:

This development article modifies the Experience Manager JCR from an external Java application. In contrast, you can modify the JCR from within an OSGi bundle using the JCR API. For details, see Persisting CQ data in the Java Content Repository.

Note:

To learn how to query the JCR by using the JCR Query API, see Querying Adobe Experience Manager Data using the JCR API.

Add the JCR JAR file

To use the JCR API, add the version of the jackrabbit-standalone JAR file that works with your instance of Adobe Experience Manager. For example, you are using AEM 5.5, you can use jackrabbit-standalone-2.4.3.jar. If you are using AEM 5.5, and you use a newer jackrabbit-standalone JAR file, such as jackrabbit-standalone-2.6.5.jar, then an exception is thrown. You can obtain the jackrabbit-standalone JAR file from http://jackrabbit.apache.org/jcr/jcr-api.html.

Create a Repository instance

Although there are different ways to connect to a repository and establish a connection, this development article uses a static method that belongs to the org.apache.jackrabbit.commons.JcrUtils class. The name of the method is getRepository. This method takes a string parameter that represents the URL of the Experience Manager server. For example http://localhost:4502/crx/server.

The getRepository method returns a Repository instance, as shown in the following code example.

//Create a connection to AEM 
Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server");

 

Note:

Calling jcrUtils.getRepository() is used from an external Java application. If you are using the JCR API from within an OSGi bundle, do not use this method. For information about using the JCR API from within an OSGi bundle, see Querying Adobe Experience Manager Data using the JCR API.

 

Create a Session instance

The Repository instance represents the CRX repository. You use the Repository instance to establish a session with the repository. To create a session, invoke the Repository instance’s login method and pass a javax.jcr.SimpleCredentials object. The login method returns a javax.jcr.Session instance.

You create a SimpleCredentials object by using its constructor and passing the following string values:

  • The user name 
  • The corresponding password

When passing the second parameter, call the String object’s toCharArray method. The following code shows how to call the login method that returns a javax.jcr.Session instance.

//Create a Session instance
javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray()));


Create a Node instance

Use a Session instance to create a javax.jcr.Node instance. A Node instance lets you perform node operations. For example, you can create a new node. To create a node that represents the root node, invoke the Session instance's getRootNode method, as shown in the following line of code.

//Create a Node
Node root = session.getRootNode();

Once you create a Node instance, you can perform tasks such as creating another node and adding a value to it. For example, the following code creates two nodes and adds a value to the second node.

// Store content 
Node day = adobe.addNode("adobe");
day.setProperty("message", "Adobe Experience Manager is part of the Adobe Digital Marketing Suite!");

Retrieve Node Values

To retrieve a node and its value, invoke the Node instance’s getNode method and pass a string value that represents the fully-qualified path to the node. Consider the node structure created in the previous code example. To retrieve the node, specify adobe/cq, as shown in the following code:

// Retrieve content 
Node node = root.getNode("adobe/cq"); 
System.out.println(node.getPath()); 
System.out.println(node.getProperty("message").getString());

Retrieve an Array Node Value

Lets assume that a node has a property that is a String array, as shown in the following illustration.

StringArr3

You can read the property named testprop that is a String array by using the following Java logic.

// Retrieve content 
  Node node = root.getNode("adobe/cq"); 
  
  Property references = node.getProperty("testprop");  
  Value[] values = references.getValues();
  String myVal = values[0].getString();     

Create nodes in the Experience Manager Repository

The following Java code example represents a Java class that connects to the Experiene Manager server, creates a Session instance, and adds new nodes. A node is assigned a data value and then the value of the node and its path is written out to the console.

/*
 * This Java Quick Start uses the jackrabbit-standalone-2.4.0.jar
 * file. See the previous section for the location of this JAR file
 */

import javax.jcr.Repository; 
import javax.jcr.Session; 
import javax.jcr.SimpleCredentials; 
import javax.jcr.Node; 

import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.core.TransientRepository; 

  
public class GetRepository { 

  
public static void main(String[] args) throws Exception { 

try { 

    //Create a connection to the CQ repository running on local host 
    Repository repository = JcrUtils.getRepository("http://localhost:4503/crx/server");
  
   //Create a Session
   javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray())); 

  //Create a node that represents the root node
  Node root = session.getRootNode(); 

  // Store content 
  Node adobe = root.addNode("adobe"); 
  Node day = adobe.addNode("cq"); 
  day.setProperty("message", "Adobe Experience Manager is part of the Adobe Digital Marketing Suite!"); 
   

  // Retrieve content 
  Node node = root.getNode("adobe/cq"); 
  System.out.println(node.getPath()); 
  System.out.println(node.getProperty("message").getString()); 

  // Save the session changes and log out
  session.save(); 
  session.logout();
  }
 catch(Exception e){
  e.printStackTrace();
  }
 } 
} 

View the new nodes in CRXDE Lite

After you run the full code example and create the nodes, you can view the new nodes in the CRXDE Lite, as shown in the following illustration.

View

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy