PayloadSummaryServlet.java 13.7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.projects.api.Project
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.Rendition
 *  com.day.cq.dam.commons.util.UIHelper
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.request.RequestPathInfo
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.servlets.SlingSafeMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.projects.impl.servlet;

import com.adobe.cq.projects.api.Project;
import com.adobe.cq.projects.impl.servlet.ProjectServletUtil;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.commons.util.UIHelper;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(name="com.adobe.granite.workflow.PayloadSummaryServlet", immediate=1, label="PayloadSummaryServlet", metatype=0, description="Outputs payload information for project tasks")
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}, propertyPrivate=1), @Property(name="sling.servlet.selectors", value={"payloadsummary", "largeicon.payloadsummary"}), @Property(name="sling.servlet.extensions", value={"json"})})
public class PayloadSummaryServlet
extends SlingSafeMethodsServlet {
    private static final Logger LOGGER = LoggerFactory.getLogger(PayloadSummaryServlet.class);
    private static final String LARGEICON_SELECTOR = "largeicon";
    private static final String MIMETYPE_DAM_ASSET_MANUSCRIPT = "text/plain";
    private static final String NODENAME_MANUSCRIPT_ASSIGNMENT = "jcr:content/assignment";
    private static final String KEY_EXTRA = "extra";
    private static final String KEY_WRITER = "writer";
    private static final String KEY_EDITOR = "editor";
    private static final String KEY_STATUS = "status";
    private static final String KEY_BRIEF = "brief";
    private static final String DAM_PATH = "/content/dam";

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        response.setContentType(request.getResponseContentType());
        response.setCharacterEncoding("UTF-8");
        boolean largeIcon = false;
        String[] selectors = request.getRequestPathInfo().getSelectors();
        if (selectors != null) {
            for (String selector : selectors) {
                if (!"largeicon".equals(selector)) continue;
                largeIcon = true;
                break;
            }
        }
        try {
            JSONWriter jsonWriter = new JSONWriter((Writer)response.getWriter());
            Resource resource = request.getResource();
            ValueMap values = (ValueMap)resource.adaptTo(ValueMap.class);
            int iconSize = 48;
            if (largeIcon) {
                iconSize = 319;
            }
            jsonWriter.object();
            Project project = (Project)resource.adaptTo(Project.class);
            if (project != null) {
                this.handleProject(jsonWriter, resource, values, iconSize, project);
            } else if (resource.isResourceType("dam:Asset")) {
                this.handleDamAsset(jsonWriter, resource, iconSize);
            } else if (this.isDAMFolder(resource)) {
                this.handeDAMFolder(jsonWriter, resource, values, iconSize);
            } else if (resource.isResourceType("cq:Page") || resource.isResourceType("cq:PageContent") || "cq:PageContent".equals(values.get((Object)"jcr:primaryType"))) {
                this.handlePage(jsonWriter, resource, values, iconSize);
            } else {
                String title;
                LOGGER.debug("no thumbnail found for resource at {}", (Object)resource.getPath());
                jsonWriter.key("icon").value((Object)(resource.getPath() + ".thumb." + iconSize + "." + iconSize + ".png"));
                String description = this.findDescription(resource, values);
                if (description != null) {
                    jsonWriter.key("description").value((Object)description);
                }
                if ((title = this.findTitle(resource, values)) != null) {
                    jsonWriter.key("title").value((Object)title);
                }
            }
            jsonWriter.endObject();
        }
        catch (JSONException ex) {
            throw new ServletException("JSONException in doGet", (Throwable)ex);
        }
    }

    private boolean handeDAMFolder(JSONWriter jsonWriter, Resource resource, ValueMap values, int iconSize) throws JSONException {
        String title;
        if (resource.isResourceType("dam/collection") || resource.isResourceType("dam/smartcollection")) {
            jsonWriter.key("openPayloadUrl").value((Object)("/libs/dam/gui/content/collections/collectiondetails.html" + Text.escapePath((String)resource.getPath())));
        } else {
            jsonWriter.key("openPayloadUrl").value((Object)("/assets.html" + Text.escapePath((String)resource.getPath())));
        }
        jsonWriter.key("icon").value((Object)(resource.getPath() + ".folderthumbnail.jpg?width=" + iconSize + "&height=" + iconSize));
        String description = this.findDescription(resource, values);
        if (description != null) {
            jsonWriter.key("description").value((Object)description);
        }
        if ((title = this.findTitle(resource, values)) != null) {
            jsonWriter.key("title").value((Object)title);
        }
        return true;
    }

    private void handlePage(JSONWriter jsonWriter, Resource resource, ValueMap values, int iconSize) throws JSONException {
        String title;
        jsonWriter.key("openPayloadUrl").value((Object)("/sites.html" + Text.escapePath((String)resource.getPath())));
        jsonWriter.key("icon").value((Object)(resource.getPath() + ".thumb." + iconSize + "." + iconSize + ".png"));
        String description = this.findDescription(resource, values);
        if (description != null) {
            jsonWriter.key("description").value((Object)description);
        }
        if ((title = this.findTitle(resource, values)) != null) {
            jsonWriter.key("title").value((Object)title);
        }
    }

    private boolean handleProject(JSONWriter jsonWriter, Resource resource, ValueMap values, int iconSize, Project project) throws JSONException {
        String title;
        String thumbnailUrl;
        if (project == null) {
            return false;
        }
        String description = this.findDescription(resource, values);
        if (description != null) {
            jsonWriter.key("description").value((Object)description);
        }
        if ((title = this.findTitle(resource, values)) != null) {
            jsonWriter.key("title").value((Object)title);
        }
        String detailsUrl = ProjectServletUtil.getProjectDetailsUrl(resource);
        jsonWriter.key("openPayloadUrl").value((Object)(detailsUrl + Text.escapePath((String)resource.getPath())));
        Resource projectCover = project.getProjectCover();
        if (projectCover != null && (thumbnailUrl = this.getAssetUrl(projectCover, iconSize)) != null) {
            jsonWriter.key("icon").value((Object)thumbnailUrl);
        }
        return true;
    }

    private boolean isDAMFolder(Resource resource) {
        if (resource == null) {
            return false;
        }
        if (resource.getPath().startsWith("/content/dam")) {
            if (resource.isResourceType("nt:folder") || resource.isResourceType("dam/collection") || resource.isResourceType("dam/smartcollection")) {
                return true;
            }
            Node node = (Node)resource.adaptTo(Node.class);
            if (node != null) {
                try {
                    if (node.isNodeType("nt:folder")) {
                        return true;
                    }
                }
                catch (RepositoryException e) {
                    LOGGER.debug("failed to check node type for resource " + resource.getPath(), (Throwable)e);
                }
            }
        }
        return false;
    }

    private String findTitle(Resource resource, ValueMap values) {
        return this.findProperty(resource, values, "jcr:title");
    }

    private String findDescription(Resource resource, ValueMap values) {
        return this.findProperty(resource, values, "jcr:description");
    }

    private String findProperty(Resource resource, ValueMap values, String propertyName) {
        String propertyValue = this.getStringProperty(values, propertyName);
        if (propertyValue != null) {
            return (String)values.get(propertyName, String.class);
        }
        Resource jcrContent = resource.getChild("jcr:content");
        if (jcrContent != null) {
            ValueMap contentValueMap = (ValueMap)jcrContent.adaptTo(ValueMap.class);
            return this.getStringProperty(contentValueMap, propertyName);
        }
        return null;
    }

    private String getStringProperty(ValueMap values, String propertyName) {
        if (values == null || propertyName == null) {
            return null;
        }
        if (values.containsKey((Object)propertyName)) {
            return (String)values.get(propertyName, String.class);
        }
        return null;
    }

    private boolean handleDamAsset(JSONWriter jsonWriter, Resource resource, int iconSize) throws JSONException {
        ValueMap assignmentMap;
        Resource assignmentResource;
        String thumbnailUrl;
        ValueMap metadataMap;
        Asset asset = (Asset)resource.adaptTo(Asset.class);
        if (asset == null) {
            return false;
        }
        jsonWriter.key("openPayloadUrl").value((Object)("/assetdetails.html" + Text.escapePath((String)resource.getPath())));
        Resource metadataResource = resource.getChild("jcr:content/metadata");
        if (metadataResource != null && (metadataMap = (ValueMap)metadataResource.adaptTo(ValueMap.class)) != null) {
            String description;
            String title = (String)metadataMap.get("dc:title", String.class);
            if (title != null) {
                jsonWriter.key("title").value((Object)title);
            }
            if ((description = (String)metadataMap.get("dc:description", String.class)) != null) {
                jsonWriter.key("description").value((Object)description);
            }
        }
        if ((thumbnailUrl = this.getAssetUrl(resource, asset, iconSize)) != null) {
            jsonWriter.key("icon").value((Object)thumbnailUrl);
        }
        if ("text/plain".equals(asset.getMimeType()) && (assignmentResource = resource.getChild("jcr:content/assignment")) != null && (assignmentMap = (ValueMap)assignmentResource.adaptTo(ValueMap.class)) != null) {
            jsonWriter.key("extra").object();
            if (assignmentMap.containsKey((Object)"writer")) {
                jsonWriter.key("Writer").value(assignmentMap.get("writer", String.class));
            }
            if (assignmentMap.containsKey((Object)"editor")) {
                jsonWriter.key("Editor").value(assignmentMap.get("editor", String.class));
            }
            if (assignmentMap.containsKey((Object)"status")) {
                jsonWriter.key("Status").value(assignmentMap.get("status", String.class));
            }
            if (assignmentMap.containsKey((Object)"brief")) {
                jsonWriter.key("Brief").value(assignmentMap.get("brief", String.class));
            }
            jsonWriter.endObject();
        }
        return true;
    }

    private String getAssetUrl(Resource assetResource, int iconSize) {
        Asset asset = (Asset)assetResource.adaptTo(Asset.class);
        return this.getAssetUrl(assetResource, asset, iconSize);
    }

    private String getAssetUrl(Resource assetResource, Asset asset, int iconSize) {
        String thumbnailUrl = null;
        if (assetResource != null && asset != null) {
            Rendition thumbnailRendition = UIHelper.getBestfitRendition((Asset)asset, (int)iconSize);
            if (thumbnailRendition != null) {
                try {
                    thumbnailUrl = asset.getPath() + "/jcr:content/renditions/" + thumbnailRendition.getName();
                }
                catch (Exception e) {
                    LOGGER.info("Unable to get thumbnail rendition information", (Throwable)e);
                }
            } else {
                thumbnailUrl = assetResource.getPath() + ".thumb." + iconSize + "." + iconSize + ".png";
            }
        }
        return thumbnailUrl;
    }
}