CallResultsImpl.java 2.11 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.httpclient.Header
 *  org.apache.commons.httpclient.HttpMethod
 *  org.apache.commons.httpclient.URI
 *  org.apache.commons.httpclient.URIException
 */
package com.day.cq.mcm.campaign.impl;

import com.day.cq.mcm.campaign.CallResults;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.URIException;

public class CallResultsImpl
implements CallResults {
    private final HttpMethod method;
    private boolean isUsed;

    CallResultsImpl(HttpMethod method) {
        this.method = method;
        this.isUsed = false;
    }

    @Override
    public int getStatus() {
        return this.method.getStatusCode();
    }

    @Override
    public Map<String, String> getResponseHeaders() {
        Header[] headerArray;
        LinkedHashMap<String, String> headers = new LinkedHashMap<String, String>();
        for (Header header : headerArray = this.method.getResponseHeaders()) {
            headers.put(header.getName(), header.getValue());
        }
        return headers;
    }

    @Override
    public InputStream bodyAsStream() throws IOException {
        this.isUsed = true;
        return this.method.getResponseBodyAsStream();
    }

    @Override
    public String bodyAsString() throws IOException {
        this.isUsed = true;
        return this.method.getResponseBodyAsString();
    }

    @Override
    public void destroy() {
        if (!this.isUsed) {
            try {
                this.method.getResponseBody();
            }
            catch (IOException ioe) {
                // empty catch block
            }
        }
        this.method.releaseConnection();
    }

    @Override
    public String getCompleteURL() {
        try {
            return this.method.getURI().toString();
        }
        catch (URIException ue) {
            return "<URL not available>";
        }
    }
}