16
16
17
17
package org .springframework .ws .transport .http ;
18
18
19
- import java .io .IOException ;
20
- import java .net .URI ;
21
19
import java .time .Duration ;
22
20
import java .util .Map ;
23
21
24
22
import org .apache .hc .client5 .http .auth .AuthScope ;
25
23
import org .apache .hc .client5 .http .auth .Credentials ;
26
24
import org .apache .hc .client5 .http .classic .HttpClient ;
27
- import org .apache .hc .client5 .http .classic .methods .HttpPost ;
28
- import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
29
25
import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
30
26
import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
31
- import org .apache .hc .core5 .http .EntityDetails ;
32
- import org .apache .hc .core5 .http .HttpException ;
33
- import org .apache .hc .core5 .http .HttpHeaders ;
34
- import org .apache .hc .core5 .http .HttpHost ;
35
- import org .apache .hc .core5 .http .HttpRequest ;
36
27
import org .apache .hc .core5 .http .HttpRequestInterceptor ;
37
- import org .apache .hc .core5 .http .protocol .HttpContext ;
38
28
39
- import org .springframework .beans .factory .DisposableBean ;
40
29
import org .springframework .beans .factory .InitializingBean ;
41
30
import org .springframework .util .Assert ;
42
- import org .springframework .ws .transport .WebServiceConnection ;
43
31
44
32
/**
45
- * {@code WebServiceMessageSender } implementation that uses
46
- * <a href="http://hc.apache.org/httpcomponents-client">Apache HttpClient</a> to execute
47
- * POST requests.
33
+ * {@code AbstractHttpComponents5MessageSender } implementation that configures the
34
+ * underlying <a href="http://hc.apache.org/httpcomponents-client">Apache HttpClient</a>
35
+ * that executes POST requests.
48
36
* <p>
49
- * Allows to use a pre-configured HttpClient instance, potentially with authentication,
50
- * HTTP connection pooling, etc. Authentication can also be set by injecting a
51
- * {@link Credentials} instance (such as the
52
- * {@link org.apache.hc.client5.http.auth.UsernamePasswordCredentials}).
37
+ * To specify the {@link HttpClient}, consider using
38
+ * {@link SimpleHttpComponents5MessageSender} instead.
53
39
*
54
40
* @author Alan Stewart
55
41
* @author Barry Pitman
59
45
* @since 4.0.5
60
46
* @see HttpClient
61
47
*/
62
- public class HttpComponents5MessageSender extends AbstractHttpWebServiceMessageSender
63
- implements InitializingBean , DisposableBean {
48
+ public class HttpComponents5MessageSender extends AbstractHttpComponents5MessageSender implements InitializingBean {
64
49
65
50
private static final String HTTP_CLIENT_ALREADY_SET = "httpClient already set" ;
66
51
@@ -87,40 +72,43 @@ public HttpComponents5MessageSender() {
87
72
* {@linkplain HttpClientBuilder#addRequestInterceptorFirst(HttpRequestInterceptor)
88
73
* add} the {@link RemoveSoapHeadersInterceptor}.
89
74
* @param httpClient the HttpClient instance to use for this sender
75
+ * @deprecated as of 4.1.0 in favor of {@link SimpleHttpComponents5MessageSender}
90
76
*/
77
+ @ Deprecated (since = "4.1.0" , forRemoval = true )
91
78
public HttpComponents5MessageSender (HttpClient httpClient ) {
92
79
this ();
93
80
Assert .notNull (httpClient , "httpClient must not be null" );
94
81
this .httpClient = httpClient ;
95
82
}
96
83
97
- /*
98
- * * @see HttpComponents5ClientFactory#setAuthScope(AuthScope)
84
+ @ Override
85
+ public HttpClient getHttpClient () {
86
+ return this .httpClient ;
87
+ }
88
+
89
+ /**
90
+ * Set the authentication scope to be used. Only used when the {@code credentials}
91
+ * property has been set.
92
+ * @see HttpComponents5ClientFactory#setAuthScope(AuthScope)
99
93
*/
100
94
public void setAuthScope (AuthScope authScope ) {
101
- if (getHttpClient () != null ) {
95
+ if (this . httpClient != null ) {
102
96
throw new IllegalStateException (HTTP_CLIENT_ALREADY_SET );
103
97
}
104
98
this .clientFactory .setAuthScope (authScope );
105
99
}
106
100
107
- /*
108
- * * @see HttpComponents5ClientFactory#setCredentials(Credentials)
101
+ /**
102
+ * Set the credentials to be used. If not set, no authentication is done.
103
+ * @see HttpComponents5ClientFactory#setCredentials(Credentials)
109
104
*/
110
105
public void setCredentials (Credentials credentials ) {
111
- if (getHttpClient () != null ) {
106
+ if (this . httpClient != null ) {
112
107
throw new IllegalStateException (HTTP_CLIENT_ALREADY_SET );
113
108
}
114
109
this .clientFactory .setCredentials (credentials );
115
110
}
116
111
117
- /**
118
- * Returns the {@code HttpClient} used by this message sender.
119
- */
120
- public HttpClient getHttpClient () {
121
- return this .httpClient ;
122
- }
123
-
124
112
/**
125
113
* Set the {@code HttpClient} used by this message sender.
126
114
* <p>
@@ -129,7 +117,9 @@ public HttpClient getHttpClient() {
129
117
* {@linkplain HttpClientBuilder#addRequestInterceptorFirst(HttpRequestInterceptor)
130
118
* add} the {@link RemoveSoapHeadersInterceptor}.
131
119
* @param httpClient the HttpClient to use
120
+ * @deprecated as of 4.1.0 in favor of {@link SimpleHttpComponents5MessageSender}
132
121
*/
122
+ @ Deprecated (since = "4.1.0" , forRemoval = true )
133
123
public void setHttpClient (HttpClient httpClient ) {
134
124
this .httpClient = httpClient ;
135
125
}
@@ -139,7 +129,7 @@ public void setHttpClient(HttpClient httpClient) {
139
129
* @see HttpComponents5ClientFactory#setConnectionTimeout(Duration)
140
130
*/
141
131
public void setConnectionTimeout (Duration timeout ) {
142
- if (getHttpClient () != null ) {
132
+ if (this . httpClient != null ) {
143
133
throw new IllegalStateException (HTTP_CLIENT_ALREADY_SET );
144
134
}
145
135
this .clientFactory .setConnectionTimeout (timeout );
@@ -150,7 +140,7 @@ public void setConnectionTimeout(Duration timeout) {
150
140
* @see HttpComponents5ClientFactory#setReadTimeout(Duration)
151
141
*/
152
142
public void setReadTimeout (Duration timeout ) {
153
- if (getHttpClient () != null ) {
143
+ if (this . httpClient != null ) {
154
144
throw new IllegalStateException (HTTP_CLIENT_ALREADY_SET );
155
145
}
156
146
this .clientFactory .setReadTimeout (timeout );
@@ -161,7 +151,7 @@ public void setReadTimeout(Duration timeout) {
161
151
* @see HttpComponents5ClientFactory#setMaxTotalConnections(int)
162
152
*/
163
153
public void setMaxTotalConnections (int maxTotalConnections ) {
164
- if (getHttpClient () != null ) {
154
+ if (this . httpClient != null ) {
165
155
throw new IllegalStateException (HTTP_CLIENT_ALREADY_SET );
166
156
}
167
157
this .clientFactory .setMaxTotalConnections (maxTotalConnections );
@@ -172,74 +162,17 @@ public void setMaxTotalConnections(int maxTotalConnections) {
172
162
* @see HttpComponents5ClientFactory#setMaxConnectionsPerHost(Map)
173
163
*/
174
164
public void setMaxConnectionsPerHost (Map <String , String > maxConnectionsPerHost ) {
175
- if (getHttpClient () != null ) {
165
+ if (this . httpClient != null ) {
176
166
throw new IllegalStateException (HTTP_CLIENT_ALREADY_SET );
177
167
}
178
168
this .clientFactory .setMaxConnectionsPerHost (maxConnectionsPerHost );
179
169
}
180
170
181
171
@ Override
182
172
public void afterPropertiesSet () throws Exception {
183
- if (getHttpClient () == null ) {
173
+ if (this . httpClient == null ) {
184
174
this .httpClient = this .clientFactory .getObject ();
185
175
}
186
176
}
187
177
188
- @ Override
189
- public WebServiceConnection createConnection (URI uri ) throws IOException {
190
-
191
- HttpPost httpPost = new HttpPost (uri );
192
-
193
- if (isAcceptGzipEncoding ()) {
194
- httpPost .addHeader (HttpTransportConstants .HEADER_ACCEPT_ENCODING ,
195
- HttpTransportConstants .CONTENT_ENCODING_GZIP );
196
- }
197
-
198
- HttpHost httpHost = HttpHost .create (uri );
199
- HttpContext httpContext = createContext (uri );
200
-
201
- return new HttpComponents5Connection (getHttpClient (), httpHost , httpPost , httpContext );
202
- }
203
-
204
- /**
205
- * Template method that allows for creation of an {@link HttpContext} for the given
206
- * uri. Default implementation returns {@code null}.
207
- * @param uri the URI to create the context for
208
- * @return the context, or {@code null}
209
- */
210
- protected HttpContext createContext (URI uri ) {
211
- return null ;
212
- }
213
-
214
- @ Override
215
- public void destroy () throws Exception {
216
-
217
- if (getHttpClient () instanceof CloseableHttpClient client ) {
218
- client .close ();
219
- }
220
- }
221
-
222
- /**
223
- * HttpClient {@link HttpRequestInterceptor} implementation that removes
224
- * {@code Content-Length} and {@code Transfer-Encoding} headers from the request.
225
- * Necessary, because some SAAJ and other SOAP implementations set these headers
226
- * themselves, and HttpClient throws an exception if they have been set.
227
- */
228
- public static class RemoveSoapHeadersInterceptor implements HttpRequestInterceptor {
229
-
230
- @ Override
231
- public void process (HttpRequest request , EntityDetails entityDetails , HttpContext httpContext )
232
- throws HttpException , IOException {
233
-
234
- if (request .containsHeader (HttpHeaders .TRANSFER_ENCODING )) {
235
- request .removeHeaders (HttpHeaders .TRANSFER_ENCODING );
236
- }
237
-
238
- if (request .containsHeader (HttpHeaders .CONTENT_LENGTH )) {
239
- request .removeHeaders (HttpHeaders .CONTENT_LENGTH );
240
- }
241
- }
242
-
243
- }
244
-
245
178
}
0 commit comments