Skip to content

Commit 5236eb6

Browse files
committed
Allow default settings of a custom HttpAsyncClient to apply
This is a rework of 71783c5 for SPR-12540 for the async extension that was not merging the internal RequestConfig as it should. Issue: SPR-13125
1 parent 10cb80a commit 5236eb6

File tree

4 files changed

+92
-15
lines changed

4 files changed

+92
-15
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,6 +76,10 @@ public URI getURI() {
7676
return this.httpRequest.getURI();
7777
}
7878

79+
HttpContext getHttpContext() {
80+
return this.httpContext;
81+
}
82+
7983
@Override
8084
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput)
8185
throws IOException {

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

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* HttpAsyncClient 4.0</a> to create requests.
4141
*
4242
* @author Arjen Poutsma
43+
* @author Stephane Nicoll
4344
* @since 4.0
4445
* @see HttpAsyncClient
4546
*/
@@ -122,21 +123,41 @@ public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod)
122123
if (context == null) {
123124
context = HttpClientContext.create();
124125
}
125-
// Request configuration not set in the context
126-
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
127-
// Use request configuration given by the user, when available
128-
RequestConfig config = null;
129-
if (httpRequest instanceof Configurable) {
130-
config = ((Configurable) httpRequest).getConfig();
131-
}
132-
if (config == null) {
133-
config = RequestConfig.DEFAULT;
134-
}
135-
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
136-
}
126+
// Request configuration not set in the context
127+
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
128+
// Use request configuration given by the user, when available
129+
RequestConfig config = null;
130+
if (httpRequest instanceof Configurable) {
131+
config = ((Configurable) httpRequest).getConfig();
132+
}
133+
if (config == null) {
134+
config = createRequestConfig(asyncClient);
135+
}
136+
if (config != null) {
137+
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
138+
}
139+
}
137140
return new HttpComponentsAsyncClientHttpRequest(asyncClient, httpRequest, context);
138141
}
139142

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+
140161
@Override
141162
public void destroy() throws Exception {
142163
try {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ protected RequestConfig createRequestConfig(HttpClient client) {
245245
return this.requestConfig;
246246
}
247247

248-
private RequestConfig mergeRequestConfig(RequestConfig defaultRequestConfig) {
248+
protected RequestConfig mergeRequestConfig(RequestConfig defaultRequestConfig) {
249249
if (this.requestConfig == null) { // nothing to merge
250250
return defaultRequestConfig;
251251
}
@@ -265,6 +265,10 @@ private RequestConfig mergeRequestConfig(RequestConfig defaultRequestConfig) {
265265
return builder.build();
266266
}
267267

268+
protected final RequestConfig getInternalRequestConfig() {
269+
return this.requestConfig;
270+
}
271+
268272
/**
269273
* Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
270274
* @param httpMethod the HTTP method

spring-web/src/test/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequestFactoryTests.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,21 @@
1616

1717
package org.springframework.http.client;
1818

19+
import java.net.URI;
20+
21+
import org.apache.http.client.config.RequestConfig;
22+
import org.apache.http.client.protocol.HttpClientContext;
23+
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
24+
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
1925
import org.junit.Test;
2026

2127
import org.springframework.http.HttpMethod;
2228

29+
import static org.junit.Assert.*;
30+
2331
/**
2432
* @author Arjen Poutsma
33+
* @author Stephane Nicoll
2534
*/
2635
public class HttpComponentsAsyncClientHttpRequestFactoryTests extends AbstractAsyncHttpRequestFactoryTestCase {
2736

@@ -38,4 +47,43 @@ public void httpMethods() throws Exception {
3847
assertHttpMethod("patch", HttpMethod.PATCH);
3948
}
4049

50+
@Test
51+
public void customHttpAsyncClientUsesItsDefault() throws Exception {
52+
HttpComponentsAsyncClientHttpRequestFactory factory =
53+
new HttpComponentsAsyncClientHttpRequestFactory();
54+
55+
URI uri = new URI(baseUrl + "/status/ok");
56+
HttpComponentsAsyncClientHttpRequest request = (HttpComponentsAsyncClientHttpRequest)
57+
factory.createAsyncRequest(uri, HttpMethod.GET);
58+
59+
assertNull("No custom config should be set with a custom HttpAsyncClient",
60+
request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG));
61+
}
62+
63+
@Test
64+
public void defaultSettingsOfHttpAsyncClientLostOnExecutorCustomization() throws Exception {
65+
CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create()
66+
.setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(1234).build())
67+
.build();
68+
HttpComponentsAsyncClientHttpRequestFactory factory = new HttpComponentsAsyncClientHttpRequestFactory(client);
69+
70+
URI uri = new URI(baseUrl + "/status/ok");
71+
HttpComponentsAsyncClientHttpRequest request = (HttpComponentsAsyncClientHttpRequest)
72+
factory.createAsyncRequest(uri, HttpMethod.GET);
73+
74+
assertNull("No custom config should be set with a custom HttpClient",
75+
request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG));
76+
77+
factory.setConnectionRequestTimeout(4567);
78+
HttpComponentsAsyncClientHttpRequest request2 = (HttpComponentsAsyncClientHttpRequest)
79+
factory.createAsyncRequest(uri, HttpMethod.GET);
80+
Object requestConfigAttribute = request2.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG);
81+
assertNotNull(requestConfigAttribute);
82+
RequestConfig requestConfig = (RequestConfig) requestConfigAttribute;
83+
84+
assertEquals(4567, requestConfig.getConnectionRequestTimeout());
85+
// No way to access the request config of the HTTP client so no way to "merge" our customizations
86+
assertEquals(-1, requestConfig.getConnectTimeout());
87+
}
88+
4189
}

0 commit comments

Comments
 (0)