33
33
34
34
import org .springframework .beans .BeanUtils ;
35
35
import org .springframework .http .client .AbstractClientHttpRequestFactoryWrapper ;
36
+ import org .springframework .http .client .ClientHttpRequest ;
36
37
import org .springframework .http .client .ClientHttpRequestFactory ;
37
38
import org .springframework .http .client .ClientHttpRequestInterceptor ;
39
+ import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
40
+ import org .springframework .http .client .SimpleClientHttpRequestFactory ;
38
41
import org .springframework .http .converter .HttpMessageConverter ;
39
42
import org .springframework .util .Assert ;
40
43
import org .springframework .util .CollectionUtils ;
@@ -508,11 +511,13 @@ public RestTemplateBuilder setReadTimeout(Duration readTimeout) {
508
511
}
509
512
510
513
/**
511
- * Sets the bufferrequestbody value on the underlying
512
- * {@link ClientHttpRequestFactory} .
514
+ * Sets if the underling {@link ClientHttpRequestFactory} should buffer the
515
+ * {@linkplain ClientHttpRequest#getBody() request body} internally .
513
516
* @param bufferRequestBody value of the bufferRequestBody parameter
514
517
* @return a new builder instance.
515
- * @since 2.1.0
518
+ * @since 2.2.0
519
+ * @see SimpleClientHttpRequestFactory#setBufferRequestBody(boolean)
520
+ * @see HttpComponentsClientHttpRequestFactory#setBufferRequestBody(boolean)
516
521
*/
517
522
public RestTemplateBuilder setBufferRequestBody (boolean bufferRequestBody ) {
518
523
return new RestTemplateBuilder (this .detectRequestFactory , this .rootUri ,
@@ -634,52 +639,46 @@ private static class RequestFactoryCustomizer
634
639
635
640
private final Duration readTimeout ;
636
641
637
- private final boolean bufferRequestBody ;
638
-
639
- private final boolean bufferRequestBodyFlag ;
642
+ private final Boolean bufferRequestBody ;
640
643
641
644
RequestFactoryCustomizer () {
642
- this (null , null , true , false );
645
+ this (null , null , null );
643
646
}
644
647
645
648
private RequestFactoryCustomizer (Duration connectTimeout , Duration readTimeout ,
646
- boolean bufferRequestBody , boolean bufferRequestBodyFlag ) {
649
+ Boolean bufferRequestBody ) {
647
650
this .connectTimeout = connectTimeout ;
648
651
this .readTimeout = readTimeout ;
649
652
this .bufferRequestBody = bufferRequestBody ;
650
- this .bufferRequestBodyFlag = bufferRequestBodyFlag ;
651
653
}
652
654
653
655
public RequestFactoryCustomizer connectTimeout (Duration connectTimeout ) {
654
656
return new RequestFactoryCustomizer (connectTimeout , this .readTimeout ,
655
- this .bufferRequestBody , this . bufferRequestBodyFlag );
657
+ this .bufferRequestBody );
656
658
}
657
659
658
660
public RequestFactoryCustomizer readTimeout (Duration readTimeout ) {
659
661
return new RequestFactoryCustomizer (this .connectTimeout , readTimeout ,
660
- this .bufferRequestBody , this . bufferRequestBodyFlag );
662
+ this .bufferRequestBody );
661
663
}
662
664
663
665
public RequestFactoryCustomizer bufferRequestBody (boolean bufferRequestBody ) {
664
666
return new RequestFactoryCustomizer (this .connectTimeout , this .readTimeout ,
665
- bufferRequestBody , true );
667
+ bufferRequestBody );
666
668
}
667
669
668
670
@ Override
669
671
public void accept (ClientHttpRequestFactory requestFactory ) {
670
672
ClientHttpRequestFactory unwrappedRequestFactory = unwrapRequestFactoryIfNecessary (
671
673
requestFactory );
672
674
if (this .connectTimeout != null ) {
673
- new TimeoutRequestFactoryCustomizer (this .connectTimeout ,
674
- "setConnectTimeout" ).customize (unwrappedRequestFactory );
675
+ setConnectTimeout (unwrappedRequestFactory );
675
676
}
676
677
if (this .readTimeout != null ) {
677
- new TimeoutRequestFactoryCustomizer (this .readTimeout , "setReadTimeout" )
678
- .customize (unwrappedRequestFactory );
678
+ setReadTimeout (unwrappedRequestFactory );
679
679
}
680
- if (this .bufferRequestBodyFlag ) {
681
- new BufferRequestBodyFactoryCustomizer (this .bufferRequestBody ,
682
- "setBufferRequestBody" ).customize (unwrappedRequestFactory );
680
+ if (this .bufferRequestBody != null ) {
681
+ setBufferRequestBody (unwrappedRequestFactory );
683
682
}
684
683
}
685
684
@@ -699,68 +698,37 @@ private ClientHttpRequestFactory unwrapRequestFactoryIfNecessary(
699
698
return unwrappedRequestFactory ;
700
699
}
701
700
702
- /**
703
- * {@link ClientHttpRequestFactory} customizer to call a "set timeout" method.
704
- */
705
- private static final class TimeoutRequestFactoryCustomizer {
706
-
707
- private final Duration timeout ;
708
-
709
- private final String methodName ;
710
-
711
- TimeoutRequestFactoryCustomizer (Duration timeout , String methodName ) {
712
- this .timeout = timeout ;
713
- this .methodName = methodName ;
714
- }
715
-
716
- void customize (ClientHttpRequestFactory factory ) {
717
- ReflectionUtils .invokeMethod (findMethod (factory ), factory ,
718
- Math .toIntExact (this .timeout .toMillis ()));
719
- }
720
-
721
- private Method findMethod (ClientHttpRequestFactory factory ) {
722
- Method method = ReflectionUtils .findMethod (factory .getClass (),
723
- this .methodName , int .class );
724
- if (method != null ) {
725
- return method ;
726
- }
727
- throw new IllegalStateException ("Request factory " + factory .getClass ()
728
- + " does not have a " + this .methodName + "(int) method" );
729
- }
730
-
701
+ private void setConnectTimeout (ClientHttpRequestFactory factory ) {
702
+ Method method = findMethod (factory , "setConnectTimeout" , int .class );
703
+ int timeout = Math .toIntExact (this .connectTimeout .toMillis ());
704
+ invoke (factory , method , timeout );
731
705
}
732
706
733
- /**
734
- * {@link ClientHttpRequestFactory} customizer to call a "set buffer request body"
735
- * method.
736
- */
737
- private static final class BufferRequestBodyFactoryCustomizer {
738
-
739
- private final boolean bufferRequestBody ;
740
-
741
- private final String methodName ;
742
-
743
- BufferRequestBodyFactoryCustomizer (boolean bufferRequestBody ,
744
- String methodName ) {
745
- this .bufferRequestBody = bufferRequestBody ;
746
- this .methodName = methodName ;
747
- }
707
+ private void setReadTimeout (ClientHttpRequestFactory factory ) {
708
+ Method method = findMethod (factory , "setReadTimeout" , int .class );
709
+ int timeout = Math .toIntExact (this .readTimeout .toMillis ());
710
+ invoke (factory , method , timeout );
711
+ }
748
712
749
- void customize (ClientHttpRequestFactory factory ) {
750
- ReflectionUtils . invokeMethod ( findMethod (factory ), factory ,
751
- this .bufferRequestBody );
752
- }
713
+ private void setBufferRequestBody (ClientHttpRequestFactory factory ) {
714
+ Method method = findMethod (factory , "setBufferRequestBody" , boolean . class );
715
+ invoke ( factory , method , this .bufferRequestBody );
716
+ }
753
717
754
- private Method findMethod (ClientHttpRequestFactory factory ) {
755
- Method method = ReflectionUtils .findMethod (factory .getClass (),
756
- this .methodName , boolean .class );
757
- if (method != null ) {
758
- return method ;
759
- }
760
- throw new IllegalStateException ("Request factory " + factory .getClass ()
761
- + " does not have a " + this .methodName + "(boolean) method" );
718
+ private Method findMethod (ClientHttpRequestFactory requestFactory ,
719
+ String methodName , Class <?>... parameters ) {
720
+ Method method = ReflectionUtils .findMethod (requestFactory .getClass (),
721
+ methodName , parameters );
722
+ if (method != null ) {
723
+ return method ;
762
724
}
725
+ throw new IllegalStateException ("Request factory " + requestFactory .getClass ()
726
+ + " does not have a suitable " + methodName + " method" );
727
+ }
763
728
729
+ private void invoke (ClientHttpRequestFactory requestFactory , Method method ,
730
+ Object ... parameters ) {
731
+ ReflectionUtils .invokeMethod (method , requestFactory , parameters );
764
732
}
765
733
766
734
}
0 commit comments