3131import org .apache .hc .core5 .http .ContentType ;
3232import org .apache .hc .core5 .http .HttpEntity ;
3333import org .apache .hc .core5 .http .HttpHeaders ;
34+ import org .apache .hc .core5 .http .HttpHost ;
3435import org .apache .hc .core5 .http .HttpResponse ;
3536import org .apache .hc .core5 .http .NameValuePair ;
3637import org .apache .hc .core5 .http .io .entity .ByteArrayEntity ;
4344
4445/**
4546 * Implementation of {@link WebServiceConnection} that is based on Apache HttpClient 5.
46- * Exposes a {@link HttpPost} and {@link HttpResponse}.
47+ * Exposes the {@linkplain #getHttpHost() HTTP host}, {@linkplain #getHttpPost() HTTP
48+ * port}, and {@linkplain #getHttpResponse() HTTP response}.
4749 *
4850 * @author Alan Stewart
4951 * @author Barry Pitman
5052 * @author Arjen Poutsma
5153 * @author Greg Turnquist
5254 * @author Lars Uffmann
55+ * @author Brian Clozel
5356 * @since 4.0.5
5457 */
5558public class HttpComponents5Connection extends AbstractHttpSenderConnection {
5659
5760 private final HttpClient httpClient ;
5861
62+ private final HttpHost httpHost ;
63+
5964 private final HttpPost httpPost ;
6065
6166 private final HttpContext httpContext ;
@@ -64,16 +69,23 @@ public class HttpComponents5Connection extends AbstractHttpSenderConnection {
6469
6570 private ByteArrayOutputStream requestBuffer ;
6671
67- protected HttpComponents5Connection (HttpClient httpClient , HttpPost httpPost , HttpContext httpContext ) {
72+ protected HttpComponents5Connection (HttpClient httpClient , HttpHost httpHost , HttpPost httpPost ,
73+ HttpContext httpContext ) {
6874
6975 Assert .notNull (httpClient , "httpClient must not be null" );
76+ Assert .notNull (httpHost , "httpHost must not be null" );
7077 Assert .notNull (httpPost , "httpPost must not be null" );
7178
7279 this .httpClient = httpClient ;
80+ this .httpHost = httpHost ;
7381 this .httpPost = httpPost ;
7482 this .httpContext = httpContext ;
7583 }
7684
85+ public HttpHost getHttpHost () {
86+ return this .httpHost ;
87+ }
88+
7789 public HttpPost getHttpPost () {
7890 return this .httpPost ;
7991 }
@@ -84,12 +96,11 @@ public HttpResponse getHttpResponse() {
8496
8597 @ Override
8698 public void onClose () throws IOException {
87-
8899 if (this .httpResponse instanceof ClassicHttpResponse response ) {
89-
90100 if (response .getEntity () != null ) {
91101 EntityUtils .consume (response .getEntity ());
92102 }
103+ response .close ();
93104 }
94105 }
95106
@@ -121,20 +132,11 @@ protected OutputStream getRequestOutputStream() throws IOException {
121132 }
122133
123134 @ Override
124- @ SuppressWarnings ("deprecation" )
125135 protected void onSendAfterWrite (WebServiceMessage message ) throws IOException {
126-
127136 String contentType = this .httpPost .getFirstHeader (HttpHeaders .CONTENT_TYPE ).getValue ();
128137 this .httpPost .setEntity (new ByteArrayEntity (this .requestBuffer .toByteArray (), ContentType .parse (contentType )));
129-
130138 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- }
139+ this .httpResponse = this .httpClient .executeOpen (this .httpHost , this .httpPost , this .httpContext );
138140 }
139141
140142 /*
0 commit comments