@@ -414,7 +414,7 @@ private void readResponseContent() {
414
414
@ Override
415
415
public HttpResponse <Void > asVoid () {
416
416
readResponseContent ();
417
- return new HttpVoidResponse (httpResponse );
417
+ return new HttpWrapperResponse <> (httpResponse );
418
418
}
419
419
420
420
@ Override
@@ -424,50 +424,75 @@ public <T> T read(BodyReader<T> reader) {
424
424
}
425
425
426
426
@ Override
427
- public <T > T bean (Class <T > cls ) {
427
+ public <T > HttpResponse <T > as (Class <T > type ) {
428
+ return new HttpWrapperResponse <>(bean (type ), httpResponse );
429
+ }
430
+
431
+ @ Override
432
+ public <T > HttpResponse <T > as (ParameterizedType type ) {
433
+ return new HttpWrapperResponse <>(bean (type ), httpResponse );
434
+ }
435
+
436
+ @ Override
437
+ public <T > T bean (Class <T > type ) {
428
438
readResponseContent ();
429
- return context .readBean (cls , encodedResponseBody );
439
+ return context .readBean (type , encodedResponseBody );
430
440
}
431
441
432
442
@ Override
433
- public <T > List < T > list ( Class < T > cls ) {
443
+ public <T > T bean ( ParameterizedType type ) {
434
444
readResponseContent ();
435
- return context .readList ( cls , encodedResponseBody );
445
+ return context .readBean ( type , encodedResponseBody );
436
446
}
437
447
438
448
@ Override
439
- public <T > Stream <T > stream (Class <T > cls ) {
440
- final HttpResponse <Stream <String >> res = handler (HttpResponse .BodyHandlers .ofLines ());
441
- this .httpResponse = res ;
442
- if (res .statusCode () >= 300 ) {
443
- throw new HttpException (res , context );
444
- }
445
- final BodyReader <T > bodyReader = context .beanReader (cls );
446
- return res .body ().map (bodyReader ::readBody );
449
+ public <T > HttpResponse <List <T >> asList (Class <T > type ) {
450
+ return new HttpWrapperResponse <>(list (type ), httpResponse );
447
451
}
448
452
453
+ @ Override
454
+ public <T > HttpResponse <List <T >> asList (ParameterizedType type ) {
455
+ return new HttpWrapperResponse <>(list (type ), httpResponse );
456
+ }
449
457
450
458
@ Override
451
- public <T > T bean ( ParameterizedType cls ) {
459
+ public <T > List < T > list ( Class < T > type ) {
452
460
readResponseContent ();
453
- return context .readBean ( cls , encodedResponseBody );
461
+ return context .readList ( type , encodedResponseBody );
454
462
}
455
463
456
464
@ Override
457
- public <T > List <T > list (ParameterizedType cls ) {
465
+ public <T > List <T > list (ParameterizedType type ) {
458
466
readResponseContent ();
459
- return context .readList (cls , encodedResponseBody );
467
+ return context .readList (type , encodedResponseBody );
468
+ }
469
+
470
+ @ Override
471
+ public <T > HttpResponse <Stream <T >> asStream (Class <T > type ) {
472
+ return new HttpWrapperResponse <>(stream (type ), httpResponse );
473
+ }
474
+
475
+ @ Override
476
+ public <T > HttpResponse <Stream <T >> asStream (ParameterizedType type ) {
477
+ return new HttpWrapperResponse <>(stream (type ), httpResponse );
460
478
}
461
479
480
+ @ Override
481
+ public <T > Stream <T > stream (Class <T > type ) {
482
+ return stream (context .beanReader (type ));
483
+ }
462
484
463
485
@ Override
464
- public <T > Stream <T > stream (ParameterizedType cls ) {
486
+ public <T > Stream <T > stream (ParameterizedType type ) {
487
+ return stream (context .beanReader (type ));
488
+ }
489
+
490
+ private <T > Stream <T > stream (BodyReader <T > bodyReader ) {
465
491
final HttpResponse <Stream <String >> res = handler (HttpResponse .BodyHandlers .ofLines ());
466
492
this .httpResponse = res ;
467
493
if (res .statusCode () >= 300 ) {
468
494
throw new HttpException (res , context );
469
495
}
470
- final BodyReader <T > bodyReader = context .beanReader (cls );
471
496
return res .body ().map (bodyReader ::readBody );
472
497
}
473
498
@@ -484,7 +509,7 @@ public <T> HttpResponse<T> handler(HttpResponse.BodyHandler<T> responseHandler)
484
509
private <T > HttpResponse <T > sendWith (HttpResponse .BodyHandler <T > responseHandler ) {
485
510
context .beforeRequest (this );
486
511
addHeaders ();
487
- HttpResponse <T > response = performSend (responseHandler );
512
+ final HttpResponse <T > response = performSend (responseHandler );
488
513
httpResponse = response ;
489
514
return response ;
490
515
}
@@ -508,49 +533,49 @@ protected <T> CompletableFuture<HttpResponse<T>> performSendAsync(boolean loggab
508
533
509
534
protected HttpResponse <Void > asyncVoid (HttpResponse <byte []> response ) {
510
535
afterAsyncEncoded (response );
511
- return new HttpVoidResponse (response );
536
+ return new HttpWrapperResponse <> (response );
512
537
}
513
538
514
- protected <E > E asyncBean (Class <E > type , HttpResponse <byte []> response ) {
539
+ protected <E > HttpResponse <E > asyncBean (Class <E > type , HttpResponse <byte []> response ) {
540
+ afterAsyncEncoded (response );
541
+ return new HttpWrapperResponse <>(context .readBean (type , encodedResponseBody ), httpResponse );
542
+ }
543
+
544
+ protected <E > E asyncBean (ParameterizedType type , HttpResponse <byte []> response ) {
515
545
afterAsyncEncoded (response );
516
546
return context .readBean (type , encodedResponseBody );
517
547
}
518
548
519
- protected <E > List <E > asyncList (Class <E > type , HttpResponse <byte []> response ) {
549
+ protected <E > HttpResponse < List <E > > asyncList (Class <E > type , HttpResponse <byte []> response ) {
520
550
afterAsyncEncoded (response );
521
- return context .readList (type , encodedResponseBody );
551
+ return new HttpWrapperResponse <>(context .readList (type , encodedResponseBody ), httpResponse );
552
+ }
553
+
554
+ protected <E > HttpResponse <List <E >> asyncList (ParameterizedType type , HttpResponse <byte []> response ) {
555
+ afterAsyncEncoded (response );
556
+ return new HttpWrapperResponse <>(context .readList (type , encodedResponseBody ), httpResponse );
522
557
}
523
558
524
- protected <E > Stream <E > asyncStream (Class <E > type , HttpResponse <Stream <String >> response ) {
559
+ protected <E > HttpResponse < Stream <E > > asyncStream (Class <E > type , HttpResponse <Stream <String >> response ) {
525
560
responseTimeNanos = System .nanoTime () - startAsyncNanos ;
526
561
httpResponse = response ;
527
562
context .afterResponse (this );
528
563
if (response .statusCode () >= 300 ) {
529
564
throw new HttpException (response , context );
530
565
}
531
566
final BodyReader <E > bodyReader = context .beanReader (type );
532
- return response .body ().map (bodyReader ::readBody );
567
+ return new HttpWrapperResponse <>( response .body ().map (bodyReader ::readBody ), httpResponse );
533
568
}
534
569
535
- protected <E > E asyncBean (ParameterizedType type , HttpResponse <byte []> response ) {
536
- afterAsyncEncoded (response );
537
- return context .readBean (type , encodedResponseBody );
538
- }
539
-
540
- protected <E > List <E > asyncList (ParameterizedType type , HttpResponse <byte []> response ) {
541
- afterAsyncEncoded (response );
542
- return context .readList (type , encodedResponseBody );
543
- }
544
-
545
- protected <E > Stream <E > asyncStream (ParameterizedType type , HttpResponse <Stream <String >> response ) {
570
+ protected <E > HttpResponse <Stream <E >> asyncStream (ParameterizedType type , HttpResponse <Stream <String >> response ) {
546
571
responseTimeNanos = System .nanoTime () - startAsyncNanos ;
547
572
httpResponse = response ;
548
573
context .afterResponse (this );
549
574
if (response .statusCode () >= 300 ) {
550
575
throw new HttpException (response , context );
551
576
}
552
577
final BodyReader <E > bodyReader = context .beanReader (type );
553
- return response .body ().map (bodyReader ::readBody );
578
+ return new HttpWrapperResponse <>( response .body ().map (bodyReader ::readBody ), httpResponse );
554
579
}
555
580
556
581
private void afterAsyncEncoded (HttpResponse <byte []> response ) {
@@ -728,13 +753,20 @@ public String responseBody() {
728
753
}
729
754
}
730
755
731
- static class HttpVoidResponse implements HttpResponse <Void > {
756
+ static final class HttpWrapperResponse < B > implements HttpResponse <B > {
732
757
733
758
private final HttpResponse <?> orig ;
759
+ private final B body ;
760
+
761
+ HttpWrapperResponse (HttpResponse <?> orig ) {
762
+ this .orig = orig ;
763
+ this .body = null ;
764
+ }
734
765
735
766
@ SuppressWarnings ({"raw" })
736
- HttpVoidResponse ( HttpResponse <?> orig ) {
767
+ HttpWrapperResponse ( B body , HttpResponse <?> orig ) {
737
768
this .orig = orig ;
769
+ this .body = body ;
738
770
}
739
771
740
772
@ Override
@@ -747,9 +779,11 @@ public HttpRequest request() {
747
779
return orig .request ();
748
780
}
749
781
782
+ @ SuppressWarnings ("unchecked" )
750
783
@ Override
751
- public Optional <HttpResponse <Void >> previousResponse () {
752
- return Optional .empty ();
784
+ public Optional <HttpResponse <B >> previousResponse () {
785
+ final Optional <? extends HttpResponse <?>> previous = orig .previousResponse ();
786
+ return (Optional <HttpResponse <B >>)previous ;
753
787
}
754
788
755
789
@ Override
@@ -758,8 +792,8 @@ public HttpHeaders headers() {
758
792
}
759
793
760
794
@ Override
761
- public Void body () {
762
- return null ;
795
+ public B body () {
796
+ return body ;
763
797
}
764
798
765
799
@ Override
0 commit comments