35
35
import java .net .http .HttpHeaders ;
36
36
import java .net .http .HttpRequest ;
37
37
import java .net .http .HttpResponse ;
38
- import java .time .Duration ;
39
38
import java .util .*;
40
- import java .util .concurrent .ExecutorService ;
41
- import java .util .concurrent .Executors ;
42
39
import java .util .function .Supplier ;
43
40
44
41
/**
@@ -269,10 +266,16 @@ public Resource get(
269
266
final URI uri ,
270
267
final Supplier <Map <String , List <String >>> headers
271
268
) {
272
- final ExecutorService exec = Executors .newSingleThreadExecutor ();
273
- HttpClient client = this .newHttpClient (exec );
274
269
try {
275
- final HttpResponse <String > response = client .send (
270
+ final HttpClient .Version version ;
271
+ if (this .useOldHttpProtocol ) {
272
+ version = HttpClient .Version .HTTP_1_1 ;
273
+ } else {
274
+ version = HttpClient .Version .HTTP_2 ;
275
+ }
276
+ final HttpResponse <String > response = GlobalHttpClient .instance (
277
+ version
278
+ ).send (
276
279
this .request (
277
280
uri ,
278
281
"GET" ,
@@ -291,10 +294,6 @@ public Resource get(
291
294
"Couldn't GET [" + uri .toString () +"]" ,
292
295
ex
293
296
);
294
- } finally {
295
- exec .shutdownNow ();
296
- client = null ;
297
- System .gc ();
298
297
}
299
298
}
300
299
@@ -304,10 +303,16 @@ public Resource post(
304
303
final Supplier <Map <String , List <String >>> headers ,
305
304
final JsonValue body
306
305
) {
307
- final ExecutorService exec = Executors .newSingleThreadExecutor ();
308
- HttpClient client = this .newHttpClient (exec );
309
306
try {
310
- final HttpResponse <String > response = client .send (
307
+ final HttpClient .Version version ;
308
+ if (this .useOldHttpProtocol ) {
309
+ version = HttpClient .Version .HTTP_1_1 ;
310
+ } else {
311
+ version = HttpClient .Version .HTTP_2 ;
312
+ }
313
+ final HttpResponse <String > response = GlobalHttpClient .instance (
314
+ version
315
+ ).send (
311
316
this .request (
312
317
uri ,
313
318
"POST" ,
@@ -329,10 +334,6 @@ public Resource post(
329
334
+ " to [" + uri .toString () +"]" ,
330
335
ex
331
336
);
332
- } finally {
333
- exec .shutdownNow ();
334
- client = null ;
335
- System .gc ();
336
337
}
337
338
}
338
339
@@ -342,10 +343,16 @@ public Resource patch(
342
343
final Supplier <Map <String , List <String >>> headers ,
343
344
final JsonValue body
344
345
) {
345
- final ExecutorService exec = Executors .newSingleThreadExecutor ();
346
- HttpClient client = this .newHttpClient (exec );
347
346
try {
348
- final HttpResponse <String > response = client .send (
347
+ final HttpClient .Version version ;
348
+ if (this .useOldHttpProtocol ) {
349
+ version = HttpClient .Version .HTTP_1_1 ;
350
+ } else {
351
+ version = HttpClient .Version .HTTP_2 ;
352
+ }
353
+ final HttpResponse <String > response = GlobalHttpClient .instance (
354
+ version
355
+ ).send (
349
356
this .request (
350
357
uri ,
351
358
"PATCH" ,
@@ -367,10 +374,6 @@ public Resource patch(
367
374
+ " at [" + uri .toString () +"]" ,
368
375
ex
369
376
);
370
- } finally {
371
- exec .shutdownNow ();
372
- client = null ;
373
- System .gc ();
374
377
}
375
378
}
376
379
@@ -380,10 +383,16 @@ public Resource put(
380
383
final Supplier <Map <String , List <String >>> headers ,
381
384
final JsonValue body
382
385
) {
383
- final ExecutorService exec = Executors .newSingleThreadExecutor ();
384
- HttpClient client = this .newHttpClient (exec );
385
386
try {
386
- final HttpResponse <String > response = client .send (
387
+ final HttpClient .Version version ;
388
+ if (this .useOldHttpProtocol ) {
389
+ version = HttpClient .Version .HTTP_1_1 ;
390
+ } else {
391
+ version = HttpClient .Version .HTTP_2 ;
392
+ }
393
+ final HttpResponse <String > response = GlobalHttpClient .instance (
394
+ version
395
+ ).send (
387
396
this .request (
388
397
uri ,
389
398
"PUT" ,
@@ -405,10 +414,6 @@ public Resource put(
405
414
+ " at [" + uri .toString () +"]" ,
406
415
ex
407
416
);
408
- } finally {
409
- exec .shutdownNow ();
410
- client = null ;
411
- System .gc ();
412
417
}
413
418
}
414
419
@@ -418,10 +423,16 @@ public Resource delete(
418
423
final Supplier <Map <String , List <String >>> headers ,
419
424
final JsonValue body
420
425
) {
421
- final ExecutorService exec = Executors .newSingleThreadExecutor ();
422
- HttpClient client = this .newHttpClient (exec );
423
426
try {
424
- final HttpResponse <String > response = client .send (
427
+ final HttpClient .Version version ;
428
+ if (this .useOldHttpProtocol ) {
429
+ version = HttpClient .Version .HTTP_1_1 ;
430
+ } else {
431
+ version = HttpClient .Version .HTTP_2 ;
432
+ }
433
+ final HttpResponse <String > response = GlobalHttpClient .instance (
434
+ version
435
+ ).send (
425
436
this .request (
426
437
uri ,
427
438
"DELETE" ,
@@ -443,10 +454,6 @@ public Resource delete(
443
454
+ " at [" + uri .toString () +"]" ,
444
455
ex
445
456
);
446
- } finally {
447
- exec .shutdownNow ();
448
- client = null ;
449
- System .gc ();
450
457
}
451
458
}
452
459
@@ -495,32 +502,6 @@ private HttpRequest request(
495
502
return requestBuilder .build ();
496
503
}
497
504
498
- /**
499
- * Creates a new http client.
500
- * @param executor ExecutorService to use internally and close on end.
501
- * @return HttpClient.
502
- */
503
- private HttpClient newHttpClient (final ExecutorService executor ) {
504
- final HttpClient client ;
505
- if (this .useOldHttpProtocol ) {
506
- client = HttpClient
507
- .newBuilder ()
508
- .version (HttpClient .Version .HTTP_1_1 )
509
- .followRedirects (HttpClient .Redirect .NORMAL )
510
- .connectTimeout (Duration .ofSeconds (10 ))
511
- .executor (executor )
512
- .build ();
513
- } else {
514
- client = HttpClient
515
- .newBuilder ()
516
- .followRedirects (HttpClient .Redirect .NORMAL )
517
- .connectTimeout (Duration .ofSeconds (10 ))
518
- .executor (executor )
519
- .build ();
520
- }
521
- return client ;
522
- }
523
-
524
505
/**
525
506
* Calling HttpHeaders.map() will return a Map<String, List<String>>,
526
507
* BUT it will not split the header's value by comma. We have to do
0 commit comments