PublisherImpl.java 7.54 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.ReplicationActionType
 *  com.day.cq.replication.ReplicationException
 *  com.day.cq.replication.ReplicationOptions
 *  com.day.cq.replication.ReplicationStatus
 *  com.day.cq.replication.Replicator
 *  com.day.cq.wcm.api.reference.Reference
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.security.AccessControlManager
 *  javax.jcr.security.Privilege
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mcm.campaign.impl;

import com.adobe.cq.mcm.campaign.NewsletterException;
import com.adobe.cq.mcm.campaign.NewsletterReplicationException;
import com.adobe.cq.mcm.campaign.Publisher;
import com.adobe.cq.mcm.campaign.impl.ReferenceCollector;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationOptions;
import com.day.cq.replication.ReplicationStatus;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.api.reference.Reference;
import java.util.Calendar;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.security.AccessControlManager;
import javax.jcr.security.Privilege;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Component(metatype=0)
public class PublisherImpl
implements Publisher {
    private final Logger log;
    @org.apache.felix.scr.annotations.Reference
    private ReferenceCollector referenceAggregator;
    @org.apache.felix.scr.annotations.Reference
    protected Replicator replicator;

    public PublisherImpl() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    private void replicateRecursively(Resource toPublish, Session session, ReplicationOptions options) throws NewsletterException {
        this.log.debug("Replicating '{}'", (Object)toPublish.getPath());
        if ("cq:Page".equals(toPublish.getResourceType())) {
            this.replicatePage(toPublish, session, options, -1);
            Iterable children = toPublish.getChildren();
            for (Resource child : children) {
                this.replicateRecursively(child, session, options);
            }
        }
    }

    private void replicatePage(Resource resource, Session session, ReplicationOptions options, long lastModified) throws NewsletterException {
        try {
            AccessControlManager acMgr = session.getAccessControlManager();
            Privilege[] privileges = new Privilege[]{acMgr.privilegeFromName("{http://www.day.com/crx/1.0}replicate")};
            if (!acMgr.hasPrivileges(resource.getPath(), privileges)) {
                throw new NewsletterReplicationException("Current session is not authorized to replicate " + resource.getPath(), null, NewsletterReplicationException.Type.NO_ACCESS, session.getUserID(), resource.getPath());
            }
            boolean requiresReplication = true;
            String reason = "unknown";
            ReplicationStatus replStatus = (ReplicationStatus)resource.adaptTo(ReplicationStatus.class);
            if (replStatus != null && (replStatus.isDelivered() || replStatus.isActivated())) {
                Calendar lastModifiedCal;
                if (lastModified < 0 && (lastModifiedCal = this.getLastModified(resource)) != null) {
                    lastModified = lastModifiedCal.getTimeInMillis();
                }
                if (lastModified >= 0) {
                    long lastPublished = replStatus.getLastPublished().getTimeInMillis();
                    boolean bl = requiresReplication = lastPublished < lastModified;
                    if (!requiresReplication) {
                        reason = "already replicated; timecodes: " + lastPublished + " (publish) " + lastModified + " (modified)";
                    }
                }
            }
            if (requiresReplication) {
                this.log.debug("Replicating '{}'", (Object)resource.getPath());
                this.replicator.replicate(session, ReplicationActionType.ACTIVATE, resource.getPath(), options);
            } else {
                this.log.debug("Replication of '{}' skipped; reason: {}", (Object[])new String[]{resource.getPath(), reason});
            }
        }
        catch (RepositoryException e) {
            throw new NewsletterReplicationException("Could not replicate newsletter", (Throwable)e, NewsletterReplicationException.Type.GENERIC);
        }
        catch (ReplicationException e) {
            throw new NewsletterReplicationException("Could not replicate newsletter", (Throwable)e, NewsletterReplicationException.Type.GENERIC);
        }
    }

    private Calendar getLastModified(Resource page) {
        Resource pageContent = page.getChild("jcr:content");
        ValueMap contentProps = (ValueMap)pageContent.adaptTo(ValueMap.class);
        Calendar lastModified = null;
        if (contentProps != null && (lastModified = (Calendar)contentProps.get("cq:lastModified", Calendar.class)) == null) {
            lastModified = (Calendar)contentProps.get("jcr:created", Calendar.class);
        }
        return lastModified;
    }

    @Override
    public void publish(Resource resource) throws NewsletterException {
        try {
            Session session = ((Node)resource.adaptTo(Node.class)).getSession();
            ReplicationOptions options = new ReplicationOptions();
            options.setSynchronous(true);
            List<Reference> references = this.referenceAggregator.getReferences(resource);
            Calendar lastPageModifCal = this.getLastModified(resource);
            long lastPageModif = lastPageModifCal != null ? lastPageModifCal.getTimeInMillis() : -1;
            references.add(new Reference(null, null, resource, lastPageModif));
            for (Reference reference : references) {
                String type = reference.getType();
                boolean isCampaignRef = "campaign".equals(type);
                resource = reference.getResource();
                if (resource == null) continue;
                if (!isCampaignRef) {
                    long lastModified = reference.getLastModified();
                    this.replicatePage(resource, session, options, lastModified);
                    continue;
                }
                this.replicateRecursively(resource, session, options);
            }
        }
        catch (RepositoryException e) {
            throw new NewsletterReplicationException("Could not replicate newsletter", (Throwable)e, NewsletterReplicationException.Type.GENERIC);
        }
    }

    protected void bindReferenceAggregator(ReferenceCollector referenceCollector) {
        this.referenceAggregator = referenceCollector;
    }

    protected void unbindReferenceAggregator(ReferenceCollector referenceCollector) {
        if (this.referenceAggregator == referenceCollector) {
            this.referenceAggregator = null;
        }
    }

    protected void bindReplicator(Replicator replicator) {
        this.replicator = replicator;
    }

    protected void unbindReplicator(Replicator replicator) {
        if (this.replicator == replicator) {
            this.replicator = null;
        }
    }
}