TemplateListServlet.java 9.14 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  javax.servlet.http.HttpServletResponse
 *  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.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.mcm.campaign.servlets;

import com.adobe.cq.mcm.campaign.CampaignProxy;
import com.day.cq.mcm.campaign.CallResults;
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.GenericCampaignConnector;
import com.day.cq.mcm.campaign.servlets.AbstractProxyServlet;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
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.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(selectors={"campaign.templates"}, resourceTypes={"mcm/campaign/components/newsletter", "mcm/campaign/components/campaign_newsletterpage", "mcm/campaign/components/profile"}, extensions={"json"}, methods={"GET"})
public class TemplateListServlet
extends AbstractProxyServlet {
    private final Logger log;
    private static final String SRC_PRM_QUERY = "query";
    private static final String SRC_PRM_START = "start";
    private static final String SRC_PRM_LIMIT = "limit";
    private static final String SRC_PRM_URL = "url";
    @Reference
    private GenericCampaignConnector connector;
    @Reference
    private CampaignProxy proxy;

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

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private int determineTotalTemplates(String mappingId, String queryParam, CampaignCredentials credentials) throws CampaignException, IOException, JSONException {
        int totalTemplates;
        HashMap<String, String> parameters = new HashMap<String, String>();
        if (queryParam != null) {
            parameters.put("searchTerm", queryParam);
        }
        CallResults callResults = null;
        try {
            callResults = this.connector.callFunction("amcGetDeliveryTemplates.jssp", parameters, credentials);
            String jsonStr = callResults.bodyAsString();
            JSONObject parsedJSON = new JSONObject(jsonStr);
            JSONArray templates = parsedJSON.getJSONArray("templates");
            totalTemplates = templates.length();
        }
        finally {
            if (callResults != null) {
                callResults.destroy();
            }
        }
        return totalTemplates;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void doUrlBasedAccess(SlingHttpServletRequest request, SlingHttpServletResponse response, CampaignCredentials credentials) throws CampaignException, IOException, JSONException {
        String outputJSON;
        String url = request.getParameter("url");
        int qsPos = url.indexOf("?");
        String path = url;
        String queryString = null;
        if (qsPos > 0) {
            path = url.substring(0, qsPos);
            queryString = url.substring(qsPos + 1);
        }
        CallResults callResults = null;
        try {
            callResults = this.connector.callGenericWithBasicAuth(path, queryString, credentials);
            String jsonStr = callResults.bodyAsString();
            JSONObject parsedJSON = new JSONObject(jsonStr);
            if (!parsedJSON.has("templates")) {
                JSONArray templates = parsedJSON.getJSONArray("content");
                parsedJSON.remove("content");
                parsedJSON.put("templates", (Object)templates);
            }
            outputJSON = parsedJSON.toString();
        }
        finally {
            if (callResults != null) {
                callResults.destroy();
            }
        }
        PrintWriter out = response.getWriter();
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        out.print(outputJSON);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    protected void performGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws CampaignException, IOException, JSONException {
        block16 : {
            Resource resource = request.getResource();
            Configuration config = this.connector.getWebserviceConfig(resource);
            CampaignCredentials credentials = this.connector.retrieveCredentials(config);
            ValueMap values = ResourceUtil.getValueMap((Resource)resource);
            String mappingId = (String)values.get("acMapping", String.class);
            if (mappingId == null) {
                throw new ConfigurationException("Missing mapping ID on newsletter");
            }
            String url = request.getParameter("url");
            if (url != null) {
                this.doUrlBasedAccess(request, response, credentials);
                return;
            }
            String queryParam = request.getParameter("query");
            String startParam = request.getParameter("start");
            String limitParam = request.getParameter("limit");
            HashMap<String, String> parameters = new HashMap<String, String>();
            if (queryParam != null) {
                parameters.put("searchTerm", queryParam);
            }
            if (startParam != null) {
                parameters.put("startLine", startParam);
            }
            if (limitParam != null) {
                parameters.put("lineCount", limitParam);
            }
            CallResults callResults = null;
            try {
                boolean isPagingAvailable;
                if (limitParam == null) {
                    this.proxy(this.proxy, "amcGetDeliveryTemplates.jssp", parameters, resource, (HttpServletResponse)response);
                    return;
                }
                callResults = this.connector.callFunction("amcGetDeliveryTemplates.jssp", parameters, credentials);
                String jsonStr = callResults.bodyAsString();
                JSONObject parsedJSON = new JSONObject(jsonStr);
                JSONArray templates = parsedJSON.getJSONArray("templates");
                boolean bl = isPagingAvailable = !parsedJSON.has("next");
                if (isPagingAvailable) {
                    int startItem = Integer.parseInt(startParam);
                    int pageSize = Integer.parseInt(limitParam);
                    int pageTemplates = templates.length();
                    int totalTemplates = startItem + pageTemplates;
                    if (pageTemplates == pageSize) {
                        totalTemplates = this.determineTotalTemplates(mappingId, queryParam, credentials);
                    }
                    parsedJSON.put("total", totalTemplates);
                } else if (parsedJSON.isNull("next")) {
                    parsedJSON.put("next", (Object)new JSONObject());
                }
                String finalJSON = parsedJSON.toString();
                if (finalJSON != null) {
                    PrintWriter out = response.getWriter();
                    response.setContentType("application/json");
                    response.setCharacterEncoding("utf-8");
                    out.print(finalJSON);
                    break block16;
                }
                throw new CampaignException("Internal error processing JSON");
            }
            finally {
                if (callResults != null) {
                    callResults.destroy();
                }
            }
        }
    }

    protected void bindConnector(GenericCampaignConnector genericCampaignConnector) {
        this.connector = genericCampaignConnector;
    }

    protected void unbindConnector(GenericCampaignConnector genericCampaignConnector) {
        if (this.connector == genericCampaignConnector) {
            this.connector = null;
        }
    }

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

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