Editing jcr:data/binary/rendition in AEM

Sharing a code snippet to update a Binary file stored in AEM. Here, we are updating a binary file stored as jcr:data in the original rendition.

package com.techrevel.dam.core.workflows;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.commons.jcr.JcrConstants;
@Component(service = WorkflowProcess.class, property = { "process.label=Update jcr:data binary" })
public class UpdateBinaryWorkflowProcess implements WorkflowProcess {
private static final Logger LOG = LoggerFactory.getLogger(UpdateBinaryWorkflowProcess.class);
private ResourceResolverFactory resolverFactory;
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
throws WorkflowException {
String path = getPath(workItem);
ResourceResolver resolver = null;
try {
resolver = getResolver(workflowSession);
if (null != resolver) {
Resource fileResource = resolver.getResource(path);
if (null != fileResource) {
Node fileNode = fileResource.adaptTo(Node.class);
Session session = resolver.adaptTo(Session.class);
if (null != session && null != fileNode && fileNode.hasProperty(JcrConstants.JCR_DATA)) {
// Fetching XML from jcr:data property
InputStream xmlContent = fileNode.getProperty(JcrConstants.JCR_DATA).getBinary().getStream();
String xmlString = readXml(xmlContent);
String updatedXml = xmlString.replaceAll("a", "b"); //Updated XML to be persisted
InputStream targetStream = new ByteArrayInputStream(updatedXml.getBytes());
Binary contentValue = session.getValueFactory().createBinary(targetStream);
fileNode.setProperty(JcrConstants.JCR_DATA, contentValue);
LOG.debug("Successfully updated original rendition");
} catch (
RepositoryException e) {
LOG.error("Exception while updating xml:lang attribute : {}", e.getMessage());
ResourceResolver getResolver(WorkflowSession workflowSession) {
ResourceResolver rr = workflowSession.adaptTo(ResourceResolver.class);
return rr;
String getPath(WorkItem workItem) {
String path = workItem.getWorkflowData().getPayload().toString() + "/" + JcrConstants.JCR_CONTENT;
return path;
public String readXml(InputStream content) {
String readLine;
BufferedReader br = new BufferedReader(new InputStreamReader(content));
StringBuilder strBuilder = new StringBuilder();
try {
while (((readLine = br.readLine()) != null)) {
} catch (IOException e) {
LOG.error("IOException while reading InputStream into String : {}", e.getMessage());
return strBuilder.toString();

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 )

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