15
15
*/
16
16
package org .kairosdb .client ;
17
17
18
- import com .google .common .collect .ImmutableListMultimap ;
19
- import com .google .common .collect .ListMultimap ;
20
18
import com .google .common .reflect .TypeToken ;
21
- import com .proofpoint .http .client .BodySource ;
22
- import com .proofpoint .http .client .HeaderName ;
23
- import com .proofpoint .http .client .Request ;
24
- import com .proofpoint .http .client .Response ;
25
- import org .apache .http .Header ;
26
- import org .apache .http .HttpEntity ;
27
19
import org .apache .http .HttpResponse ;
28
20
import org .apache .http .client .entity .EntityBuilder ;
29
21
import org .apache .http .client .methods .HttpDelete ;
30
22
import org .apache .http .client .methods .HttpGet ;
31
23
import org .apache .http .client .methods .HttpPost ;
32
24
import org .apache .http .client .methods .HttpUriRequest ;
33
- import org .apache .http .entity .ContentType ;
34
25
import org .apache .http .impl .client .CloseableHttpClient ;
35
26
import org .apache .http .impl .client .HttpClientBuilder ;
36
27
import org .apache .http .impl .client .StandardHttpRequestRetryHandler ;
39
30
import org .kairosdb .client .response .JsonResponseHandler ;
40
31
import org .kairosdb .client .response .QueryResponse ;
41
32
import org .kairosdb .client .response .QueryTagResponse ;
42
- import sun . reflect . generics . reflectiveObjects . NotImplementedException ;
33
+ import org . kairosdb . client . response . ResponseHelper ;
43
34
44
35
import java .io .IOException ;
45
- import java .io .InputStream ;
46
36
import java .lang .reflect .Type ;
37
+ import java .net .ConnectException ;
47
38
import java .net .MalformedURLException ;
48
39
import java .net .URI ;
49
40
import java .net .URISyntaxException ;
50
41
import java .net .URL ;
51
42
import java .util .List ;
52
43
import java .util .Map ;
53
44
54
- import static com . proofpoint . http . client . ResponseHandlerUtils . propagate ;
45
+ import static java . util . Objects . requireNonNull ;
55
46
import static org .apache .http .HttpHeaders .*;
56
47
import static org .apache .http .entity .ContentType .APPLICATION_JSON ;
57
48
import static org .apache .http .entity .ContentType .TEXT_PLAIN ;
49
+ import static org .kairosdb .client .util .Exceptions .propagate ;
58
50
import static org .kairosdb .client .util .Preconditions .checkNotNullOrEmpty ;
59
- import static org .weakref .jmx .internal .guava .base .Preconditions .checkNotNull ;
60
51
61
52
/**
62
53
* HTTP implementation of a client.
@@ -105,7 +96,7 @@ public HttpClient(String url) throws MalformedURLException
105
96
public HttpClient (HttpClientBuilder builder , String url ) throws MalformedURLException
106
97
{
107
98
checkNotNullOrEmpty (url , "url cannot be null" );
108
- checkNotNull (builder , "builder must not be null" );
99
+ requireNonNull (builder , "builder must not be null" );
109
100
this .url = url ;
110
101
new URL (url ); // validate url
111
102
client = builder .build ();
@@ -115,7 +106,7 @@ public HttpClient(HttpClientBuilder builder, String url) throws MalformedURLExce
115
106
public HttpClient (CloseableHttpClient client , String url ) throws MalformedURLException
116
107
{
117
108
checkNotNullOrEmpty (url , "url cannot be null" );
118
- checkNotNull (client , "client must not be null" );
109
+ requireNonNull (client , "client must not be null" );
119
110
this .url = url ;
120
111
new URL (url ); // validate url
121
112
this .client = client ;
@@ -125,7 +116,7 @@ public HttpClient(CloseableHttpClient client, String url) throws MalformedURLExc
125
116
public void registerCustomDataType (String groupType , Class dataPointClass )
126
117
{
127
118
checkNotNullOrEmpty (groupType , "groupType may not be null or empty" );
128
- checkNotNull (dataPointClass , "dataPointClass may not be null" );
119
+ requireNonNull (dataPointClass , "dataPointClass may not be null" );
129
120
typeRegistry .registerCustomDataType (groupType , dataPointClass );
130
121
}
131
122
@@ -196,17 +187,17 @@ public int getStatusCheck()
196
187
return queryData (PATH_CHECK , new JsonResponseHandler <Integer >()
197
188
{
198
189
@ Override
199
- public Integer handleException (Request request , Exception e ) throws RuntimeException
190
+ public Integer handleException (HttpUriRequest request , Exception exception ) throws RuntimeException
200
191
{
201
- throw propagate (request , e );
192
+ throw propagate (request , exception );
202
193
}
203
194
204
195
@ Override
205
- public Integer handle (Request request , Response response ) throws RuntimeException
196
+ public Integer handle (HttpUriRequest request , ResponseHelper response ) throws RuntimeException
206
197
{
207
198
return response .getStatusCode ();
208
199
}
209
- }, ImmutableListMultimap . of () );
200
+ });
210
201
}
211
202
212
203
@ SuppressWarnings ("unchecked" )
@@ -274,40 +265,43 @@ private <T> T postData(String path, String json, JsonResponseHandler<T> response
274
265
275
266
private <T > T postData (String path , String json , JsonResponseHandler <T > responseHandler , boolean compressed )
276
267
{
277
- Request request ;
268
+ HttpPost post = new HttpPost (createURI (path ));
269
+
270
+ EntityBuilder entityBuilder = EntityBuilder .create ()
271
+ .setContentType (APPLICATION_JSON )
272
+ .setText (json );
273
+
278
274
if (compressed )
279
275
{
280
- request = new Request (createURI (path ), "POST" ,
281
- ImmutableListMultimap .of (CONTENT_ENCODING , GZIP ),
282
- new StringBodySource (json , TEXT_PLAIN , true ));
276
+ entityBuilder .gzipCompress ();
277
+ post .addHeader (CONTENT_ENCODING , GZIP );
283
278
}
284
279
else
285
280
{
286
- request = new Request (createURI (path ), "POST" ,
287
- ImmutableListMultimap .of (CONTENT_TYPE , APPLICATION_JSON .toString ()),
288
- new StringBodySource (json , APPLICATION_JSON ));
281
+ post .addHeader (CONTENT_TYPE , APPLICATION_JSON .toString ());
289
282
}
290
- return execute (request , responseHandler );
291
- }
283
+ post .setEntity (entityBuilder .build ());
292
284
293
- private <T > T queryData (String path , JsonResponseHandler <T > responseHandler )
294
- {
295
- return this .queryData (path , responseHandler , ImmutableListMultimap .of (ACCEPT , APPLICATION_JSON .toString (), ACCEPT_ENCODING , "gzip" ));
285
+ return execute (post , responseHandler );
296
286
}
297
287
298
288
@ SuppressWarnings ("unchecked" )
299
- private <T > T queryData (String path , JsonResponseHandler <T > responseHandler , ImmutableListMultimap headers )
289
+ private <T > T queryData (String path , JsonResponseHandler <T > responseHandler )
300
290
{
301
- Request request = new Request (createURI (path ), "GET" , headers , null );
302
- return execute (request , responseHandler );
291
+ HttpGet get = new HttpGet (createURI (path ));
292
+ get .addHeader (ACCEPT , APPLICATION_JSON .toString ());
293
+ get .addHeader (ACCEPT_ENCODING , "gzip" );
294
+
295
+ return execute (get , responseHandler );
303
296
}
304
297
305
298
@ SuppressWarnings ("UnusedReturnValue" )
306
299
private <T > T delete (String path , JsonResponseHandler <T > responseHandler )
307
300
{
308
- Request request = new Request (createURI (path ), "DELETE" ,
309
- ImmutableListMultimap .of (ACCEPT , APPLICATION_JSON .toString ()), null );
310
- return execute (request , responseHandler );
301
+ HttpDelete delete = new HttpDelete (createURI (path ));
302
+ delete .addHeader (ACCEPT , APPLICATION_JSON .toString ());
303
+
304
+ return execute (delete , responseHandler );
311
305
}
312
306
313
307
private URI createURI (String path )
@@ -322,12 +316,12 @@ private URI createURI(String path)
322
316
}
323
317
}
324
318
325
- private <T > T execute (Request request , JsonResponseHandler <T > responseHandler )
319
+ private <T > T execute (HttpUriRequest request , JsonResponseHandler <T > responseHandler )
326
320
{
327
321
try
328
322
{
329
- HttpResponse response = client .execute (convertToApacheRequest ( request ) );
330
- return responseHandler .handle (request , new ApacheResponse (response ));
323
+ HttpResponse response = client .execute (request );
324
+ return responseHandler .handle (request , new ResponseHelper (response ));
331
325
}
332
326
catch (IOException e )
333
327
{
@@ -341,127 +335,6 @@ public void close() throws IOException
341
335
client .close ();
342
336
}
343
337
344
- private static class ApacheResponse implements Response
345
- {
346
- private final HttpResponse response ;
347
-
348
- ApacheResponse (HttpResponse response )
349
- {
350
- this .response = response ;
351
- }
352
-
353
- @ Override
354
- public int getStatusCode ()
355
- {
356
- return response .getStatusLine ().getStatusCode ();
357
- }
358
-
359
- @ Override
360
- public String getStatusMessage ()
361
- {
362
- return response .getStatusLine ().getReasonPhrase ();
363
- }
364
-
365
- @ Override
366
- public ListMultimap <HeaderName , String > getHeaders ()
367
- {
368
- ImmutableListMultimap .Builder <HeaderName , String > builder = ImmutableListMultimap .builder ();
369
- for (Header header : response .getAllHeaders ())
370
- {
371
- builder .put (HeaderName .of (header .getName ()), header .getValue ());
372
- }
373
- return builder .build ();
374
- }
375
-
376
- @ Override
377
- public long getBytesRead ()
378
- {
379
- throw new NotImplementedException ();
380
- }
381
-
382
- @ Override
383
- public InputStream getInputStream () throws IOException
384
- {
385
- if (response .getEntity () != null )
386
- return response .getEntity ().getContent ();
387
- return null ;
388
- }
389
- }
390
-
391
- private HttpUriRequest convertToApacheRequest (Request request )
392
- {
393
- HttpUriRequest apacheRequest ;
394
- switch (request .getMethod ())
395
- {
396
- case "POST" :
397
- apacheRequest = new HttpPost (request .getUri ());
398
- if (request .getBodySource () instanceof StringBodySource )
399
- {
400
- ((HttpPost ) apacheRequest ).setEntity (((StringBodySource ) request .getBodySource ()).toHttpEntity ());
401
- }
402
- else
403
- {
404
- throw new IllegalArgumentException ("Request has unsupported BodySource type " + request .getBodySource ().getClass ().getName ());
405
- }
406
- break ;
407
- case "GET" :
408
- apacheRequest = new HttpGet (request .getUri ());
409
- break ;
410
- case "DELETE" :
411
- apacheRequest = new HttpDelete (request .getUri ());
412
- break ;
413
- default :
414
- throw new IllegalArgumentException ("Request method is invalid must be one of POST, GET, or DELETE" );
415
- }
416
-
417
- for (Map .Entry <String , String > header : request .getHeaders ().entries ())
418
- {
419
- apacheRequest .addHeader (header .getKey (), header .getValue ());
420
- }
421
-
422
- return apacheRequest ;
423
- }
424
-
425
- private class StringBodySource implements BodySource
426
- {
427
- private final String body ;
428
- private final ContentType contentType ;
429
- private final boolean compressed ;
430
-
431
- StringBodySource (String body , ContentType contentType )
432
- {
433
- this .body = body ;
434
- this .contentType = contentType ;
435
- this .compressed = false ;
436
- }
437
-
438
- StringBodySource (String body , ContentType contentType , boolean compressed )
439
- {
440
- this .body = body ;
441
- this .contentType = contentType ;
442
- this .compressed = compressed ;
443
- }
444
-
445
- @ Override
446
- public long getLength ()
447
- {
448
- return body .length ();
449
- }
450
-
451
- HttpEntity toHttpEntity ()
452
- {
453
- EntityBuilder builder = EntityBuilder .create ()
454
- .setContentType (contentType )
455
- .setText (body );
456
-
457
- if (compressed )
458
- {
459
- builder .gzipCompress ();
460
- }
461
-
462
- return builder .build ();
463
- }
464
- }
465
338
466
339
@ SuppressWarnings ("unused" )
467
340
private class Results
0 commit comments