Expand my Community achievements bar.

SOLVED

How to set json resource for Asset in workflow?

Avatar

Level 4

how to get  resourceResolver.getResource(payload) json resource while calling this method in junit? 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Sharing the code to read a json resource

 

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 ReadJsonWorkflowProcess implements WorkflowProcess {

    private static final Logger LOG = LoggerFactory.getLogger(ReadJsonWorkflowProcess.class);

    @reference
    private ResourceResolverFactory resolverFactory;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) {
        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 Json from jcr:data property
                        InputStream jsonContent = fileNode.getProperty(JcrConstants.JCR_DATA).getBinary().getStream();
                        String jsonString = readJson(jsonContent);

                    }
                }
            }
        } 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 readJson(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();
    }
}

Aanchal Sikka

View solution in original post

3 Replies

Avatar

Community Advisor

@karthick1356 

You could probably do that using the AemContext object.

...
private AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);

...
/* Getting Resolver from current AemContext */ ResourceResolver resourceResolver = context.request().getResourceResolver();
resourceResolver.getResource(payload)

 

Avatar

Correct answer by
Community Advisor

Sharing the code to read a json resource

 

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 ReadJsonWorkflowProcess implements WorkflowProcess {

    private static final Logger LOG = LoggerFactory.getLogger(ReadJsonWorkflowProcess.class);

    @reference
    private ResourceResolverFactory resolverFactory;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) {
        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 Json from jcr:data property
                        InputStream jsonContent = fileNode.getProperty(JcrConstants.JCR_DATA).getBinary().getStream();
                        String jsonString = readJson(jsonContent);

                    }
                }
            }
        } 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 readJson(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();
    }
}

Aanchal Sikka