3535import java .util .concurrent .ExecutionException ;
3636import java .util .concurrent .Executor ;
3737import java .util .function .Function ;
38- import java .util .function .Supplier ;
3938
4039import io .cryostat .agent .FlightRecorderHelper .ConfigurationInfo ;
4140import io .cryostat .agent .FlightRecorderHelper .TemplatedRecording ;
5251import jdk .jfr .Recording ;
5352import org .apache .commons .io .FileUtils ;
5453import org .apache .commons .io .input .CountingInputStream ;
55- import org .apache .http .HttpHeaders ;
56- import org .apache .http .HttpResponse ;
57- import org .apache .http .client .HttpClient ;
58- import org .apache .http .client .methods .HttpDelete ;
59- import org .apache .http .client .methods .HttpGet ;
60- import org .apache .http .client .methods .HttpPost ;
61- import org .apache .http .client .methods .HttpRequestBase ;
62- import org .apache .http .client .methods .HttpUriRequest ;
63- import org .apache .http .entity .ContentType ;
64- import org .apache .http .entity .StringEntity ;
65- import org .apache .http .entity .mime .FormBodyPartBuilder ;
66- import org .apache .http .entity .mime .MultipartEntityBuilder ;
67- import org .apache .http .entity .mime .content .ByteArrayBody ;
68- import org .apache .http .entity .mime .content .InputStreamBody ;
69- import org .apache .http .entity .mime .content .StringBody ;
54+ import org .apache .hc .client5 .http .classic .HttpClient ;
55+ import org .apache .hc .client5 .http .classic .methods .HttpDelete ;
56+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
57+ import org .apache .hc .client5 .http .classic .methods .HttpPost ;
58+ import org .apache .hc .client5 .http .classic .methods .HttpUriRequest ;
59+ import org .apache .hc .client5 .http .classic .methods .HttpUriRequestBase ;
60+ import org .apache .hc .client5 .http .entity .mime .ByteArrayBody ;
61+ import org .apache .hc .client5 .http .entity .mime .FormBodyPartBuilder ;
62+ import org .apache .hc .client5 .http .entity .mime .InputStreamBody ;
63+ import org .apache .hc .client5 .http .entity .mime .MultipartEntityBuilder ;
64+ import org .apache .hc .client5 .http .entity .mime .StringBody ;
65+ import org .apache .hc .core5 .http .ClassicHttpResponse ;
66+ import org .apache .hc .core5 .http .ContentType ;
67+ import org .apache .hc .core5 .http .HttpHeaders ;
68+ import org .apache .hc .core5 .http .HttpHost ;
69+ import org .apache .hc .core5 .http .io .entity .StringEntity ;
7070import org .slf4j .Logger ;
7171import org .slf4j .LoggerFactory ;
7272
@@ -81,8 +81,8 @@ public class CryostatClient {
8181
8282 private final Executor executor ;
8383 private final ObjectMapper mapper ;
84+ private final HttpHost host ;
8485 private final HttpClient http ;
85- private final Supplier <Optional <String >> authorizationSupplier ;
8686
8787 private final String appName ;
8888 private final String instanceId ;
@@ -94,16 +94,15 @@ public class CryostatClient {
9494 Executor executor ,
9595 ObjectMapper mapper ,
9696 HttpClient http ,
97- Supplier <Optional <String >> authorizationSupplier ,
9897 String instanceId ,
9998 String jvmId ,
10099 String appName ,
101100 URI baseUri ,
102101 String realm ) {
103102 this .executor = executor ;
104103 this .mapper = mapper ;
104+ this .host = HttpHost .create (baseUri );
105105 this .http = http ;
106- this .authorizationSupplier = authorizationSupplier ;
107106 this .instanceId = instanceId ;
108107 this .jvmId = jvmId ;
109108 this .appName = appName ;
@@ -286,7 +285,7 @@ private CompletableFuture<Integer> submitCredentials(
286285 res -> {
287286 if (!isOkStatus (res )) {
288287 try {
289- if (res .getStatusLine (). getStatusCode () == 409 ) {
288+ if (res .getCode () == 409 ) {
290289 int queried = queryExistingCredentials (callback ).get ();
291290 if (queried >= 0 ) {
292291 return queried ;
@@ -393,9 +392,9 @@ public CompletableFuture<Void> pushHeapDump(Path heapDump, String requestId)
393392 log .trace (
394393 "{} {} ({} -> {}): {}/{}" ,
395394 req .getMethod (),
396- res .getStatusLine (). getStatusCode (),
395+ res .getCode (),
397396 heapDump .getFileName ().toString (),
398- req .getURI (),
397+ req .getRequestUri (),
399398 FileUtils .byteCountToDisplaySize (is .getByteCount ()),
400399 Duration .between (start , finish ));
401400 assertOkStatus (req , res );
@@ -485,9 +484,9 @@ public CompletableFuture<Void> upload(
485484 log .trace (
486485 "{} {} ({} -> {}): {}/{}" ,
487486 req .getMethod (),
488- res .getStatusLine (). getStatusCode (),
487+ res .getCode (),
489488 fileName ,
490- req .getURI (),
489+ req .getRequestUri (),
491490 FileUtils .byteCountToDisplaySize (is .getByteCount ()),
492491 Duration .between (start , finish ));
493492 assertOkStatus (req , res );
@@ -496,24 +495,19 @@ public CompletableFuture<Void> upload(
496495 .whenComplete ((v , t ) -> req .reset ());
497496 }
498497
499- private HttpResponse logResponse (HttpRequestBase req , HttpResponse res ) {
500- log .trace ("{} {} : {}" , req .getMethod (), req .getURI (), res .getStatusLine (). getStatusCode ());
498+ private ClassicHttpResponse logResponse (HttpUriRequestBase req , ClassicHttpResponse res ) {
499+ log .trace ("{} {} : {}" , req .getMethod (), req .getRequestUri (), res .getCode ());
501500 return res ;
502501 }
503502
504- private <T > CompletableFuture <T > supply (HttpRequestBase req , Function <HttpResponse , T > fn ) {
505- // FIXME Apache httpclient 4 does not support Bearer token auth easily, so we explicitly set
506- // the header here. This is a form of preemptive auth - the token is always sent with the
507- // request. It would be better to attempt to send the request to the server first and see if
508- // it responds with an auth challenge, and then send the auth information we have, and use
509- // the client auth cache. This flow is supported for Bearer tokens in httpclient 5.
510- authorizationSupplier .get ().ifPresent (v -> req .addHeader (HttpHeaders .AUTHORIZATION , v ));
503+ private <T > CompletableFuture <T > supply (
504+ HttpUriRequestBase req , Function <ClassicHttpResponse , T > fn ) {
511505 return CompletableFuture .supplyAsync (() -> fn .apply (executeQuiet (req )), executor );
512506 }
513507
514- private HttpResponse executeQuiet (HttpUriRequest req ) {
508+ private ClassicHttpResponse executeQuiet (HttpUriRequest req ) {
515509 try {
516- return http .execute (req );
510+ return http .execute (host , req );
517511 } catch (IOException ioe ) {
518512 throw new CompletionException (ioe );
519513 }
@@ -530,18 +524,18 @@ private String selfMatchExpression(URI callback) {
530524 callback , instanceId );
531525 }
532526
533- private boolean isOkStatus (HttpResponse res ) {
534- int sc = res .getStatusLine (). getStatusCode ();
527+ private boolean isOkStatus (ClassicHttpResponse res ) {
528+ int sc = res .getCode ();
535529 // 2xx is OK, 3xx is redirect range so allow those too
536530 return 200 <= sc && sc < 400 ;
537531 }
538532
539- private HttpResponse assertOkStatus (HttpRequestBase req , HttpResponse res ) {
540- int sc = res .getStatusLine (). getStatusCode ();
533+ private ClassicHttpResponse assertOkStatus (HttpUriRequestBase req , ClassicHttpResponse res ) {
534+ int sc = res .getCode ();
541535 if (!isOkStatus (res )) {
542- URI uri = req .getURI ();
543- log .error ("Non-OK response ({}) on HTTP API {}" , sc , uri );
544536 try {
537+ URI uri = req .getUri ();
538+ log .error ("Non-OK response ({}) on HTTP API {}" , sc , uri );
545539 throw new HttpException (
546540 sc ,
547541 new URI (uri .getScheme (), uri .getAuthority (), uri .getPath (), null , null ));
0 commit comments