ConfigPostProcessor.java 6.07 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.msm.api.BlueprintManager
 *  com.day.cq.wcm.msm.api.LiveCopy
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveRelationshipManager
 *  com.day.cq.wcm.msm.api.RolloutConfig
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.servlets.post.Modification
 *  org.apache.sling.servlets.post.SlingPostProcessor
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl.postprocessing;

import com.day.cq.wcm.msm.api.BlueprintManager;
import com.day.cq.wcm.msm.api.LiveCopy;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.impl.BlueprintImpl;
import com.day.cq.wcm.msm.impl.BlueprintManagerImpl;
import com.day.cq.wcm.msm.impl.LiveCopyManagerImpl;
import com.day.cq.wcm.msm.impl.LiveCopyServiceImpl;
import com.day.cq.wcm.msm.impl.LiveRelationshipEditingUtil;
import java.util.List;
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.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
public class ConfigPostProcessor
implements SlingPostProcessor {
    private static final Logger log = LoggerFactory.getLogger(ConfigPostProcessor.class);
    private static final String WRITE_BLUEPRINT_CONFIG_PARAM = "msm:writeBlueprintConfig";
    private static final String BLUEPRINT_ROLLOUTCONFIGS_PARAM = "msm:blueprintRolloutConfigs";
    private static final String WRITE_LIVECOPY_CONFIG_PARAM = "msm:writeLiveCopyConfig";
    private static final String LIVECOPY_ROLLOUTCONFIGS_PARAM = "msm:liveCopyRolloutConfigs";
    private static final String INHERIT_ROLLOUTCONFIGS_PARAM = "msm:inheritRolloutConfigs";
    @Reference
    private LiveRelationshipManager relationshipManager = null;
    @Reference
    private LiveCopyServiceImpl liveCopyService = null;

    public void process(SlingHttpServletRequest request, List<Modification> modifications) throws Exception {
        if ("true".equals(request.getParameter("msm:writeBlueprintConfig"))) {
            try {
                Resource resource = request.getResource();
                String[] rolloutConfigs = request.getParameterValues("msm:blueprintRolloutConfigs");
                if (rolloutConfigs != null && rolloutConfigs.length == 1 && "".equals(rolloutConfigs[0])) {
                    rolloutConfigs = null;
                }
                Resource configResource = resource.getName().equals("jcr:content") ? resource.getParent() : resource;
                ResourceResolver resolver = request.getResourceResolver();
                BlueprintManagerImpl bpm = (BlueprintManagerImpl)resolver.adaptTo(BlueprintManager.class);
                BlueprintImpl blueprint = bpm.getContainingBlueprint(configResource.getPath());
                if (blueprint != null) {
                    String relPath = "";
                    if (configResource.getPath().length() > blueprint.getSitePath().length()) {
                        relPath = relPath + configResource.getPath().substring(blueprint.getSitePath().length() + 1);
                    }
                    blueprint.setRolloutConfigs(relPath, rolloutConfigs);
                    log.debug("Updated blueprint config at {}", (Object)configResource.getPath());
                }
            }
            catch (Exception e) {
                log.error("Error while updating blueprint config", (Throwable)e);
            }
        }
        if ("true".equals(request.getParameter("msm:writeLiveCopyConfig"))) {
            LiveRelationshipEditingUtil.processCancel(request, this.relationshipManager);
            try {
                ResourceResolver resolver = request.getResourceResolver();
                Resource resource = request.getResource();
                LiveRelationship currentRelation = this.relationshipManager.getLiveRelationship(resource, true);
                String[] rolloutConfigs = request.getParameterValues("msm:liveCopyRolloutConfigs");
                if (rolloutConfigs == null || "true".equals(request.getParameter("msm:inheritRolloutConfigs"))) {
                    rolloutConfigs = new String[]{};
                }
                Boolean deepParam = Boolean.valueOf(request.getParameter("msm:isDeep"));
                log.debug("Updating LiveCopy at {}", (Object)currentRelation.getLiveCopy().getPath());
                LiveCopyManagerImpl lcManager = this.liveCopyService.createLiveCopyManager(resolver);
                LiveRelationshipEditingUtil.editLiveCopy(resolver, lcManager, this.relationshipManager, currentRelation, deepParam, rolloutConfigs);
            }
            catch (Exception e) {
                log.error("Error while updating LiveCopy", (Throwable)e);
            }
        }
    }

    protected void bindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        this.relationshipManager = liveRelationshipManager;
    }

    protected void unbindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        if (this.relationshipManager == liveRelationshipManager) {
            this.relationshipManager = null;
        }
    }

    protected void bindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
        this.liveCopyService = liveCopyServiceImpl;
    }

    protected void unbindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
        if (this.liveCopyService == liveCopyServiceImpl) {
            this.liveCopyService = null;
        }
    }
}