@@ -206,8 +206,8 @@ public Builder mutate() {
206
206
207
207
@ Nullable
208
208
@ SuppressWarnings ({"rawtypes" , "unchecked" })
209
- private <T > T readWithMessageConverters (ClientHttpResponse clientResponse , Runnable callback , Type bodyType ,
210
- Class <T > bodyClass , @ Nullable Observation observation ) {
209
+ private <T > T readWithMessageConverters (
210
+ ClientHttpResponse clientResponse , Runnable callback , Type bodyType , Class <T > bodyClass ) {
211
211
212
212
MediaType contentType = getContentType (clientResponse );
213
213
@@ -257,21 +257,8 @@ else if (messageConverter.canRead(bodyClass, contentType)) {
257
257
else {
258
258
cause = exc ;
259
259
}
260
- RestClientException restClientException = new RestClientException ("Error while extracting response for type [" +
260
+ throw new RestClientException ("Error while extracting response for type [" +
261
261
ResolvableType .forType (bodyType ) + "] and content type [" + contentType + "]" , cause );
262
- if (observation != null ) {
263
- observation .error (restClientException );
264
- }
265
- throw restClientException ;
266
- }
267
- catch (RestClientException restClientException ) {
268
- if (observation != null ) {
269
- observation .error (restClientException );
270
- }
271
- throw restClientException ;
272
- }
273
- finally {
274
- clientResponse .close ();
275
262
}
276
263
}
277
264
@@ -536,14 +523,16 @@ private void logBody(Object body, @Nullable MediaType mediaType, HttpMessageConv
536
523
537
524
@ Override
538
525
public ResponseSpec retrieve () {
539
- return exchangeInternal ( DefaultResponseSpec :: new , false );
526
+ return new DefaultResponseSpec ( this );
540
527
}
541
528
542
529
@ Override
530
+ @ Nullable
543
531
public <T > T exchange (ExchangeFunction <T > exchangeFunction , boolean close ) {
544
532
return exchangeInternal (exchangeFunction , close );
545
533
}
546
534
535
+ @ Nullable
547
536
private <T > T exchangeInternal (ExchangeFunction <T > exchangeFunction , boolean close ) {
548
537
Assert .notNull (exchangeFunction , "ExchangeFunction must not be null" );
549
538
@@ -578,39 +567,31 @@ private <T> T exchangeInternal(ExchangeFunction<T> exchangeFunction, boolean clo
578
567
}
579
568
clientResponse = clientRequest .execute ();
580
569
observationContext .setResponse (clientResponse );
581
- ConvertibleClientHttpResponse convertibleWrapper = new DefaultConvertibleClientHttpResponse (clientResponse , observation , observationScope );
570
+ ConvertibleClientHttpResponse convertibleWrapper = new DefaultConvertibleClientHttpResponse (clientResponse );
582
571
return exchangeFunction .exchange (clientRequest , convertibleWrapper );
583
572
}
584
573
catch (IOException ex ) {
585
574
ResourceAccessException resourceAccessException = createResourceAccessException (uri , this .httpMethod , ex );
586
- if (observationScope != null ) {
587
- observationScope .close ();
588
- }
589
575
if (observation != null ) {
590
576
observation .error (resourceAccessException );
591
- observation .stop ();
592
577
}
593
578
throw resourceAccessException ;
594
579
}
595
580
catch (Throwable error ) {
596
- if (observationScope != null ) {
597
- observationScope .close ();
598
- }
599
581
if (observation != null ) {
600
582
observation .error (error );
601
- observation .stop ();
602
583
}
603
584
throw error ;
604
585
}
605
586
finally {
587
+ if (observationScope != null ) {
588
+ observationScope .close ();
589
+ }
590
+ if (observation != null ) {
591
+ observation .stop ();
592
+ }
606
593
if (close && clientResponse != null ) {
607
594
clientResponse .close ();
608
- if (observationScope != null ) {
609
- observationScope .close ();
610
- }
611
- if (observation != null ) {
612
- observation .stop ();
613
- }
614
595
}
615
596
}
616
597
}
@@ -719,17 +700,14 @@ private interface InternalBody {
719
700
720
701
private class DefaultResponseSpec implements ResponseSpec {
721
702
722
- private final HttpRequest clientRequest ;
723
-
724
- private final ClientHttpResponse clientResponse ;
703
+ private final RequestHeadersSpec <?> requestHeadersSpec ;
725
704
726
705
private final List <StatusHandler > statusHandlers = new ArrayList <>(1 );
727
706
728
707
private final int defaultStatusHandlerCount ;
729
708
730
- DefaultResponseSpec (HttpRequest clientRequest , ClientHttpResponse clientResponse ) {
731
- this .clientRequest = clientRequest ;
732
- this .clientResponse = clientResponse ;
709
+ DefaultResponseSpec (RequestHeadersSpec <?> requestHeadersSpec ) {
710
+ this .requestHeadersSpec = requestHeadersSpec ;
733
711
this .statusHandlers .addAll (DefaultRestClient .this .defaultStatusHandlers );
734
712
this .statusHandlers .add (StatusHandler .defaultHandler (DefaultRestClient .this .messageConverters ));
735
713
this .defaultStatusHandlerCount = this .statusHandlers .size ();
@@ -761,15 +739,15 @@ private ResponseSpec onStatusInternal(StatusHandler statusHandler) {
761
739
@ Override
762
740
@ Nullable
763
741
public <T > T body (Class <T > bodyType ) {
764
- return readBody (bodyType , bodyType );
742
+ return executeAndExtract (( request , response ) -> readBody (request , response , bodyType , bodyType ) );
765
743
}
766
744
767
745
@ Override
768
746
@ Nullable
769
747
public <T > T body (ParameterizedTypeReference <T > bodyType ) {
770
748
Type type = bodyType .getType ();
771
749
Class <T > bodyClass = bodyClass (type );
772
- return readBody (type , bodyClass );
750
+ return executeAndExtract (( request , response ) -> readBody (request , response , type , bodyClass ) );
773
751
}
774
752
775
753
@ Override
@@ -785,50 +763,64 @@ public <T> ResponseEntity<T> toEntity(ParameterizedTypeReference<T> bodyType) {
785
763
}
786
764
787
765
private <T > ResponseEntity <T > toEntityInternal (Type bodyType , Class <T > bodyClass ) {
788
- T body = readBody (bodyType , bodyClass );
789
- try {
790
- return ResponseEntity .status (this .clientResponse .getStatusCode ())
791
- .headers (this .clientResponse .getHeaders ())
792
- .body (body );
793
- }
794
- catch (IOException ex ) {
795
- throw new ResourceAccessException ("Could not retrieve response status code: " + ex .getMessage (), ex );
796
- }
766
+ ResponseEntity <T > entity = executeAndExtract ((request , response ) -> {
767
+ T body = readBody (request , response , bodyType , bodyClass );
768
+ try {
769
+ return ResponseEntity .status (response .getStatusCode ())
770
+ .headers (response .getHeaders ())
771
+ .body (body );
772
+ }
773
+ catch (IOException ex ) {
774
+ throw new ResourceAccessException (
775
+ "Could not retrieve response status code: " + ex .getMessage (), ex );
776
+ }
777
+ });
778
+ Assert .state (entity != null , "No ResponseEntity" );
779
+ return entity ;
797
780
}
798
781
799
782
@ Override
800
783
public ResponseEntity <Void > toBodilessEntity () {
801
- try (this .clientResponse ) {
802
- applyStatusHandlers ();
803
- return ResponseEntity .status (this .clientResponse .getStatusCode ())
804
- .headers (this .clientResponse .getHeaders ())
805
- .build ();
806
- }
807
- catch (UncheckedIOException ex ) {
808
- throw new ResourceAccessException ("Could not retrieve response status code: " + ex .getMessage (), ex .getCause ());
809
- }
810
- catch (IOException ex ) {
811
- throw new ResourceAccessException ("Could not retrieve response status code: " + ex .getMessage (), ex );
812
- }
784
+ ResponseEntity <Void > entity = executeAndExtract ((request , response ) -> {
785
+ try (response ) {
786
+ applyStatusHandlers (request , response );
787
+ return ResponseEntity .status (response .getStatusCode ())
788
+ .headers (response .getHeaders ())
789
+ .build ();
790
+ }
791
+ catch (UncheckedIOException ex ) {
792
+ throw new ResourceAccessException (
793
+ "Could not retrieve response status code: " + ex .getMessage (), ex .getCause ());
794
+ }
795
+ catch (IOException ex ) {
796
+ throw new ResourceAccessException (
797
+ "Could not retrieve response status code: " + ex .getMessage (), ex );
798
+ }
799
+ });
800
+ Assert .state (entity != null , "No ResponseEntity" );
801
+ return entity ;
813
802
}
814
803
804
+ @ Nullable
805
+ public <T > T executeAndExtract (RequestHeadersSpec .ExchangeFunction <T > exchangeFunction ) {
806
+ return this .requestHeadersSpec .exchange (exchangeFunction );
807
+ }
815
808
816
809
@ Nullable
817
- private <T > T readBody (Type bodyType , Class <T > bodyClass ) {
818
- return DefaultRestClient .this .readWithMessageConverters (this . clientResponse , this :: applyStatusHandlers ,
819
- bodyType , bodyClass , getCurrentObservation () );
810
+ private <T > T readBody (HttpRequest request , ClientHttpResponse response , Type bodyType , Class <T > bodyClass ) {
811
+ return DefaultRestClient .this .readWithMessageConverters (
812
+ response , () -> applyStatusHandlers ( request , response ), bodyType , bodyClass );
820
813
821
814
}
822
815
823
- private void applyStatusHandlers () {
816
+ private void applyStatusHandlers (HttpRequest request , ClientHttpResponse response ) {
824
817
try {
825
- ClientHttpResponse response = this .clientResponse ;
826
818
if (response instanceof DefaultConvertibleClientHttpResponse convertibleResponse ) {
827
819
response = convertibleResponse .delegate ;
828
820
}
829
821
for (StatusHandler handler : this .statusHandlers ) {
830
822
if (handler .test (response )) {
831
- handler .handle (this . clientRequest , response );
823
+ handler .handle (request , response );
832
824
return ;
833
825
}
834
826
}
@@ -838,44 +830,29 @@ private void applyStatusHandlers() {
838
830
}
839
831
}
840
832
841
- @ Nullable
842
- private Observation getCurrentObservation () {
843
- if (this .clientResponse instanceof DefaultConvertibleClientHttpResponse convertibleResponse ) {
844
- return convertibleResponse .observation ;
845
- }
846
- return null ;
847
- }
848
-
849
833
}
850
834
851
835
852
836
private class DefaultConvertibleClientHttpResponse implements RequestHeadersSpec .ConvertibleClientHttpResponse {
853
837
854
838
private final ClientHttpResponse delegate ;
855
839
856
- private final Observation observation ;
857
-
858
- private final Observation .Scope observationScope ;
859
-
860
- public DefaultConvertibleClientHttpResponse (ClientHttpResponse delegate , Observation observation , Observation .Scope observationScope ) {
840
+ public DefaultConvertibleClientHttpResponse (ClientHttpResponse delegate ) {
861
841
this .delegate = delegate ;
862
- this .observation = observation ;
863
- this .observationScope = observationScope ;
864
842
}
865
843
866
-
867
844
@ Nullable
868
845
@ Override
869
846
public <T > T bodyTo (Class <T > bodyType ) {
870
- return readWithMessageConverters (this .delegate , () -> {} , bodyType , bodyType , this . observation );
847
+ return readWithMessageConverters (this .delegate , () -> {} , bodyType , bodyType );
871
848
}
872
849
873
850
@ Nullable
874
851
@ Override
875
852
public <T > T bodyTo (ParameterizedTypeReference <T > bodyType ) {
876
853
Type type = bodyType .getType ();
877
854
Class <T > bodyClass = bodyClass (type );
878
- return readWithMessageConverters (this .delegate , () -> {}, type , bodyClass , this . observation );
855
+ return readWithMessageConverters (this .delegate , () -> {}, type , bodyClass );
879
856
}
880
857
881
858
@ Override
@@ -901,8 +878,6 @@ public String getStatusText() throws IOException {
901
878
@ Override
902
879
public void close () {
903
880
this .delegate .close ();
904
- this .observationScope .close ();
905
- this .observation .stop ();
906
881
}
907
882
908
883
}
0 commit comments