forked from paypal/paypalhttp_java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpClient.java
208 lines (169 loc) · 6.63 KB
/
HttpClient.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.paypal.http;
import com.paypal.http.exceptions.HttpException;
import com.paypal.http.internal.TLSSocketFactory;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static com.paypal.http.serializer.StreamUtils.writeOutputStream;
import static java.net.HttpURLConnection.HTTP_OK;
import static java.net.HttpURLConnection.HTTP_PARTIAL;
public class HttpClient {
private SSLSocketFactory sslSocketFactory;
private String userAgent;
private int connectTimeout;
private int readTimeout;
private Environment environment;
private Encoder encoder;
List<Injector> mInjectors;
public HttpClient(Environment environment) {
this.readTimeout = (int) TimeUnit.SECONDS.toMillis(30);
this.connectTimeout = readTimeout;
this.userAgent = "Java HTTP/1.1"; // TODO: add version string to build.gradle
this.mInjectors = new ArrayList<>();
this.environment = environment;
this.encoder = new Encoder();
addInjector(this::injectStandardHeaders);
try {
sslSocketFactory = new TLSSocketFactory();
} catch (SSLException e) {
sslSocketFactory = null;
}
}
/**
* Override this method in a custom subclass to use a custom connect timeout value.
*/
protected int getConnectTimeout() { return connectTimeout; }
/**
* Override this method in a custom subclass to use a custom read timeout value.
*/
protected int getReadTimeout() { return readTimeout; }
/**
* Override this method in a custom subclass to use a SSLSocketFactory.
*/
protected SSLSocketFactory getSSLSocketFactory() { return sslSocketFactory; }
/**
* Override this method in a custom subclass to use a User Agent.
*/
protected String getUserAgent() { return userAgent; }
public Encoder getEncoder() { return encoder; }
protected Environment getEnvironment() { return environment; }
public void setSSLSocketFactory(SSLSocketFactory factory) { sslSocketFactory = factory; }
public void setUserAgent(String userAgent) { this.userAgent = userAgent; }
public void setConnectTimeout(int connectTimeout) { this.connectTimeout = connectTimeout; }
public void setReadTimeout(int readTimeout) { this.readTimeout = readTimeout; }
public synchronized void addInjector(Injector injector) {
if (injector != null) {
mInjectors.add(injector);
}
}
public <T> HttpResponse<T> execute(HttpRequest<T> request) throws IOException {
HttpRequest<T> requestCopy = request.copy();
for (Injector injector : mInjectors) {
injector.inject(requestCopy);
}
HttpURLConnection connection = getConnection(requestCopy);
try {
return parseResponse(connection, requestCopy.responseClass());
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
private void applyHeadersFromRequest(HttpURLConnection connection, HttpRequest request) {
for (String key: request.headers()) {
connection.setRequestProperty(key, request.headers().header(key));
}
}
HttpURLConnection getConnection(HttpRequest request) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(environment.baseUrl() + request.path()).openConnection();
if (connection instanceof HttpsURLConnection) {
if (sslSocketFactory == null) {
String message = "SSLSocketFactory was not set or failed to initialize";
System.out.println(message);
throw new SSLException(message);
}
((HttpsURLConnection) connection).setSSLSocketFactory(getSSLSocketFactory());
}
connection.setReadTimeout(getReadTimeout());
connection.setConnectTimeout(getConnectTimeout());
setRequestVerb(request.verb(), connection);
if (request.requestBody() != null) {
connection.setDoOutput(true);
byte[] data = encoder.serializeRequest(request);
applyHeadersFromRequest(connection, request);
writeOutputStream(connection.getOutputStream(), data);
} else {
applyHeadersFromRequest(connection, request);
}
return connection;
}
/**
* Workaround for a bug in {@code HttpURLConnection.setRequestMethod(String)}
* The implementation of Sun/Oracle is throwing a {@code ProtocolException}
* when the method is other than the HTTP/1.1 default methods. So to use {@code PATCH}
* and others, we must apply this workaround.
*
* See issue https://bugs.openjdk.java.net/browse/JDK-7016595
*/
private void setRequestVerb(String verb, HttpURLConnection connection) {
try {
connection.setRequestMethod(verb.toUpperCase());
} catch (ProtocolException ignored) {
try {
Field delegateField = connection.getClass().getDeclaredField("delegate");
delegateField.setAccessible(true);
HttpURLConnection delegateConnection = (HttpURLConnection) delegateField.get(connection);
setRequestVerb(verb, delegateConnection);
} catch (NoSuchFieldException e) {
Field methodField = null;
Class connectionClass = connection.getClass();
while (methodField == null) {
try {
methodField = connectionClass.getDeclaredField("method");
methodField.setAccessible(true);
methodField.set(connection, "PATCH");
} catch (IllegalAccessException | NoSuchFieldException _ignored) {
connectionClass = connectionClass.getSuperclass();
}
}
} catch (IllegalAccessException ignoredIllegalAccess) {}
}
}
Headers parseResponseHeaders(URLConnection connection) {
Headers headers = new Headers();
for (String key : connection.getHeaderFields().keySet()) {
headers.header(key, connection.getHeaderField(key));
}
return headers;
}
private <T> HttpResponse<T> parseResponse(HttpURLConnection connection, Class<T> responseClass) throws IOException {
Headers responseHeaders = parseResponseHeaders(connection);
String responseBody;
int statusCode;
statusCode = connection.getResponseCode();
if (statusCode >= HTTP_OK && statusCode <= HTTP_PARTIAL) {
T deserializedResponse = null;
if (!Void.class.isAssignableFrom(responseClass)) {
deserializedResponse = encoder.deserializeResponse(connection.getInputStream(), responseClass, responseHeaders);
}
return new HttpResponse<>(responseHeaders, statusCode, deserializedResponse);
} else {
responseBody = encoder.deserializeResponse(connection.getErrorStream(), String.class, responseHeaders);
throw new HttpException(responseBody, statusCode, responseHeaders);
}
}
private void injectStandardHeaders(HttpRequest request) throws IOException {
request.headers()
.headerIfNotPresent(Headers.USER_AGENT, getUserAgent());
}
}