@ -25,14 +25,17 @@ import java.util.ArrayList;
import java.util.Arrays ;
import java.util.Collection ;
import java.util.Collections ;
import java.util.LinkedHashMap ;
import java.util.LinkedHashSet ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import java.util.function.Consumer ;
import java.util.function.Supplier ;
import reactor.netty.http.client.HttpClientRequest ;
import org.springframework.beans.BeanUtils ;
import org.springframework.http.HttpHeaders ;
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper ;
import org.springframework.http.client.ClientHttpRequest ;
import org.springframework.http.client.ClientHttpRequestFactory ;
@ -70,25 +73,29 @@ import org.springframework.web.util.UriTemplateHandler;
* /
public class RestTemplateBuilder {
private final RequestFactoryCustomizer requestFactoryCustomizer ;
private final boolean detectRequestFactory ;
private final String rootUri ;
private final Set < HttpMessageConverter < ? > > messageConverters ;
private final Supplier < ClientHttpRequestFactory > requestFactorySupplier ;
private final Set < ClientHttpRequestInterceptor > interceptors ;
private final Supplier < ClientHttpRequestFactory > requestFactory ;
private final UriTemplateHandler uriTemplateHandler ;
private final ResponseErrorHandler errorHandler ;
private final Set< RestTemplateCustomizer > restTemplateCustomizers ;
private final BasicAuthentication basicAuthentication ;
private final RequestFactoryCustomizer requestFactoryCustomizer ;
private final Map< String , String > defaultHeaders ;
private final Set < ClientHttpRequestInterceptor> intercepto rs;
private final Set < RestTemplateCustomizer> customize rs;
private final Set < HttpHeadersCustomizer> httpHeaders Customizers;
private final Set < RestTemplateRequestCustomizer< ? > > request Customizers;
/ * *
* Create a new { @link RestTemplateBuilder } instance .
@ -97,33 +104,38 @@ public class RestTemplateBuilder {
* /
public RestTemplateBuilder ( RestTemplateCustomizer . . . customizers ) {
Assert . notNull ( customizers , "Customizers must not be null" ) ;
this . requestFactoryCustomizer = new RequestFactoryCustomizer ( ) ;
this . detectRequestFactory = true ;
this . rootUri = null ;
this . messageConverters = null ;
this . requestFactorySupplier = null ;
this . interceptors = Collections . emptySet ( ) ;
this . requestFactory = null ;
this . uriTemplateHandler = null ;
this . errorHandler = null ;
this . restTemplateCustomizers = Collections . unmodifiableSet ( new LinkedHashSet < > ( Arrays . asList ( customizers ) ) ) ;
this . requestFactoryCustomizer = new RequestFactoryCustomizer ( ) ;
this . interceptors = Collections . emptySet ( ) ;
this . httpHeaders Customizers = Collections . emptySet ( ) ;
this . basicAuthentication = null ;
this . defaultHeaders = Collections . emptyMap ( ) ;
this . customizers = setOf ( customizers ) ;
this . request Customizers = Collections . emptySet ( ) ;
}
private RestTemplateBuilder ( boolean detectRequestFactory , String rootUri ,
Set < HttpMessageConverter < ? > > messageConverters , Supplier < ClientHttpRequestFactory > requestFactorySupplier ,
private RestTemplateBuilder ( RequestFactoryCustomizer requestFactoryCustomizer , boolean detectRequestFactory ,
String rootUri , Set < HttpMessageConverter < ? > > messageConverters ,
Set < ClientHttpRequestInterceptor > interceptors , Supplier < ClientHttpRequestFactory > requestFactorySupplier ,
UriTemplateHandler uriTemplateHandler , ResponseErrorHandler errorHandler ,
Set < RestTemplateCustomizer > restTemplateCustomizers , RequestFactoryCustomizer requestFactoryCustomizer ,
Set < ClientHttpRequestInterceptor > interceptors , Set < HttpHeadersCustomizer > httpHeadersCustomizers ) {
BasicAuthentication basicAuthentication , Map < String , String > defaultHeaders ,
Set < RestTemplateCustomizer > customizers , Set < RestTemplateRequestCustomizer < ? > > requestCustomizers ) {
this . requestFactoryCustomizer = requestFactoryCustomizer ;
this . detectRequestFactory = detectRequestFactory ;
this . rootUri = rootUri ;
this . messageConverters = messageConverters ;
this . requestFactorySupplier = requestFactorySupplier ;
this . interceptors = interceptors ;
this . requestFactory = requestFactorySupplier ;
this . uriTemplateHandler = uriTemplateHandler ;
this . errorHandler = errorHandler ;
this . restTemplateCustomizers = restTemplateCustomizers ;
this . requestFactoryCustomizer = requestFactoryCustomizer ;
this . interceptors = intercepto rs;
this . httpHeadersCustomizers = httpHeaders Customizers;
this . basicAuthentication = basicAuthentication ;
this . defaultHeaders = defaultHeaders ;
this . customizers = customize rs;
this . requestCustomizers = request Customizers;
}
/ * *
@ -134,9 +146,10 @@ public class RestTemplateBuilder {
* @return a new builder instance
* /
public RestTemplateBuilder detectRequestFactory ( boolean detectRequestFactory ) {
return new RestTemplateBuilder ( detectRequestFactory , this . rootUri , this . messageConverters ,
this . requestFactorySupplier , this . uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer , this . interceptors , this . httpHeadersCustomizers ) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer , detectRequestFactory , this . rootUri ,
this . messageConverters , this . interceptors , this . requestFactory , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthentication , this . defaultHeaders , this . customizers ,
this . requestCustomizers ) ;
}
/ * *
@ -146,9 +159,10 @@ public class RestTemplateBuilder {
* @return a new builder instance
* /
public RestTemplateBuilder rootUri ( String rootUri ) {
return new RestTemplateBuilder ( this . detectRequestFactory , rootUri , this . messageConverters ,
this . requestFactorySupplier , this . uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer , this . interceptors , this . httpHeadersCustomizers ) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer , this . detectRequestFactory , rootUri ,
this . messageConverters , this . interceptors , this . requestFactory , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthentication , this . defaultHeaders , this . customizers ,
this . requestCustomizers ) ;
}
/ * *
@ -176,10 +190,10 @@ public class RestTemplateBuilder {
* /
public RestTemplateBuilder messageConverters ( Collection < ? extends HttpMessageConverter < ? > > messageConverters ) {
Assert . notNull ( messageConverters , "MessageConverters must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory, this . rootUri ,
Collections. unmodifiableSet ( new LinkedHashSet < HttpMessageConverter < ? > > ( messageConverters ) ) ,
this . requestFactorySupplier, this . uriTemplateHandler , this . errorHandler , this . restTemplateC ustomizers,
this . request FactoryCustomizer, this . interceptors , this . httpHeaders Customizers) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer, this . detectRequestFactory, this . rootUri ,
setOf( messageConverters ) , this . interceptors , this . requestFactory , this . uriTemplateHandler ,
this . errorHandler, this . basicAuthentication , this . defaultHeaders , this . c ustomizers,
this . request Customizers) ;
}
/ * *
@ -192,22 +206,7 @@ public class RestTemplateBuilder {
* /
public RestTemplateBuilder additionalMessageConverters ( HttpMessageConverter < ? > . . . messageConverters ) {
Assert . notNull ( messageConverters , "MessageConverters must not be null" ) ;
return additionalMessageConverters ( true , messageConverters ) ;
}
/ * *
* Add additional { @link HttpMessageConverter HttpMessageConverters } that should be
* used with the { @link RestTemplate } . Any converters configured on the builder will
* replace RestTemplate ' s default converters .
* @param append if true adds converters to the end otherwise to the beginning
* @param messageConverters the converters to add
* @return a new builder instance
* @see # messageConverters ( HttpMessageConverter . . . )
* /
public RestTemplateBuilder additionalMessageConverters ( boolean append ,
HttpMessageConverter < ? > . . . messageConverters ) {
Assert . notNull ( messageConverters , "MessageConverters must not be null" ) ;
return additionalMessageConverters ( append , Arrays . asList ( messageConverters ) ) ;
return additionalMessageConverters ( Arrays . asList ( messageConverters ) ) ;
}
/ * *
@ -221,26 +220,10 @@ public class RestTemplateBuilder {
public RestTemplateBuilder additionalMessageConverters (
Collection < ? extends HttpMessageConverter < ? > > messageConverters ) {
Assert . notNull ( messageConverters , "MessageConverters must not be null" ) ;
return additionalMessageConverters ( true , messageConverters ) ;
}
/ * *
* Add additional { @link HttpMessageConverter HttpMessageConverters } that should be
* used with the { @link RestTemplate } . Any converters configured on the builder will
* replace RestTemplate ' s default converters .
* @param append if true adds converters to the end otherwise to the beginning
* @param messageConverters the converters to add
* @return a new builder instance
* @see # messageConverters ( HttpMessageConverter . . . )
* /
public RestTemplateBuilder additionalMessageConverters ( boolean append ,
Collection < ? extends HttpMessageConverter < ? > > messageConverters ) {
Assert . notNull ( messageConverters , "MessageConverters must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
append ? append ( this . messageConverters , messageConverters )
: append ( messageConverters , this . messageConverters ) ,
this . requestFactorySupplier , this . uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer , this . interceptors , this . httpHeadersCustomizers ) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer , this . detectRequestFactory , this . rootUri ,
append ( this . messageConverters , messageConverters ) , this . interceptors , this . requestFactory ,
this . uriTemplateHandler , this . errorHandler , this . basicAuthentication , this . defaultHeaders ,
this . customizers , this . requestCustomizers ) ;
}
/ * *
@ -251,10 +234,10 @@ public class RestTemplateBuilder {
* @see # messageConverters ( HttpMessageConverter . . . )
* /
public RestTemplateBuilder defaultMessageConverters ( ) {
return new RestTemplateBuilder ( this . detectRequestFactory, this . rootUri ,
Collections. unmodifiableSet ( new LinkedHashSet < > ( new RestTemplate ( ) . getMessageConverters ( ) ) ) ,
this . requestFactorySupplier, this . uriTemplateHandler, this . errorHandler , this . restTemplateCustomiz ers,
this . requestFactoryCustomizer, this . interceptors , this . httpHeaders Customizers) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer, this . detectRequestFactory, this . rootUri ,
setOf ( new RestTemplate ( ) . getMessageConverters ( ) ) , this . interceptors , this . requestFactory ,
this . uriTemplateHandler, this . errorHandler , this . basicAuthentication, this . defaultHead ers,
this . customizers, this . request Customizers) ;
}
/ * *
@ -282,10 +265,10 @@ public class RestTemplateBuilder {
* /
public RestTemplateBuilder interceptors ( Collection < ClientHttpRequestInterceptor > interceptors ) {
Assert . notNull ( interceptors , "interceptors must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory, this . rootUri , this . messageConverters ,
this . requestFactorySupplier, this . uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer, Collections . unmodifiableSet ( new LinkedHashSet < > ( interceptors ) ) ,
this . httpHeaders Customizers) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer, this . detectRequestFactory, this . rootUri ,
this . messageConverters, setOf ( interceptors ) , this . requestFactory , this . uriTemplateHandler ,
this . errorHandler, this . basicAuthentication , this . defaultHeaders , this . customizers ,
this . request Customizers) ;
}
/ * *
@ -311,9 +294,10 @@ public class RestTemplateBuilder {
* /
public RestTemplateBuilder additionalInterceptors ( Collection < ? extends ClientHttpRequestInterceptor > interceptors ) {
Assert . notNull ( interceptors , "interceptors must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri , this . messageConverters ,
this . requestFactorySupplier , this . uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer , append ( this . interceptors , interceptors ) , this . httpHeadersCustomizers ) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer , this . detectRequestFactory , this . rootUri ,
this . messageConverters , append ( this . interceptors , interceptors ) , this . requestFactory ,
this . uriTemplateHandler , this . errorHandler , this . basicAuthentication , this . defaultHeaders ,
this . customizers , this . requestCustomizers ) ;
}
/ * *
@ -341,15 +325,15 @@ public class RestTemplateBuilder {
/ * *
* Set the { @code Supplier } of { @link ClientHttpRequestFactory } that should be called
* each time we { @link # build ( ) } a new { @link RestTemplate } instance .
* @param requestFactory Supplier the supplier for the request factory
* @param requestFactory the supplier for the request factory
* @return a new builder instance
* @since 2.0 .0
* /
public RestTemplateBuilder requestFactory ( Supplier < ClientHttpRequestFactory > requestFactory Supplier ) {
Assert . notNull ( requestFactory Supplier , "RequestFactory Supplier must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory, this . rootUri , this . messageConverters ,
requestFactory Supplier , this . uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer, this . interceptors , this . httpHeaders Customizers) ;
public RestTemplateBuilder requestFactory ( Supplier < ClientHttpRequestFactory > requestFactory ) {
Assert . notNull ( requestFactory , "RequestFactory Supplier must not be null" ) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer, this . detectRequestFactory, this . rootUri ,
this . messageConverters , this . interceptors , requestFactory , this . uriTemplateHandler , this . errorHandler ,
this . basicAuthentication, this . defaultHeaders , this . customizers , this . request Customizers) ;
}
/ * *
@ -360,9 +344,9 @@ public class RestTemplateBuilder {
* /
public RestTemplateBuilder uriTemplateHandler ( UriTemplateHandler uriTemplateHandler ) {
Assert . notNull ( uriTemplateHandler , "UriTemplateHandler must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory, this . rootUri , this . messageConverters ,
this . requestFactorySupplier , uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer, this . interceptors , this . httpHeaders Customizers) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer, this . detectRequestFactory, this . rootUri ,
this . messageConverters, this . interceptors , this . requestFactory, uriTemplateHandler , this . errorHandler ,
this . basicAuthentication, this . defaultHeaders , this . customizers , this . request Customizers) ;
}
/ * *
@ -373,156 +357,95 @@ public class RestTemplateBuilder {
* /
public RestTemplateBuilder errorHandler ( ResponseErrorHandler errorHandler ) {
Assert . notNull ( errorHandler , "ErrorHandler must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory, this . rootUri , this . messageConverters ,
this . requestFactorySupplier , this . uriTemplateHandler , errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer, this . interceptors , this . httpHeaders Customizers) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer, this . detectRequestFactory, this . rootUri ,
this . messageConverters, this . interceptors , this . requestFactory, this . uriTemplateHandler , errorHandler ,
this . basicAuthentication, this . defaultHeaders , this . customizers , this . request Customizers) ;
}
/ * *
* Add a header to requests with the given value unless a custom header has been set
* before .
* @param header the header
* @param value the value
* @return a new builder instance
* @see # additionalHttpHeadersCustomizers ( HttpHeadersCustomizer . . . )
* /
public RestTemplateBuilder defaultHeader ( String header , String value ) {
return additionalHttpHeadersCustomizers ( SimpleHttpHeaderDefaultingCustomizer . singleHeader ( header , value ) ) ;
}
/ * *
* Add a headers to requests with the given value unless a custom header has been set
* before .
* @param headers the headers
* @return a new builder instance
* @see # additionalHttpHeadersCustomizers ( HttpHeadersCustomizer . . . )
* /
public RestTemplateBuilder defaultHeaders ( HttpHeaders headers ) {
return additionalHttpHeadersCustomizers ( new SimpleHttpHeaderDefaultingCustomizer ( headers ) ) ;
}
/ * *
* Set the { @link HttpHeadersCustomizer HttpHeadersCustomizers } that should be applied
* to the { @link HttpHeaders } on the request . Customizers are applied in the order
* that they were added . Setting this value will replace any previously configured
* http headers customizers .
* @param httpHeadersCustomizers the customizers to set
* @return a new builder instance
* @see # additionalHttpHeadersCustomizers ( HttpHeadersCustomizer . . . )
* /
public RestTemplateBuilder httpHeadersCustomizers ( HttpHeadersCustomizer . . . httpHeadersCustomizers ) {
Assert . notNull ( httpHeadersCustomizers , "HttpHeadersCustomizers must not be null" ) ;
return httpHeadersCustomizers ( Arrays . asList ( httpHeadersCustomizers ) ) ;
}
/ * *
* Set the { @link HttpHeadersCustomizer HttpHeadersCustomizers } that should be applied
* to the { @link HttpHeaders } on the request . Customizers are applied in the order
* that they were added . Setting this value will replace any previously configured
* http headers customizers .
* @param httpHeadersCustomizers the customizers to set
* @return a new builder instance
* @see # additionalHttpHeadersCustomizers ( HttpHeadersCustomizer . . . )
* /
public RestTemplateBuilder httpHeadersCustomizers (
Collection < ? extends HttpHeadersCustomizer > httpHeadersCustomizers ) {
Assert . notNull ( httpHeadersCustomizers , "HttpHeadersCustomizers must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri , this . messageConverters ,
this . requestFactorySupplier , this . uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer , this . interceptors ,
Collections . unmodifiableSet ( new LinkedHashSet < > ( this . httpHeadersCustomizers ) ) ) ;
}
/ * *
* Add the { @link HttpHeadersCustomizer HttpHeadersCustomizers } that should be applied
* to the { @link HttpHeaders } on the request . Customizers are applied in the order
* that they were added .
* @param httpHeadersCustomizers the customizers to set
* Add HTTP Basic Authentication to requests with the given username / password pair ,
* unless a custom Authorization header has been set before .
* @param username the user name
* @param password the password
* @return a new builder instance
* @see # httpHeadersCustomizers ( HttpHeadersCustomizer . . . )
* @since 2.1 .0
* @see # basicAuthentication ( String , String , Charset )
* /
public RestTemplateBuilder additionalHttpHeadersCustomizers ( HttpHeadersCustomizer . . . httpHeadersCustomizers ) {
Assert . notNull ( httpHeadersCustomizers , "HttpHeadersCustomizers must not be null" ) ;
return additionalHttpHeadersCustomizers ( true , httpHeadersCustomizers ) ;
public RestTemplateBuilder basicAuthentication ( String username , String password ) {
return basicAuthentication ( username , password , null ) ;
}
/ * *
* Add the { @link HttpHeadersCustomizer HttpHeadersCustomizers } that should be applied
* to the { @link HttpHeaders } on the request . Customizers are applied in the order
* that they were added .
* @param append if true adds customizers to the end otherwise to the beginning
* @param httpHeadersCustomizers the customizers to set
* Add HTTP Basic Authentication to requests with the given username / password pair ,
* unless a custom Authorization header has been set before .
* @param username the user name
* @param password the password
* @param charset the charset to use
* @return a new builder instance
* @s ee # httpHeadersCustomizers ( HttpHeadersCustomizer . . . )
* @since 2.2 .0
* /
public RestTemplateBuilder additionalHttpHeadersCustomizers ( boolean append ,
HttpHeadersCustomizer . . . httpHeadersCustomizers ) {
Assert . notNull ( httpHeadersCustomizers , "HttpHeadersCustomizers must not be null" ) ;
return additionalHttpHeadersCustomizers ( append , Arrays . asList ( httpHeadersCustomizers ) ) ;
public RestTemplateBuilder basicAuthentication ( String username , String password , Charset charset ) {
return new RestTemplateBuilder ( this . requestFactoryCustomizer , this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . interceptors , this . requestFactory , this . uriTemplateHandler ,
this . errorHandler , new BasicAuthentication ( username , password , charset ) , this . defaultHeaders ,
this . customizers , this . requestCustomizers ) ;
}
/ * *
* Add the { @link HttpHeadersCustomizer HttpHeadersCustomizers } that should be applied
* to the { @link HttpHeaders } on the request . Customizers are applied in the order
* that they were added .
* @param httpHeadersCustomizers the customizers to set
* Add a default header that will be set if not already present on the outgoing
* { @link HttpClientRequest } .
* @param name the name of the header
* @param value the header value
* @return a new builder instance
* @s ee # httpHeadersCustomizers ( HttpHeadersCustomizer . . . )
* @since 2.2 .0
* /
public RestTemplateBuilder additionalHttpHeadersCustomizers (
Collection < ? extends HttpHeadersCustomizer > httpHeadersCustomizers ) {
return additionalHttpHeadersCustomizers ( true , httpHeadersCustomizers ) ;
public RestTemplateBuilder defaultHeader ( String name , String value ) {
return new RestTemplateBuilder ( this . requestFactoryCustomizer , this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . interceptors , this . requestFactory , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthentication , append ( this . defaultHeaders , name , value ) , this . customizers ,
this . requestCustomizers ) ;
}
/ * *
* Add the { @link HttpHeadersCustomizer HttpHeadersCustomizers } that should be applied
* to the { @link HttpHeaders } on the request . Customizers are applied in the order
* that they were added .
* @param append if true adds customizers to the end otherwise to the beginning
* @param httpHeadersCustomizers the customizers to set
* @return a new builder instance
* @see # httpHeadersCustomizers ( HttpHeadersCustomizer . . . )
* Sets the connection timeout on the underlying { @link ClientHttpRequestFactory } .
* @param connectTimeout the connection timeout
* @return a new builder instance .
* @since 2.1 .0
* /
public RestTemplateBuilder additionalHttpHeadersCustomizers ( boolean append ,
Collection < ? extends HttpHeadersCustomizer > httpHeadersCustomizers ) {
Assert . notNull ( httpHeadersCustomizers , "HttpHeadersCustomizers must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri , this . messageConverters ,
this . requestFactorySupplier , this . uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer , this . interceptors ,
append ? append ( this . httpHeadersCustomizers , httpHeadersCustomizers )
: append ( httpHeadersCustomizers , this . httpHeadersCustomizers ) ) ;
public RestTemplateBuilder setConnectTimeout ( Duration connectTimeout ) {
return new RestTemplateBuilder ( this . requestFactoryCustomizer . connectTimeout ( connectTimeout ) ,
this . detectRequestFactory , this . rootUri , this . messageConverters , this . interceptors , this . requestFactory ,
this . uriTemplateHandler , this . errorHandler , this . basicAuthentication , this . defaultHeaders ,
this . customizers , this . requestCustomizers ) ;
}
/ * *
* Add HTTP Basic Authentication to requests with the given username / password pair ,
* unless a custom Authorization header has been set before .
* @param username the user name
* @param password the password
* @return a new builder instance
* Sets the read timeout on the underlying { @link ClientHttpRequestFactory } .
* @param readTimeout the read timeout
* @return a new builder instance .
* @since 2.1 .0
* @see # basicAuthentication ( String , String , Charset )
* /
public RestTemplateBuilder basicAuthentication ( String username , String password ) {
return basicAuthentication ( username , password , null ) ;
public RestTemplateBuilder setReadTimeout ( Duration readTimeout ) {
return new RestTemplateBuilder ( this . requestFactoryCustomizer . readTimeout ( readTimeout ) ,
this . detectRequestFactory , this . rootUri , this . messageConverters , this . interceptors , this . requestFactory ,
this . uriTemplateHandler , this . errorHandler , this . basicAuthentication , this . defaultHeaders ,
this . customizers , this . requestCustomizers ) ;
}
/ * *
* Add HTTP Basic Authentication to requests with the given username / password pair ,
* unless a custom Authorization header has been set before . Customizer is added to
* the beginning of the { @link HttpHeadersCustomizer HttpHeadersCustomizers }
* collection .
* @param username the user name
* @param password the password
* @param charset the charset to use
* @return a new builder instance
* Sets if the underling { @link ClientHttpRequestFactory } should buffer the
* { @linkplain ClientHttpRequest # getBody ( ) request body } internally .
* @param bufferRequestBody value of the bufferRequestBody parameter
* @return a new builder instance .
* @since 2.2 .0
* @see # additionalHttpHeadersCustomizers ( HttpHeadersCustomizer . . . )
* @see SimpleHttpHeaderDefaultingCustomizer # basicAuthentication ( String , String ,
* Charset )
* @see SimpleClientHttpRequestFactory # setBufferRequestBody ( boolean )
* @see HttpComponentsClientHttpRequestFactory # setBufferRequestBody ( boolean )
* /
public RestTemplateBuilder basicAuthentication ( String username , String password , Charset charset ) {
return additionalHttpHeadersCustomizers ( false ,
SimpleHttpHeaderDefaultingCustomizer . basicAuthentication ( username , password , charset ) ) ;
public RestTemplateBuilder setBufferRequestBody ( boolean bufferRequestBody ) {
return new RestTemplateBuilder ( this . requestFactoryCustomizer . bufferRequestBody ( bufferRequestBody ) ,
this . detectRequestFactory , this . rootUri , this . messageConverters , this . interceptors , this . requestFactory ,
this . uriTemplateHandler , this . errorHandler , this . basicAuthentication , this . defaultHeaders ,
this . customizers , this . requestCustomizers ) ;
}
/ * *
@ -530,13 +453,13 @@ public class RestTemplateBuilder {
* applied to the { @link RestTemplate } . Customizers are applied in the order that they
* were added after builder configuration has been applied . Setting this value will
* replace any previously configured customizers .
* @param restTemplateC ustomizers the customizers to set
* @param c ustomizers the customizers to set
* @return a new builder instance
* @see # additionalCustomizers ( RestTemplateCustomizer . . . )
* /
public RestTemplateBuilder customizers ( RestTemplateCustomizer . . . restTemplateC ustomizers) {
Assert . notNull ( restTemplateCustomizers, "RestTemplate Customizers must not be null") ;
return customizers ( Arrays . asList ( restTemplateC ustomizers) ) ;
public RestTemplateBuilder customizers ( RestTemplateCustomizer . . . c ustomizers) {
Assert . notNull ( customizers, " Customizers must not be null") ;
return customizers ( Arrays . asList ( c ustomizers) ) ;
}
/ * *
@ -544,45 +467,29 @@ public class RestTemplateBuilder {
* applied to the { @link RestTemplate } . Customizers are applied in the order that they
* were added after builder configuration has been applied . Setting this value will
* replace any previously configured customizers .
* @param restTemplateC ustomizers the customizers to set
* @param c ustomizers the customizers to set
* @return a new builder instance
* @see # additionalCustomizers ( RestTemplateCustomizer . . . )
* /
public RestTemplateBuilder customizers ( Collection < ? extends RestTemplateCustomizer > restTemplateCustomizers ) {
Assert . notNull ( restTemplateCustomizers , "RestTemplateCustomizers must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri , this . messageConverters ,
this . requestFactorySupplier , this . uriTemplateHandler , this . errorHandler ,
Collections . unmodifiableSet ( new LinkedHashSet < RestTemplateCustomizer > ( restTemplateCustomizers ) ) ,
this . requestFactoryCustomizer , this . interceptors , this . httpHeadersCustomizers ) ;
}
/ * *
* Add { @link RestTemplateCustomizer RestTemplateCustomizers } that should be applied
* to the { @link RestTemplate } . Customizers are applied in the order that they were
* added after builder configuration has been applied .
* @param restTemplateCustomizers the customizers to add
* @return a new builder instance
* @see # customizers ( RestTemplateCustomizer . . . )
* /
public RestTemplateBuilder additionalCustomizers ( RestTemplateCustomizer . . . restTemplateCustomizers ) {
Assert . notNull ( restTemplateCustomizers , "RestTemplateCustomizers must not be null" ) ;
return additionalCustomizers ( true , restTemplateCustomizers ) ;
public RestTemplateBuilder customizers ( Collection < ? extends RestTemplateCustomizer > customizers ) {
Assert . notNull ( customizers , "Customizers must not be null" ) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer , this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . interceptors , this . requestFactory , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthentication , this . defaultHeaders , setOf ( customizers ) ,
this . requestCustomizers ) ;
}
/ * *
* Add { @link RestTemplateCustomizer RestTemplateCustomizers } that should be applied
* to the { @link RestTemplate } . Customizers are applied in the order that they were
* added after builder configuration has been applied .
* @param append if true adds customizers to the end otherwise to the beginning
* @param restTemplateCustomizers the customizers to add
* @param customizers the customizers to add
* @return a new builder instance
* @see # customizers ( RestTemplateCustomizer . . . )
* /
public RestTemplateBuilder additionalCustomizers ( boolean append ,
RestTemplateCustomizer . . . restTemplateCustomizers ) {
Assert . notNull ( restTemplateCustomizers , "RestTemplateCustomizers must not be null" ) ;
return additionalCustomizers ( append , Arrays . asList ( restTemplateCustomizers ) ) ;
public RestTemplateBuilder additionalCustomizers ( RestTemplateCustomizer . . . customizers ) {
Assert . notNull ( customizers , "Customizers must not be null" ) ;
return additionalCustomizers ( Arrays . asList ( customizers ) ) ;
}
/ * *
@ -595,67 +502,76 @@ public class RestTemplateBuilder {
* /
public RestTemplateBuilder additionalCustomizers ( Collection < ? extends RestTemplateCustomizer > customizers ) {
Assert . notNull ( customizers , "RestTemplateCustomizers must not be null" ) ;
return additionalCustomizers ( true , customizers ) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer , this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . interceptors , this . requestFactory , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthentication , this . defaultHeaders , append ( this . customizers , customizers ) ,
this . requestCustomizers ) ;
}
/ * *
* Add { @link RestTemplateCustomizer RestTemplateCustomizers } that should be applied
* to the { @link RestTemplate } . Customizers are applied in the order that they were
* added after builder configuration has been applied .
* @param append if true adds customizers to the end otherwise to the beginning
* @param customizers the customizers to add
* Set the { @link RestTemplateRequestCustomizer RestTemplateRequestCustomizers } that
* should be applied to the { @link ClientHttpRequest } . Customizers are applied in the
* order that they were added . Setting this value will replace any previously
* configured request customizers .
* @param requestCustomizers the request customizers to set
* @return a new builder instance
* @see # customizers ( RestTemplateCustomizer . . . )
* @since 2.2 .0
* @see # additionalRequestCustomizers ( RestTemplateRequestCustomizer . . . )
* /
public RestTemplateBuilder additionalCustomizers ( boolean append ,
Collection < ? extends RestTemplateCustomizer > customizers ) {
Assert . notNull ( customizers , "RestTemplateCustomizers must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri , this . messageConverters ,
this . requestFactorySupplier , this . uriTemplateHandler , this . errorHandler ,
append ? append ( this . restTemplateCustomizers , customizers )
: append ( customizers , this . restTemplateCustomizers ) ,
this . requestFactoryCustomizer , this . interceptors , this . httpHeadersCustomizers ) ;
public RestTemplateBuilder requestCustomizers ( RestTemplateRequestCustomizer < ? > . . . requestCustomizers ) {
Assert . notNull ( requestCustomizers , "RequestCustomizers must not be null" ) ;
return requestCustomizers ( Arrays . asList ( requestCustomizers ) ) ;
}
/ * *
* Sets the connection timeout on the underlying { @link ClientHttpRequestFactory } .
* @param connectTimeout the connection timeout
* @return a new builder instance .
* @since 2.1 .0
* Set the { @link RestTemplateRequestCustomizer RestTemplateRequestCustomizers } that
* should be applied to the { @link ClientHttpRequest } . Customizers are applied in the
* order that they were added . Setting this value will replace any previously
* configured request customizers .
* @param requestCustomizers the request customizers to set
* @return a new builder instance
* @since 2.2 .0
* @see # additionalRequestCustomizers ( RestTemplateRequestCustomizer . . . )
* /
public RestTemplateBuilder setConnectTimeout ( Duration connectTimeout ) {
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri , this . messageConverters ,
this . requestFactorySupplier , this . uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer . connectTimeout ( connectTimeout ) , this . interceptors ,
this . httpHeadersCustomizers ) ;
public RestTemplateBuilder requestCustomizers (
Collection < ? extends RestTemplateRequestCustomizer < ? > > requestCustomizers ) {
Assert . notNull ( requestCustomizers , "RequestCustomizers must not be null" ) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer , this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . interceptors , this . requestFactory , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthentication , this . defaultHeaders , this . customizers ,
setOf ( requestCustomizers ) ) ;
}
/ * *
* Sets the read timeout on the underlying { @link ClientHttpRequestFactory } .
* @param readTimeout the read timeout
* @return a new builder instance .
* @since 2.1 .0
* Add the { @link RestTemplateRequestCustomizer RestTemplateRequestCustomizers } that
* should be applied to the { @link ClientHttpRequest } . Customizers are applied in the
* order that they were added .
* @param requestCustomizers the request customizers to add
* @return a new builder instance
* @since 2.2 .0
* @see # requestCustomizers ( RestTemplateRequestCustomizer . . . )
* /
public RestTemplateBuilder setReadTimeout ( Duration readTimeout ) {
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri , this . messageConverters ,
this . requestFactorySupplier , this . uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer . readTimeout ( readTimeout ) , this . interceptors , this . httpHeadersCustomizers ) ;
public RestTemplateBuilder additionalRequestCustomizers ( RestTemplateRequestCustomizer < ? > . . . requestCustomizers ) {
Assert . notNull ( requestCustomizers , "RequestCustomizers must not be null" ) ;
return additionalRequestCustomizers ( Arrays . asList ( requestCustomizers ) ) ;
}
/ * *
* Sets if the underling { @link ClientHttpRequestFactory } should buffer the
* { @linkplain ClientHttpRequest # getBody ( ) request body } internally .
* @param bufferRequestBody value of the bufferRequestBody parameter
* @return a new builder instance .
* Add the { @link RestTemplateRequestCustomizer RestTemplateRequestCustomizers } that
* should be applied to the { @link ClientHttpRequest } . Customizers are applied in the
* order that they were added .
* @param requestCustomizers the request customizers to add
* @return a new builder instance
* @since 2.2 .0
* @see SimpleClientHttpRequestFactory # setBufferRequestBody ( boolean )
* @see HttpComponentsClientHttpRequestFactory # setBufferRequestBody ( boolean )
* @see # requestCustomizers ( Collection )
* /
public RestTemplateBuilder setBufferRequestBody ( boolean bufferRequestBody ) {
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri , this . messageConverters ,
this . requestFactorySupplier , this . uriTemplateHandler , this . errorHandler , this . restTemplateCustomizers ,
this . requestFactoryCustomizer . bufferRequestBody ( bufferRequestBody ) , this . interceptors ,
this . httpHeadersCustomizers ) ;
public RestTemplateBuilder additionalRequestCustomizers (
Collection < ? extends RestTemplateRequestCustomizer < ? > > requestCustomizers ) {
Assert . notNull ( requestCustomizers , "RequestCustomizers must not be null" ) ;
return new RestTemplateBuilder ( this . requestFactoryCustomizer , this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . interceptors , this . requestFactory , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthentication , this . defaultHeaders , this . customizers ,
append ( this . requestCustomizers , requestCustomizers ) ) ;
}
/ * *
@ -694,9 +610,7 @@ public class RestTemplateBuilder {
if ( requestFactory ! = null ) {
restTemplate . setRequestFactory ( requestFactory ) ;
}
if ( ! CollectionUtils . isEmpty ( this . httpHeadersCustomizers ) ) {
configureHttpHeadersCustomizers ( restTemplate ) ;
}
addClientHttpRequestFactoryWrapper ( restTemplate ) ;
if ( ! CollectionUtils . isEmpty ( this . messageConverters ) ) {
restTemplate . setMessageConverters ( new ArrayList < > ( this . messageConverters ) ) ;
}
@ -710,8 +624,8 @@ public class RestTemplateBuilder {
RootUriTemplateHandler . addTo ( restTemplate , this . rootUri ) ;
}
restTemplate . getInterceptors ( ) . addAll ( this . interceptors ) ;
if ( ! CollectionUtils . isEmpty ( this . restTemplateC ustomizers) ) {
for ( RestTemplateCustomizer customizer : this . restTemplateC ustomizers) {
if ( ! CollectionUtils . isEmpty ( this . c ustomizers) ) {
for ( RestTemplateCustomizer customizer : this . c ustomizers) {
customizer . customize ( restTemplate ) ;
}
}
@ -726,8 +640,8 @@ public class RestTemplateBuilder {
* /
public ClientHttpRequestFactory buildRequestFactory ( ) {
ClientHttpRequestFactory requestFactory = null ;
if ( this . requestFactory Supplier ! = null ) {
requestFactory = this . requestFactory Supplier . get ( ) ;
if ( this . requestFactory ! = null ) {
requestFactory = this . requestFactory . get ( ) ;
}
else if ( this . detectRequestFactory ) {
requestFactory = new ClientHttpRequestFactorySupplier ( ) . get ( ) ;
@ -740,7 +654,10 @@ public class RestTemplateBuilder {
return requestFactory ;
}
private void configureHttpHeadersCustomizers ( RestTemplate restTemplate ) {
private void addClientHttpRequestFactoryWrapper ( RestTemplate restTemplate ) {
if ( this . basicAuthentication = = null & & this . defaultHeaders . isEmpty ( ) & & this . requestCustomizers . isEmpty ( ) ) {
return ;
}
List < ClientHttpRequestInterceptor > interceptors = null ;
if ( ! restTemplate . getInterceptors ( ) . isEmpty ( ) ) {
// Stash and clear the interceptors so we can access the real factory
@ -748,14 +665,24 @@ public class RestTemplateBuilder {
restTemplate . getInterceptors ( ) . clear ( ) ;
}
ClientHttpRequestFactory requestFactory = restTemplate . getRequestFactory ( ) ;
restTemplate . setRequestFactory (
new HttpHeadersCustomizingClientHttpRequestFactory ( this . httpHeadersCustomizers , requestFactory ) ) ;
ClientHttpRequestFactory wrapper = new RestTemplateBuilderClientHttpRequestFactoryWrapper ( requestFactory ,
this . basicAuthentication , this . defaultHeaders , this . requestCustomizers ) ;
restTemplate . setRequestFactory ( wrapper ) ;
// Restore the original interceptors
if ( interceptors ! = null ) {
restTemplate . getInterceptors ( ) . addAll ( interceptors ) ;
}
}
@SuppressWarnings ( "unchecked" )
private < T > Set < T > setOf ( T . . . items ) {
return setOf ( Arrays . asList ( items ) ) ;
}
private < T > Set < T > setOf ( Collection < ? extends T > collection ) {
return Collections . unmodifiableSet ( new LinkedHashSet < > ( collection ) ) ;
}
private static < T > Set < T > append ( Collection < ? extends T > collection , Collection < ? extends T > additions ) {
Set < T > result = new LinkedHashSet < > ( ( collection ! = null ) ? collection : Collections . emptySet ( ) ) ;
if ( additions ! = null ) {
@ -764,6 +691,15 @@ public class RestTemplateBuilder {
return Collections . unmodifiableSet ( result ) ;
}
private static < K , V > Map < K , V > append ( Map < K , V > map , K key , V value ) {
Map < K , V > result = new LinkedHashMap < > ( ( map ! = null ) ? map : Collections . emptyMap ( ) ) ;
result . put ( key , value ) ;
return Collections . unmodifiableMap ( result ) ;
}
/ * *
* Internal customizer used to apply { @link ClientHttpRequestFactory } settings .
* /
private static class RequestFactoryCustomizer implements Consumer < ClientHttpRequestFactory > {
private final Duration connectTimeout ;