Skip to content

Commit fa46f47

Browse files
committed
Merge branch '4.0.x'
Closes gh-1513
2 parents 8336f3d + 611eb9b commit fa46f47

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
@@ -64,16 +64,15 @@ public class HttpComponents5MessageSender extends AbstractHttpWebServiceMessageS
6464

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

67-
private HttpClient httpClient;
67+
private final HttpComponents5ClientFactory clientFactory;
6868

69-
private HttpComponents5ClientFactory clientFactory;
69+
private HttpClient httpClient;
7070

7171
/**
7272
* Create a new instance of the {@code HttpClientMessageSender} with a default
7373
* {@link HttpClient} that uses a default {@link PoolingHttpClientConnectionManager}.
7474
*/
7575
public HttpComponents5MessageSender() {
76-
7776
this.clientFactory = new HttpComponents5ClientFactory();
7877
this.clientFactory.setClientBuilderCustomizer(
7978
httpClientBuilder -> httpClientBuilder.addRequestInterceptorFirst(new RemoveSoapHeadersInterceptor()));
@@ -90,7 +89,7 @@ public HttpComponents5MessageSender() {
9089
* @param httpClient the HttpClient instance to use for this sender
9190
*/
9291
public HttpComponents5MessageSender(HttpClient httpClient) {
93-
92+
this();
9493
Assert.notNull(httpClient, "httpClient must not be null");
9594
this.httpClient = httpClient;
9695
}
@@ -99,23 +98,19 @@ public HttpComponents5MessageSender(HttpClient httpClient) {
9998
* * @see HttpComponents5ClientFactory#setAuthScope(AuthScope)
10099
*/
101100
public void setAuthScope(AuthScope authScope) {
102-
103101
if (getHttpClient() != null) {
104102
throw new IllegalStateException(HTTP_CLIENT_ALREADY_SET);
105103
}
106-
107104
this.clientFactory.setAuthScope(authScope);
108105
}
109106

110107
/*
111108
* * @see HttpComponents5ClientFactory#setCredentials(Credentials)
112109
*/
113110
public void setCredentials(Credentials credentials) {
114-
115111
if (getHttpClient() != null) {
116112
throw new IllegalStateException(HTTP_CLIENT_ALREADY_SET);
117113
}
118-
119114
this.clientFactory.setCredentials(credentials);
120115
}
121116

@@ -128,63 +123,64 @@ public HttpClient getHttpClient() {
128123

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

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

148-
/*
149-
* * @see HttpComponents5ClientFactory#setReadTimeout(Duration)
148+
/**
149+
* Set the socket read timeout for the underlying HttpClient.
150+
* @see HttpComponents5ClientFactory#setReadTimeout(Duration)
150151
*/
151152
public void setReadTimeout(Duration timeout) {
152-
153153
if (getHttpClient() != null) {
154154
throw new IllegalStateException(HTTP_CLIENT_ALREADY_SET);
155155
}
156-
157156
this.clientFactory.setReadTimeout(timeout);
158157
}
159158

160-
/*
161-
* * @see HttpComponents5ClientFactory#setMaxTotalConnections(int)
159+
/**
160+
* Sets the maximum number of connections allowed for the underlying HttpClient.
161+
* @see HttpComponents5ClientFactory#setMaxTotalConnections(int)
162162
*/
163163
public void setMaxTotalConnections(int maxTotalConnections) {
164-
165164
if (getHttpClient() != null) {
166165
throw new IllegalStateException(HTTP_CLIENT_ALREADY_SET);
167166
}
168-
169167
this.clientFactory.setMaxTotalConnections(maxTotalConnections);
170168
}
171169

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

184181
@Override
185182
public void afterPropertiesSet() throws Exception {
186-
187-
if (this.clientFactory != null) {
183+
if (getHttpClient() == null) {
188184
this.httpClient = this.clientFactory.getObject();
189185
}
190186
}

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)