DecompressingMethod.java 9.02 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.httpclient.Header
 *  org.apache.commons.httpclient.HeaderElement
 *  org.apache.commons.httpclient.HostConfiguration
 *  org.apache.commons.httpclient.HttpConnection
 *  org.apache.commons.httpclient.HttpException
 *  org.apache.commons.httpclient.HttpMethod
 *  org.apache.commons.httpclient.HttpState
 *  org.apache.commons.httpclient.NameValuePair
 *  org.apache.commons.httpclient.StatusLine
 *  org.apache.commons.httpclient.URI
 *  org.apache.commons.httpclient.URIException
 *  org.apache.commons.httpclient.auth.AuthState
 *  org.apache.commons.httpclient.params.HttpMethodParams
 *  org.apache.commons.io.IOUtils
 */
package com.day.cq.searchpromote.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.zip.GZIPInputStream;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HeaderElement;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.StatusLine;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.auth.AuthState;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;

public class DecompressingMethod
implements HttpMethod {
    private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
    private static final Header DEFAULT_HEADER = new Header("Accept-Encoding", "gzip;q=1.0, *;q=0");
    private final HttpMethod decorated;

    public DecompressingMethod(HttpMethod decorated) {
        this.decorated = decorated;
    }

    public byte[] getResponseBody() throws IOException {
        byte[] raw = this.decorated.getResponseBody();
        if (this.isGzipped()) {
            ByteArrayOutputStream decompressed = new ByteArrayOutputStream();
            IOUtils.copy((InputStream)new GZIPInputStream(new ByteArrayInputStream(raw)), (OutputStream)decompressed);
            return decompressed.toByteArray();
        }
        return raw;
    }

    public InputStream getResponseBodyAsStream() throws IOException {
        return this.decorated.getResponseBodyAsStream();
    }

    public String getResponseBodyAsString() throws IOException {
        return IOUtils.toString((InputStream)new ByteArrayInputStream(this.getResponseBody()), (String)this.getResponseEncoding());
    }

    public int execute(HttpState httpState, HttpConnection httpConnection) throws HttpException, IOException {
        this.decorated.setRequestHeader(DEFAULT_HEADER);
        return this.decorated.execute(httpState, httpConnection);
    }

    public Header[] getRequestHeaders() {
        return this.getDecompressingRequestHeaders();
    }

    public Header[] getRequestHeaders(String s) {
        ArrayList<Header> headers = new ArrayList<Header>();
        if (s != null) {
            for (Header header : this.getDecompressingRequestHeaders()) {
                if (!s.equalsIgnoreCase(header.getName())) continue;
                headers.add(header);
            }
        }
        return headers.toArray((T[])new Header[headers.size()]);
    }

    public Header getRequestHeader(String s) {
        if (s != null) {
            for (Header header : this.getDecompressingRequestHeaders()) {
                if (!s.equalsIgnoreCase(header.getName())) continue;
                return header;
            }
        }
        return null;
    }

    public String getName() {
        return "DecompressingMethod";
    }

    private Header[] getDecompressingRequestHeaders() {
        ArrayList<Header> headers = new ArrayList<Header>(Arrays.asList(this.decorated.getRequestHeaders()));
        for (Header header : headers) {
            if (!"Accept-Encoding".equalsIgnoreCase(header.getName())) continue;
            headers.remove((Object)header);
        }
        headers.add(DEFAULT_HEADER);
        return headers.toArray((T[])new Header[headers.size()]);
    }

    private boolean isGzipped() {
        Header ceh = this.decorated.getResponseHeader("Content-Encoding");
        if (ceh != null) {
            for (HeaderElement he : ceh.getElements()) {
                String elt = he.getName().toLowerCase(Locale.US);
                if (!"gzip".equalsIgnoreCase(elt) && !"x-gzip".equalsIgnoreCase(elt)) continue;
                return true;
            }
        }
        return false;
    }

    private String getResponseEncoding() {
        int charsetIndex;
        String defaultEncoding = "8859_1";
        Header hdr = this.decorated.getResponseHeader("Content-Type");
        if (hdr != null && hdr.getValue() != null && (charsetIndex = hdr.getValue().indexOf("charset=")) != -1) {
            return hdr.getValue().substring(charsetIndex + "charset=".length()).trim();
        }
        return "8859_1";
    }

    public void setRequestHeader(String s, String s1) {
        this.decorated.setRequestHeader(s, s1);
    }

    public void setRequestHeader(Header header) {
        this.decorated.setRequestHeader(header);
    }

    public void addRequestHeader(String s, String s1) {
        this.decorated.addRequestHeader(s, s1);
    }

    public void addRequestHeader(Header header) {
        this.decorated.addRequestHeader(header);
    }

    public void removeRequestHeader(String s) {
        this.decorated.removeRequestHeader(s);
    }

    public void removeRequestHeader(Header header) {
        this.decorated.removeRequestHeader(header);
    }

    public Header[] getResponseHeaders() {
        return this.decorated.getResponseHeaders();
    }

    public Header getResponseHeader(String s) {
        return this.decorated.getResponseHeader(s);
    }

    public Header[] getResponseHeaders(String s) {
        return this.decorated.getResponseHeaders(s);
    }

    public boolean hasBeenUsed() {
        return this.decorated.hasBeenUsed();
    }

    public void abort() {
        this.decorated.abort();
    }

    @Deprecated
    public void recycle() {
        this.decorated.recycle();
    }

    public void releaseConnection() {
        this.decorated.releaseConnection();
    }

    public void addResponseFooter(Header header) {
        this.decorated.addResponseFooter(header);
    }

    public StatusLine getStatusLine() {
        return this.decorated.getStatusLine();
    }

    public boolean getDoAuthentication() {
        return this.decorated.getDoAuthentication();
    }

    public void setDoAuthentication(boolean b) {
        this.decorated.setDoAuthentication(b);
    }

    public HttpMethodParams getParams() {
        return this.decorated.getParams();
    }

    public void setParams(HttpMethodParams httpMethodParams) {
        this.decorated.setParams(httpMethodParams);
    }

    public AuthState getHostAuthState() {
        return this.decorated.getHostAuthState();
    }

    public AuthState getProxyAuthState() {
        return this.decorated.getProxyAuthState();
    }

    public boolean isRequestSent() {
        return this.decorated.isRequestSent();
    }

    @Deprecated
    public HostConfiguration getHostConfiguration() {
        return this.decorated.getHostConfiguration();
    }

    public void setPath(String s) {
        this.decorated.setPath(s);
    }

    public String getPath() {
        return this.decorated.getPath();
    }

    public URI getURI() throws URIException {
        return this.decorated.getURI();
    }

    public void setURI(URI uri) throws URIException {
        this.decorated.setURI(uri);
    }

    @Deprecated
    public void setStrictMode(boolean b) {
        this.decorated.setStrictMode(b);
    }

    @Deprecated
    public boolean isStrictMode() {
        return this.decorated.isStrictMode();
    }

    public boolean getFollowRedirects() {
        return this.decorated.getFollowRedirects();
    }

    public void setFollowRedirects(boolean b) {
        this.decorated.setFollowRedirects(b);
    }

    public void setQueryString(String s) {
        this.decorated.setQueryString(s);
    }

    public void setQueryString(NameValuePair[] nameValuePairs) {
        this.decorated.setQueryString(nameValuePairs);
    }

    public String getQueryString() {
        return this.decorated.getQueryString();
    }

    public boolean validate() {
        return this.decorated.validate();
    }

    public int getStatusCode() {
        return this.decorated.getStatusCode();
    }

    public String getStatusText() {
        return this.decorated.getStatusText();
    }

    public Header[] getResponseFooters() {
        return this.decorated.getResponseFooters();
    }

    public Header getResponseFooter(String s) {
        return this.decorated.getResponseFooter(s);
    }
}