Scene7ConfigurationEventHandler.java 12.1 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.resource.PersistableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.event.jobs.JobManager
 *  org.apache.sling.event.jobs.Queue
 *  org.apache.sling.event.jobs.QueueConfiguration
 *  org.apache.sling.event.jobs.QueueConfiguration$Type
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.apache.sling.jcr.resource.JcrResourceResolverFactory
 *  org.apache.sling.jcr.resource.JcrResourceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.scene7.impl;

import com.day.cq.dam.scene7.api.S7Config;
import com.day.cq.dam.scene7.api.S7ConfigResolver;
import com.day.cq.dam.scene7.api.Scene7AssetMimetypeService;
import com.day.cq.dam.scene7.api.Scene7PresetsService;
import com.day.cq.dam.scene7.api.Scene7Service;
import com.day.cq.dam.scene7.impl.importer.ImportJobUtils;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.PersistableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.Queue;
import org.apache.sling.event.jobs.QueueConfiguration;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.apache.sling.jcr.resource.JcrResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Scene7ConfigurationEventHandler
implements Runnable {
    private static final Logger LOG = LoggerFactory.getLogger(Scene7ConfigurationEventHandler.class);
    private String path;
    private JcrResourceResolverFactory rrf;
    private S7ConfigResolver s7ConfigResolver;
    private Scene7Service scene7Service;
    private Scene7PresetsService scene7PresetService;
    private JobManager slingJobManager;
    private Scene7AssetMimetypeService scene7MimeTypeService;
    private SlingRepository slingRepository;

    public Scene7ConfigurationEventHandler(String path, JcrResourceResolverFactory rrf, S7ConfigResolver s7ConfigResolver, Scene7Service scene7Service, Scene7PresetsService scene7PresetService, JobManager slingJobManager, Scene7AssetMimetypeService scene7MimeTypeService, SlingRepository slingRepository) {
        this.path = path;
        this.rrf = rrf;
        this.s7ConfigResolver = s7ConfigResolver;
        this.scene7Service = scene7Service;
        this.scene7PresetService = scene7PresetService;
        this.slingJobManager = slingJobManager;
        this.scene7MimeTypeService = scene7MimeTypeService;
        this.slingRepository = slingRepository;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void run() {
        block12 : {
            ResourceResolver configResourceResolver = null;
            ResourceResolver assetResourceResolver = null;
            Session configSession = null;
            Session assetSession = null;
            try {
                String s7ConfigPath;
                PersistableValueMap properties;
                S7Config s7Config;
                configSession = this.slingRepository.loginService("scene7configservice", null);
                assetSession = this.slingRepository.loginService("scene7assetservice", null);
                configResourceResolver = this.rrf.getResourceResolver(configSession);
                assetResourceResolver = this.rrf.getResourceResolver(assetSession);
                Resource configResourceJcrContent = configResourceResolver.getResource(this.path);
                if (configResourceJcrContent == null || (properties = (PersistableValueMap)configResourceJcrContent.adaptTo(PersistableValueMap.class)) == null || properties.get((Object)"companyHandle") == null || (s7Config = this.s7ConfigResolver.getS7Config(configResourceResolver, s7ConfigPath = ResourceUtil.getParent((String)this.path))) == null) break block12;
                this.createTargetFolder(assetResourceResolver, s7ConfigPath, s7Config);
                this.cacheServersURLs(properties, s7ConfigPath, s7Config);
                String topicHash = Text.md5((String)s7Config.getTargetPath());
                boolean createConfig = this.generateImportQueueHandleIfNotExists(properties, s7ConfigPath, topicHash);
                try {
                    properties.save();
                    LOG.info("Detected Scene7 configuration changes. Caching settings.");
                }
                catch (PersistenceException e) {
                    LOG.error("Unable to persist configuration changes", (Throwable)e);
                }
                if (createConfig) {
                    this.createImportQueueConfig(configResourceResolver, s7Config, topicHash);
                }
                this.updateImportQueueState(configResourceResolver, s7Config);
                this.scene7PresetService.updateEncodingPresets(configResourceJcrContent);
                this.scene7PresetService.updateViewerPresets(configResourceJcrContent);
                this.scene7MimeTypeService.updateSupportedMimeTypes(configResourceJcrContent, configResourceResolver);
            }
            catch (RepositoryException e) {
                LOG.error("Unable to retrieve a resource resolver", (Throwable)e);
            }
            finally {
                if (configResourceResolver != null) {
                    configResourceResolver.close();
                }
                if (configSession != null) {
                    configSession.logout();
                }
                if (assetResourceResolver != null) {
                    assetResourceResolver.close();
                }
                if (assetSession != null) {
                    assetSession.logout();
                }
            }
        }
    }

    private void cacheServersURLs(PersistableValueMap properties, String s7ConfigPath, S7Config s7Config) {
        String publishServer = this.scene7Service.getPublishServer(s7Config);
        String flashTemplatesServer = this.scene7Service.getFlashTemplatesServer(s7Config);
        String fxgServer = this.scene7Service.getFxgServer(s7Config);
        if (publishServer != null && !publishServer.equals(properties.get("publishServer", String.class))) {
            properties.put((Object)"publishServer", (Object)publishServer);
            LOG.debug("Caching publish server URL {} for the Scene7 configuration from {}", (Object)publishServer, (Object)s7ConfigPath);
        }
        if (flashTemplatesServer != null && !flashTemplatesServer.equals(properties.get("flashTemplatesServer", String.class))) {
            properties.put((Object)"flashTemplatesServer", (Object)flashTemplatesServer);
            LOG.debug("Caching flash templates server URL {} for the Scene7 configuration from {}", (Object)flashTemplatesServer, (Object)s7ConfigPath);
        }
        if (fxgServer != null && !fxgServer.equals(properties.get("fxgServer", String.class))) {
            properties.put((Object)"fxgServer", (Object)fxgServer);
            LOG.debug("Caching FXG server URL {} for the Scene7 configuration from {}", (Object)fxgServer, (Object)s7ConfigPath);
        }
    }

    private boolean generateImportQueueHandleIfNotExists(PersistableValueMap properties, String s7ConfigPath, String topicHash) {
        String importQueueName = (String)properties.get("importQueueName", String.class);
        if (importQueueName == null) {
            importQueueName = "Scene7 Import Queue-" + ResourceUtil.getName((String)s7ConfigPath);
            properties.put((Object)"importQueueName", (Object)importQueueName);
            String importQueueTopic = "com/dam/cq/scene7/importer/" + topicHash;
            properties.put((Object)"importQueueTopic", (Object)importQueueTopic);
            LOG.debug("Generating new import queue {} for Scene7 configuration {}", (Object)importQueueName, (Object)s7ConfigPath);
            return true;
        }
        return false;
    }

    private void createTargetFolder(ResourceResolver rr, String s7ConfigPath, S7Config s7Config) {
        String targetPath = s7Config.getTargetPath();
        if (targetPath != null) {
            Session session = (Session)rr.adaptTo(Session.class);
            try {
                if (targetPath.endsWith("/")) {
                    targetPath = targetPath.substring(0, targetPath.length() - 1);
                }
                Node s7AssetsFolder = JcrResourceUtil.createPath((String)targetPath, (String)"sling:OrderedFolder", (String)"sling:OrderedFolder", (Session)session, (boolean)true);
                s7AssetsFolder.setProperty("dam:scene7CloudConfigPath", s7ConfigPath);
                session.save();
            }
            catch (RepositoryException e) {
                LOG.error("Unable to create folder " + targetPath + " for target path", (Throwable)e);
            }
        }
    }

    private void updateImportQueueState(ResourceResolver rr, S7Config s7Config) {
        if (s7Config != null) {
            Resource pollingImporterResource = rr.getResource(s7Config.getCloudConfigPath() + "/" + "jcr:content" + "/" + "pollConfig");
            if (pollingImporterResource != null) {
                ValueMap pollingImporterProperties = (ValueMap)pollingImporterResource.adaptTo(ValueMap.class);
                Boolean cancelImporter = (Boolean)pollingImporterProperties.get("cancelImporter", (Object)Boolean.FALSE);
                Boolean importerEnabled = (Boolean)pollingImporterProperties.get("enabled", (Object)Boolean.FALSE);
                Queue importQueue = ImportJobUtils.getImportJobQueue(s7Config, this.slingJobManager);
                if (importQueue != null) {
                    if (importerEnabled.booleanValue()) {
                        importQueue.resume();
                    } else if (cancelImporter.booleanValue()) {
                        LOG.info("Canceling running Scene7 import job for config {}", (Object)s7Config.getCloudConfigPath());
                        LOG.debug("Found the {} property set to true, suspending the S7 import queue!", (Object)"cancelImporter");
                        importQueue.suspend();
                    }
                }
            } else {
                LOG.warn("Could not find the polling importer configuration for S7 config {}", (Object)s7Config.getCloudConfigPath());
            }
        } else {
            LOG.warn("Null Scene7 Configuration!");
        }
    }

    private void createImportQueueConfig(ResourceResolver rr, S7Config s7Config, String topicHash) {
        String queueConfigName = "org.apache.sling.event.jobs.QueueConfiguration-" + topicHash;
        String importQueueNodePath = "/apps/dam/config/" + queueConfigName;
        Session session = (Session)rr.adaptTo(Session.class);
        Node importQueueNode = null;
        try {
            importQueueNode = JcrResourceUtil.createPath((String)importQueueNodePath, (String)"nt:folder", (String)"sling:OsgiConfig", (Session)session, (boolean)true);
            importQueueNode.setProperty("queue.maxparallel", 5);
            importQueueNode.setProperty("queue.name", "Scene7 Import Queue-" + ResourceUtil.getName((String)s7Config.getCloudConfigPath()));
            importQueueNode.setProperty("queue.topics", "com/dam/cq/scene7/importer/" + topicHash);
            importQueueNode.setProperty("queue.type", QueueConfiguration.Type.TOPIC_ROUND_ROBIN.toString());
            importQueueNode.setProperty("queue.retries", 0);
            session.save();
        }
        catch (Exception e) {
            LOG.error("Could not update queue configuration node " + importQueueNodePath, (Throwable)e);
        }
    }
}