S7DeliveryContentBuilder.java 7.96 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.AgentConfig
 *  com.day.cq.replication.ContentBuilder
 *  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.scene7.is.catalog.service.publish.atomic.BatchStreamingPublishingService
 *  com.scene7.is.catalog.service.publish.atomic.BatchStreamingPublishingService$
 *  com.scene7.is.catalog.service.publish.atomic.PublishingService
 *  com.scene7.is.util.FileUtil
 *  com.scene7.is.util.IOUtil
 *  com.scene7.is.util.callbacks.Functions
 *  com.scene7.is.util.callbacks.Prox1
 *  com.scene7.is.util.collections.CollectionUtil
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  scala.Function1
 */
package com.adobe.cq.dam.aod.replication;

import com.adobe.cq.dam.aod.replication.helpers.PublishingBuilder;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.ContentBuilder;
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.scene7.is.catalog.service.publish.atomic.BatchStreamingPublishingService;
import com.scene7.is.catalog.service.publish.atomic.PublishingService;
import com.scene7.is.util.FileUtil;
import com.scene7.is.util.IOUtil;
import com.scene7.is.util.callbacks.Functions;
import com.scene7.is.util.callbacks.Prox1;
import com.scene7.is.util.collections.CollectionUtil;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.Function1;

@Component
@Service
public final class S7DeliveryContentBuilder
implements ContentBuilder {
    private static final Logger LOGGER = LoggerFactory.getLogger(S7DeliveryContentBuilder.class);
    @Reference
    private final ResourceResolverFactory resolverFactory;
    @Property(name="name", propertyPrivate=1)
    private static final String NAME = "s7delivery";

    public S7DeliveryContentBuilder() {
        this(null);
    }

    S7DeliveryContentBuilder(ResourceResolverFactory resolverFactory) {
        this.resolverFactory = resolverFactory;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public ReplicationContent create(Session session, ReplicationAction action, ReplicationContentFactory contentFactory) throws ReplicationException {
        ReplicationContent replicationContent;
        if (action == null || contentFactory == null) {
            throw new IllegalStateException("session: " + (Object)session + " action: " + (Object)action + " contentFactory: " + (Object)contentFactory);
        }
        S7DeliveryContentBuilder.info(action, "Building content");
        AgentConfig config = action.getConfig();
        File file = File.createTempFile("replicationContent", "dat");
        try {
            long startTime = System.currentTimeMillis();
            DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
            try {
                ResourceResolver resolver = this.getResolver(config);
                try {
                    S7DeliveryContentBuilder.buildContent(out, PublishingBuilder.publish(action, resolver));
                }
                finally {
                    resolver.close();
                }
            }
            finally {
                IOUtil.closeQuietly((DataOutputStream)out);
            }
            ReplicationContent rv = contentFactory.create("application/octet-stream", file, true);
            S7DeliveryContentBuilder.logSuccess(action, startTime);
            replicationContent = rv;
        }
        catch (Throwable var12_14) {
            try {
                FileUtil.deleteQuietly((File)file);
                throw var12_14;
            }
            catch (Error | RuntimeException e) {
                S7DeliveryContentBuilder.logError(action, e);
                throw e;
            }
            catch (Exception e) {
                S7DeliveryContentBuilder.logError(action, e);
                throw new ReplicationException(e.getMessage(), e);
            }
        }
        FileUtil.deleteQuietly((File)file);
        return replicationContent;
    }

    public ReplicationContent create(Session session, ReplicationAction action, ReplicationContentFactory contentFactory, Map<String, Object> parameters) throws ReplicationException {
        return this.create(session, action, contentFactory);
    }

    public String getName() {
        return "s7delivery";
    }

    public String getTitle() {
        return "S7 Delivery";
    }

    private ResourceResolver getResolver(AgentConfig config) throws ReplicationException {
        try {
            String userId = config.getAgentUserId();
            Map map = CollectionUtil.map();
            map.put("sling.service.subservice", "dmreplicateonmodify");
            map.put("user.impersonation", userId);
            return this.resolverFactory.getServiceResourceResolver(map);
        }
        catch (LoginException e) {
            throw new ReplicationException(e.getMessage(), (Exception)e);
        }
    }

    private static void buildContent(DataOutput out, Prox1<PublishingService, RepositoryException> code) {
        BatchStreamingPublishingService..MODULE$.send(out, Functions.scalaFunc(code));
    }

    private static void info(ReplicationAction action, String message) {
        String msg = String.format("%s for %s", message, S7DeliveryContentBuilder.toString(action));
        action.getLog().info(msg);
        LOGGER.info(msg);
    }

    private static void logSuccess(ReplicationAction action, long startTime) {
        long elapsedTime = System.currentTimeMillis() - startTime;
        String message = String.format("Building content succeeded in %d ms for %s", elapsedTime, S7DeliveryContentBuilder.toString(action));
        action.getLog().info(message);
        LOGGER.info(message);
    }

    private static void logError(ReplicationAction action, Throwable e) {
        String message = String.format("Content building failed for %s. %s: '%s'", S7DeliveryContentBuilder.toString(action), e.getClass().getName(), e.getMessage());
        action.getLog().error(message);
        LOGGER.error(message, e);
    }

    private static String toString(ReplicationAction action) {
        return (Object)action.getType() + ": " + CollectionUtil.mkString((Object[])action.getPaths(), (String)"[", (String)", ", (String)"]");
    }

    protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resolverFactory = resourceResolverFactory;
    }

    protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resolverFactory == resourceResolverFactory) {
            this.resolverFactory = null;
        }
    }
}