CampaignSeedDataServlet.java 7.06 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  javax.servlet.ServletOutputStream
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.io.IOUtils
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  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.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.mcm.campaign.servlets;

import com.adobe.cq.mcm.campaign.CampaignProxy;
import com.adobe.cq.mcm.campaign.NewsletterException;
import com.adobe.cq.mcm.campaign.NewsletterManager;
import com.day.cq.mcm.campaign.CallResults;
import com.day.cq.mcm.campaign.CampaignConnector;
import com.day.cq.mcm.campaign.CampaignCredentials;
import com.day.cq.mcm.campaign.CampaignException;
import com.day.cq.mcm.campaign.ConfigurationException;
import com.day.cq.mcm.campaign.ConnectionException;
import com.day.cq.mcm.campaign.servlets.AbstractProxyServlet;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
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.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(selectors={"campaign.seeddata"}, resourceTypes={"mcm/campaign/components/newsletter", "mcm/campaign/components/campaign_newsletterpage", "mcm/campaign/components/profile"}, extensions={"json"}, methods={"GET"})
public class CampaignSeedDataServlet
extends AbstractProxyServlet {
    private static final String PRM_TEMPLATE = "template";
    private static final String PRM_DELIVERY = "delivery";
    private static final String PRM_ID = "id";
    private final Logger log;
    @Reference
    private CampaignProxy proxy;
    @Reference
    private CampaignConnector connector;
    @Reference
    private NewsletterManager manager;

    public CampaignSeedDataServlet() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    @Override
    protected void performGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws CampaignException {
        Resource resource = request.getResource();
        Page page = (Page)resource.getParent().adaptTo(Page.class);
        Configuration config = this.connector.getWebserviceConfig(resource);
        CampaignCredentials credentials = this.connector.retrieveCredentials(config);
        ValueMap values = ResourceUtil.getValueMap((Resource)resource);
        String templateId = (String)values.get("acTemplateId", String.class);
        if (templateId == null) {
            throw new ConfigurationException("Missing template ID on newsletter");
        }
        String id = request.getRequestPathInfo().getSuffix();
        if (id == null || id.length() < 2) {
            throw new CampaignException("Invalid or no ID suffix");
        }
        id = id.substring(1);
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("template", templateId);
        params.put("id", id);
        String[] links = null;
        if (this.manager.isNewsletter(page)) {
            try {
                links = this.manager.getLinkedDeliveries(page);
            }
            catch (NewsletterException e) {
                throw new CampaignException("Error getting the linked deliveries");
            }
        }
        if (links != null) {
            for (String deliveryId : links) {
                this.log.debug("Trying delivery '{}'.", (Object)deliveryId);
                params.put("delivery", deliveryId);
                if (!this.checkAndProcess(params, credentials, (HttpServletResponse)response, this.connector)) continue;
                this.log.debug("Success; using meta data of delivery '{}'.", (Object)deliveryId);
                return;
            }
        }
        this.proxy(this.proxy, "amcGetSeedData.jssp", new String[]{"template", templateId, "delivery", templateId, "id", id}, resource, (HttpServletResponse)response);
    }

    private boolean checkAndProcess(Map<String, String> params, CampaignCredentials credentials, HttpServletResponse response, CampaignConnector connector) throws CampaignException {
        InputStream is = null;
        CallResults callResults = null;
        try {
            callResults = connector.callFunction("amcGetSeedData.jssp", params, credentials);
            Map<String, String> headers = callResults.getResponseHeaders();
            for (String name2 : headers.keySet()) {
                response.addHeader(name2, headers.get(name2));
            }
            is = callResults.bodyAsStream();
            ServletOutputStream os = response.getOutputStream();
            IOUtils.copy((InputStream)is, (OutputStream)os);
            boolean name2 = true;
            return name2;
        }
        catch (ConnectionException ce) {
            if (ce.getStatusCode() != 404) {
                if (ce.getStatusCode() == 500) {
                    throw new CampaignException("Remote error", ce);
                }
                throw new CampaignException("Caught exception while writing response", ce);
            }
        }
        catch (IOException ioe) {
            throw new CampaignException("Caught exception while writing response", ioe);
        }
        finally {
            IOUtils.closeQuietly((InputStream)is);
            if (callResults != null) {
                callResults.destroy();
            }
        }
        return false;
    }

    protected void bindProxy(CampaignProxy campaignProxy) {
        this.proxy = campaignProxy;
    }

    protected void unbindProxy(CampaignProxy campaignProxy) {
        if (this.proxy == campaignProxy) {
            this.proxy = null;
        }
    }

    protected void bindConnector(CampaignConnector campaignConnector) {
        this.connector = campaignConnector;
    }

    protected void unbindConnector(CampaignConnector campaignConnector) {
        if (this.connector == campaignConnector) {
            this.connector = null;
        }
    }

    protected void bindManager(NewsletterManager newsletterManager) {
        this.manager = newsletterManager;
    }

    protected void unbindManager(NewsletterManager newsletterManager) {
        if (this.manager == newsletterManager) {
            this.manager = null;
        }
    }
}