3636import java .io .IOException ;
3737import java .io .UncheckedIOException ;
3838import java .math .BigDecimal ;
39- import java .util .Collections ;
4039import java .util .Date ;
41- import java .util .Map ;
40+ import java .util .logging .Level ;
41+ import java .util .logging .Logger ;
42+ import okhttp3 .Call ;
43+ import okhttp3 .MediaType ;
44+ import okhttp3 .OkHttpClient ;
45+ import okhttp3 .Request ;
46+ import okhttp3 .RequestBody ;
47+ import okhttp3 .Response ;
48+ import okhttp3 .ResponseBody ;
4249
4350final class XraySamplerClient {
4451
@@ -51,18 +58,19 @@ final class XraySamplerClient {
5158 // In case API is extended with new fields.
5259 .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , /* state= */ false );
5360
54- private static final Map <String , String > JSON_CONTENT_TYPE =
55- Collections .singletonMap ("Content-Type" , "application/json" );
61+ private static final MediaType JSON_CONTENT_TYPE = MediaType .get ("application/json" );
62+
63+ private static final Logger logger = Logger .getLogger (XraySamplerClient .class .getName ());
5664
5765 private final String getSamplingRulesEndpoint ;
5866 private final String getSamplingTargetsEndpoint ;
59- private final JdkHttpClient httpClient ;
67+ private final Call . Factory httpClient ;
6068
6169 XraySamplerClient (String host ) {
6270 this .getSamplingRulesEndpoint = host + "/GetSamplingRules" ;
6371 // Lack of Get may look wrong but is correct.
6472 this .getSamplingTargetsEndpoint = host + "/SamplingTargets" ;
65- httpClient = new JdkHttpClient ();
73+ httpClient = new OkHttpClient ();
6674 }
6775
6876 GetSamplingRulesResponse getSamplingRules (GetSamplingRulesRequest request ) {
@@ -82,8 +90,19 @@ private <T> T executeJsonRequest(String endpoint, Object request, Class<T> respo
8290 throw new UncheckedIOException ("Failed to serialize request." , e );
8391 }
8492
85- String response =
86- httpClient .fetchString ("POST" , endpoint , JSON_CONTENT_TYPE , null , requestBody );
93+ Call call =
94+ httpClient .newCall (
95+ new Request .Builder ()
96+ .url (endpoint )
97+ .post (RequestBody .create (JSON_CONTENT_TYPE , requestBody ))
98+ .build ());
99+
100+ final String response ;
101+ try (Response httpResponse = call .execute ()) {
102+ response = readResponse (httpResponse , endpoint );
103+ } catch (IOException e ) {
104+ throw new UncheckedIOException ("Failed to execute sampling request." , e );
105+ }
87106
88107 try {
89108 return OBJECT_MAPPER .readValue (response , responseType );
@@ -92,6 +111,26 @@ private <T> T executeJsonRequest(String endpoint, Object request, Class<T> respo
92111 }
93112 }
94113
114+ private static String readResponse (Response response , String endpoint ) throws IOException {
115+ if (!response .isSuccessful ()) {
116+ logger .log (
117+ Level .FINE ,
118+ "Error response from "
119+ + endpoint
120+ + " code ("
121+ + response .code ()
122+ + ") text "
123+ + response .message ());
124+ return "" ;
125+ }
126+
127+ ResponseBody body = response .body ();
128+ if (body != null ) {
129+ return body .string ();
130+ }
131+ return "" ;
132+ }
133+
95134 @ SuppressWarnings ("JavaUtilDate" )
96135 private static class FloatDateDeserializer extends StdDeserializer <Date > {
97136
0 commit comments