PushPublishCampaignTransportHandler.java 4.67 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.AgentConfig
 *  com.day.cq.replication.ReplicationAction
 *  com.day.cq.replication.ReplicationActionType
 *  com.day.cq.replication.ReplicationContent
 *  com.day.cq.replication.ReplicationContentFactory
 *  com.day.cq.replication.ReplicationException
 *  com.day.cq.replication.ReplicationLog
 *  com.day.cq.replication.ReplicationResult
 *  com.day.cq.replication.ReplicationTransaction
 *  com.day.cq.replication.TransportContext
 *  com.day.cq.replication.TransportHandler
 *  javax.jcr.RepositoryException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.testandtarget.impl;

import com.day.cq.analytics.testandtarget.TargetMediator;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationContent;
import com.day.cq.replication.ReplicationContentFactory;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationLog;
import com.day.cq.replication.ReplicationResult;
import com.day.cq.replication.ReplicationTransaction;
import com.day.cq.replication.TransportContext;
import com.day.cq.replication.TransportHandler;
import java.util.List;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
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.ResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={TransportHandler.class})
public class PushPublishCampaignTransportHandler
implements TransportHandler {
    @Reference
    private TargetMediator campaignMediator;
    private static final Logger LOG = LoggerFactory.getLogger(PushPublishCampaignTransportHandler.class);

    public boolean canHandle(AgentConfig config) {
        return config.getTransportURI().startsWith("tnt://");
    }

    public ReplicationResult deliver(TransportContext ctx, ReplicationTransaction tx) throws ReplicationException {
        try {
            return this.process(tx);
        }
        catch (RepositoryException e) {
            return this.logAndReturn(tx, (Throwable)e);
        }
        catch (LoginException e) {
            return this.logAndReturn(tx, (Throwable)e);
        }
        catch (TestandtargetException e) {
            return this.logAndReturn(tx, e);
        }
    }

    private ReplicationResult logAndReturn(ReplicationTransaction tx, Throwable t) {
        tx.getLog().error("Error replicating " + tx.getAction().getPath() + " - " + t.getClass().getName() + " : " + t.getMessage());
        return new ReplicationResult(false, 0, t.getMessage());
    }

    private ReplicationResult process(ReplicationTransaction tx) throws RepositoryException, LoginException, TestandtargetException, ReplicationException {
        String path = tx.getAction().getPath();
        ReplicationActionType actionType = tx.getAction().getType();
        LOG.debug("Received action {} for path {}", (Object)actionType, (Object)path);
        switch (actionType) {
            case ACTIVATE: {
                this.campaignMediator.syncPublishCampaign(path);
                return new ReplicationResult(true, 0, null);
            }
            case DEACTIVATE: {
                this.campaignMediator.deactivatePublishCampaign(path);
                return new ReplicationResult(true, 0, null);
            }
            case DELETE: {
                this.campaignMediator.deactivatePublishCampaign(ResourceUtil.getParent((String)path));
                return new ReplicationResult(true, 0, null);
            }
        }
        throw new ReplicationException("Not supported");
    }

    public ReplicationResult poll(TransportContext ctx, ReplicationTransaction tx, List<ReplicationContent> result, ReplicationContentFactory factory) throws ReplicationException {
        throw new ReplicationException("Not supported");
    }

    protected void bindCampaignMediator(TargetMediator targetMediator) {
        this.campaignMediator = targetMediator;
    }

    protected void unbindCampaignMediator(TargetMediator targetMediator) {
        if (this.campaignMediator == targetMediator) {
            this.campaignMediator = null;
        }
    }

}