-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathJdkHttpClient.java
122 lines (114 loc) · 3.92 KB
/
JdkHttpClient.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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.aws.resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
/** A simple HTTP client based on JDK HttpURLConnection. Not meant for high throughput. */
final class JdkHttpClient {
private static final Logger logger = Logger.getLogger(JdkHttpClient.class.getName());
private static final int TIMEOUT = 2000;
/** Fetch a string from a remote server. */
public String fetchString(
String httpMethod, String urlStr, Map<String, String> headers, @Nullable String certPath) {
try {
// create URL from string
URL url = new URL(urlStr);
// create connection
URLConnection connection = url.openConnection();
// https
if (connection instanceof HttpsURLConnection) {
// cast
HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
// check CA cert path is available
if (certPath != null) {
// create trust manager
X509TrustManager trustManager = SslSocketFactoryBuilder.createTrustManager(certPath);
// socket factory
SSLSocketFactory socketFactory =
SslSocketFactoryBuilder.createSocketFactory(trustManager);
if (socketFactory != null) {
// update connection
httpsConnection.setSSLSocketFactory(socketFactory);
}
}
// process request
return processRequest(httpsConnection, httpMethod, urlStr, headers);
}
// http
if (connection instanceof HttpURLConnection) {
// cast
HttpURLConnection httpConnection = (HttpURLConnection) connection;
// process request
return processRequest(httpConnection, httpMethod, urlStr, headers);
}
// not http
logger.log(Level.FINE, "JdkHttpClient only HTTP/HTTPS connections are supported.");
} catch (MalformedURLException e) {
logger.log(Level.FINE, "JdkHttpClient invalid URL.", e);
} catch (IOException e) {
logger.log(Level.FINE, "JdkHttpClient fetch string failed.", e);
}
return "";
}
private static String processRequest(
HttpURLConnection httpConnection,
String httpMethod,
String urlStr,
Map<String, String> headers)
throws IOException {
// set method
httpConnection.setRequestMethod(httpMethod);
// set headers
headers.forEach(httpConnection::setRequestProperty);
// timeouts
httpConnection.setConnectTimeout(TIMEOUT);
httpConnection.setReadTimeout(TIMEOUT);
// connect
httpConnection.connect();
try {
// status code
int responseCode = httpConnection.getResponseCode();
if (responseCode != 200) {
logger.log(
Level.FINE,
"Error response from "
+ urlStr
+ " code ("
+ responseCode
+ ") text "
+ httpConnection.getResponseMessage());
return "";
}
// read response
try (InputStream inputStream = httpConnection.getInputStream()) {
// store read data in byte array
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
// read all bytes
int b;
while ((b = inputStream.read()) != -1) {
outputStream.write(b);
}
// result
return outputStream.toString("UTF-8");
}
}
} finally {
// disconnect, no need for persistent connections
httpConnection.disconnect();
}
}
}