JSONOAuthHttpClient.java 7.1 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.http.Header
 *  org.apache.http.HttpException
 *  org.apache.http.HttpResponse
 *  org.apache.http.StatusLine
 *  org.apache.http.client.HttpClient
 *  org.apache.http.client.config.RequestConfig
 *  org.apache.http.client.config.RequestConfig$Builder
 *  org.apache.http.client.methods.HttpGet
 *  org.apache.http.client.methods.HttpRequestBase
 *  org.apache.http.client.methods.HttpUriRequest
 *  org.apache.http.config.SocketConfig
 *  org.apache.http.config.SocketConfig$Builder
 *  org.apache.http.conn.HttpClientConnectionManager
 *  org.apache.http.impl.client.CloseableHttpClient
 *  org.apache.http.impl.client.HttpClientBuilder
 *  org.apache.http.impl.conn.PoolingHttpClientConnectionManager
 *  org.apache.http.osgi.services.HttpClientBuilderFactory
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.aam.client;

import com.adobe.cq.aam.client.AudienceManagerClientImpl;
import com.adobe.cq.aam.client.OAuthResponseImpl;
import com.adobe.cq.aam.client.spi.AudienceManagerAccessDenied;
import com.adobe.cq.aam.client.spi.HCCookieStore;
import java.io.IOException;
import java.net.URI;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JSONOAuthHttpClient {
    private static final int READ_TIMEOUT_MS = 120000;
    private static final int CONNECTION_TIMEOUT_MS = 10000;
    private static final int TOTAL_CONNECTIONS = 50;
    private static final int CONNECTIONS_PER_HOST = 25;
    private static final int SO_LINGER_SECONDS = 5;
    private static final int SLOW_RESPONSE_LOG = 2000;
    private static final Logger LOGGER = LoggerFactory.getLogger(JSONOAuthHttpClient.class);
    private HttpClient httpClient;
    private AudienceManagerClientImpl.MethodFactory mf;

    public JSONOAuthHttpClient(HttpClientBuilderFactory httpClientBuilderFactory, AudienceManagerClientImpl.MethodFactory methodFactory) {
        this.mf = methodFactory;
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(50);
        cm.setDefaultMaxPerRoute(25);
        RequestConfig rc = RequestConfig.custom().setCookieSpec("ignoreCookies").setConnectTimeout(10000).setStaleConnectionCheckEnabled(true).build();
        SocketConfig sc = SocketConfig.custom().setSoLinger(5).setSoTimeout(120000).build();
        this.httpClient = httpClientBuilderFactory.newBuilder().setConnectionManager((HttpClientConnectionManager)cm).setDefaultRequestConfig(rc).setDefaultSocketConfig(sc).disableRedirectHandling().build();
    }

    public <T extends HttpRequestBase> T prepareMethod(OAuthResponseImpl aAuthResponse, T method) {
        method.addHeader("Authorization", aAuthResponse.getBearerAuth());
        LOGGER.debug("Authorization header [{}]", (Object)aAuthResponse.getBearerAuth());
        method.addHeader("Accept", "application/json");
        return method;
    }

    public <T> T executeJSONObject(HttpRequestBase method, int expectedCode, Class<T> type) throws IOException, AudienceManagerAccessDenied {
        try {
            return this.executeJSONObject(method, expectedCode, type, null);
        }
        catch (HttpException e) {
            LOGGER.error(e.getMessage(), (Throwable)e);
            return null;
        }
    }

    /*
     * Exception decompiling
     */
    public <T> T executeJSONObject(HttpRequestBase method, int expectedCode, Class<T> type, HCCookieStore cookieStore) throws IOException, AudienceManagerAccessDenied, HttpException {
        // This method has failed to decompile.  When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
        // java.lang.ClassCastException: org.benf.cfr.reader.bytecode.analysis.parse.statement.Nop cannot be cast to org.benf.cfr.reader.bytecode.analysis.parse.statement.TryStatement
        // org.benf.cfr.reader.bytecode.analysis.parse.utils.finalhelp.FinalAnalyzer.identifyFinally(FinalAnalyzer.java:163)
        // org.benf.cfr.reader.bytecode.analysis.opgraph.op3rewriters.FinallyRewriter.identifyFinally(FinallyRewriter.java:40)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:483)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:217)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:162)
        // org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:95)
        // org.benf.cfr.reader.entities.Method.analyse(Method.java:355)
        // org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:768)
        // org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:700)
        // org.benf.cfr.reader.Main.doJar(Main.java:134)
        // org.benf.cfr.reader.Main.main(Main.java:189)
        throw new IllegalStateException("Decompilation failed");
    }

    private HttpResponse executeGet(HttpRequestBase method, HCCookieStore cookieStore) throws IOException {
        if (cookieStore != null) {
            method.setHeader(cookieStore.getCookieHeader());
        }
        method.setHeader("Accept", "application/json");
        method.addHeader("Connection", "close");
        LOGGER.debug("Executing {} ", (Object)method.getURI().toString());
        HttpResponse httpResp = this.httpClient.execute((HttpUriRequest)method);
        LOGGER.debug("Done Executing {} response {} ", (Object)method.getURI().toString(), (Object)httpResp);
        int status = 0;
        if (httpResp != null) {
            status = httpResp.getStatusLine().getStatusCode();
        }
        if (status == 302) {
            if (cookieStore != null) {
                method.setHeader(httpResp.getHeaders("Set-Cookie")[0]);
                cookieStore.update(method);
                cookieStore.save();
            }
            String location = httpResp.getHeaders("Location")[0].getValue();
            method.releaseConnection();
            return this.executeGet((HttpRequestBase)this.mf.getGetMethod(location), cookieStore);
        }
        if (status >= 200 && status < 400) {
            if (cookieStore != null) {
                cookieStore.update(method);
                cookieStore.save();
            }
            return httpResp;
        }
        LOGGER.warn("AAM Server response for :" + status + " " + httpResp.getStatusLine().toString() + " for " + method.getURI().toString());
        return httpResp;
    }
}