5
5
6
6
package io .opentelemetry .contrib .aws .resource ;
7
7
8
+ import java .io .ByteArrayOutputStream ;
8
9
import java .io .FileInputStream ;
9
10
import java .io .IOException ;
11
+ import java .io .InputStream ;
12
+ import java .net .HttpURLConnection ;
13
+ import java .net .URL ;
14
+ import java .nio .charset .StandardCharsets ;
10
15
import java .security .KeyStore ;
11
16
import java .security .cert .Certificate ;
12
17
import java .security .cert .CertificateFactory ;
13
18
import java .time .Duration ;
14
19
import java .util .Collection ;
15
20
import java .util .Map ;
21
+ import java .util .Objects ;
16
22
import java .util .logging .Level ;
17
23
import java .util .logging .Logger ;
18
24
import javax .annotation .Nullable ;
25
+ import javax .net .ssl .HttpsURLConnection ;
19
26
import javax .net .ssl .SSLContext ;
20
27
import javax .net .ssl .SSLSocketFactory ;
21
28
import javax .net .ssl .TrustManager ;
22
29
import javax .net .ssl .TrustManagerFactory ;
23
30
import javax .net .ssl .X509TrustManager ;
24
- import okhttp3 .OkHttpClient ;
25
- import okhttp3 .Request ;
26
- import okhttp3 .RequestBody ;
27
- import okhttp3 .Response ;
28
- import okhttp3 .ResponseBody ;
29
31
30
32
/** A simple HTTP client based on OkHttp. Not meant for high throughput. */
31
33
final class SimpleHttpClient {
@@ -34,56 +36,57 @@ final class SimpleHttpClient {
34
36
35
37
private static final Duration TIMEOUT = Duration .ofSeconds (2 );
36
38
37
- private static final RequestBody EMPTY_BODY = RequestBody .create (new byte [0 ]);
39
+ @ Nullable
40
+ private static SSLSocketFactory getSslSocketFactoryForCertPath (@ Nullable String certPath ) {
41
+ if (Objects .isNull (certPath )) {
42
+ return null ;
43
+ }
44
+
45
+ KeyStore keyStore = getKeystoreForTrustedCert (certPath );
46
+ X509TrustManager trustManager = buildTrustManager (keyStore );
47
+ return buildSslSocketFactory (trustManager );
48
+ }
49
+
50
+ private static HttpURLConnection setupUrlConnection (
51
+ String urlStr , String httpMethod , Map <String , String > headers ) throws Exception {
52
+ try {
53
+ HttpURLConnection urlConnection = (HttpURLConnection ) new URL (urlStr ).openConnection ();
54
+ urlConnection .setRequestMethod (httpMethod );
55
+ headers .forEach (urlConnection ::setRequestProperty );
56
+ urlConnection .setConnectTimeout ((int ) TIMEOUT .toMillis ());
57
+ urlConnection .setReadTimeout ((int ) TIMEOUT .toMillis ());
58
+ urlConnection .setDoInput (true );
59
+ urlConnection .setDoOutput (false );
60
+ return urlConnection ;
61
+ } catch (IOException e ) {
62
+ logger .log (Level .WARNING , "Cannot open connection to " + urlStr , e );
63
+ throw e ;
64
+ }
65
+ }
38
66
39
67
/** Fetch a string from a remote server. */
40
68
public String fetchString (
41
69
String httpMethod , String urlStr , Map <String , String > headers , @ Nullable String certPath ) {
42
70
43
- OkHttpClient .Builder clientBuilder =
44
- new OkHttpClient .Builder ()
45
- .callTimeout (TIMEOUT )
46
- .connectTimeout (TIMEOUT )
47
- .readTimeout (TIMEOUT );
48
-
49
- if (urlStr .startsWith ("https" ) && certPath != null ) {
50
- KeyStore keyStore = getKeystoreForTrustedCert (certPath );
51
- X509TrustManager trustManager = buildTrustManager (keyStore );
52
- SSLSocketFactory socketFactory = buildSslSocketFactory (trustManager );
53
- if (socketFactory != null ) {
54
- clientBuilder .sslSocketFactory (socketFactory , trustManager );
71
+ try {
72
+ HttpURLConnection httpUrlConnection = setupUrlConnection (urlStr , httpMethod , headers );
73
+ if (urlStr .startsWith ("https" )) {
74
+ HttpsURLConnection urlConnection = (HttpsURLConnection ) httpUrlConnection ;
75
+ SSLSocketFactory sslSocketFactory = getSslSocketFactoryForCertPath (certPath );
76
+ urlConnection .setSSLSocketFactory (sslSocketFactory );
55
77
}
56
- }
57
-
58
- OkHttpClient client = clientBuilder .build ();
59
-
60
- // AWS incorrectly uses PUT despite having no request body, OkHttp will only allow us to send
61
- // GET with null body or PUT with empty string body
62
- RequestBody requestBody = null ;
63
- if (httpMethod .equals ("PUT" )) {
64
- requestBody = EMPTY_BODY ;
65
- }
66
- Request .Builder requestBuilder =
67
- new Request .Builder ().url (urlStr ).method (httpMethod , requestBody );
68
78
69
- headers .forEach (requestBuilder ::addHeader );
79
+ int responseCode = httpUrlConnection .getResponseCode ();
80
+ String responseBody = convert (httpUrlConnection .getInputStream ());
70
81
71
- try (Response response = client .newCall (requestBuilder .build ()).execute ()) {
72
- int responseCode = response .code ();
73
82
if (responseCode != 200 ) {
74
83
logger .log (
75
84
Level .FINE ,
76
- "Error response from "
77
- + urlStr
78
- + " code ("
79
- + responseCode
80
- + ") text "
81
- + response .message ());
85
+ "Error response from " + urlStr + " code (" + responseCode + ") text " + responseBody );
82
86
return "" ;
83
87
}
84
- ResponseBody body = response .body ();
85
- return body != null ? body .string () : "" ;
86
- } catch (IOException e ) {
88
+ return responseBody ;
89
+ } catch (Exception e ) {
87
90
logger .log (Level .FINE , "SimpleHttpClient fetch string failed." , e );
88
91
}
89
92
@@ -142,4 +145,14 @@ private static KeyStore getKeystoreForTrustedCert(String certPath) {
142
145
return null ;
143
146
}
144
147
}
148
+
149
+ public static String convert (InputStream inputStream ) throws IOException {
150
+ ByteArrayOutputStream result = new ByteArrayOutputStream ();
151
+ byte [] buffer = new byte [1024 ];
152
+ int length ;
153
+ while ((length = inputStream .read (buffer )) != -1 ) {
154
+ result .write (buffer , 0 , length );
155
+ }
156
+ return result .toString (StandardCharsets .UTF_8 .name ());
157
+ }
145
158
}
0 commit comments