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.

See gh-1469

Signed-off-by: Brian Clozel <brian.clozel@broadcom.com>
This commit is contained in:
Brian Clozel
2025-03-11 13:08:45 +01:00
committed by Stéphane Nicoll
parent 072e4960dd
commit 3154279ee4
2 changed files with 16 additions and 14 deletions

View File

@@ -31,6 +31,7 @@ import org.apache.hc.core5.http.ClassicHttpResponse;
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 class HttpComponents5Connection extends AbstractHttpSenderConnection {
@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 @@ public class HttpComponents5Connection extends AbstractHttpSenderConnection {
}
@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);
}
/*

View File

@@ -31,6 +31,7 @@ import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
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 class HttpComponents5MessageSender extends AbstractHttpWebServiceMessageS
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);
}
/**