Skip to content

Instantly share code, notes, and snippets.

@aanchalsikka
Last active February 12, 2022 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aanchalsikka/9f6ab7f60082d4af1299e1772f67069b to your computer and use it in GitHub Desktop.
Save aanchalsikka/9f6ab7f60082d4af1299e1772f67069b to your computer and use it in GitHub Desktop.
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment