Skip to content

Commit bca08a2

Browse files
committed
Remove deprecated HttpComponents5Connection usage
Prior to this commit, `HttpComponents5Connection` would use deprecated methods from `HttpClient`. This commit updates the deprecated `execute` in favor of `executeOpen`, while checking that the response resource is properly closed when the call is done. Fixes gh-1469 Signed-off-by: Brian Clozel <[email protected]>
1 parent 3463205 commit bca08a2

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.hc.core5.http.ContentType;
3232
import org.apache.hc.core5.http.HttpEntity;
3333
import org.apache.hc.core5.http.HttpHeaders;
34+
import org.apache.hc.core5.http.HttpHost;
3435
import org.apache.hc.core5.http.HttpResponse;
3536
import org.apache.hc.core5.http.NameValuePair;
3637
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
@@ -56,6 +57,8 @@ public class HttpComponents5Connection extends AbstractHttpSenderConnection {
5657

5758
private final HttpClient httpClient;
5859

60+
private final HttpHost httpHost;
61+
5962
private final HttpPost httpPost;
6063

6164
private final HttpContext httpContext;
@@ -64,16 +67,23 @@ public class HttpComponents5Connection extends AbstractHttpSenderConnection {
6467

6568
private ByteArrayOutputStream requestBuffer;
6669

67-
protected HttpComponents5Connection(HttpClient httpClient, HttpPost httpPost, HttpContext httpContext) {
70+
protected HttpComponents5Connection(HttpClient httpClient, HttpHost httpHost, HttpPost httpPost,
71+
HttpContext httpContext) {
6872

6973
Assert.notNull(httpClient, "httpClient must not be null");
74+
Assert.notNull(httpHost, "httpHost must not be null");
7075
Assert.notNull(httpPost, "httpPost must not be null");
7176

7277
this.httpClient = httpClient;
78+
this.httpHost = httpHost;
7379
this.httpPost = httpPost;
7480
this.httpContext = httpContext;
7581
}
7682

83+
public HttpHost getHttpHost() {
84+
return this.httpHost;
85+
}
86+
7787
public HttpPost getHttpPost() {
7888
return this.httpPost;
7989
}
@@ -84,12 +94,11 @@ public HttpResponse getHttpResponse() {
8494

8595
@Override
8696
public void onClose() throws IOException {
87-
8897
if (this.httpResponse instanceof ClassicHttpResponse response) {
89-
9098
if (response.getEntity() != null) {
9199
EntityUtils.consume(response.getEntity());
92100
}
101+
response.close();
93102
}
94103
}
95104

@@ -121,20 +130,11 @@ protected OutputStream getRequestOutputStream() throws IOException {
121130
}
122131

123132
@Override
124-
@SuppressWarnings("deprecation")
125133
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
126-
127134
String contentType = this.httpPost.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
128135
this.httpPost.setEntity(new ByteArrayEntity(this.requestBuffer.toByteArray(), ContentType.parse(contentType)));
129-
130136
this.requestBuffer = null;
131-
132-
if (this.httpContext != null) {
133-
this.httpResponse = this.httpClient.execute(this.httpPost, this.httpContext);
134-
}
135-
else {
136-
this.httpResponse = this.httpClient.execute(this.httpPost);
137-
}
137+
this.httpResponse = this.httpClient.executeOpen(this.httpHost, this.httpPost, this.httpContext);
138138
}
139139

140140
/*

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.hc.core5.http.EntityDetails;
3232
import org.apache.hc.core5.http.HttpException;
3333
import org.apache.hc.core5.http.HttpHeaders;
34+
import org.apache.hc.core5.http.HttpHost;
3435
import org.apache.hc.core5.http.HttpRequest;
3536
import org.apache.hc.core5.http.HttpRequestInterceptor;
3637
import org.apache.hc.core5.http.protocol.HttpContext;
@@ -198,9 +199,10 @@ public WebServiceConnection createConnection(URI uri) throws IOException {
198199
HttpTransportConstants.CONTENT_ENCODING_GZIP);
199200
}
200201

202+
HttpHost httpHost = HttpHost.create(uri);
201203
HttpContext httpContext = createContext(uri);
202204

203-
return new HttpComponents5Connection(getHttpClient(), httpPost, httpContext);
205+
return new HttpComponents5Connection(getHttpClient(), httpHost, httpPost, httpContext);
204206
}
205207

206208
/**

0 commit comments

Comments
 (0)