From bca08a203b1e325c8f86b45ac863e6c94b22e6c7 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 11 Mar 2025 13:08:45 +0100 Subject: [PATCH] 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 --- .../http/HttpComponents5Connection.java | 26 +++++++++---------- .../http/HttpComponents5MessageSender.java | 4 ++- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpComponents5Connection.java b/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpComponents5Connection.java index e6c553ea6..7a9da41fb 100644 --- a/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpComponents5Connection.java +++ b/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpComponents5Connection.java @@ -31,6 +31,7 @@ import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpResponse; import org.apache.hc.core5.http.NameValuePair; import org.apache.hc.core5.http.io.entity.ByteArrayEntity; @@ -56,6 +57,8 @@ public class HttpComponents5Connection extends AbstractHttpSenderConnection { private final HttpClient httpClient; + private final HttpHost httpHost; + private final HttpPost httpPost; private final HttpContext httpContext; @@ -64,16 +67,23 @@ public class HttpComponents5Connection extends AbstractHttpSenderConnection { private ByteArrayOutputStream requestBuffer; - protected HttpComponents5Connection(HttpClient httpClient, HttpPost httpPost, HttpContext httpContext) { + protected HttpComponents5Connection(HttpClient httpClient, HttpHost httpHost, HttpPost httpPost, + HttpContext httpContext) { Assert.notNull(httpClient, "httpClient must not be null"); + Assert.notNull(httpHost, "httpHost must not be null"); Assert.notNull(httpPost, "httpPost must not be null"); this.httpClient = httpClient; + this.httpHost = httpHost; this.httpPost = httpPost; this.httpContext = httpContext; } + public HttpHost getHttpHost() { + return this.httpHost; + } + public HttpPost getHttpPost() { return this.httpPost; } @@ -84,12 +94,11 @@ public HttpResponse getHttpResponse() { @Override public void onClose() throws IOException { - if (this.httpResponse instanceof ClassicHttpResponse response) { - if (response.getEntity() != null) { EntityUtils.consume(response.getEntity()); } + response.close(); } } @@ -121,20 +130,11 @@ protected OutputStream getRequestOutputStream() throws IOException { } @Override - @SuppressWarnings("deprecation") protected void onSendAfterWrite(WebServiceMessage message) throws IOException { - String contentType = this.httpPost.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue(); this.httpPost.setEntity(new ByteArrayEntity(this.requestBuffer.toByteArray(), ContentType.parse(contentType))); - this.requestBuffer = null; - - if (this.httpContext != null) { - this.httpResponse = this.httpClient.execute(this.httpPost, this.httpContext); - } - else { - this.httpResponse = this.httpClient.execute(this.httpPost); - } + this.httpResponse = this.httpClient.executeOpen(this.httpHost, this.httpPost, this.httpContext); } /* diff --git a/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpComponents5MessageSender.java b/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpComponents5MessageSender.java index 1a1069b4a..e9573e4d5 100644 --- a/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpComponents5MessageSender.java +++ b/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpComponents5MessageSender.java @@ -31,6 +31,7 @@ import org.apache.hc.core5.http.EntityDetails; import org.apache.hc.core5.http.HttpException; import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpRequest; import org.apache.hc.core5.http.HttpRequestInterceptor; import org.apache.hc.core5.http.protocol.HttpContext; @@ -198,9 +199,10 @@ public WebServiceConnection createConnection(URI uri) throws IOException { HttpTransportConstants.CONTENT_ENCODING_GZIP); } + HttpHost httpHost = HttpHost.create(uri); HttpContext httpContext = createContext(uri); - return new HttpComponents5Connection(getHttpClient(), httpPost, httpContext); + return new HttpComponents5Connection(getHttpClient(), httpHost, httpPost, httpContext); } /**