Skip to content

Commit 24b8274

Browse files
committed
Polish upgrade to HttpComponents 4.3
Polish fbe6051 to make explicit the fact that a null RequestConfig can be used to force the defaults of the current HttpClient. Issue: SPR-11113
1 parent d8a01cb commit 24b8274

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

spring-web/src/main/java/org/springframework/remoting/httpinvoker/HttpComponentsHttpInvokerRequestExecutor.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
* <p>As of Spring 4.1, this request executor requires Apache HttpComponents 4.3 or higher.
5757
*
5858
* @author Juergen Hoeller
59+
* @author Stephane Nicoll
5960
* @since 3.1
6061
* @see org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor
6162
*/
@@ -213,7 +214,10 @@ protected RemoteInvocationResult doExecuteRequest(
213214
*/
214215
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
215216
HttpPost httpPost = new HttpPost(config.getServiceUrl());
216-
httpPost.setConfig(createRequestConfig(config, httpPost));
217+
RequestConfig requestConfig = createRequestConfig(config);
218+
if (requestConfig != null) {
219+
httpPost.setConfig(requestConfig);
220+
}
217221
LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
218222
if (localeContext != null) {
219223
Locale locale = localeContext.getLocale();
@@ -228,13 +232,14 @@ protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws
228232
}
229233

230234
/**
231-
* Create a {@link RequestConfig} for the given configuration and {@link HttpPost}.
235+
* Create a {@link RequestConfig} for the given configuration. Can return {@code null}
236+
* to indicate that no custom request config should be set and the defaults of the
237+
* {@link HttpClient} should be used.
232238
* @param config the HTTP invoker configuration that specifies the
233239
* target service
234-
* @param httpPost the HttpPost instance
235-
* @return the RequestConfig to use for that HttpPost
240+
* @return the RequestConfig to use
236241
*/
237-
protected RequestConfig createRequestConfig(HttpInvokerClientConfiguration config, HttpPost httpPost) {
242+
protected RequestConfig createRequestConfig(HttpInvokerClientConfiguration config) {
238243
if (this.connectionTimeout > 0 || this.readTimeout > 0) {
239244
return RequestConfig.custom()
240245
.setConnectTimeout(this.connectionTimeout)

spring-web/src/test/java/org/springframework/remoting/httpinvoker/HttpComponentsHttpInvokerRequestExecutorTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import java.io.IOException;
44

5+
import org.apache.http.client.HttpClient;
6+
import org.apache.http.client.config.RequestConfig;
57
import org.apache.http.client.methods.HttpPost;
8+
import org.apache.http.impl.client.CloseableHttpClient;
9+
import org.apache.http.impl.client.HttpClientBuilder;
610
import org.junit.Test;
711

812
import static org.junit.Assert.*;
@@ -14,6 +18,21 @@
1418
*/
1519
public class HttpComponentsHttpInvokerRequestExecutorTests {
1620

21+
@SuppressWarnings("deprecation")
22+
@Test
23+
public void assertLegacyCustomConfig() {
24+
HttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient(); // Does not support RequestConfig
25+
HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(httpClient);
26+
27+
executor.setConnectTimeout(1234);
28+
assertEquals(1234, httpClient.getParams().getIntParameter(
29+
org.apache.http.params.CoreConnectionPNames.CONNECTION_TIMEOUT, 0));
30+
31+
executor.setReadTimeout(4567);
32+
assertEquals(4567, httpClient.getParams().getIntParameter(
33+
org.apache.http.params.CoreConnectionPNames.SO_TIMEOUT, 0));
34+
}
35+
1736
@Test
1837
public void customizeConnectionTimeout() throws IOException {
1938
HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor();
@@ -34,6 +53,21 @@ public void customizeReadTimeout() throws IOException {
3453
assertEquals(10000, httpPost.getConfig().getSocketTimeout());
3554
}
3655

56+
@Test
57+
public void ignoreFactorySettings() throws IOException {
58+
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
59+
HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(httpClient) {
60+
@Override
61+
protected RequestConfig createRequestConfig(HttpInvokerClientConfiguration config) {
62+
return null;
63+
}
64+
};
65+
66+
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
67+
HttpPost httpPost = executor.createHttpPost(config);
68+
assertNull("custom request config should not be set", httpPost.getConfig());
69+
}
70+
3771
private HttpInvokerClientConfiguration mockHttpInvokerClientConfiguration(String serviceUrl) {
3872
HttpInvokerClientConfiguration config = mock(HttpInvokerClientConfiguration.class);
3973
when(config.getServiceUrl()).thenReturn(serviceUrl);

0 commit comments

Comments
 (0)