Skip to content

Commit 0434890

Browse files
committed
Unified createRequestConfig(Object) method, avoiding getInternalRequestConfig()
Issue: SPR-13125
1 parent 10a2f50 commit 0434890

File tree

2 files changed

+35
-46
lines changed

2 files changed

+35
-46
lines changed

spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequestFactory.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public HttpComponentsAsyncClientHttpRequestFactory() {
6565
*/
6666
public HttpComponentsAsyncClientHttpRequestFactory(CloseableHttpAsyncClient httpAsyncClient) {
6767
super();
68-
Assert.notNull(httpAsyncClient, "'httpAsyncClient' must not be null");
68+
Assert.notNull(httpAsyncClient, "HttpAsyncClient must not be null");
6969
this.httpAsyncClient = httpAsyncClient;
7070
}
7171

@@ -79,7 +79,7 @@ public HttpComponentsAsyncClientHttpRequestFactory(
7979
CloseableHttpClient httpClient, CloseableHttpAsyncClient httpAsyncClient) {
8080

8181
super(httpClient);
82-
Assert.notNull(httpAsyncClient, "'httpAsyncClient' must not be null");
82+
Assert.notNull(httpAsyncClient, "HttpAsyncClient must not be null");
8383
this.httpAsyncClient = httpAsyncClient;
8484
}
8585

@@ -140,24 +140,6 @@ public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod)
140140
return new HttpComponentsAsyncClientHttpRequest(asyncClient, httpRequest, context);
141141
}
142142

143-
/**
144-
* Create a default {@link RequestConfig} to use with the given client.
145-
* Can return {@code null} to indicate that no custom request config should
146-
* be set and the defaults of the {@link HttpAsyncClient} should be used.
147-
* <p>The default implementation tries to merge the defaults of the client
148-
* with the local customizations of this instance, if any.
149-
* @param client the client
150-
* @return the RequestConfig to use
151-
* @since 4.2
152-
*/
153-
protected RequestConfig createRequestConfig(HttpAsyncClient client) {
154-
if (client instanceof Configurable) {
155-
RequestConfig clientRequestConfig = ((Configurable) client).getConfig();
156-
return mergeRequestConfig(clientRequestConfig);
157-
}
158-
return getInternalRequestConfig();
159-
}
160-
161143
@Override
162144
public void destroy() throws Exception {
163145
try {

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* @author Oleg Kalnichevski
5454
* @author Arjen Poutsma
5555
* @author Stephane Nicoll
56+
* @author Juergen Hoeller
5657
* @since 3.1
5758
*/
5859
public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean {
@@ -109,8 +110,7 @@ public HttpClient getHttpClient() {
109110
*/
110111
public void setConnectTimeout(int timeout) {
111112
Assert.isTrue(timeout >= 0, "Timeout must be a non-negative value");
112-
this.requestConfig = cloneRequestConfig()
113-
.setConnectTimeout(timeout).build();
113+
this.requestConfig = requestConfigBuilder().setConnectTimeout(timeout).build();
114114
setLegacyConnectionTimeout(getHttpClient(), timeout);
115115
}
116116

@@ -131,8 +131,7 @@ public void setConnectTimeout(int timeout) {
131131
@SuppressWarnings("deprecation")
132132
private void setLegacyConnectionTimeout(HttpClient client, int timeout) {
133133
if (org.apache.http.impl.client.AbstractHttpClient.class.isInstance(client)) {
134-
client.getParams().setIntParameter(
135-
org.apache.http.params.CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
134+
client.getParams().setIntParameter(org.apache.http.params.CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
136135
}
137136
}
138137

@@ -146,8 +145,7 @@ private void setLegacyConnectionTimeout(HttpClient client, int timeout) {
146145
* @see RequestConfig#getConnectionRequestTimeout()
147146
*/
148147
public void setConnectionRequestTimeout(int connectionRequestTimeout) {
149-
this.requestConfig = cloneRequestConfig()
150-
.setConnectionRequestTimeout(connectionRequestTimeout).build();
148+
this.requestConfig = requestConfigBuilder().setConnectionRequestTimeout(connectionRequestTimeout).build();
151149
}
152150

153151
/**
@@ -160,8 +158,7 @@ public void setConnectionRequestTimeout(int connectionRequestTimeout) {
160158
*/
161159
public void setReadTimeout(int timeout) {
162160
Assert.isTrue(timeout >= 0, "Timeout must be a non-negative value");
163-
this.requestConfig = cloneRequestConfig()
164-
.setSocketTimeout(timeout).build();
161+
this.requestConfig = requestConfigBuilder().setSocketTimeout(timeout).build();
165162
setLegacySocketTimeout(getHttpClient(), timeout);
166163
}
167164

@@ -175,15 +172,10 @@ public void setReadTimeout(int timeout) {
175172
@SuppressWarnings("deprecation")
176173
private void setLegacySocketTimeout(HttpClient client, int timeout) {
177174
if (org.apache.http.impl.client.AbstractHttpClient.class.isInstance(client)) {
178-
client.getParams().setIntParameter(
179-
org.apache.http.params.CoreConnectionPNames.SO_TIMEOUT, timeout);
175+
client.getParams().setIntParameter(org.apache.http.params.CoreConnectionPNames.SO_TIMEOUT, timeout);
180176
}
181177
}
182178

183-
private RequestConfig.Builder cloneRequestConfig() {
184-
return this.requestConfig != null ? RequestConfig.copy(this.requestConfig) : RequestConfig.custom();
185-
}
186-
187179
/**
188180
* Indicates whether this request factory should buffer the request body internally.
189181
* <p>Default is {@code true}. When sending large amounts of data via POST or PUT, it is
@@ -226,29 +218,48 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
226218
}
227219
}
228220

221+
222+
/**
223+
* Return a builder for modifying the factory-level {@link RequestConfig}.
224+
* @since 4.2
225+
*/
226+
private RequestConfig.Builder requestConfigBuilder() {
227+
return (this.requestConfig != null ? RequestConfig.copy(this.requestConfig) : RequestConfig.custom());
228+
}
229+
229230
/**
230231
* Create a default {@link RequestConfig} to use with the given client.
231232
* Can return {@code null} to indicate that no custom request config should
232233
* be set and the defaults of the {@link HttpClient} should be used.
233234
* <p>The default implementation tries to merge the defaults of the client
234-
* with the local customizations of this instance, if any.
235-
* @param client the client
236-
* @return the RequestConfig to use
235+
* with the local customizations of this factory instance, if any.
236+
* @param client the {@link HttpClient} (or {@code HttpAsyncClient}) to check
237+
* @return the actual RequestConfig to use (may be {@code null})
237238
* @since 4.2
239+
* @see #mergeRequestConfig(RequestConfig)
238240
*/
239-
protected RequestConfig createRequestConfig(HttpClient client) {
241+
protected RequestConfig createRequestConfig(Object client) {
240242
if (client instanceof Configurable) {
241243
RequestConfig clientRequestConfig = ((Configurable) client).getConfig();
242244
return mergeRequestConfig(clientRequestConfig);
243245
}
244246
return this.requestConfig;
245247
}
246248

247-
protected RequestConfig mergeRequestConfig(RequestConfig defaultRequestConfig) {
248-
if (this.requestConfig == null) { // nothing to merge
249-
return defaultRequestConfig;
249+
/**
250+
* Merge the given {@link HttpClient}-level {@link RequestConfig} with
251+
* the factory-level {@link RequestConfig}, if necessary.
252+
* @param clientConfig the config held by the current
253+
* @return the merged request config
254+
* (may be {@code null} if the given client config is {@code null})
255+
* @since 4.2
256+
*/
257+
protected RequestConfig mergeRequestConfig(RequestConfig clientConfig) {
258+
if (this.requestConfig == null) { // nothing to merge
259+
return clientConfig;
250260
}
251-
RequestConfig.Builder builder = RequestConfig.copy(defaultRequestConfig);
261+
262+
RequestConfig.Builder builder = RequestConfig.copy(clientConfig);
252263
int connectTimeout = this.requestConfig.getConnectTimeout();
253264
if (connectTimeout >= 0) {
254265
builder.setConnectTimeout(connectTimeout);
@@ -264,10 +275,6 @@ protected RequestConfig mergeRequestConfig(RequestConfig defaultRequestConfig) {
264275
return builder.build();
265276
}
266277

267-
protected final RequestConfig getInternalRequestConfig() {
268-
return this.requestConfig;
269-
}
270-
271278
/**
272279
* Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
273280
* @param httpMethod the HTTP method

0 commit comments

Comments
 (0)