AEM 60 - Replicate Assets from 60 Author Instance to 61 Author Instance

Goal


Replicate Assets from 60 author to 61 author using workflows (ideally any author-author)

Demo | Package Install (Includes Replication Agent and Workflow)


Solution


1) Create a Replication Agent on 60 author connecting to 61 author. Assuming the source author 60 is running on http://localhost:4502/ and destination author 61 running on http://localhost:5502/....

           a. Access http://localhost:4502/miscadmin#/etc/replication/agents.author

           b. Click New, select Replication Agent

                         Title: Replicate to 61 Author
                         Name: 61_author (this name is used in AgentFilter of ReplicationStep, added in next steps)



         
c. Agent as xml - /etc/replication/agents.author/61_author

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="cq:Page">
    <jcr:content
        cq:lastModified="{Date}2015-08-13T11:51:39.314-05:00"
        cq:lastModifiedBy="admin"
        cq:template="/libs/cq/replication/templates/agent"
        jcr:lastModified="{Date}2015-08-13T11:51:39.286-05:00"
        jcr:lastModifiedBy="admin"
        jcr:primaryType="nt:unstructured"
        jcr:title="Replicate to 61 Author"
        sling:resourceType="cq/replication/components/agent"
        enabled="true"
        protocolHTTPMethod="POST"
        serializationType="durbo"
        ssl="default"
        transportPassword="\{2878481d659c461d12b8db7a89ee0c422749e5f2958a757d00a79819da0f0187}"
        transportUri="http://localhost:5502/bin/receive?sling:authRequestLogin=1"
        transportUser="admin"
        triggerSpecific="true"/>
</jcr:root>

2) Create a Process Step apps.experienceaem.replication.ReplicationStep used for replicating the asset, with following code

package apps.experienceaem.replication;

import com.day.cq.dam.api.Asset;
import com.day.cq.dam.commons.process.AbstractAssetWorkflowProcess;
import com.day.cq.replication.*;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.metadata.MetaDataMap;
import org.apache.felix.scr.annotations.*;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Session;
import java.util.ArrayList;
import java.util.List;

@Component
@Service
@Properties({@Property(name = "process.label", value = "Replicate Assets to 61 Author")})
public class ReplicationStep extends AbstractAssetWorkflowProcess {
    private static final Logger log = LoggerFactory.getLogger(ReplicationStep.class);

    // the agent used for replication
    private static final String REP_AGENT_61 = "61_author";

    @Reference
    Replicator replicator;

    @Reference
    private AgentManager agentMgr = null;

    @Reference
    ResourceResolverFactory resourceResolverFactory;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args)
            throws WorkflowException {
        try{
            if(!agentExists()){
                log.info("NO Replication agent " + REP_AGENT_61 + ", skipping replicating to 61");
                return;
            }

            Session session = workflowSession.getSession();
            Asset asset = getAssetFromPayload(workItem, session);

            List<String> paths = new ArrayList<String>();

            //add the file path to replication queue
            paths.add(asset.getPath());

            doReplicate(paths, session);
        }catch(Exception e){
            throw new WorkflowException("Error replicating to 61 Author", e);
        }
    }

    private boolean agentExists(){
        return agentMgr.getAgents().containsKey(REP_AGENT_61);
    }

    private void doReplicate(List<String> paths, Session session) throws Exception{
        ReplicationOptions opts = new ReplicationOptions();

        //use the 61 replication agent
        opts.setFilter(new AgentFilter() {
            public boolean isIncluded(com.day.cq.replication.Agent agent) {
                return agent.getId().equalsIgnoreCase(REP_AGENT_61);
            }
        });

        for(String path : paths){
            replicator.replicate(session, ReplicationActionType.ACTIVATE, path, opts);
        }
    }

    public ResourceResolver getResourceResolver(final Session session) {
        ResourceResolver resourceResolver = null;

        try {
            resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
        } catch (LoginException e) {
            log.error("Error obtaining the resourceresolver",e);
        }

        return resourceResolver;
    }
}


3)  Create a workflow Replicate to 61 Author - /etc/workflow/models/replicate-to-61-author




4) Select an asset and start workflow Replicate to 61 Author



No comments:

Post a Comment