Read/Write content node(s) or files from/into the CRX repository

Almost all of the content (either as specific types or unstructured) is stored in the CRX repository, as part of the ADEP platform. You may also want to add your custom content into the repository as part of a larger process or workflow, and then retrieve the same (maybe at a later stage).

While you could do this using classic JCR APIs, the Adobe Digital Enterprise Platform provides convenient APIs (via the delivered SDKs) to do so.

Below is a sample CRXFileManager that performs the following using these APIs:

  • Read nodes/files from CRX
  • Write nodes/files into CRX
  • Read a list of nodes/files from CRX, given a folder node

Here’s how the CRXFileManager looks like:


import java.io.BufferedInputStream;
import java.io.InputStream;


import java.util.ArrayList;
import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;


import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.ResourceResolver;


import org.springframework.beans.factory.annotation.Autowired;


import com.adobe.livecycle.content.File
import com.adobe.livecycle.content.repository.FileService;
import com.adobe.livecycle.content.sling.ResourceResolverHolder;


public class CRXFileManager {

@Autowired
private FileService fileService;

/**
* Write content (given as input stream) in repository and returns the path.
*/
public String createContentInCRXRepository(InputStream inputStream, String mimeType)
{
try
{
   File file = fileService.createFile(inputStream, mimeType, null, null);
   Session currentSession = getSession();
currentSession.save();
return file.getTempPath();
}
catch (Exception e)
{
// log or throw
}
}


/**
* Read content (given the absolute path) from the repository.
*/
public static byte[] retrieveContentFromCRXRepository(String absPath)
{
byte[] result = null;
try
{
Node ntFileNode = getSession().getNode(absPath);
Node ntResourceNode = ntFileNode.getNode("jcr:content");
InputStream is = ntResourceNode.getProperty("jcr:data").getBinary().getStream();
BufferedInputStream bin = new BufferedInputStream(is);
result = IOUtils.toByteArray(bin);
bin.close();
is.close();
}
catch (Exception ex)
{
// log or throw
}
return result;
}

/**
* Read list of nodes (given the absolute folder path) from the repository.
*/
public static List<Node> retrieveNodesFromCRXRepository(String absFolderPath)
{
List<Node> nodes = new ArrayList<Node>();
try
{
Node ntFolderNode = getSession().getNode(absFolderPath);
NodeIterator ni = ntFileNode.getNodes();
while (ni.hasNext()) {
nodes.add((Node) ni.next(););
}
}
catch (Exception ex)
{
// log or throw
}
return nodes;
}


private static Session getSession() throws RepositoryException {
ResourceResolver resolver = ResourceResolverHolder.getResourceResolver();
return resolver.adaptTo(Session.class);
}

}

Of course, you would need to have the ADEP SDK setup in your eclipse. Specifically, you would need the lc-content-api.jar library from the ADEP SDK (found under \etc\aep\sdks\riaservices\riacore\10.0.0.0\java).