Skip to content

Commit d7524e3

Browse files
committed
Merge pull request #1469 from bclozel
* pr/1497: Polish "Remove deprecated HttpComponents5Connection usage" Remove deprecated HttpComponents5Connection usage Closes gh-1469
2 parents 072e496 + f1e1496 commit d7524e3

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

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

Lines changed: 16 additions & 14 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;
@@ -43,19 +44,23 @@
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
*/
5558
public 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
/*

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)