EditLaunchCommand.java 7.35 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.launches.api.Launch
 *  com.adobe.cq.launches.api.LaunchManager
 *  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.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.List;
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 EditLaunchCommand
extends LaunchesCommand {
    private final Logger log = LoggerFactory.getLogger(EditLaunchCommand.class);
    @Reference
    private XSSAPI xssAPI;

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

    public HtmlResponse performCommand(WCMCommandContext ctx, SlingHttpServletRequest request, SlingHttpServletResponse response, PageManager pageManager) {
        try {
            Resource[] resourceList;
            String path = request.getParameter("path");
            if (StringUtils.isEmpty((String)path)) {
                return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)"Incomplete request: Path of launch to edit was not specified.");
            }
            ResourceResolver resolver = request.getResourceResolver();
            LaunchManager launchManager = this.launchesManagerFactory.getLaunchManager(resolver);
            Launch launch = launchManager.getLaunch(path);
            if (launch == null) {
                return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)("Error during operation. Provided path is not a launch: " + path));
            }
            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."));
            }
            String[] shallowValues = request.getParameterValues("shallowList");
            if (shallowValues == 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));
            }
            if (launchSourceList.size() == 0) {
                return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"launch child can not be empty set"));
            }
            launch = launchManager.updateLaunchSources(launch, launchSourceList);
            return HtmlStatusResponseHelper.createStatusResponse((boolean)true, (String)I18n.get((HttpServletRequest)request, (String)"Launch updated: {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;
        }
    }
}