GuideModuleContentProviderServlet.java 13.4 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.service.GuideModuleImporter
 *  com.adobe.aemds.guide.utils.GuideContainerThreadLocal
 *  com.adobe.aemds.guide.utils.GuideUtils
 *  com.adobe.aemds.guide.utils.XMLUtils
 *  com.adobe.forms.common.service.ContentType
 *  com.adobe.forms.common.service.DataOptions
 *  com.adobe.forms.common.service.FormDataProviderRegistry
 *  com.adobe.forms.common.service.PrefillData
 *  com.adobe.granite.resourceresolverhelper.ResourceResolverHelper
 *  com.adobe.granite.xss.XSSAPI
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  org.apache.commons.lang3.StringUtils
 *  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.ReferenceCardinality
 *  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.SlingAllMethodsServlet
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.fd.adaddon.servlet;

import com.adobe.aemds.guide.service.GuideModuleImporter;
import com.adobe.aemds.guide.utils.GuideContainerThreadLocal;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.aemds.guide.utils.XMLUtils;
import com.adobe.forms.common.service.ContentType;
import com.adobe.forms.common.service.DataOptions;
import com.adobe.forms.common.service.FormDataProviderRegistry;
import com.adobe.forms.common.service.PrefillData;
import com.adobe.granite.resourceresolverhelper.ResourceResolverHelper;
import com.adobe.granite.xss.XSSAPI;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.lang3.StringUtils;
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.ReferenceCardinality;
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.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"fd/adaddon/components/guideAdModule", "fd/adaddon/components/guideAdModuleGroup"}), @Property(name="sling.servlet.methods", value={"GET"}), @Property(name="service.description", value={"Adaptive Form Document Fragment / Group content provider"}), @Property(name="sling.servlet.selectors", value={"ad.modulecontentprovider"})})
public class GuideModuleContentProviderServlet
extends SlingAllMethodsServlet {
    private Logger logger = LoggerFactory.getLogger(GuideModuleContentProviderServlet.class);
    @Reference
    ResourceResolverHelper resolverHelper;
    @Reference
    FormDataProviderRegistry formDataProviderRegistry;
    @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
    private GuideModuleImporter guideModuleImporter;

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String originalGuideContainerPath = GuideContainerThreadLocal.getGuideContainerPath();
        try {
            String guideContainerPath = request.getParameter("guideContainerPath");
            GuideContainerThreadLocal.setGuideContainerPath((String)guideContainerPath);
            this.handleGet(request, response);
        }
        finally {
            GuideContainerThreadLocal.setGuideContainerPath((String)originalGuideContainerPath);
        }
    }

    protected void handleGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        Resource moduleResource = request.getResource();
        String normalizedResourceType = GuideUtils.getNormalizedNodeType((String)moduleResource.getResourceType(), (String)moduleResource.getResourceSuperType());
        String resultantHtml = "";
        String resolvedHtml = "";
        ValueMap moduleProperties = moduleResource.getValueMap();
        if (moduleProperties != null && this.guideModuleImporter != null) {
            Resource itemsResource;
            String assetRef = (String)moduleProperties.get((Object)"assetRef");
            Object layoutConfigProperty = moduleProperties.get((Object)"layoutConfig");
            Object[] layoutConfigArray = null;
            String layoutConfig = "";
            if (layoutConfigProperty != null) {
                if (layoutConfigProperty instanceof String[]) {
                    layoutConfigArray = (String[])layoutConfigProperty;
                }
                layoutConfig = layoutConfigArray != null ? Arrays.toString(layoutConfigArray) : "[" + (String)layoutConfigProperty + "]";
            }
            if (StringUtils.isNotEmpty((CharSequence)assetRef)) {
                HashMap<String, Object> moduleParameter;
                if ("fd/adaddon/components/guideAdModuleGroup".equals(normalizedResourceType)) {
                    moduleParameter = new HashMap<String, Object>();
                    String guideContainerPath = request.getParameter("guideContainerPath");
                    if (guideContainerPath != null) {
                        Resource guideContainerResource;
                        String letterRef = "";
                        if (StringUtils.isNotEmpty((CharSequence)guideContainerPath) && (guideContainerResource = moduleResource.getResourceResolver().getResource(guideContainerPath)) != null) {
                            ValueMap containerValueMap = guideContainerResource.getValueMap();
                            letterRef = (String)containerValueMap.get("letterRef", (Object)"");
                        }
                        moduleParameter.put("letterRef", letterRef);
                    }
                    ValueMap guideProperties = this.getGuideProperties(request);
                    moduleParameter.put("ddRef", this.getDDRef(guideProperties));
                    moduleParameter.put("letterRef", this.getLetterRef(guideProperties));
                    moduleParameter.put("AD_DATA_DOCUMENT", this.getDataDocument(request));
                    resolvedHtml = this.guideModuleImporter.getModuleGroupResolution(assetRef, moduleParameter);
                } else if ("fd/adaddon/components/guideAdModule".equals(normalizedResourceType)) {
                    moduleParameter = new HashMap();
                    ValueMap guideProperties = this.getGuideProperties(request);
                    moduleParameter.put("ddRef", this.getDDRef(guideProperties));
                    moduleParameter.put("letterRef", this.getLetterRef(guideProperties));
                    moduleParameter.put("AD_DATA_DOCUMENT", this.getDataDocument(request));
                    resolvedHtml = this.guideModuleImporter.getModuleResolution(assetRef, moduleParameter);
                }
            } else if ("fd/adaddon/components/guideAdModuleGroup".equals(normalizedResourceType) && (itemsResource = moduleResource.getChild("items")) != null) {
                ArrayList<String> moduleAssetRefs = new ArrayList<String>();
                for (Resource itemResource : itemsResource.getChildren()) {
                    ValueMap itemValueMap = ResourceUtil.getValueMap((Resource)itemResource);
                    String itemAssetRef = (String)itemValueMap.get("assetRef", (Object)"");
                    if (StringUtils.isEmpty((CharSequence)itemAssetRef)) continue;
                    moduleAssetRefs.add(itemAssetRef);
                }
                HashMap<String, Object> moduleParameter = new HashMap<String, Object>();
                moduleParameter.put("moduleList", moduleAssetRefs);
                ValueMap guideProperties = this.getGuideProperties(request);
                moduleParameter.put("ddRef", this.getDDRef(guideProperties));
                moduleParameter.put("letterRef", this.getLetterRef(guideProperties));
                moduleParameter.put("AD_DATA_DOCUMENT", this.getDataDocument(request));
                resolvedHtml = this.guideModuleImporter.getModuleGroupResolution(null, moduleParameter);
            }
            XSSAPI xssapi = (XSSAPI)request.adaptTo(XSSAPI.class);
            resultantHtml = "<div data-module-layout-config='" + GuideUtils.encodeForHtmlAttr((String)layoutConfig, (XSSAPI)xssapi) + "'>" + GuideUtils.filterHtml((String)resolvedHtml, (XSSAPI)xssapi) + "</div>";
        }
        response.getWriter().write(resultantHtml);
    }

    private Document getDataDocument(SlingHttpServletRequest httpServletRequest) throws ServletException {
        String dataRef = httpServletRequest.getParameter("dataRef");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            XMLUtils.disableExternalEntities((DocumentBuilderFactory)dbf);
            DocumentBuilder db = dbf.newDocumentBuilder();
            if (dataRef != null) {
                ResourceResolver rr = this.resolverHelper.getResourceResolver();
                if (rr == null) {
                    throw new ServletException("Resource resolver is null");
                }
                String guideContainerPath = httpServletRequest.getParameter("guideContainerPath");
                if (StringUtils.isBlank((CharSequence)guideContainerPath)) {
                    throw new ServletException("Could not find guideContainerNode Path");
                }
                Resource aemFormContainer = rr.getResource(guideContainerPath);
                DataOptions options = new DataOptions();
                options.setFormResource(aemFormContainer);
                options.setDataRef(dataRef);
                options.setContentType(ContentType.XML);
                Map prefillServiceParamMap = (Map)httpServletRequest.getAttribute("paramMap");
                if (prefillServiceParamMap != null) {
                    options.setExtras(prefillServiceParamMap);
                }
                PrefillData prefillData = this.formDataProviderRegistry.getDataFromService(options);
                return db.parse(prefillData.getInputStream());
            }
            return db.parse(new ByteArrayInputStream("<?xml version=\"1.0\" ?><root/>".getBytes("UTF-8")));
        }
        catch (Exception e) {
            this.logger.error("Error creating data dom", (Throwable)e);
            throw new ServletException((Throwable)e);
        }
    }

    private ValueMap getGuideProperties(SlingHttpServletRequest request) {
        Resource guideContainerResource;
        String guideContainerPath = request.getParameter("guideContainerPath");
        if (guideContainerPath != null && !StringUtils.isEmpty((CharSequence)guideContainerPath) && (guideContainerResource = request.getResourceResolver().getResource(guideContainerPath)) != null) {
            return guideContainerResource.getValueMap();
        }
        return null;
    }

    private String getDDRef(ValueMap valueMap) {
        if (valueMap != null) {
            return (String)valueMap.get((Object)"ddRef");
        }
        return null;
    }

    private String getLetterRef(ValueMap valueMap) {
        if (valueMap != null) {
            return (String)valueMap.get((Object)"letterRef");
        }
        return null;
    }

    protected void bindResolverHelper(ResourceResolverHelper resourceResolverHelper) {
        this.resolverHelper = resourceResolverHelper;
    }

    protected void unbindResolverHelper(ResourceResolverHelper resourceResolverHelper) {
        if (this.resolverHelper == resourceResolverHelper) {
            this.resolverHelper = null;
        }
    }

    protected void bindFormDataProviderRegistry(FormDataProviderRegistry formDataProviderRegistry) {
        this.formDataProviderRegistry = formDataProviderRegistry;
    }

    protected void unbindFormDataProviderRegistry(FormDataProviderRegistry formDataProviderRegistry) {
        if (this.formDataProviderRegistry == formDataProviderRegistry) {
            this.formDataProviderRegistry = null;
        }
    }

    protected void bindGuideModuleImporter(GuideModuleImporter guideModuleImporter) {
        this.guideModuleImporter = guideModuleImporter;
    }

    protected void unbindGuideModuleImporter(GuideModuleImporter guideModuleImporter) {
        if (this.guideModuleImporter == guideModuleImporter) {
            this.guideModuleImporter = null;
        }
    }
}