CampaignProxyImpl.java 6.77 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  javax.servlet.ServletOutputStream
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.io.IOUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 */
package com.adobe.cq.mcm.campaign.profile.impl;

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.GenericCampaignConnector;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;

@Component(metatype=0)
@Service
public class CampaignProxyImpl
implements CampaignProxy {
    @Reference
    protected GenericCampaignConnector connector;

    private CampaignCredentials getCredentials(Configuration config) throws CampaignException {
        return this.connector.retrieveCredentials(config);
    }

    private CampaignCredentials getCredentials(Resource resource) throws CampaignException {
        Configuration config = this.connector.getWebserviceConfig(resource);
        return this.getCredentials(config);
    }

    @Override
    public void get(String remoteFunction, Map<String, String> params, CampaignCredentials credentials, HttpServletResponse response) throws CampaignException {
        InputStream is = null;
        CallResults callResults = null;
        try {
            callResults = this.connector.callGeneric(remoteFunction, params, credentials);
            Map<String, String> headers = callResults.getResponseHeaders();
            for (String name : headers.keySet()) {
                response.addHeader(name, headers.get(name));
            }
            is = callResults.bodyAsStream();
            ServletOutputStream os = response.getOutputStream();
            IOUtils.copy((InputStream)is, (OutputStream)os);
        }
        catch (IOException ioe) {
            throw new CampaignException("Caught exception while writing response", ioe);
        }
        finally {
            IOUtils.closeQuietly((InputStream)is);
            if (callResults != null) {
                callResults.destroy();
            }
        }
    }

    @Override
    public void get(String remoteFunction, Map<String, String> params, Resource resource, HttpServletResponse response) throws CampaignException {
        this.get(remoteFunction, params, this.getCredentials(resource), response);
    }

    @Override
    public void get(String remoteFunction, Map<String, String> params, Configuration config, HttpServletResponse response) throws CampaignException {
        this.get(remoteFunction, params, this.getCredentials(config), response);
    }

    @Override
    public void post(String remoteFunction, Map<String, String> postData, CampaignCredentials credentials, HttpServletResponse response) throws CampaignException {
        InputStream is = null;
        CallResults callResults = null;
        try {
            callResults = this.connector.postGeneric(remoteFunction, postData, credentials);
            Map<String, String> headers = callResults.getResponseHeaders();
            for (String name : headers.keySet()) {
                response.addHeader(name, headers.get(name));
            }
            is = callResults.bodyAsStream();
            ServletOutputStream os = response.getOutputStream();
            IOUtils.copy((InputStream)is, (OutputStream)os);
        }
        catch (IOException ioe) {
            throw new CampaignException("Caught exception while writing response", ioe);
        }
        finally {
            IOUtils.closeQuietly((InputStream)is);
            if (callResults != null) {
                callResults.destroy();
            }
        }
    }

    @Override
    public void post(String remoteFunction, Map<String, String> postData, Resource resource, HttpServletResponse response) throws CampaignException {
        this.post(remoteFunction, postData, this.getCredentials(resource), response);
    }

    @Override
    public void post(String remoteFunction, Map<String, String> postData, Configuration config, HttpServletResponse response) throws CampaignException {
        this.post(remoteFunction, postData, this.getCredentials(config), response);
    }

    @Override
    public void getWithBasicAuth(String url, CampaignCredentials credentials, HttpServletResponse response) throws CampaignException {
        int qsPos = url.indexOf("?");
        String path = url;
        String queryString = null;
        if (qsPos > 0) {
            path = url.substring(0, qsPos);
            queryString = url.substring(qsPos + 1);
        }
        InputStream is = null;
        CallResults callResults = null;
        try {
            callResults = this.connector.callGenericWithBasicAuth(path, queryString, credentials);
            Map<String, String> headers = callResults.getResponseHeaders();
            for (String name : headers.keySet()) {
                response.addHeader(name, headers.get(name));
            }
            is = callResults.bodyAsStream();
            ServletOutputStream os = response.getOutputStream();
            IOUtils.copy((InputStream)is, (OutputStream)os);
        }
        catch (IOException ioe) {
            throw new CampaignException("Caught exception while writing response", ioe);
        }
        finally {
            IOUtils.closeQuietly((InputStream)is);
            if (callResults != null) {
                callResults.destroy();
            }
        }
    }

    @Override
    public void getWithBasicAuth(String url, Resource resource, HttpServletResponse response) throws CampaignException {
        this.getWithBasicAuth(url, this.getCredentials(resource), response);
    }

    @Override
    public void getWithBasicAuth(String url, Configuration config, HttpServletResponse response) throws CampaignException {
        this.getWithBasicAuth(url, this.getCredentials(config), response);
    }

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

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