JSONOAuthHttpClient.java
7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* 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;
}
}