I am working on a project where we have: a publish instance (P1) for public facing content, a publish instance (P2) for authenticated users, and both instance reverse replicate content to the same author (A1). On P1's dispatcher, we allow anonymous user to submit request for information or/and demo from partners who are required to login and use a system with a set of services/features on P2. So, when anonymous users submit requests, we need to reverse replicate to A1 and then replicate to P2. That is because the request(s) are directed to specific users (partners) depending on the page they send the request from. Below is the code fragment that I did (and it worked):

 

Code Fragement 1

package com.myproject.eventlisteners;

import com.day.cq.replication.Agent;
import com.day.cq.replication.AgentFilter;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationOptions;
import com.day.cq.replication.Replicator;
import java.util.Set;
import javax.jcr.Session;
import org.osgi.service.event.Event; import org.osgi.service.event.EventHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Properties; import org.apache.felix.scr.annotations.Property; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import org.apache.sling.api.SlingConstants; import org.apache.sling.api.resource.LoginException; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceResolverFactory; import org.apache.sling.event.EventUtil; import org.apache.sling.event.jobs.JobProcessor; import org.apache.sling.event.jobs.JobUtil; import org.apache.sling.settings.SlingSettingsService; import org.osgi.framework.Constants; import org.osgi.service.event.EventConstants; @Component(label = "P1 to A1 to P2 Portal Replication Handler", immediate=true, metatype=false) @Service(EventHandler.class) @Properties({ @Property(name = Constants.SERVICE_DESCRIPTION, value = "Handles user replication events on author instance."), @Property(name = Constants.SERVICE_VENDOR, value = "My Company"), @Property(name = EventConstants.EVENT_TOPIC, value = { SlingConstants.TOPIC_RESOURCE_ADDED, SlingConstants.TOPIC_RESOURCE_CHANGED }) }) public class ReplicationEventListener implements EventHandler, JobProcessor
{ private static final Logger LOGGER = LoggerFactory.getLogger(ReplicationEventListener.class); @Reference private SlingSettingsService slingSettingsService; @Reference private Replicator replicator; @Reference private ResourceResolverFactory resourceResolverFactory; private String path;
private final ReplicationOptions opts = new ReplicationOptions();
private ResourceResolver resourceResolver; private Session session; @Override public void handleEvent(Event event) { if (EventUtil.isLocal(event)) { JobUtil.processJob(event, this); } } public boolean process(Event event) { boolean flag = false; // grab run mode Set<String> runModes = slingSettingsService.getRunModes(); if (runModes.contains("author")) { path = (String) event.getProperty(SlingConstants.PROPERTY_PATH); this.processReplicateFromA1ToP2(); } return flag; } private void processReplicateFromA1ToP2() { if (path.startsWith("{the-path-pattern-to-be-detected}")) { try { resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null); if (resourceResolver != null) { session = resourceResolver.adaptTo(Session.class); Resource resource = resourceResolver.getResource(path); if (resource != null) { activatePage(); } } } catch (LoginException ex) { LOGGER.error(ex.getMessage()); } } }

private void activatePage()
{
if (!(path == null || session == null || replicator == null) && path.length() > 0)
{
try { opts.setFilter(new AgentFilter() { @Override public boolean isIncluded(Agent agent) { return agent.getId().startsWith("{the-replication-agent-name}"); } }); replicator.checkPermission(session, ReplicationActionType.ACTIVATE, path); replicator.replicate(session, ReplicationActionType.ACTIVATE, path, opts); } catch (ReplicationException ex) { LOGGER.error(ex.getMessage()); }
}
} }