Skip to content

Instantly share code, notes, and snippets.

@kuckmc01
Created January 19, 2017 15:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuckmc01/8fa10234dc3dbc55447adf1b621c2ed9 to your computer and use it in GitHub Desktop.
Save kuckmc01/8fa10234dc3dbc55447adf1b621c2ed9 to your computer and use it in GitHub Desktop.
Sample Replication Event Listener
package com.mk.core.services;
import com.adobe.granite.workflow.PayloadMap;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.wcm.api.NameConstants;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowService;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.model.WorkflowModel;
import org.apache.felix.scr.annotations.Component;
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.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.settings.SlingSettingsService;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import java.util.Collections;
import java.util.Set;
@Component
@Service
@Property(name="event.topics",value= ReplicationAction.EVENT_TOPIC)
public class PublishAuditEventListener implements EventHandler {
private Logger log = LoggerFactory.getLogger(this.getClass());
private BundleContext bundleContext;
private Set<String> runmodes = Collections.emptySet();
@Reference
private SlingSettingsService slingSettingsService;
@Reference
private ResourceResolverFactory rrf;
@Reference
private WorkflowService workflowService;
private ResourceResolver resourceResolver;
public static final String AUTHOR_RUN_MODE = "author";
public void handleEvent(Event event) {
if(runmodes.contains(AUTHOR_RUN_MODE) && resourceResolver != null){
log.trace("Replication Event triggered on author");
final ReplicationAction action = ReplicationAction.fromEvent(event);
if(action != null) {
final String userId = action.getUserId();
log.debug("Replication action {} at path {} ", action.getType().getName(), action.getPath());
final Resource resource = resourceResolver.resolve(action.getPath());
if(resource != null){
if(resource.getResourceType().equalsIgnoreCase(NameConstants.NT_PAGE)){
if(ReplicationActionType.ACTIVATE == action.getType()){
performWorkflow(
"/etc/workflow/models/scheduled_activation/jcr:content/model",
userId,
action.getPath());
}else if(ReplicationActionType.DEACTIVATE == action.getType()){
performWorkflow(
"/etc/workflow/models/scheduled_activation/jcr:content/model",
userId,
action.getPath());
}
}
}
}
}
}
private void performWorkflow(final String model, final String userId, final String path){
try {
log.debug("Attempting to configure workflow");
final Session adminSession = resourceResolver.adaptTo(Session.class);
final Session userSession = adminSession.impersonate (new SimpleCredentials(userId,new char[0]));
WorkflowSession wfSession = workflowService.getWorkflowSession(userSession);
WorkflowModel wfModel = wfSession.getModel(model);
if(wfModel != null){
WorkflowData wfData = wfSession.newWorkflowData(PayloadMap.TYPE_JCR_PATH, path);
log.debug("Starting workflow using model: {}",wfModel.getId());
wfSession.startWorkflow(wfModel, wfData);
}else{
log.warn("No workflow model found. Skipping transaction.");
}
} catch (WorkflowException ex) {
log.error("Error starting workflow.", ex);
} catch (javax.jcr.LoginException e) {
log.error("Error starting workflow.", e);
} catch (RepositoryException e) {
log.error("Error starting workflow.", e);
}
}
protected void activate(ComponentContext ctx) {
this.bundleContext = ctx.getBundleContext();
runmodes = slingSettingsService.getRunModes();
try {
//TODO: change to service resolver
resourceResolver = rrf.getAdministrativeResourceResolver(null);
} catch (LoginException e) {
e.printStackTrace();
}
}
protected void deactivate(ComponentContext ctx) {
this.bundleContext = null;
if(resourceResolver != null && resourceResolver.isLive()){
resourceResolver.close();
resourceResolver = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment