AbstractProxyServlet.java 3.34 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  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.CampaignException;
import com.day.cq.mcm.campaign.ConfigurationException;
import com.day.cq.mcm.campaign.ConnectionException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

abstract class AbstractProxyServlet
extends SlingAllMethodsServlet {
    private final Logger log;

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

    protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        try {
            this.performGet(request, response);
        }
        catch (ConfigurationException e) {
            this.log.info("Webservice configuration not found or invalid");
            response.sendError(500, e.getMessage());
        }
        catch (ConnectionException e) {
            this.log.warn("Could not connect to Adobe Campaign", (Throwable)e);
            response.sendError(500, e.getMessage());
        }
        catch (Exception e) {
            this.log.info("Internal error while proxying data; see log file.", (Throwable)e);
            response.sendError(500, e.getMessage());
        }
    }

    protected abstract void performGet(SlingHttpServletRequest var1, SlingHttpServletResponse var2) throws CampaignException, IOException, JSONException;

    protected void proxy(CampaignProxy proxy, String remoteFunction, Map<String, String> params, Resource resource, HttpServletResponse response) throws CampaignException {
        proxy.get("/jssp/nms/" + remoteFunction, params, resource, response);
    }

    protected void proxy(CampaignProxy proxy, String remoteFunction, String[] params, Resource resource, HttpServletResponse response) throws CampaignException {
        int paramCnt = params.length;
        if (paramCnt % 2 != 0) {
            throw new IllegalArgumentException("Invalid number of parameters; must be paired (key, value, key, value, ...)");
        }
        HashMap<String, String> paramMap = new HashMap<String, String>(paramCnt);
        for (int p = 0; p < paramCnt; p += 2) {
            paramMap.put(params[p], params[p + 1]);
        }
        this.proxy(proxy, remoteFunction, paramMap, resource, response);
    }

    protected void proxyWithBasicAuth(CampaignProxy proxy, String url, Resource resource, HttpServletResponse response) throws CampaignException {
        proxy.getWithBasicAuth(url, resource, response);
    }
}