WorkflowSummaryServlet.java 6.36 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.xss.ProtectionContext
 *  com.adobe.granite.xss.XSSFilter
 *  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.Reference
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  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.resource.ResourceUtil
 *  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
 */
package com.adobe.granite.workflow.console.servlet;

import com.adobe.granite.xss.ProtectionContext;
import com.adobe.granite.xss.XSSFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Iterator;
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.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
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.resource.ResourceUtil;
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;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(name="com.adobe.granite.workflow.WorkflowSummaryServlet")
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}), @Property(name="sling.servlet.selectors", value={"wfsummary"}), @Property(name="sling.servlet.extensions", value={"json"})})
public class WorkflowSummaryServlet
extends SlingSafeMethodsServlet {
    protected static final String FORM_START_RESOURCE_TYPE = "foundation/components/form/start";
    protected static final String FORM_END_RESOURCE_TYPE = "foundation/components/form/end";
    @Reference(policy=ReferencePolicy.STATIC)
    private XSSFilter xss;

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        block6 : {
            response.setContentType(request.getResponseContentType());
            response.setCharacterEncoding("UTF-8");
            try {
                JSONWriter w = new JSONWriter((Writer)response.getWriter());
                Resource resource = request.getResource();
                ResourceResolver resolver = request.getResourceResolver();
                ValueMap values = (ValueMap)resource.adaptTo(ValueMap.class);
                ValueMap parentValues = (ValueMap)ResourceUtil.getParent((Resource)resource).adaptTo(ValueMap.class);
                if (parentValues.containsKey((Object)"formPath")) {
                    Resource form = resolver.getResource(ResourceUtil.getParent((String)((String)parentValues.get((Object)"formPath"))));
                    String formStartName = ResourceUtil.getName((String)((String)parentValues.get((Object)"formPath")));
                    Iterator children = resolver.listChildren(form);
                    while (children.hasNext()) {
                        Resource child = (Resource)children.next();
                        if (!ResourceUtil.isA((Resource)child, (String)"foundation/components/form/start") || !ResourceUtil.getName((Resource)child).equals(formStartName)) continue;
                        String description = this.getFormDescription(values, children);
                        w.object();
                        w.key("description").value((Object)description.toString());
                        w.endObject();
                        break block6;
                    }
                    break block6;
                }
                if (values.containsKey((Object)"jcr:description")) {
                    w.object();
                    w.key("description").value(values.get((Object)"jcr:description"));
                    w.endObject();
                } else {
                    w.object();
                    w.key("exception").value((Object)("no .wfsummary.json renderer provided for resource " + request.getResource().getPath()));
                    w.endObject();
                }
            }
            catch (JSONException ex) {
                throw new ServletException("JSONException in doGet", (Throwable)ex);
            }
        }
    }

    private String getFormDescription(ValueMap values, Iterator<Resource> children) {
        StringBuffer description = new StringBuffer();
        int count = 0;
        while (children.hasNext()) {
            Resource child = children.next();
            ValueMap fieldValues = (ValueMap)child.adaptTo(ValueMap.class);
            if (ResourceUtil.isA((Resource)child, (String)"foundation/components/form/end") || count >= 3) break;
            if (!fieldValues.containsKey((Object)"name")) continue;
            if (count > 0 && count < 3) {
                description.append("<br>");
            }
            description.append(fieldValues.get((Object)"name"));
            description.append(": ");
            String value = (String)values.get(fieldValues.get((Object)"name"));
            if (this.xss != null) {
                value = this.xss.filter(ProtectionContext.PLAIN_HTML_CONTENT, (String)values.get(fieldValues.get((Object)"name")));
            }
            description.append(value);
            ++count;
        }
        return description.toString();
    }

    protected void bindXss(XSSFilter xSSFilter) {
        this.xss = xSSFilter;
    }

    protected void unbindXss(XSSFilter xSSFilter) {
        if (this.xss == xSSFilter) {
            this.xss = null;
        }
    }
}