CompareLaunchServlet.java 8.22 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.LaunchManagerFactory
 *  com.adobe.cq.launches.api.LaunchPromotionScope
 *  com.adobe.cq.launches.api.LaunchResourceStatus
 *  com.adobe.cq.launches.api.LaunchResourceStatus$LaunchStatusType
 *  com.adobe.granite.security.user.util.AuthorizableUtil
 *  com.day.cq.commons.JSONWriterUtil
 *  com.day.cq.commons.JSONWriterUtil$WriteMode
 *  com.day.cq.xss.XSSProtectionService
 *  javax.jcr.RangeIterator
 *  javax.servlet.ServletException
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  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.SlingSafeMethodsServlet
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.launches.impl;

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.launches.api.LaunchPromotionScope;
import com.adobe.cq.launches.api.LaunchResourceStatus;
import com.adobe.cq.wcm.launches.utils.LaunchUtils;
import com.adobe.granite.security.user.util.AuthorizableUtil;
import com.day.cq.commons.JSONWriterUtil;
import com.day.cq.xss.XSSProtectionService;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Date;
import javax.jcr.RangeIterator;
import javax.servlet.ServletException;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
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.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(resourceTypes={"wcm/launches/components/launch"}, selectors={"compare"}, extensions={"json"}, methods={"GET"})
public class CompareLaunchServlet
extends SlingSafeMethodsServlet {
    private static final long serialVersionUID = -9165801886033663549L;
    private static final String START_PATH_PARAM = "startPath";
    private static final String SCOPE_PARAM = "scope";
    private static final String TARGET_PARAM = "target";
    private static final String MAX_REF_NO = "maxRefNo";
    protected static final Logger log = LoggerFactory.getLogger(CompareLaunchServlet.class);
    @Reference
    LaunchManagerFactory launchesManagerFactory = null;
    @Reference
    private XSSProtectionService xss;

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        try {
            long totalNo;
            Resource launchResource;
            Integer maxRefNo;
            String maxRefNoStr;
            LaunchPromotionScope promotionScope;
            Resource startResource;
            Resource resource = request.getResource();
            Launch launch = (Launch)resource.adaptTo(Launch.class);
            ResourceResolver resolver = request.getResourceResolver();
            JSONObject object = new JSONObject();
            String startPath = request.getParameter("startPath");
            startPath = StringUtils.isEmpty((String)startPath) ? null : startPath;
            String promotionScopeString = request.getParameter("scope");
            try {
                promotionScope = LaunchPromotionScope.valueOf((String)promotionScopeString.toUpperCase());
            }
            catch (IllegalArgumentException e) {
                throw new LaunchException("Unrecognized launch promotion scope: " + promotionScopeString);
            }
            Launch target = null;
            String targetPath = request.getParameter("target");
            if (targetPath != null && (launchResource = request.getResourceResolver().getResource(targetPath)) != null) {
                target = (Launch)launchResource.adaptTo(Launch.class);
            }
            Integer n = maxRefNo = StringUtils.isEmpty((String)(maxRefNoStr = request.getParameter("maxRefNo"))) ? null : Integer.valueOf(Integer.parseInt(maxRefNoStr));
            if (startPath != null && LaunchUtils.isLaunchBasedPath(startPath)) {
                startResource = LaunchUtils.getTargetResource(resolver.getResource(startPath), target);
                if (startResource == null) {
                    startResource = resolver.getResource(startPath);
                }
            } else {
                startResource = resolver.getResource(startPath);
            }
            LaunchManager launchManager = this.launchesManagerFactory.getLaunchManager(resolver);
            RangeIterator statusesIt = launchManager.getResourcesStatus(launch, startResource, promotionScope, target);
            JSONArray pages = new JSONArray();
            for (totalNo = 0; statusesIt.hasNext() && (maxRefNo == null || totalNo <= (long)maxRefNo.intValue()); ++totalNo) {
                LaunchResourceStatus status = (LaunchResourceStatus)statusesIt.next();
                JSONObject stat = new JSONObject();
                JSONWriterUtil.put((JSONObject)stat, (String)"path", (String)status.getResourcePath(), (JSONWriterUtil.WriteMode)JSONWriterUtil.WriteMode.BOTH, (XSSProtectionService)this.xss);
                JSONWriterUtil.put((JSONObject)stat, (String)"title", (String)status.getTitle(), (JSONWriterUtil.WriteMode)JSONWriterUtil.WriteMode.BOTH, (XSSProtectionService)this.xss);
                JSONWriterUtil.putOptional((JSONObject)stat, (String)"launchLastModifiedBy", (String)AuthorizableUtil.getFormattedName((ResourceResolver)resolver, (String)status.getLaunchUserId()), (JSONWriterUtil.WriteMode)JSONWriterUtil.WriteMode.BOTH, (XSSProtectionService)this.xss);
                stat.put("launchLastModified", (Object)(status.getLaunchModificationDate() != null ? Long.valueOf(status.getLaunchModificationDate().getTime()) : null));
                JSONWriterUtil.putOptional((JSONObject)stat, (String)"productionLastModifiedBy", (String)AuthorizableUtil.getFormattedName((ResourceResolver)resolver, (String)status.getProductionUserId()), (JSONWriterUtil.WriteMode)JSONWriterUtil.WriteMode.BOTH, (XSSProtectionService)this.xss);
                stat.put("productionLastModified", (Object)(status.getProductionModificationDate() != null ? Long.valueOf(status.getProductionModificationDate().getTime()) : null));
                stat.put("type", (Object)status.getType().toString());
                pages.put((Object)stat);
            }
            object.put("pages", (Object)pages);
            if (maxRefNo != null && totalNo > (long)maxRefNo.intValue() && statusesIt.hasNext()) {
                totalNo = statusesIt.getSize();
            }
            object.put("totalPages", totalNo);
            object.write((Writer)response.getWriter());
        }
        catch (Exception e) {
            throw new ServletException((Throwable)e);
        }
    }

    protected void bindLaunchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
        this.launchesManagerFactory = launchManagerFactory;
    }

    protected void unbindLaunchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
        if (this.launchesManagerFactory == launchManagerFactory) {
            this.launchesManagerFactory = null;
        }
    }

    protected void bindXss(XSSProtectionService xSSProtectionService) {
        this.xss = xSSProtectionService;
    }

    protected void unbindXss(XSSProtectionService xSSProtectionService) {
        if (this.xss == xSSProtectionService) {
            this.xss = null;
        }
    }
}