Skip to content

Commit 611eb9b

Browse files
committed
Use provided HttpClient consistently
This commit harmonizes the behavior of HttpComponents5MessageSender to reuse a provided HttpClient, be it provided via constructor or property. Closes gh-1512
1 parent 93eb5f4 commit 611eb9b

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpComponents5MessageSender.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,15 @@ public class HttpComponents5MessageSender extends AbstractHttpWebServiceMessageS
6363

6464
private static final String HTTP_CLIENT_ALREADY_SET = "httpClient already set";
6565

66-
private HttpClient httpClient;
66+
private final HttpComponents5ClientFactory clientFactory;
6767

68-
private HttpComponents5ClientFactory clientFactory;
68+
private HttpClient httpClient;
6969

7070
/**
7171
* Create a new instance of the {@code HttpClientMessageSender} with a default
7272
* {@link HttpClient} that uses a default {@link PoolingHttpClientConnectionManager}.
7373
*/
7474
public HttpComponents5MessageSender() {
75-
7675
this.clientFactory = new HttpComponents5ClientFactory();
7776
this.clientFactory.setClientBuilderCustomizer(
7877
httpClientBuilder -> httpClientBuilder.addRequestInterceptorFirst(new RemoveSoapHeadersInterceptor()));
@@ -89,7 +88,7 @@ public HttpComponents5MessageSender() {
8988
* @param httpClient the HttpClient instance to use for this sender
9089
*/
9190
public HttpComponents5MessageSender(HttpClient httpClient) {
92-
91+
this();
9392
Assert.notNull(httpClient, "httpClient must not be null");
9493
this.httpClient = httpClient;
9594
}
@@ -98,23 +97,19 @@ public HttpComponents5MessageSender(HttpClient httpClient) {
9897
* * @see HttpComponents5ClientFactory#setAuthScope(AuthScope)
9998
*/
10099
public void setAuthScope(AuthScope authScope) {
101-
102100
if (getHttpClient() != null) {
103101
throw new IllegalStateException(HTTP_CLIENT_ALREADY_SET);
104102
}
105-
106103
this.clientFactory.setAuthScope(authScope);
107104
}
108105

109106
/*
110107
* * @see HttpComponents5ClientFactory#setCredentials(Credentials)
111108
*/
112109
public void setCredentials(Credentials credentials) {
113-
114110
if (getHttpClient() != null) {
115111
throw new IllegalStateException(HTTP_CLIENT_ALREADY_SET);
116112
}
117-
118113
this.clientFactory.setCredentials(credentials);
119114
}
120115

@@ -127,63 +122,64 @@ public HttpClient getHttpClient() {
127122

128123
/**
129124
* Set the {@code HttpClient} used by this message sender.
125+
* <p>
126+
* This effectively disable any customization and does not change the given
127+
* {@code HttpClient} in any way. As such, it does not set timeouts, nor does it
128+
* {@linkplain HttpClientBuilder#addRequestInterceptorFirst(HttpRequestInterceptor)
129+
* add} the {@link RemoveSoapHeadersInterceptor}.
130+
* @param httpClient the HttpClient to use
130131
*/
131132
public void setHttpClient(HttpClient httpClient) {
132133
this.httpClient = httpClient;
133134
}
134135

135-
/*
136-
* * @see HttpComponents5ClientFactory#setConnectionTimeout(Duration)
136+
/**
137+
* Set the timeout until a connection is established.
138+
* @see HttpComponents5ClientFactory#setConnectionTimeout(Duration)
137139
*/
138140
public void setConnectionTimeout(Duration timeout) {
139-
140141
if (getHttpClient() != null) {
141142
throw new IllegalStateException(HTTP_CLIENT_ALREADY_SET);
142143
}
143-
144144
this.clientFactory.setConnectionTimeout(timeout);
145145
}
146146

147-
/*
148-
* * @see HttpComponents5ClientFactory#setReadTimeout(Duration)
147+
/**
148+
* Set the socket read timeout for the underlying HttpClient.
149+
* @see HttpComponents5ClientFactory#setReadTimeout(Duration)
149150
*/
150151
public void setReadTimeout(Duration timeout) {
151-
152152
if (getHttpClient() != null) {
153153
throw new IllegalStateException(HTTP_CLIENT_ALREADY_SET);
154154
}
155-
156155
this.clientFactory.setReadTimeout(timeout);
157156
}
158157

159-
/*
160-
* * @see HttpComponents5ClientFactory#setMaxTotalConnections(int)
158+
/**
159+
* Sets the maximum number of connections allowed for the underlying HttpClient.
160+
* @see HttpComponents5ClientFactory#setMaxTotalConnections(int)
161161
*/
162162
public void setMaxTotalConnections(int maxTotalConnections) {
163-
164163
if (getHttpClient() != null) {
165164
throw new IllegalStateException(HTTP_CLIENT_ALREADY_SET);
166165
}
167-
168166
this.clientFactory.setMaxTotalConnections(maxTotalConnections);
169167
}
170168

171-
/*
172-
* * @see HttpComponents5ClientFactory#setMaxConnectionsPerHost(Map)
169+
/**
170+
* Sets the maximum number of connections per host for the underlying HttpClient.
171+
* @see HttpComponents5ClientFactory#setMaxConnectionsPerHost(Map)
173172
*/
174173
public void setMaxConnectionsPerHost(Map<String, String> maxConnectionsPerHost) {
175-
176174
if (getHttpClient() != null) {
177175
throw new IllegalStateException(HTTP_CLIENT_ALREADY_SET);
178176
}
179-
180177
this.clientFactory.setMaxConnectionsPerHost(maxConnectionsPerHost);
181178
}
182179

183180
@Override
184181
public void afterPropertiesSet() throws Exception {
185-
186-
if (this.clientFactory != null) {
182+
if (getHttpClient() == null) {
187183
this.httpClient = this.clientFactory.getObject();
188184
}
189185
}

spring-ws-core/src/test/java/org/springframework/ws/transport/http/HttpComponents5MessageSenderTest.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,39 @@
2525
import static org.assertj.core.api.Assertions.assertThat;
2626
import static org.assertj.core.api.Assertions.assertThatCode;
2727

28+
/**
29+
* Tests for {@link HttpComponents5MessageSender}.
30+
*
31+
* @author Lars Uffmann
32+
* @author Stephane Nicoll
33+
*/
2834
class HttpComponents5MessageSenderTest {
2935

3036
@Test
3137
void afterPropertiesSetShouldProperlyInitializeHttpClient() throws Exception {
32-
3338
HttpComponents5MessageSender messageSender = new HttpComponents5MessageSender();
3439
assertThat(messageSender.getHttpClient()).isNull();
35-
36-
Duration timeout = Duration.ofSeconds(1);
37-
assertThatCode(() -> messageSender.setConnectionTimeout(timeout)).doesNotThrowAnyException();
40+
messageSender.setConnectionTimeout(Duration.ofSeconds(1));
3841

3942
messageSender.afterPropertiesSet();
4043
assertThat(messageSender.getHttpClient()).isNotNull();
4144
}
4245

4346
@Test
44-
void afterPropertiesSetShouldUseAlreadyProvidedHttpClientIfAvailable() throws Exception {
45-
47+
void afterPropertiesSetShouldUseAlreadyProvidedHttpClientIfAvailableWithConstructor() throws Exception {
4648
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
4749
HttpComponents5MessageSender messageSender = new HttpComponents5MessageSender(httpClient);
50+
assertThatCode(() -> messageSender.setConnectionTimeout(Duration.ofSeconds(1)))
51+
.isInstanceOf(IllegalStateException.class);
52+
messageSender.afterPropertiesSet();
53+
assertThat(messageSender.getHttpClient()).isSameAs(httpClient);
54+
}
4855

49-
Duration timeout = Duration.ofSeconds(1);
50-
assertThatCode(() -> messageSender.setConnectionTimeout(timeout)).isInstanceOf(IllegalStateException.class);
51-
56+
@Test
57+
void afterPropertiesSetShouldUseAlreadyProvidedHttpClientIfAvailableWithProperty() throws Exception {
58+
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
59+
HttpComponents5MessageSender messageSender = new HttpComponents5MessageSender();
60+
messageSender.setHttpClient(httpClient);
5261
messageSender.afterPropertiesSet();
5362
assertThat(messageSender.getHttpClient()).isSameAs(httpClient);
5463
}

0 commit comments

Comments
 (0)