FormsPayloadInfoBuilder.java 6.18 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.workflow.exec.InboxItem
 *  com.adobe.granite.workflow.exec.WorkItem
 *  com.adobe.granite.workflow.metadata.MetaDataMap
 *  com.adobe.granite.workflow.model.WorkflowNode
 *  com.adobe.granite.workflow.payload.PayloadInfo
 *  com.adobe.granite.workflow.payload.PayloadInfoBuilder
 *  com.adobe.granite.workflow.payload.PayloadInfoBuilderContext
 *  com.adobe.granite.xss.ProtectionContext
 *  com.adobe.granite.xss.XSSFilter
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  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.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.foundation.forms.impl;

import com.adobe.granite.workflow.exec.InboxItem;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.granite.workflow.model.WorkflowNode;
import com.adobe.granite.workflow.payload.PayloadInfo;
import com.adobe.granite.workflow.payload.PayloadInfoBuilder;
import com.adobe.granite.workflow.payload.PayloadInfoBuilderContext;
import com.adobe.granite.xss.ProtectionContext;
import com.adobe.granite.xss.XSSFilter;
import java.util.Iterator;
import org.apache.commons.lang3.StringUtils;
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.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={PayloadInfoBuilder.class})
@Property(name="service.ranking", intValue={-100}, propertyPrivate=1)
public class FormsPayloadInfoBuilder
implements PayloadInfoBuilder {
    private static final Logger log = LoggerFactory.getLogger(FormsPayloadInfoBuilder.class);
    private static final String FORM_PATH_PROPERTY = "formPath";
    private static final String FORM_START_RESOURCE_TYPE = "foundation/components/form/start";
    private static final String FORM_END_RESOURCE_TYPE = "foundation/components/form/end";
    private static final String DISABLE_WCM_EDIT_MODE = "?wcmmode=disabled";
    @Reference(policy=ReferencePolicy.STATIC)
    private XSSFilter xss;

    public PayloadInfo getPayloadInfo(PayloadInfoBuilderContext context) {
        Resource res;
        String payloadPath = context.getInboxItem().getContentPath();
        if (StringUtils.isBlank((CharSequence)payloadPath)) {
            payloadPath = context.getPayloadPath();
        }
        if ((res = context.getResourceResolver().getResource(payloadPath)) != null) {
            WorkflowNode node;
            String formPath;
            Resource parent = res.getParent();
            if (parent != null) {
                ValueMap values = res.getValueMap();
                ValueMap parentValues = parent.getValueMap();
                String formPath2 = (String)parentValues.get("formPath", String.class);
                if (StringUtils.isNotBlank((CharSequence)formPath2)) {
                    String path = res.getPath();
                    String browserPath = path + ".html";
                    String description = "";
                    Resource form = context.getResourceResolver().getResource(ResourceUtil.getParent((String)formPath2));
                    String formStartName = ResourceUtil.getName((String)((String)parentValues.get("formPath", (Object)"")));
                    Iterator children = form.listChildren();
                    while (children.hasNext()) {
                        Resource child = (Resource)children.next();
                        if (!child.isResourceType("foundation/components/form/start") || !child.getName().equals(formStartName)) continue;
                        description = this.getDescription(values, children);
                        PayloadInfo info = context.createPayloadInfo();
                        info.setPath(path).setBrowserPath(browserPath).setDescription(description);
                        return info;
                    }
                }
            }
            if (context.getInboxItem() != null && context.getInboxItem() instanceof WorkItem && (node = ((WorkItem)context.getInboxItem()).getNode()) != null && (formPath = (String)node.getMetaDataMap().get("FORM_PATH", String.class)) != null && formPath.startsWith("/")) {
                String path = context.getInboxItem().getContentPath();
                String browserPath = path + ".form.html" + formPath + "?wcmmode=disabled";
                PayloadInfo info = context.createPayloadInfo();
                info.setPath(path).setBrowserPath(browserPath);
                return info;
            }
        }
        return null;
    }

    private String getDescription(ValueMap values, Iterator<Resource> children) {
        StringBuilder builder = new StringBuilder();
        int count = 0;
        while (children.hasNext() && count < 3) {
            Resource child = children.next();
            ValueMap fieldValues = child.getValueMap();
            if (child.isResourceType("foundation/components/form/end")) break;
            if (!fieldValues.containsKey((Object)"name")) continue;
            String name = (String)fieldValues.get("name", (Object)"");
            builder.append("<br>").append(name).append(": ").append(this.xss.filter(ProtectionContext.PLAIN_HTML_CONTENT, (String)values.get(name, (Object)"")));
            ++count;
        }
        return builder.toString();
    }

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

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