CreateLaunchCommand.java 8.51 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.launches.api.Launch
 *  com.adobe.cq.launches.api.LaunchException
 *  com.adobe.cq.launches.api.LaunchManager
 *  com.adobe.cq.launches.api.LaunchManager$CreateOptions
 *  com.adobe.cq.launches.api.LaunchManagerFactory
 *  com.adobe.granite.xss.XSSAPI
 *  com.day.cq.commons.LanguageUtil
 *  com.day.cq.commons.servlets.HtmlStatusResponseHelper
 *  com.day.cq.i18n.I18n
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.commands.WCMCommandContext
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang.StringUtils
 *  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.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.HtmlResponse
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.launches.impl.commands;

import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchException;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.wcm.launches.impl.LaunchSourceImpl;
import com.adobe.cq.wcm.launches.impl.commands.LaunchesCommand;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.commons.LanguageUtil;
import com.day.cq.commons.servlets.HtmlStatusResponseHelper;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.commands.WCMCommandContext;
import java.util.ArrayList;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
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.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HtmlResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class CreateLaunchCommand
extends LaunchesCommand {
    private final Logger log = LoggerFactory.getLogger(CreateLaunchCommand.class);
    @Reference
    private XSSAPI xssAPI;

    public String getCommandName() {
        return "createLaunch";
    }

    public HtmlResponse performCommand(WCMCommandContext ctx, SlingHttpServletRequest request, SlingHttpServletResponse response, PageManager pageManager) {
        try {
            Calendar liveDate;
            String[] shallowValues;
            String title;
            Resource[] resourceList;
            String[] srcPathArray = request.getParameterValues("srcPathList");
            ArrayList<String> srcPathList = new ArrayList<String>();
            if (srcPathArray != null && srcPathArray.length > 0) {
                for (int index = 0; index < srcPathArray.length; ++index) {
                    if (StringUtils.isEmpty((String)srcPathArray[index])) continue;
                    srcPathList.add(srcPathArray[index]);
                }
            } else {
                String srcPath = request.getParameter("srcPath");
                if (!StringUtils.isEmpty((String)srcPath)) {
                    srcPathList.add(srcPath);
                }
            }
            if (srcPathList.size() == 0) {
                return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Page path needs to be specified."));
            }
            try {
                title = this.getStringParam(request, "title", "title", true, null);
            }
            catch (LaunchException e) {
                return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"{0}", (String)null, (Object[])new Object[]{e.getMessage()}));
            }
            try {
                liveDate = this.getCalendarParam(request, "liveDate", "live date", false, null);
            }
            catch (LaunchException e) {
                return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"{0}", (String)null, (Object[])new Object[]{e.getMessage()}));
            }
            ResourceResolver resolver = request.getResourceResolver();
            LaunchManager launchManager = this.launchesManagerFactory.getLaunchManager(resolver);
            boolean shallow = "true".equals(request.getParameter("shallow"));
            boolean isLiveCopy = "true".equals(request.getParameter("isLiveCopy"));
            String template = request.getParameter("template");
            if (template != null) {
                shallow = true;
                isLiveCopy = false;
            }
            if ((shallowValues = request.getParameterValues("shallowList")) == null) {
                shallowValues = new String[]{request.getParameter("shallow")};
            }
            if ((resourceList = new Resource[srcPathList.size()]).length != shallowValues.length) {
                return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Source path list does not match with shallow param list", (String)null));
            }
            ArrayList<LaunchSourceImpl> launchSourceList = new ArrayList<LaunchSourceImpl>();
            String strResourceSitePath = null;
            for (int index = 0; index < srcPathList.size(); ++index) {
                String srcPath = (String)srcPathList.get(index);
                Resource resource = resolver.getResource(srcPath);
                if (resource == null) {
                    return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Source path does not point to a resource: {0}", (String)null, (Object[])new Object[]{srcPath}));
                }
                String strSitePath = this.calculateSitePath(resource);
                if (strResourceSitePath == null) {
                    strResourceSitePath = strSitePath;
                } else if (!strResourceSitePath.equals(strSitePath)) {
                    return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Source path does not point to same site: {0}", (String)null, (Object[])new Object[]{srcPath}));
                }
                boolean childDeep = !"true".equals(shallowValues[index]);
                launchSourceList.add(new LaunchSourceImpl(resource, childDeep));
            }
            String[] sourceConfigs = request.getParameterValues("sourceRolloutConfigs");
            String[] promoteConfigs = request.getParameterValues("promoteRolloutConfigs");
            LaunchManager.CreateOptions options = new LaunchManager.CreateOptions();
            options.launchSourceList = launchSourceList;
            options.resource = null;
            options.title = title;
            options.liveDate = liveDate;
            options.isDeep = !shallow;
            options.isLiveCopy = isLiveCopy;
            options.template = template;
            options.sourceRolloutConfigs = sourceConfigs;
            options.promoteRolloutConfigs = promoteConfigs;
            Launch launch = launchManager.createLaunch(options);
            return HtmlStatusResponseHelper.createStatusResponse((boolean)true, (String)I18n.get((HttpServletRequest)request, (String)"Launch created: {0}", (String)null, (Object[])new Object[]{this.xssAPI.filterHTML(launch.getTitle())}), (String)launch.getRootResource().getPath());
        }
        catch (Exception e) {
            this.log.error("Error during creation of launch.", (Throwable)e);
            return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"{0}", (String)null, (Object[])new Object[]{e.getMessage()}));
        }
    }

    private String calculateSitePath(Resource resource) {
        String strRetVal = LanguageUtil.getLanguageRoot((String)resource.getPath());
        if (strRetVal == null) {
            strRetVal = "";
        }
        return strRetVal;
    }

    protected void bindXssAPI(XSSAPI xSSAPI) {
        this.xssAPI = xSSAPI;
    }

    protected void unbindXssAPI(XSSAPI xSSAPI) {
        if (this.xssAPI == xSSAPI) {
            this.xssAPI = null;
        }
    }
}