LaunchesListServlet.java 8.99 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.security.user.UserProperties
 *  com.adobe.granite.security.user.UserPropertiesManager
 *  com.adobe.granite.xss.XSSAPI
 *  com.day.cq.commons.servlets.AbstractListServlet
 *  com.day.cq.commons.servlets.AbstractListServlet$ListItem
 *  com.day.cq.i18n.I18n
 *  com.day.cq.wcm.api.Page
 *  com.day.text.Text
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.collections.Predicate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  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.LaunchManager;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.wcm.launches.utils.LaunchUtils;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.commons.servlets.AbstractListServlet;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
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.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Property(name="service.description", value={"List"})
@Component(metatype=0)
@SlingServlet(methods={"GET"}, resourceTypes={"sling/servlet/default"}, selectors={"launches"}, extensions={"json"}, generateComponent=0)
public class LaunchesListServlet
extends AbstractListServlet {
    private static final Logger log = LoggerFactory.getLogger(LaunchesListServlet.class);
    @Reference
    protected LaunchManagerFactory launchManagerFactory;
    @Reference
    private XSSAPI xssApi;

    protected List<AbstractListServlet.ListItem> getItems(SlingHttpServletRequest request, Predicate predicate) throws Exception {
        Resource requestRes = request.getResource();
        ResourceResolver resolver = request.getResourceResolver();
        Page requestPage = (Page)requestRes.adaptTo(Page.class);
        if (requestPage == null) {
            log.warn("Ignoring '{}' for non-page resource: {}", (Object)this.getClass().getCanonicalName(), (Object)requestRes.getPath());
            return Collections.emptyList();
        }
        ArrayList<AbstractListServlet.ListItem> items = new ArrayList<AbstractListServlet.ListItem>();
        int results = 0;
        UserPropertiesManager userPropsMgr = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
        Resource productionRes = LaunchUtils.getTargetResource(requestRes, null);
        if (productionRes != null) {
            Page productionPage = (Page)productionRes.adaptTo(Page.class);
            if (productionPage != null) {
                String title = I18n.get((HttpServletRequest)request, (String)"Production", (String)"Title to use for the production page in context of launches");
                items.add(new LaunchItem(this, results, productionPage, title, ":production", userPropsMgr));
                ++results;
            } else {
                log.warn("Ignoring production version for non-page resource: {}", (Object)productionRes.getPath());
            }
        }
        LaunchManager launchManager = this.launchManagerFactory.getLaunchManager(resolver);
        Collection launches = launchManager.getLaunches(requestRes);
        for (Launch l : launches) {
            LaunchItem item = new LaunchItem(this, results, l, requestPage, userPropsMgr);
            items.add(item);
            ++results;
        }
        return items;
    }

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

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

    protected void bindXssApi(XSSAPI xSSAPI) {
        this.xssApi = xSSAPI;
    }

    protected void unbindXssApi(XSSAPI xSSAPI) {
        if (this.xssApi == xSSAPI) {
            this.xssApi = null;
        }
    }

    public class LaunchItem
    implements AbstractListServlet.ListItem {
        private static final String LAUNCH_PATH = "launchPath";
        private static final String ESCAPED_LAUNCH_PATH = "escapedLaunchPath";
        private static final String PAGE_PATH = "pagePath";
        private static final String ESCAPED_PAGE_PATH = "escapedPagePath";
        private final int index;
        private final Resource resource;
        private final Page page;
        private final UserPropertiesManager userPropsMgr;
        private final String title;
        private final String label;
        private final String launchPath;
        private final Calendar lastModified;
        private final String lastModifiedBy;
        final /* synthetic */ LaunchesListServlet this$0;

        public LaunchItem(LaunchesListServlet launchesListServlet, int index, Launch launch, Page page, UserPropertiesManager userPropsMgr) throws Exception {
            this.this$0 = launchesListServlet;
            this.index = index;
            this.resource = launch.getResource();
            this.page = page;
            this.userPropsMgr = userPropsMgr;
            this.title = launch.getTitle();
            this.label = launch.getResource().getName();
            this.launchPath = launch.getResource().getPath();
            this.lastModified = launch.getModified();
            this.lastModifiedBy = launch.getModifiedBy();
        }

        public LaunchItem(LaunchesListServlet launchesListServlet, int index, Page page, String title, String label, UserPropertiesManager userPropsMgr) throws Exception {
            this.this$0 = launchesListServlet;
            this.index = index;
            this.resource = (Resource)page.adaptTo(Resource.class);
            this.page = page;
            this.userPropsMgr = userPropsMgr;
            this.title = title;
            this.label = label;
            this.launchPath = null;
            this.lastModified = page.getLastModified();
            this.lastModifiedBy = page.getLastModifiedBy();
        }

        public void write(JSONWriter out, String[] customProps) throws Exception {
            UserProperties profile;
            out.object();
            this.this$0.writeKey(out, "index", this.index);
            boolean isLaunch = this.isLaunch();
            this.this$0.writeKey(out, "title", this.this$0.xssApi.filterHTML(this.title));
            this.this$0.writeKey(out, "label", this.label);
            Launch launch = null;
            if (isLaunch) {
                launch = (Launch)this.resource.adaptTo(Launch.class);
                this.this$0.writeKey(out, "launchPath", this.launchPath);
                this.this$0.writeKey(out, "escapedLaunchPath", Text.escapePath((String)this.launchPath));
            }
            Resource res = this.page.getContentResource().getParent();
            Resource pageRes = isLaunch ? LaunchUtils.getLaunchResource(launch, res) : LaunchUtils.getTargetResource(res, null);
            this.this$0.writeKey(out, "pagePath", pageRes.getPath());
            this.this$0.writeKey(out, "escapedPagePath", Text.escapePath((String)pageRes.getPath()));
            this.this$0.writeKey(out, "jcr:lastModified", this.lastModified.getTimeInMillis());
            String formattedName = this.lastModifiedBy;
            if (this.userPropsMgr != null && (profile = this.userPropsMgr.getUserProperties(this.lastModifiedBy, "profile")) != null) {
                formattedName = profile.getDisplayName();
            }
            this.this$0.writeKey(out, "jcr:lastModifiedBy", this.this$0.xssApi.filterHTML(formattedName));
            out.endObject();
        }

        public Resource getResource() {
            return this.resource;
        }

        private boolean isLaunch() {
            return !":production".equals(this.label);
        }
    }

}