Expand my Community achievements bar.

Editing jcr:data/binary/rendition in AEM | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

Editing jcr:data/binary/rendition in AEM by Adobe Experience Manager Blog

Abstract

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);

@Reference
private ResourceResolverFactory resolverFactory;

@Override
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);
session.save();
contentValue.dispose();
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)) {
strBuilder.append(readLine);
}
} catch (IOException e) {
LOG.error("IOException while reading InputStream into String : {}", e.getMessage());
}
return strBuilder.toString();
}
}

Read Full Blog

Editing jcr:data/binary/rendition in AEM

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

0 Replies