|
|
@ -33,8 +33,11 @@ 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;
|
|
|
|
|
|
|
|
import org.springframework.http.client.ClientHttpRequest;
|
|
|
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
|
|
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
|
|
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
|
|
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
|
|
|
|
|
|
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
|
|
|
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
@ -508,11 +511,13 @@ public class RestTemplateBuilder {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Sets the bufferrequestbody value on the underlying
|
|
|
|
* Sets if the underling {@link ClientHttpRequestFactory} should buffer the
|
|
|
|
* {@link ClientHttpRequestFactory}.
|
|
|
|
* {@linkplain ClientHttpRequest#getBody() request body} internally.
|
|
|
|
* @param bufferRequestBody value of the bufferRequestBody parameter
|
|
|
|
* @param bufferRequestBody value of the bufferRequestBody parameter
|
|
|
|
* @return a new builder instance.
|
|
|
|
* @return a new builder instance.
|
|
|
|
* @since 2.1.0
|
|
|
|
* @since 2.2.0
|
|
|
|
|
|
|
|
* @see SimpleClientHttpRequestFactory#setBufferRequestBody(boolean)
|
|
|
|
|
|
|
|
* @see HttpComponentsClientHttpRequestFactory#setBufferRequestBody(boolean)
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public RestTemplateBuilder setBufferRequestBody(boolean bufferRequestBody) {
|
|
|
|
public RestTemplateBuilder setBufferRequestBody(boolean bufferRequestBody) {
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
|
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
|
|
|
@ -634,35 +639,32 @@ public class RestTemplateBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
private final Duration readTimeout;
|
|
|
|
private final Duration readTimeout;
|
|
|
|
|
|
|
|
|
|
|
|
private final boolean bufferRequestBody;
|
|
|
|
private final Boolean bufferRequestBody;
|
|
|
|
|
|
|
|
|
|
|
|
private final boolean bufferRequestBodyFlag;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RequestFactoryCustomizer() {
|
|
|
|
RequestFactoryCustomizer() {
|
|
|
|
this(null, null, true, false);
|
|
|
|
this(null, null, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private RequestFactoryCustomizer(Duration connectTimeout, Duration readTimeout,
|
|
|
|
private RequestFactoryCustomizer(Duration connectTimeout, Duration readTimeout,
|
|
|
|
boolean bufferRequestBody, boolean bufferRequestBodyFlag) {
|
|
|
|
Boolean bufferRequestBody) {
|
|
|
|
this.connectTimeout = connectTimeout;
|
|
|
|
this.connectTimeout = connectTimeout;
|
|
|
|
this.readTimeout = readTimeout;
|
|
|
|
this.readTimeout = readTimeout;
|
|
|
|
this.bufferRequestBody = bufferRequestBody;
|
|
|
|
this.bufferRequestBody = bufferRequestBody;
|
|
|
|
this.bufferRequestBodyFlag = bufferRequestBodyFlag;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public RequestFactoryCustomizer connectTimeout(Duration connectTimeout) {
|
|
|
|
public RequestFactoryCustomizer connectTimeout(Duration connectTimeout) {
|
|
|
|
return new RequestFactoryCustomizer(connectTimeout, this.readTimeout,
|
|
|
|
return new RequestFactoryCustomizer(connectTimeout, this.readTimeout,
|
|
|
|
this.bufferRequestBody, this.bufferRequestBodyFlag);
|
|
|
|
this.bufferRequestBody);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public RequestFactoryCustomizer readTimeout(Duration readTimeout) {
|
|
|
|
public RequestFactoryCustomizer readTimeout(Duration readTimeout) {
|
|
|
|
return new RequestFactoryCustomizer(this.connectTimeout, readTimeout,
|
|
|
|
return new RequestFactoryCustomizer(this.connectTimeout, readTimeout,
|
|
|
|
this.bufferRequestBody, this.bufferRequestBodyFlag);
|
|
|
|
this.bufferRequestBody);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public RequestFactoryCustomizer bufferRequestBody(boolean bufferRequestBody) {
|
|
|
|
public RequestFactoryCustomizer bufferRequestBody(boolean bufferRequestBody) {
|
|
|
|
return new RequestFactoryCustomizer(this.connectTimeout, this.readTimeout,
|
|
|
|
return new RequestFactoryCustomizer(this.connectTimeout, this.readTimeout,
|
|
|
|
bufferRequestBody, true);
|
|
|
|
bufferRequestBody);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -670,16 +672,13 @@ public class RestTemplateBuilder {
|
|
|
|
ClientHttpRequestFactory unwrappedRequestFactory = unwrapRequestFactoryIfNecessary(
|
|
|
|
ClientHttpRequestFactory unwrappedRequestFactory = unwrapRequestFactoryIfNecessary(
|
|
|
|
requestFactory);
|
|
|
|
requestFactory);
|
|
|
|
if (this.connectTimeout != null) {
|
|
|
|
if (this.connectTimeout != null) {
|
|
|
|
new TimeoutRequestFactoryCustomizer(this.connectTimeout,
|
|
|
|
setConnectTimeout(unwrappedRequestFactory);
|
|
|
|
"setConnectTimeout").customize(unwrappedRequestFactory);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.readTimeout != null) {
|
|
|
|
if (this.readTimeout != null) {
|
|
|
|
new TimeoutRequestFactoryCustomizer(this.readTimeout, "setReadTimeout")
|
|
|
|
setReadTimeout(unwrappedRequestFactory);
|
|
|
|
.customize(unwrappedRequestFactory);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.bufferRequestBodyFlag) {
|
|
|
|
if (this.bufferRequestBody != null) {
|
|
|
|
new BufferRequestBodyFactoryCustomizer(this.bufferRequestBody,
|
|
|
|
setBufferRequestBody(unwrappedRequestFactory);
|
|
|
|
"setBufferRequestBody").customize(unwrappedRequestFactory);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -699,68 +698,37 @@ public class RestTemplateBuilder {
|
|
|
|
return unwrappedRequestFactory;
|
|
|
|
return unwrappedRequestFactory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
private void setConnectTimeout(ClientHttpRequestFactory factory) {
|
|
|
|
* {@link ClientHttpRequestFactory} customizer to call a "set timeout" method.
|
|
|
|
Method method = findMethod(factory, "setConnectTimeout", int.class);
|
|
|
|
*/
|
|
|
|
int timeout = Math.toIntExact(this.connectTimeout.toMillis());
|
|
|
|
private static final class TimeoutRequestFactoryCustomizer {
|
|
|
|
invoke(factory, method, timeout);
|
|
|
|
|
|
|
|
|
|
|
|
private final Duration timeout;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final String methodName;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TimeoutRequestFactoryCustomizer(Duration timeout, String methodName) {
|
|
|
|
|
|
|
|
this.timeout = timeout;
|
|
|
|
|
|
|
|
this.methodName = methodName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void customize(ClientHttpRequestFactory factory) {
|
|
|
|
|
|
|
|
ReflectionUtils.invokeMethod(findMethod(factory), factory,
|
|
|
|
|
|
|
|
Math.toIntExact(this.timeout.toMillis()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Method findMethod(ClientHttpRequestFactory factory) {
|
|
|
|
|
|
|
|
Method method = ReflectionUtils.findMethod(factory.getClass(),
|
|
|
|
|
|
|
|
this.methodName, int.class);
|
|
|
|
|
|
|
|
if (method != null) {
|
|
|
|
|
|
|
|
return method;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new IllegalStateException("Request factory " + factory.getClass()
|
|
|
|
|
|
|
|
+ " does not have a " + this.methodName + "(int) method");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
private void setReadTimeout(ClientHttpRequestFactory factory) {
|
|
|
|
* {@link ClientHttpRequestFactory} customizer to call a "set buffer request body"
|
|
|
|
Method method = findMethod(factory, "setReadTimeout", int.class);
|
|
|
|
* method.
|
|
|
|
int timeout = Math.toIntExact(this.readTimeout.toMillis());
|
|
|
|
*/
|
|
|
|
invoke(factory, method, timeout);
|
|
|
|
private static final class BufferRequestBodyFactoryCustomizer {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private final boolean bufferRequestBody;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final String methodName;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BufferRequestBodyFactoryCustomizer(boolean bufferRequestBody,
|
|
|
|
|
|
|
|
String methodName) {
|
|
|
|
|
|
|
|
this.bufferRequestBody = bufferRequestBody;
|
|
|
|
|
|
|
|
this.methodName = methodName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void customize(ClientHttpRequestFactory factory) {
|
|
|
|
private void setBufferRequestBody(ClientHttpRequestFactory factory) {
|
|
|
|
ReflectionUtils.invokeMethod(findMethod(factory), factory,
|
|
|
|
Method method = findMethod(factory, "setBufferRequestBody", boolean.class);
|
|
|
|
this.bufferRequestBody);
|
|
|
|
invoke(factory, method, this.bufferRequestBody);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Method findMethod(ClientHttpRequestFactory factory) {
|
|
|
|
private Method findMethod(ClientHttpRequestFactory requestFactory,
|
|
|
|
Method method = ReflectionUtils.findMethod(factory.getClass(),
|
|
|
|
String methodName, Class<?>... parameters) {
|
|
|
|
this.methodName, boolean.class);
|
|
|
|
Method method = ReflectionUtils.findMethod(requestFactory.getClass(),
|
|
|
|
if (method != null) {
|
|
|
|
methodName, parameters);
|
|
|
|
return method;
|
|
|
|
if (method != null) {
|
|
|
|
}
|
|
|
|
return method;
|
|
|
|
throw new IllegalStateException("Request factory " + factory.getClass()
|
|
|
|
|
|
|
|
+ " does not have a " + this.methodName + "(boolean) method");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new IllegalStateException("Request factory " + requestFactory.getClass()
|
|
|
|
|
|
|
|
+ " does not have a suitable " + methodName + " method");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void invoke(ClientHttpRequestFactory requestFactory, Method method,
|
|
|
|
|
|
|
|
Object... parameters) {
|
|
|
|
|
|
|
|
ReflectionUtils.invokeMethod(method, requestFactory, parameters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|