FragmentPreprocessor.java 6.45 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.AgentConfig
 *  com.day.cq.replication.Preprocessor
 *  com.day.cq.replication.ReplicationAction
 *  com.day.cq.replication.ReplicationActionType
 *  com.day.cq.replication.ReplicationException
 *  com.day.cq.replication.ReplicationOptions
 *  com.day.cq.replication.Replicator
 *  javax.jcr.Credentials
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.SimpleCredentials
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.ServiceReference
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dam.cfm.impl.replication;

import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.Preprocessor;
import com.day.cq.replication.ReplicationAction;
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 javax.jcr.Credentials;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import org.apache.felix.scr.annotations.Activate;
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.jcr.api.SlingRepository;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service(value={Preprocessor.class})
@Component
public class FragmentPreprocessor
implements Preprocessor {
    public static final String REPLICATION_SERVICE_USER = "replicationService";
    private final Logger log;
    private static final String PROP_CF = "jcr:content/contentFragment";
    private static final String NN_SUB_ASSETS = "subassets";
    @Reference
    private SlingRepository repository;
    private BundleContext bundleContext;

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

    private Node getFragment(Session session, String path) {
        Node fragment = null;
        try {
            Property type;
            Node replicationNode = session.getNode(path);
            if (replicationNode.isNodeType("dam:Asset") && replicationNode.hasProperty("jcr:content/contentFragment") && (type = replicationNode.getProperty("jcr:content/contentFragment")).getBoolean()) {
                fragment = replicationNode;
            }
        }
        catch (RepositoryException re) {
            // empty catch block
        }
        return fragment;
    }

    private Node getSubAssets(Node fragment) {
        Node subAssets = null;
        try {
            if (fragment.hasNode("subassets")) {
                subAssets = fragment.getNode("subassets");
            }
        }
        catch (RepositoryException re) {
            // empty catch block
        }
        return subAssets;
    }

    @Activate
    protected void activate(ComponentContext componentContext) {
        this.bundleContext = componentContext.getBundleContext();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void preprocess(ReplicationAction action, ReplicationOptions options) throws ReplicationException {
        block15 : {
            ReplicationActionType type = action.getType();
            if (type != ReplicationActionType.ACTIVATE) {
                return;
            }
            Session systemSession = null;
            Session session = null;
            try {
                String agentUserId;
                Node subAssets;
                Node fragment;
                session = systemSession = this.repository.loginService("replicationService", null);
                AgentConfig config = action.getConfig();
                if (config != null && (agentUserId = config.getAgentUserId()) != null && agentUserId.length() > 0) {
                    session = this.repository.impersonateFromService("replicationService", (Credentials)new SimpleCredentials(agentUserId, new char[0]), null);
                }
                Node node = subAssets = (fragment = this.getFragment(session, action.getPath())) != null ? this.getSubAssets(fragment) : null;
                if (subAssets == null) break block15;
                Replicator replicator = null;
                ServiceReference serviceRef = this.bundleContext.getServiceReference(Replicator.class.getName());
                if (serviceRef != null) {
                    replicator = (Replicator)this.bundleContext.getService(serviceRef);
                }
                if (replicator != null) {
                    try {
                        NodeIterator childIterator = subAssets.getNodes();
                        while (childIterator.hasNext()) {
                            Node child = childIterator.nextNode();
                            replicator.replicate(session, type, child.getPath(), options);
                        }
                        break block15;
                    }
                    finally {
                        this.bundleContext.ungetService(serviceRef);
                    }
                }
                this.log.warn("Replicator could not be acquired; sub assets are not replicated.");
            }
            catch (RepositoryException e) {
                throw new ReplicationException((Exception)e);
            }
            finally {
                if (session != null && session.isLive() && session != systemSession) {
                    session.logout();
                }
                if (systemSession != null && systemSession.isLive()) {
                    systemSession.logout();
                }
            }
        }
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }
}