forked from strimzi/strimzi-kafka-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpUtil.java
566 lines (516 loc) · 29.1 KB
/
HttpUtil.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
* Copyright 2017-2019, Strimzi authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.strimzi.kafka.oauth.common;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;
import static io.strimzi.kafka.oauth.common.ConfigUtil.getConnectTimeout;
import static io.strimzi.kafka.oauth.common.ConfigUtil.getReadTimeout;
import static io.strimzi.kafka.oauth.common.IOUtil.copy;
/**
* A helper class that performs all network calls using java.net.HttpURLConnection.
* <p>
* If application uses many concurrent threads initiating many Kafka sessions in parallel, consider setting
* 'http.maxConnections' system property to value closer to the number of parallel sessions.
* <p>
* This value controls the size of internal connection pool per destination in JDK's java.net.HttpURLConnection implementation.
* <p>
* See: https://docs.oracle.com/javase/8/docs/technotes/guides/net/http-keepalive.html
* <p>
* By default the connect timeout and read timeout are set to 60 seconds. Use system properties <em>oauth.connect.timeout.seconds</em>
* and <em>oauth.read.timeout.seconds</em>, or corresponding env variables to set custom timeouts in seconds.
*/
public class HttpUtil {
private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
static final int DEFAULT_CONNECT_TIMEOUT = getConnectTimeout(new Config());
static final int DEFAULT_READ_TIMEOUT = getReadTimeout(new Config());
/**
* A helper method that implements logic for retrying unsuccessful HTTP requests
*
* @param retries a maximum number of retries to attempt
* @param retryPauseMillis a pause between two consecutive retries in millis
* @param metricsHandler a metrics handler to track request times and failures
* @param task the task to run with retries
* @return The result of the successfully completed task
*
* @param <T> Generic type of the result type of the HttpTask
* @throws ExecutionException The exception thrown if the last retry still failed, wrapping the cause exception
*/
public static <T> T doWithRetries(int retries, long retryPauseMillis, MetricsHandler metricsHandler, HttpTask<T> task) throws ExecutionException {
if (retries < 0) {
throw new IllegalArgumentException("retries can't be negative");
}
retryPauseMillis = retryPauseMillis < 0 ? 0 : retryPauseMillis;
Exception exception;
int i = 0;
do {
i++;
if (i > 1 && retryPauseMillis > 0) {
log.debug("Pausing before retrying failed action (for {}ms)", retryPauseMillis);
try {
Thread.sleep(retryPauseMillis);
} catch (InterruptedException e) {
throw new ExecutionException("Interrupted while pausing", e);
}
}
try {
if (i > 1) {
log.debug("Request attempt no. {}", i);
}
long requestStartTime = System.currentTimeMillis();
try {
T result = task.run();
if (metricsHandler != null) {
metricsHandler.addSuccessRequestTime(System.currentTimeMillis() - requestStartTime);
}
return result;
} catch (Throwable t) {
if (metricsHandler != null) {
metricsHandler.addErrorRequestTime(t, System.currentTimeMillis() - requestStartTime);
}
throw t;
}
} catch (Exception e) {
exception = e;
log.info("Action failed on try no. {}", i, e);
}
} while (i <= retries);
String msg = "Action failed after " + i + " tries";
log.debug(msg);
throw new ExecutionException(msg, exception);
}
/**
* Perform HTTP GET request and return the response in the specified type.
*
* @param uri The target url
* @param authorization The Authorization header value
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static <T> T get(URI uri, String authorization, Class<T> responseType) throws IOException {
return request(uri, null, null, authorization, null, null, responseType);
}
/**
* Perform HTTP GET request and return the response in the specified type.
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param authorization The Authorization header value
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static <T> T get(URI uri, SSLSocketFactory socketFactory, String authorization, Class<T> responseType) throws IOException {
return request(uri, socketFactory, null, authorization, null, null, responseType);
}
/**
* Perform HTTP GET request and return the response in the specified type.
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param hostnameVerifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static <T> T get(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, Class<T> responseType) throws IOException {
return request(uri, socketFactory, hostnameVerifier, authorization, null, null, responseType);
}
/**
* Perform HTTP GET request and return the response in the specified type.
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param hostnameVerifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @param connectTimeout Connect timeout in seconds
* @param readTimeout Read timeout in seconds
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static <T> T get(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, Class<T> responseType, int connectTimeout, int readTimeout) throws IOException {
return request(uri, "GET", socketFactory, hostnameVerifier, authorization, null, null, responseType, connectTimeout, readTimeout, true);
}
/**
* Perform HTTP GET request and return the response in the specified type.
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param hostnameVerifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @param connectTimeout Connect timeout in seconds
* @param readTimeout Read timeout in seconds
* @param includeAcceptHeader Determines if <code>Accept application/json</code> is sent to the remote server.
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static <T> T get(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, Class<T> responseType, int connectTimeout, int readTimeout, boolean includeAcceptHeader) throws IOException {
return request(uri, "GET", socketFactory, hostnameVerifier, authorization, null, null, responseType, connectTimeout, readTimeout, includeAcceptHeader);
}
/**
* Perform HTTP POST request and return the response in the specified type.
*
* @param uri The target url
* @param authorization The Authorization header value
* @param contentType MIME type of the request body
* @param body The request body
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static <T> T post(URI uri, String authorization, String contentType, String body, Class<T> responseType) throws IOException {
return request(uri, null, null, authorization, contentType, body, responseType);
}
/**
* Perform HTTP POST request and return the response in the specified type.
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param authorization The Authorization header value
* @param contentType MIME type of the request body
* @param body The request body
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static <T> T post(URI uri, SSLSocketFactory socketFactory, String authorization, String contentType, String body, Class<T> responseType) throws IOException {
return request(uri, socketFactory, null, authorization, contentType, body, responseType);
}
/**
* Perform HTTP POST request and return the response in the specified type.
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param verifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @param contentType MIME type of the request body
* @param body The request body
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static <T> T post(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body, Class<T> responseType) throws IOException {
return request(uri, socketFactory, verifier, authorization, contentType, body, responseType);
}
/**
* Perform HTTP POST request and return the response in the specified type.
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param verifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @param contentType MIME type of the request body
* @param body The request body
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @param connectTimeout Connect timeout in seconds
* @param readTimeout Read timeout in seconds
* @param includeAcceptHeader TODO
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static <T> T post(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body, Class<T> responseType, int connectTimeout, int readTimeout, boolean includeAcceptHeader) throws IOException {
return request(uri, "POST", socketFactory, verifier, authorization, contentType, body, responseType, connectTimeout, readTimeout, includeAcceptHeader);
}
/**
* Perform HTTP PUT request
*
* @param uri The target url
* @param authorization The Authorization header value
* @param contentType MIME type of the request body
* @param body The request body
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static void put(URI uri, String authorization, String contentType, String body) throws IOException {
request(uri, null, null, authorization, contentType, body, null);
}
/**
* Perform HTTP PUT request
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param authorization The Authorization header value
* @param contentType MIME type of the request body
* @param body The request body
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static void put(URI uri, SSLSocketFactory socketFactory, String authorization, String contentType, String body) throws IOException {
request(uri, socketFactory, null, authorization, contentType, body, null);
}
/**
* Perform HTTP PUT request
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param verifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @param contentType MIME type of the request body
* @param body The request body
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static void put(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body) throws IOException {
request(uri, socketFactory, verifier, authorization, contentType, body, null);
}
/**
* Perform HTTP PUT request
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param verifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @param contentType MIME type of the request body
* @param body The request body
* @param connectTimeout Connect timeout in seconds
* @param readTimeout Read timeout in seconds
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static void put(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body, int connectTimeout, int readTimeout) throws IOException {
request(uri, "PUT", socketFactory, verifier, authorization, contentType, body, null, connectTimeout, readTimeout, true);
}
/**
* Perform HTTP DELETE request using the default connect and read timeouts (60 seconds).
*
* @param uri The target url
* @param authorization The Authorization header value
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static void delete(URI uri, String authorization) throws IOException {
request(uri, "DELETE", null, null, authorization, null, null, null, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, true);
}
/**
* Perform HTTP DELETE request using the default connect and read timeouts (60 seconds).
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param verifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static void delete(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization) throws IOException {
request(uri, "DELETE", socketFactory, verifier, authorization, null, null, null, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, true);
}
/**
* Perform HTTP DELETE request
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param verifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @param connectTimeout Connect timeout in seconds
* @param readTimeout Read timeout in seconds
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static void delete(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, int connectTimeout, int readTimeout) throws IOException {
request(uri, "DELETE", socketFactory, verifier, authorization, null, null, null, connectTimeout, readTimeout, true);
}
/**
* Perform an HTTP request, auto-detecting a method, and using the default connect and read timeouts (60 seconds)..
* If body is null the HTTP method GET is used.
* If responseType is null the HTTP method PUT is used.
* Otherwise, the HTTP method POST is used.
*
* @param uri The target url
* @param socketFactory Socket factory to use with https:// url
* @param hostnameVerifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @param contentType MIME type of the request body
* @param body The request body
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static <T> T request(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, String contentType, String body, Class<T> responseType) throws IOException {
return request(uri, null, socketFactory, hostnameVerifier, authorization, contentType, body, responseType, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, true);
}
/**
* Perform an HTTP request using the default connect and read timeouts (60 seconds).
*
* @param uri The target url
* @param method The HTTP request method
* @param socketFactory Socket factory to use with https:// url
* @param hostnameVerifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @param contentType MIME type of the request body
* @param body The request body
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
public static <T> T request(URI uri, String method, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, String contentType, String body, Class<T> responseType) throws IOException {
return request(uri, method, socketFactory, hostnameVerifier, authorization, contentType, body, responseType, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, true);
}
/**
* Perform an HTTP request using the default connect and read timeouts (60 seconds).
*
* @param uri The target url
* @param method The HTTP request method
* @param socketFactory Socket factory to use with https:// url
* @param hostnameVerifier HostnameVerifier to use with https:// url
* @param authorization The Authorization header value
* @param contentType MIME type of the request body
* @param body The request body
* @param responseType The type to which to convert the response (String or one of the Jackson Mapper types)
* @param connectTimeout Connect timeout in seconds
* @param readTimeout Read timeout in seconds
* @return The response as specified by the <code>responseType</code>.
* @param <T> Generic type of the <code>responseType</code>
* @param includeAcceptHeader Determines if <code>Accept application/json</code> is sent to the remote server.
* @throws IOException A connection, timeout, or network exception that occurs while performing the request
* @throws HttpException A runtime exception when an HTTP response status signals a failed request
*/
// Suppressed because of Spotbugs Java 11 bug - https://github.com/spotbugs/spotbugs/issues/756
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
public static <T> T request(URI uri, String method, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization,
String contentType, String body, Class<T> responseType, int connectTimeout, int readTimeout, boolean includeAcceptHeader) throws IOException {
HttpURLConnection con;
try {
con = (HttpURLConnection) uri.toURL().openConnection();
} catch (IOException e) {
throw new IOException("Failed to initialise connection to: " + uri, e);
}
configureTimeouts(con, connectTimeout, readTimeout);
configureTLS(con, uri, socketFactory, hostnameVerifier);
con.setUseCaches(false);
if (body != null) {
con.setDoOutput(true);
}
if (method == null) {
method = body == null ? "GET" : responseType != null ? "POST" : "PUT";
}
con.setRequestMethod(method);
if (authorization != null) {
con.setRequestProperty("Authorization", authorization);
}
if (includeAcceptHeader) {
con.setRequestProperty("Accept", "application/json");
}
if (body != null && body.length() > 0) {
if (contentType == null) {
throw new IllegalArgumentException("contentType must be set when body is not null");
}
con.setRequestProperty("Content-Type", contentType);
}
try {
con.connect();
} catch (ConnectException e) {
throw new IOException("Failed to connect to: " + uri, e);
}
if (body != null && body.length() > 0) {
try (OutputStream out = con.getOutputStream()) {
out.write(body.getBytes(StandardCharsets.UTF_8));
}
}
return handleResponse(con, method, uri, responseType);
}
private static void configureTLS(HttpURLConnection con, URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier) {
if (con instanceof HttpsURLConnection) {
HttpsURLConnection scon = (HttpsURLConnection) con;
if (socketFactory != null) {
scon.setSSLSocketFactory(socketFactory);
}
if (hostnameVerifier != null) {
scon.setHostnameVerifier(hostnameVerifier);
}
} else if (socketFactory != null) {
log.warn("SSL socket factory set but url scheme not https ({})", uri);
}
}
private static void configureTimeouts(HttpURLConnection con, int connectTimeout, int readTimeout) {
if (connectTimeout <= 0) {
throw new IllegalArgumentException("connectTimeout <= 0");
}
con.setConnectTimeout(connectTimeout * 1000);
if (readTimeout <= 0) {
throw new IllegalArgumentException("readTimeout <= 0");
}
con.setReadTimeout(readTimeout * 1000);
}
// Surpressed because of Spotbugs Java 11 bug - https://github.com/spotbugs/spotbugs/issues/756
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
private static <T> T handleResponse(HttpURLConnection con, String method, URI uri, Class<T> responseType) throws IOException {
int code = con.getResponseCode();
if (code != 200 && code != 201 && code != 204) {
InputStream err = con.getErrorStream();
if (err != null) {
ByteArrayOutputStream errbuf = new ByteArrayOutputStream(4096);
try {
copy(err, errbuf);
} catch (Exception e) {
log.warn("[IGNORED] Failed to read response body", e);
}
throw new HttpException(method, uri, code, errbuf.toString(StandardCharsets.UTF_8.name()));
} else {
throw new HttpException(method, uri, code, con.getResponseMessage());
}
}
try (InputStream response = con.getInputStream()) {
if (responseType == null) {
response.close();
return null;
}
InputStream is = response;
if (log.isTraceEnabled()) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
IOUtil.copy(response, buffer);
log.trace("Response body for " + method + " " + uri + ": " + buffer.toString("utf-8"));
is = new ByteArrayInputStream(buffer.toByteArray());
}
return JSONUtil.readJSON(is, responseType);
}
// Don't call con.disconnect() in order to allow connection reuse.
//
// The connection pool per destination is determined by http.maxConnections system property.
//
// See also:
// https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html
// https://docs.oracle.com/javase/8/docs/technotes/guides/net/http-keepalive.html
// https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html
}
}