@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2012 - 201 7 the original author or authors .
* Copyright 2012 - 201 8 the original author or authors .
*
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -27,6 +27,7 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet ;
import java.util.LinkedHashSet ;
import java.util.Map ;
import java.util.Map ;
import java.util.Set ;
import java.util.Set ;
import java.util.function.Supplier ;
import org.springframework.beans.BeanUtils ;
import org.springframework.beans.BeanUtils ;
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper ;
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper ;
@ -58,6 +59,7 @@ import org.springframework.web.util.UriTemplateHandler;
* @author Stephane Nicoll
* @author Stephane Nicoll
* @author Phillip Webb
* @author Phillip Webb
* @author Andy Wilkinson
* @author Andy Wilkinson
* @author Brian Clozel
* @since 1.4 .0
* @since 1.4 .0
* /
* /
public class RestTemplateBuilder {
public class RestTemplateBuilder {
@ -81,7 +83,7 @@ public class RestTemplateBuilder {
private final Set < HttpMessageConverter < ? > > messageConverters ;
private final Set < HttpMessageConverter < ? > > messageConverters ;
private final ClientHttpRequestFactory requestFactory ;
private final Supplier< ClientHttpRequestFactory> requestFactory Supplier ;
private final UriTemplateHandler uriTemplateHandler ;
private final UriTemplateHandler uriTemplateHandler ;
@ -105,7 +107,7 @@ public class RestTemplateBuilder {
this . detectRequestFactory = true ;
this . detectRequestFactory = true ;
this . rootUri = null ;
this . rootUri = null ;
this . messageConverters = null ;
this . messageConverters = null ;
this . requestFactory = null ;
this . requestFactory Supplier = null ;
this . uriTemplateHandler = null ;
this . uriTemplateHandler = null ;
this . errorHandler = null ;
this . errorHandler = null ;
this . basicAuthorization = null ;
this . basicAuthorization = null ;
@ -117,7 +119,7 @@ public class RestTemplateBuilder {
private RestTemplateBuilder ( boolean detectRequestFactory , String rootUri ,
private RestTemplateBuilder ( boolean detectRequestFactory , String rootUri ,
Set < HttpMessageConverter < ? > > messageConverters ,
Set < HttpMessageConverter < ? > > messageConverters ,
ClientHttpRequestFactory requestFactory ,
Supplier< ClientHttpRequestFactory> requestFactory Supplier ,
UriTemplateHandler uriTemplateHandler , ResponseErrorHandler errorHandler ,
UriTemplateHandler uriTemplateHandler , ResponseErrorHandler errorHandler ,
BasicAuthorizationInterceptor basicAuthorization ,
BasicAuthorizationInterceptor basicAuthorization ,
Set < RestTemplateCustomizer > restTemplateCustomizers ,
Set < RestTemplateCustomizer > restTemplateCustomizers ,
@ -126,7 +128,7 @@ public class RestTemplateBuilder {
this . detectRequestFactory = detectRequestFactory ;
this . detectRequestFactory = detectRequestFactory ;
this . rootUri = rootUri ;
this . rootUri = rootUri ;
this . messageConverters = messageConverters ;
this . messageConverters = messageConverters ;
this . requestFactory = requestFactory ;
this . requestFactory Supplier = requestFactory Supplier ;
this . uriTemplateHandler = uriTemplateHandler ;
this . uriTemplateHandler = uriTemplateHandler ;
this . errorHandler = errorHandler ;
this . errorHandler = errorHandler ;
this . basicAuthorization = basicAuthorization ;
this . basicAuthorization = basicAuthorization ;
@ -144,7 +146,7 @@ public class RestTemplateBuilder {
* /
* /
public RestTemplateBuilder detectRequestFactory ( boolean detectRequestFactory ) {
public RestTemplateBuilder detectRequestFactory ( boolean detectRequestFactory ) {
return new RestTemplateBuilder ( detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( detectRequestFactory , this . rootUri ,
this . messageConverters , this . requestFactory , this . uriTemplateHandler ,
this . messageConverters , this . requestFactory Supplier , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . requestFactoryCustomizers , this . interceptors ) ;
this . requestFactoryCustomizers , this . interceptors ) ;
}
}
@ -157,7 +159,7 @@ public class RestTemplateBuilder {
* /
* /
public RestTemplateBuilder rootUri ( String rootUri ) {
public RestTemplateBuilder rootUri ( String rootUri ) {
return new RestTemplateBuilder ( this . detectRequestFactory , rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , rootUri ,
this . messageConverters , this . requestFactory , this . uriTemplateHandler ,
this . messageConverters , this . requestFactory Supplier , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . requestFactoryCustomizers , this . interceptors ) ;
this . requestFactoryCustomizers , this . interceptors ) ;
}
}
@ -190,7 +192,7 @@ public class RestTemplateBuilder {
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
Collections . unmodifiableSet (
Collections . unmodifiableSet (
new LinkedHashSet < HttpMessageConverter < ? > > ( messageConverters ) ) ,
new LinkedHashSet < HttpMessageConverter < ? > > ( messageConverters ) ) ,
this . requestFactory , this . uriTemplateHandler , this . errorHandler ,
this . requestFactory Supplier , this . uriTemplateHandler , this . errorHandler ,
this . basicAuthorization , this . restTemplateCustomizers ,
this . basicAuthorization , this . restTemplateCustomizers ,
this . requestFactoryCustomizers , this . interceptors ) ;
this . requestFactoryCustomizers , this . interceptors ) ;
}
}
@ -219,7 +221,7 @@ public class RestTemplateBuilder {
Collection < ? extends HttpMessageConverter < ? > > messageConverters ) {
Collection < ? extends HttpMessageConverter < ? > > messageConverters ) {
Assert . notNull ( messageConverters , "MessageConverters must not be null" ) ;
Assert . notNull ( messageConverters , "MessageConverters must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
append ( this . messageConverters , messageConverters ) , this . requestFactory ,
append ( this . messageConverters , messageConverters ) , this . requestFactory Supplier ,
this . uriTemplateHandler , this . errorHandler , this . basicAuthorization ,
this . uriTemplateHandler , this . errorHandler , this . basicAuthorization ,
this . restTemplateCustomizers , this . requestFactoryCustomizers ,
this . restTemplateCustomizers , this . requestFactoryCustomizers ,
this . interceptors ) ;
this . interceptors ) ;
@ -236,7 +238,7 @@ public class RestTemplateBuilder {
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
Collections . unmodifiableSet (
Collections . unmodifiableSet (
new LinkedHashSet < > ( new RestTemplate ( ) . getMessageConverters ( ) ) ) ,
new LinkedHashSet < > ( new RestTemplate ( ) . getMessageConverters ( ) ) ) ,
this . requestFactory , this . uriTemplateHandler , this . errorHandler ,
this . requestFactory Supplier , this . uriTemplateHandler , this . errorHandler ,
this . basicAuthorization , this . restTemplateCustomizers ,
this . basicAuthorization , this . restTemplateCustomizers ,
this . requestFactoryCustomizers , this . interceptors ) ;
this . requestFactoryCustomizers , this . interceptors ) ;
}
}
@ -269,7 +271,7 @@ public class RestTemplateBuilder {
Collection < ClientHttpRequestInterceptor > interceptors ) {
Collection < ClientHttpRequestInterceptor > interceptors ) {
Assert . notNull ( interceptors , "interceptors must not be null" ) ;
Assert . notNull ( interceptors , "interceptors must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . requestFactory , this . uriTemplateHandler ,
this . messageConverters , this . requestFactory Supplier , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . requestFactoryCustomizers ,
this . requestFactoryCustomizers ,
Collections . unmodifiableSet ( new LinkedHashSet < > ( interceptors ) ) ) ;
Collections . unmodifiableSet ( new LinkedHashSet < > ( interceptors ) ) ) ;
@ -301,7 +303,7 @@ public class RestTemplateBuilder {
Collection < ? extends ClientHttpRequestInterceptor > interceptors ) {
Collection < ? extends ClientHttpRequestInterceptor > interceptors ) {
Assert . notNull ( interceptors , "interceptors must not be null" ) ;
Assert . notNull ( interceptors , "interceptors must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . requestFactory , this . uriTemplateHandler ,
this . messageConverters , this . requestFactory Supplier , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . requestFactoryCustomizers , append ( this . interceptors , interceptors ) ) ;
this . requestFactoryCustomizers , append ( this . interceptors , interceptors ) ) ;
}
}
@ -315,7 +317,7 @@ public class RestTemplateBuilder {
public RestTemplateBuilder requestFactory (
public RestTemplateBuilder requestFactory (
Class < ? extends ClientHttpRequestFactory > requestFactory ) {
Class < ? extends ClientHttpRequestFactory > requestFactory ) {
Assert . notNull ( requestFactory , "RequestFactory must not be null" ) ;
Assert . notNull ( requestFactory , "RequestFactory must not be null" ) ;
return requestFactory ( createRequestFactory ( requestFactory ) ) ;
return requestFactory ( ( ) - > createRequestFactory ( requestFactory ) ) ;
}
}
private ClientHttpRequestFactory createRequestFactory (
private ClientHttpRequestFactory createRequestFactory (
@ -331,15 +333,17 @@ public class RestTemplateBuilder {
}
}
/ * *
/ * *
* Set the { @link ClientHttpRequestFactory } that should be used with the
* Set the { @code Supplier } of { @link ClientHttpRequestFactory }
* { @link RestTemplate } .
* that should be called each time we { @link # build ( ) } a new
* @param requestFactory the request factory to use
* { @link RestTemplate } instance .
* @param requestFactorySupplier the supplier for the request factory
* @return a new builder instance
* @return a new builder instance
* @since 2.0 .0
* /
* /
public RestTemplateBuilder requestFactory ( ClientHttpRequestFactory requestFactory ) {
public RestTemplateBuilder requestFactory ( Supplier< ClientHttpRequestFactory> requestFactory Supplier ) {
Assert . notNull ( requestFactory , "RequestFactory must not be null") ;
Assert . notNull ( requestFactory Supplier , "RequestFactory Supplier must not be null") ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
this . messageConverters , requestFactory , this . uriTemplateHandler ,
this . messageConverters , requestFactory Supplier , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . requestFactoryCustomizers , this . interceptors ) ;
this . requestFactoryCustomizers , this . interceptors ) ;
}
}
@ -353,7 +357,7 @@ public class RestTemplateBuilder {
public RestTemplateBuilder uriTemplateHandler ( UriTemplateHandler uriTemplateHandler ) {
public RestTemplateBuilder uriTemplateHandler ( UriTemplateHandler uriTemplateHandler ) {
Assert . notNull ( uriTemplateHandler , "UriTemplateHandler must not be null" ) ;
Assert . notNull ( uriTemplateHandler , "UriTemplateHandler must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . requestFactory , uriTemplateHandler ,
this . messageConverters , this . requestFactory Supplier , uriTemplateHandler ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . requestFactoryCustomizers , this . interceptors ) ;
this . requestFactoryCustomizers , this . interceptors ) ;
}
}
@ -367,7 +371,7 @@ public class RestTemplateBuilder {
public RestTemplateBuilder errorHandler ( ResponseErrorHandler errorHandler ) {
public RestTemplateBuilder errorHandler ( ResponseErrorHandler errorHandler ) {
Assert . notNull ( errorHandler , "ErrorHandler must not be null" ) ;
Assert . notNull ( errorHandler , "ErrorHandler must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . requestFactory , this . uriTemplateHandler ,
this . messageConverters , this . requestFactory Supplier , this . uriTemplateHandler ,
errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . requestFactoryCustomizers , this . interceptors ) ;
this . requestFactoryCustomizers , this . interceptors ) ;
}
}
@ -381,7 +385,7 @@ public class RestTemplateBuilder {
* /
* /
public RestTemplateBuilder basicAuthorization ( String username , String password ) {
public RestTemplateBuilder basicAuthorization ( String username , String password ) {
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . requestFactory , this . uriTemplateHandler ,
this . messageConverters , this . requestFactory Supplier , this . uriTemplateHandler ,
this . errorHandler , new BasicAuthorizationInterceptor ( username , password ) ,
this . errorHandler , new BasicAuthorizationInterceptor ( username , password ) ,
this . restTemplateCustomizers , this . requestFactoryCustomizers ,
this . restTemplateCustomizers , this . requestFactoryCustomizers ,
this . interceptors ) ;
this . interceptors ) ;
@ -417,7 +421,7 @@ public class RestTemplateBuilder {
Assert . notNull ( restTemplateCustomizers ,
Assert . notNull ( restTemplateCustomizers ,
"RestTemplateCustomizers must not be null" ) ;
"RestTemplateCustomizers must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . requestFactory , this . uriTemplateHandler ,
this . messageConverters , this . requestFactory Supplier , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthorization ,
this . errorHandler , this . basicAuthorization ,
Collections . unmodifiableSet ( new LinkedHashSet < RestTemplateCustomizer > (
Collections . unmodifiableSet ( new LinkedHashSet < RestTemplateCustomizer > (
restTemplateCustomizers ) ) ,
restTemplateCustomizers ) ) ,
@ -451,7 +455,7 @@ public class RestTemplateBuilder {
Collection < ? extends RestTemplateCustomizer > customizers ) {
Collection < ? extends RestTemplateCustomizer > customizers ) {
Assert . notNull ( customizers , "RestTemplateCustomizers must not be null" ) ;
Assert . notNull ( customizers , "RestTemplateCustomizers must not be null" ) ;
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . requestFactory , this . uriTemplateHandler ,
this . messageConverters , this . requestFactory Supplier , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthorization ,
this . errorHandler , this . basicAuthorization ,
append ( this . restTemplateCustomizers , customizers ) ,
append ( this . restTemplateCustomizers , customizers ) ,
this . requestFactoryCustomizers , this . interceptors ) ;
this . requestFactoryCustomizers , this . interceptors ) ;
@ -465,7 +469,7 @@ public class RestTemplateBuilder {
* /
* /
public RestTemplateBuilder setConnectTimeout ( int connectTimeout ) {
public RestTemplateBuilder setConnectTimeout ( int connectTimeout ) {
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . requestFactory , this . uriTemplateHandler ,
this . messageConverters , this . requestFactory Supplier , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
append ( this . requestFactoryCustomizers ,
append ( this . requestFactoryCustomizers ,
new ConnectTimeoutRequestFactoryCustomizer ( connectTimeout ) ) ,
new ConnectTimeoutRequestFactoryCustomizer ( connectTimeout ) ) ,
@ -480,7 +484,7 @@ public class RestTemplateBuilder {
* /
* /
public RestTemplateBuilder setReadTimeout ( int readTimeout ) {
public RestTemplateBuilder setReadTimeout ( int readTimeout ) {
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
return new RestTemplateBuilder ( this . detectRequestFactory , this . rootUri ,
this . messageConverters , this . requestFactory , this . uriTemplateHandler ,
this . messageConverters , this . requestFactory Supplier , this . uriTemplateHandler ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
this . errorHandler , this . basicAuthorization , this . restTemplateCustomizers ,
append ( this . requestFactoryCustomizers ,
append ( this . requestFactoryCustomizers ,
new ReadTimeoutRequestFactoryCustomizer ( readTimeout ) ) ,
new ReadTimeoutRequestFactoryCustomizer ( readTimeout ) ) ,
@ -547,8 +551,8 @@ public class RestTemplateBuilder {
private void configureRequestFactory ( RestTemplate restTemplate ) {
private void configureRequestFactory ( RestTemplate restTemplate ) {
ClientHttpRequestFactory requestFactory = null ;
ClientHttpRequestFactory requestFactory = null ;
if ( this . requestFactory ! = null ) {
if ( this . requestFactory Supplier ! = null ) {
requestFactory = this . requestFactory ;
requestFactory = this . requestFactory Supplier. get ( ) ;
}
}
else if ( this . detectRequestFactory ) {
else if ( this . detectRequestFactory ) {
requestFactory = detectRequestFactory ( ) ;
requestFactory = detectRequestFactory ( ) ;