Skip to content

Commit aa481e6

Browse files
committed
Polishing.
Related: #1355.
1 parent 5db97a7 commit aa481e6

File tree

2 files changed

+81
-52
lines changed

2 files changed

+81
-52
lines changed

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

+48-30
Original file line numberDiff line numberDiff line change
@@ -34,45 +34,52 @@
3434

3535
import org.apache.commons.logging.Log;
3636
import org.apache.commons.logging.LogFactory;
37-
3837
import org.springframework.http.HttpStatus;
38+
import org.springframework.util.Assert;
3939
import org.springframework.ws.WebServiceMessage;
4040
import org.springframework.ws.transport.WebServiceConnection;
4141

4242
/**
43-
* Implementation of the {@link WebServiceConnection} interface that uses a Java
44-
* {@link HttpClient}.
43+
* Implementation of the {@link WebServiceConnection} interface that uses Java's built-in {@link HttpClient}.
4544
*
4645
* @author Marten Deinum
4746
* @see java.net.http.HttpClient
4847
* @see java.net.http.HttpRequest
49-
* @since 4.1
48+
* @since 4.0
5049
*/
5150
public class JdkHttpClientConnection extends AbstractHttpSenderConnection {
5251

53-
private static final List<String> DISALLOWED_HEADERS =
54-
List.of("connection", "content-length", "expect", "host", "upgrade");
52+
private static final Log logger = LogFactory.getLog(JdkHttpClientConnection.class);
5553

56-
private final Log logger = LogFactory.getLog(getClass());
57-
private final HttpClient client;
58-
private final Builder requestBuilder;
54+
private static final List<String> DISALLOWED_HEADERS = List.of("connection", "content-length", "expect", "host",
55+
"upgrade");
56+
57+
private final HttpClient httpClient;
5958

6059
private final URI uri;
6160

62-
private HttpResponse<InputStream> response;
61+
private final Builder requestBuilder;
62+
6363
private HttpRequest request;
6464

6565
private ByteArrayOutputStream requestBuffer;
6666

67-
public JdkHttpClientConnection(HttpClient client, URI uri, Duration requestTimeout) {
68-
this.client = client;
67+
private HttpResponse<InputStream> response;
68+
69+
protected JdkHttpClientConnection(HttpClient httpClient, URI uri, Duration requestTimeout) {
70+
71+
Assert.notNull(httpClient, "httpClient must not be null");
72+
Assert.notNull(uri, "uri must not be null");
73+
Assert.notNull(requestTimeout, "requestTimeout must not be null");
74+
75+
this.httpClient = httpClient;
6976
this.uri = uri;
7077
this.requestBuilder = HttpRequest.newBuilder(uri).timeout(requestTimeout);
7178
}
7279

7380
@Override
7481
protected OutputStream getRequestOutputStream() throws IOException {
75-
return this.requestBuffer;
82+
return requestBuffer;
7683
}
7784

7885
@Override
@@ -87,42 +94,51 @@ public Iterator<String> getResponseHeaders(String name) throws IOException {
8794

8895
@Override
8996
public void addRequestHeader(String name, String value) throws IOException {
97+
9098
if (DISALLOWED_HEADERS.contains(name.toLowerCase())) {
91-
logger.info("HttpClient doesn't allow setting the '"+name + "' header, ignoring!");
99+
logger.trace("HttpClient doesn't allow setting the '" + name + "' header, ignoring!");
92100
return;
93101
}
94-
this.requestBuilder.header(name, value);
102+
103+
requestBuilder.header(name, value);
95104
}
96105

97106
@Override
98107
public URI getUri() throws URISyntaxException {
99-
return this.uri;
108+
return uri;
100109
}
101110

102111
@Override
103112
protected int getResponseCode() throws IOException {
104-
return this.response != null ? this.response.statusCode() : 0;
113+
return response != null ? response.statusCode() : 0;
105114
}
106115

107116
@Override
108117
protected String getResponseMessage() throws IOException {
118+
109119
HttpStatus status = HttpStatus.resolve(getResponseCode());
110-
return status != null ? status.getReasonPhrase() : "";
120+
121+
return status != null //
122+
? status.getReasonPhrase() //
123+
: "";
111124
}
112125

113126
@Override
114127
protected long getResponseContentLength() throws IOException {
115-
if (this.response != null) {
116-
return this.response.headers()
117-
.firstValueAsLong(HttpTransportConstants.HEADER_CONTENT_LENGTH)
128+
129+
if (response != null) {
130+
131+
return response.headers() //
132+
.firstValueAsLong(HttpTransportConstants.HEADER_CONTENT_LENGTH) //
118133
.orElse(-1);
119134
}
135+
120136
return 0;
121137
}
122138

123139
@Override
124140
protected InputStream getRawResponseInputStream() throws IOException {
125-
return this.response.body();
141+
return response.body();
126142
}
127143

128144
@Override
@@ -132,22 +148,24 @@ protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
132148

133149
@Override
134150
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
135-
byte[] body = this.requestBuffer.toByteArray();
136-
this.request = requestBuilder.POST(BodyPublishers.ofByteArray(body)).build();
151+
152+
byte[] body = requestBuffer.toByteArray();
153+
154+
request = requestBuilder.POST(BodyPublishers.ofByteArray(body)).build();
155+
137156
try {
138-
this.response = this.client.send(this.request, BodyHandlers.ofInputStream());
139-
}
140-
catch (InterruptedException ex)
141-
{
157+
response = httpClient.send(request, BodyHandlers.ofInputStream());
158+
} catch (InterruptedException ex) {
142159
Thread.currentThread().interrupt();
143160
throw new IllegalStateException(ex);
144161
}
145162
}
146163

147164
@Override
148165
protected void onClose() throws IOException {
149-
if (this.response != null) {
150-
this.response.body().close();
166+
167+
if (response != null) {
168+
response.body().close();
151169
}
152170
}
153171
}

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

+33-22
Original file line numberDiff line numberDiff line change
@@ -22,60 +22,71 @@
2222
import java.time.Duration;
2323

2424
import org.springframework.beans.factory.InitializingBean;
25+
import org.springframework.lang.Nullable;
26+
import org.springframework.util.Assert;
2527
import org.springframework.ws.transport.WebServiceConnection;
2628

2729
/**
28-
* {@code WebServiceMessageSender} implementation that uses the standard Java {@code HttpClient}
29-
* facilities to execute POST requests.
30+
* {@code WebServiceMessageSender} implementation that uses the standard Java {@code HttpClient} facilities to execute
31+
* POST requests.
3032
* <p>
31-
* Can be used with a simple default configured {@code HttpClient} or can be constructed with a
32-
* pre-configured {@code HttpClient}.
33+
* Can be used with a simple default configured {@code HttpClient} or can be constructed with a pre-configured
34+
* {@code HttpClient}.
3335
*
3436
* @author Marten Deinum
3537
* @see java.net.http.HttpClient
36-
* @since 4.1
38+
* @since 4.0
3739
*/
38-
public class JdkHttpClientMessageSender extends AbstractHttpWebServiceMessageSender
39-
implements InitializingBean {
40+
public class JdkHttpClientMessageSender extends AbstractHttpWebServiceMessageSender implements InitializingBean {
4041

41-
private Duration connectionTimeout = Duration.ofSeconds(60);
42-
private Duration requestTimeout = Duration.ofSeconds(60);
42+
private static final Duration DEFAULT_CONNECTION_TIMEOUT = Duration.ofSeconds(60);
4343

44-
private HttpClient client;
44+
private static final Duration DEFAULT_REQUEST_TIMEOUT = Duration.ofSeconds(60);
45+
46+
private HttpClient httpClient;
47+
48+
private Duration connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
49+
50+
private Duration requestTimeout = DEFAULT_REQUEST_TIMEOUT;
4551

4652
public JdkHttpClientMessageSender() {}
4753

48-
public JdkHttpClientMessageSender(HttpClient client) {
49-
this.client = client;
54+
public JdkHttpClientMessageSender(HttpClient httpClient) {
55+
56+
Assert.notNull(httpClient, "httpClient must not be null");
57+
this.httpClient = httpClient;
5058
}
5159

52-
public void setConnectionTimeout(Duration connectionTimeout) {
60+
public void setHttpClient(@Nullable HttpClient httpClient) {
61+
this.httpClient = httpClient;
62+
}
63+
64+
public void setConnectionTimeout(@Nullable Duration connectionTimeout) {
5365
this.connectionTimeout = connectionTimeout;
5466
}
5567

56-
public void setRequestTimeout(Duration requestTimeout) {
68+
public void setRequestTimeout(@Nullable Duration requestTimeout) {
5769
this.requestTimeout = requestTimeout;
5870
}
5971

6072
@Override
6173
public WebServiceConnection createConnection(URI uri) throws IOException {
6274

63-
JdkHttpClientConnection connection =
64-
new JdkHttpClientConnection(this.client, uri, requestTimeout);
75+
JdkHttpClientConnection connection = new JdkHttpClientConnection(httpClient, uri, requestTimeout);
76+
6577
if (isAcceptGzipEncoding()) {
66-
connection.addRequestHeader(
67-
HttpTransportConstants.HEADER_ACCEPT_ENCODING,
78+
connection.addRequestHeader(HttpTransportConstants.HEADER_ACCEPT_ENCODING,
6879
HttpTransportConstants.CONTENT_ENCODING_GZIP);
6980
}
81+
7082
return connection;
7183
}
7284

7385
@Override
7486
public void afterPropertiesSet() throws Exception {
75-
if (this.client == null) {
76-
this.client = HttpClient.newBuilder()
77-
.connectTimeout(this.connectionTimeout)
78-
.build();
87+
88+
if (httpClient == null) {
89+
httpClient = HttpClient.newBuilder().connectTimeout(connectionTimeout).build();
7990
}
8091
}
8192
}

0 commit comments

Comments
 (0)