|
|
|
@ -33,7 +33,7 @@ import org.springframework.beans.BeanUtils;
|
|
|
|
|
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper;
|
|
|
|
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
|
|
|
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
|
|
|
|
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
|
|
|
|
|
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
|
|
|
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
@ -74,7 +74,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
|
|
|
|
|
private final ResponseErrorHandler errorHandler;
|
|
|
|
|
|
|
|
|
|
private final BasicAuthorizationInterceptor basicAuthorization;
|
|
|
|
|
private final BasicAuthenticationInterceptor basicAuthentication;
|
|
|
|
|
|
|
|
|
|
private final Set<RestTemplateCustomizer> restTemplateCustomizers;
|
|
|
|
|
|
|
|
|
@ -95,7 +95,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
this.requestFactorySupplier = null;
|
|
|
|
|
this.uriTemplateHandler = null;
|
|
|
|
|
this.errorHandler = null;
|
|
|
|
|
this.basicAuthorization = null;
|
|
|
|
|
this.basicAuthentication = null;
|
|
|
|
|
this.restTemplateCustomizers = Collections
|
|
|
|
|
.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(customizers)));
|
|
|
|
|
this.requestFactoryCustomizer = new RequestFactoryCustomizer();
|
|
|
|
@ -106,7 +106,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
Set<HttpMessageConverter<?>> messageConverters,
|
|
|
|
|
Supplier<ClientHttpRequestFactory> requestFactorySupplier,
|
|
|
|
|
UriTemplateHandler uriTemplateHandler, ResponseErrorHandler errorHandler,
|
|
|
|
|
BasicAuthorizationInterceptor basicAuthorization,
|
|
|
|
|
BasicAuthenticationInterceptor basicAuthentication,
|
|
|
|
|
Set<RestTemplateCustomizer> restTemplateCustomizers,
|
|
|
|
|
RequestFactoryCustomizer requestFactoryCustomizer,
|
|
|
|
|
Set<ClientHttpRequestInterceptor> interceptors) {
|
|
|
|
@ -116,7 +116,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
this.requestFactorySupplier = requestFactorySupplier;
|
|
|
|
|
this.uriTemplateHandler = uriTemplateHandler;
|
|
|
|
|
this.errorHandler = errorHandler;
|
|
|
|
|
this.basicAuthorization = basicAuthorization;
|
|
|
|
|
this.basicAuthentication = basicAuthentication;
|
|
|
|
|
this.restTemplateCustomizers = restTemplateCustomizers;
|
|
|
|
|
this.requestFactoryCustomizer = requestFactoryCustomizer;
|
|
|
|
|
this.interceptors = interceptors;
|
|
|
|
@ -132,7 +132,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
public RestTemplateBuilder detectRequestFactory(boolean detectRequestFactory) {
|
|
|
|
|
return new RestTemplateBuilder(detectRequestFactory, this.rootUri,
|
|
|
|
|
this.messageConverters, this.requestFactorySupplier,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
|
|
|
|
|
this.restTemplateCustomizers, this.requestFactoryCustomizer,
|
|
|
|
|
this.interceptors);
|
|
|
|
|
}
|
|
|
|
@ -146,7 +146,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
public RestTemplateBuilder rootUri(String rootUri) {
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, rootUri,
|
|
|
|
|
this.messageConverters, this.requestFactorySupplier,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
|
|
|
|
|
this.restTemplateCustomizers, this.requestFactoryCustomizer,
|
|
|
|
|
this.interceptors);
|
|
|
|
|
}
|
|
|
|
@ -182,7 +182,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
Collections.unmodifiableSet(
|
|
|
|
|
new LinkedHashSet<HttpMessageConverter<?>>(messageConverters)),
|
|
|
|
|
this.requestFactorySupplier, this.uriTemplateHandler, this.errorHandler,
|
|
|
|
|
this.basicAuthorization, this.restTemplateCustomizers,
|
|
|
|
|
this.basicAuthentication, this.restTemplateCustomizers,
|
|
|
|
|
this.requestFactoryCustomizer, this.interceptors);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -214,7 +214,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
|
append(this.messageConverters, messageConverters),
|
|
|
|
|
this.requestFactorySupplier, this.uriTemplateHandler, this.errorHandler,
|
|
|
|
|
this.basicAuthorization, this.restTemplateCustomizers,
|
|
|
|
|
this.basicAuthentication, this.restTemplateCustomizers,
|
|
|
|
|
this.requestFactoryCustomizer, this.interceptors);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -230,7 +230,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
Collections.unmodifiableSet(
|
|
|
|
|
new LinkedHashSet<>(new RestTemplate().getMessageConverters())),
|
|
|
|
|
this.requestFactorySupplier, this.uriTemplateHandler, this.errorHandler,
|
|
|
|
|
this.basicAuthorization, this.restTemplateCustomizers,
|
|
|
|
|
this.basicAuthentication, this.restTemplateCustomizers,
|
|
|
|
|
this.requestFactoryCustomizer, this.interceptors);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -263,7 +263,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
Assert.notNull(interceptors, "interceptors must not be null");
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
|
this.messageConverters, this.requestFactorySupplier,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
|
|
|
|
|
this.restTemplateCustomizers, this.requestFactoryCustomizer,
|
|
|
|
|
Collections.unmodifiableSet(new LinkedHashSet<>(interceptors)));
|
|
|
|
|
}
|
|
|
|
@ -295,7 +295,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
Assert.notNull(interceptors, "interceptors must not be null");
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
|
this.messageConverters, this.requestFactorySupplier,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
|
|
|
|
|
this.restTemplateCustomizers, this.requestFactoryCustomizer,
|
|
|
|
|
append(this.interceptors, interceptors));
|
|
|
|
|
}
|
|
|
|
@ -337,7 +337,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
"RequestFactory Supplier must not be null");
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
|
this.messageConverters, requestFactorySupplier, this.uriTemplateHandler,
|
|
|
|
|
this.errorHandler, this.basicAuthorization, this.restTemplateCustomizers,
|
|
|
|
|
this.errorHandler, this.basicAuthentication, this.restTemplateCustomizers,
|
|
|
|
|
this.requestFactoryCustomizer, this.interceptors);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -351,7 +351,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
Assert.notNull(uriTemplateHandler, "UriTemplateHandler must not be null");
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
|
this.messageConverters, this.requestFactorySupplier, uriTemplateHandler,
|
|
|
|
|
this.errorHandler, this.basicAuthorization, this.restTemplateCustomizers,
|
|
|
|
|
this.errorHandler, this.basicAuthentication, this.restTemplateCustomizers,
|
|
|
|
|
this.requestFactoryCustomizer, this.interceptors);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -365,23 +365,37 @@ public class RestTemplateBuilder {
|
|
|
|
|
Assert.notNull(errorHandler, "ErrorHandler must not be null");
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
|
this.messageConverters, this.requestFactorySupplier,
|
|
|
|
|
this.uriTemplateHandler, errorHandler, this.basicAuthorization,
|
|
|
|
|
this.uriTemplateHandler, errorHandler, this.basicAuthentication,
|
|
|
|
|
this.restTemplateCustomizers, this.requestFactoryCustomizer,
|
|
|
|
|
this.interceptors);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add HTTP basic authentication to requests. See
|
|
|
|
|
* {@link BasicAuthorizationInterceptor} for details.
|
|
|
|
|
* {@link BasicAuthenticationInterceptor} for details.
|
|
|
|
|
* @param username the user name
|
|
|
|
|
* @param password the password
|
|
|
|
|
* @return a new builder instance
|
|
|
|
|
* @deprecated since 2.1.0 in favor of
|
|
|
|
|
* {@link #basicAuthentication(String username, String password)}
|
|
|
|
|
*/
|
|
|
|
|
public RestTemplateBuilder basicAuthorization(String username, String password) {
|
|
|
|
|
return basicAuthentication(username, password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add HTTP basic authentication to requests. See
|
|
|
|
|
* {@link BasicAuthenticationInterceptor} for details.
|
|
|
|
|
* @param username the user name
|
|
|
|
|
* @param password the password
|
|
|
|
|
* @return a new builder instance
|
|
|
|
|
* @since 2.1.0
|
|
|
|
|
*/
|
|
|
|
|
public RestTemplateBuilder basicAuthentication(String username, String password) {
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
|
this.messageConverters, this.requestFactorySupplier,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler,
|
|
|
|
|
new BasicAuthorizationInterceptor(username, password),
|
|
|
|
|
new BasicAuthenticationInterceptor(username, password),
|
|
|
|
|
this.restTemplateCustomizers, this.requestFactoryCustomizer,
|
|
|
|
|
this.interceptors);
|
|
|
|
|
}
|
|
|
|
@ -417,7 +431,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
"RestTemplateCustomizers must not be null");
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
|
this.messageConverters, this.requestFactorySupplier,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
|
|
|
|
|
Collections.unmodifiableSet(new LinkedHashSet<RestTemplateCustomizer>(
|
|
|
|
|
restTemplateCustomizers)),
|
|
|
|
|
this.requestFactoryCustomizer, this.interceptors);
|
|
|
|
@ -451,7 +465,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
Assert.notNull(customizers, "RestTemplateCustomizers must not be null");
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
|
this.messageConverters, this.requestFactorySupplier,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
|
|
|
|
|
append(this.restTemplateCustomizers, customizers),
|
|
|
|
|
this.requestFactoryCustomizer, this.interceptors);
|
|
|
|
|
}
|
|
|
|
@ -465,7 +479,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
public RestTemplateBuilder setConnectTimeout(Duration connectTimeout) {
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
|
this.messageConverters, this.requestFactorySupplier,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
|
|
|
|
|
this.restTemplateCustomizers,
|
|
|
|
|
this.requestFactoryCustomizer.connectTimeout(connectTimeout),
|
|
|
|
|
this.interceptors);
|
|
|
|
@ -492,7 +506,7 @@ public class RestTemplateBuilder {
|
|
|
|
|
public RestTemplateBuilder setReadTimeout(Duration readTimeout) {
|
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
|
this.messageConverters, this.requestFactorySupplier,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
|
|
|
|
|
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
|
|
|
|
|
this.restTemplateCustomizers,
|
|
|
|
|
this.requestFactoryCustomizer.readTimeout(readTimeout),
|
|
|
|
|
this.interceptors);
|
|
|
|
@ -556,8 +570,8 @@ public class RestTemplateBuilder {
|
|
|
|
|
if (this.rootUri != null) {
|
|
|
|
|
RootUriTemplateHandler.addTo(restTemplate, this.rootUri);
|
|
|
|
|
}
|
|
|
|
|
if (this.basicAuthorization != null) {
|
|
|
|
|
restTemplate.getInterceptors().add(this.basicAuthorization);
|
|
|
|
|
if (this.basicAuthentication != null) {
|
|
|
|
|
restTemplate.getInterceptors().add(this.basicAuthentication);
|
|
|
|
|
}
|
|
|
|
|
restTemplate.getInterceptors().addAll(this.interceptors);
|
|
|
|
|
if (!CollectionUtils.isEmpty(this.restTemplateCustomizers)) {
|
|
|
|
|