3333
3434import org .springframework .beans .BeanUtils ;
3535import org .springframework .http .client .AbstractClientHttpRequestFactoryWrapper ;
36+ import org .springframework .http .client .ClientHttpRequest ;
3637import org .springframework .http .client .ClientHttpRequestFactory ;
3738import org .springframework .http .client .ClientHttpRequestInterceptor ;
39+ import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
40+ import org .springframework .http .client .SimpleClientHttpRequestFactory ;
3841import org .springframework .http .converter .HttpMessageConverter ;
3942import org .springframework .util .Assert ;
4043import org .springframework .util .CollectionUtils ;
@@ -508,11 +511,13 @@ public RestTemplateBuilder setReadTimeout(Duration readTimeout) {
508511 }
509512
510513 /**
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 .
513516 * @param bufferRequestBody value of the bufferRequestBody parameter
514517 * @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)
516521 */
517522 public RestTemplateBuilder setBufferRequestBody (boolean bufferRequestBody ) {
518523 return new RestTemplateBuilder (this .detectRequestFactory , this .rootUri ,
@@ -634,52 +639,46 @@ private static class RequestFactoryCustomizer
634639
635640 private final Duration readTimeout ;
636641
637- private final boolean bufferRequestBody ;
638-
639- private final boolean bufferRequestBodyFlag ;
642+ private final Boolean bufferRequestBody ;
640643
641644 RequestFactoryCustomizer () {
642- this (null , null , true , false );
645+ this (null , null , null );
643646 }
644647
645648 private RequestFactoryCustomizer (Duration connectTimeout , Duration readTimeout ,
646- boolean bufferRequestBody , boolean bufferRequestBodyFlag ) {
649+ Boolean bufferRequestBody ) {
647650 this .connectTimeout = connectTimeout ;
648651 this .readTimeout = readTimeout ;
649652 this .bufferRequestBody = bufferRequestBody ;
650- this .bufferRequestBodyFlag = bufferRequestBodyFlag ;
651653 }
652654
653655 public RequestFactoryCustomizer connectTimeout (Duration connectTimeout ) {
654656 return new RequestFactoryCustomizer (connectTimeout , this .readTimeout ,
655- this .bufferRequestBody , this . bufferRequestBodyFlag );
657+ this .bufferRequestBody );
656658 }
657659
658660 public RequestFactoryCustomizer readTimeout (Duration readTimeout ) {
659661 return new RequestFactoryCustomizer (this .connectTimeout , readTimeout ,
660- this .bufferRequestBody , this . bufferRequestBodyFlag );
662+ this .bufferRequestBody );
661663 }
662664
663665 public RequestFactoryCustomizer bufferRequestBody (boolean bufferRequestBody ) {
664666 return new RequestFactoryCustomizer (this .connectTimeout , this .readTimeout ,
665- bufferRequestBody , true );
667+ bufferRequestBody );
666668 }
667669
668670 @ Override
669671 public void accept (ClientHttpRequestFactory requestFactory ) {
670672 ClientHttpRequestFactory unwrappedRequestFactory = unwrapRequestFactoryIfNecessary (
671673 requestFactory );
672674 if (this .connectTimeout != null ) {
673- new TimeoutRequestFactoryCustomizer (this .connectTimeout ,
674- "setConnectTimeout" ).customize (unwrappedRequestFactory );
675+ setConnectTimeout (unwrappedRequestFactory );
675676 }
676677 if (this .readTimeout != null ) {
677- new TimeoutRequestFactoryCustomizer (this .readTimeout , "setReadTimeout" )
678- .customize (unwrappedRequestFactory );
678+ setReadTimeout (unwrappedRequestFactory );
679679 }
680- if (this .bufferRequestBodyFlag ) {
681- new BufferRequestBodyFactoryCustomizer (this .bufferRequestBody ,
682- "setBufferRequestBody" ).customize (unwrappedRequestFactory );
680+ if (this .bufferRequestBody != null ) {
681+ setBufferRequestBody (unwrappedRequestFactory );
683682 }
684683 }
685684
@@ -699,68 +698,37 @@ private ClientHttpRequestFactory unwrapRequestFactoryIfNecessary(
699698 return unwrappedRequestFactory ;
700699 }
701700
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 );
731705 }
732706
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+ }
748712
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+ }
753717
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 ;
762724 }
725+ throw new IllegalStateException ("Request factory " + requestFactory .getClass ()
726+ + " does not have a suitable " + methodName + " method" );
727+ }
763728
729+ private void invoke (ClientHttpRequestFactory requestFactory , Method method ,
730+ Object ... parameters ) {
731+ ReflectionUtils .invokeMethod (method , requestFactory , parameters );
764732 }
765733
766734 }
0 commit comments