34
34
35
35
import org .apache .commons .logging .Log ;
36
36
import org .apache .commons .logging .LogFactory ;
37
-
38
37
import org .springframework .http .HttpStatus ;
38
+ import org .springframework .util .Assert ;
39
39
import org .springframework .ws .WebServiceMessage ;
40
40
import org .springframework .ws .transport .WebServiceConnection ;
41
41
42
42
/**
43
- * Implementation of the {@link WebServiceConnection} interface that uses a Java
44
- * {@link HttpClient}.
43
+ * Implementation of the {@link WebServiceConnection} interface that uses Java's built-in {@link HttpClient}.
45
44
*
46
45
* @author Marten Deinum
47
46
* @see java.net.http.HttpClient
48
47
* @see java.net.http.HttpRequest
49
- * @since 4.1
48
+ * @since 4.0
50
49
*/
51
50
public class JdkHttpClientConnection extends AbstractHttpSenderConnection {
52
51
53
- private static final List <String > DISALLOWED_HEADERS =
54
- List .of ("connection" , "content-length" , "expect" , "host" , "upgrade" );
52
+ private static final Log logger = LogFactory .getLog (JdkHttpClientConnection .class );
55
53
56
- private final Log logger = LogFactory .getLog (getClass ());
57
- private final HttpClient client ;
58
- private final Builder requestBuilder ;
54
+ private static final List <String > DISALLOWED_HEADERS = List .of ("connection" , "content-length" , "expect" , "host" ,
55
+ "upgrade" );
56
+
57
+ private final HttpClient httpClient ;
59
58
60
59
private final URI uri ;
61
60
62
- private HttpResponse <InputStream > response ;
61
+ private final Builder requestBuilder ;
62
+
63
63
private HttpRequest request ;
64
64
65
65
private ByteArrayOutputStream requestBuffer ;
66
66
67
- public JdkHttpClientConnection (HttpClient client , URI uri , Duration requestTimeout ) {
68
- this .client = client ;
67
+ private HttpResponse <InputStream > response ;
68
+
69
+ protected JdkHttpClientConnection (HttpClient httpClient , URI uri , Duration requestTimeout ) {
70
+
71
+ Assert .notNull (httpClient , "httpClient must not be null" );
72
+ Assert .notNull (uri , "uri must not be null" );
73
+ Assert .notNull (requestTimeout , "requestTimeout must not be null" );
74
+
75
+ this .httpClient = httpClient ;
69
76
this .uri = uri ;
70
77
this .requestBuilder = HttpRequest .newBuilder (uri ).timeout (requestTimeout );
71
78
}
72
79
73
80
@ Override
74
81
protected OutputStream getRequestOutputStream () throws IOException {
75
- return this . requestBuffer ;
82
+ return requestBuffer ;
76
83
}
77
84
78
85
@ Override
@@ -87,42 +94,51 @@ public Iterator<String> getResponseHeaders(String name) throws IOException {
87
94
88
95
@ Override
89
96
public void addRequestHeader (String name , String value ) throws IOException {
97
+
90
98
if (DISALLOWED_HEADERS .contains (name .toLowerCase ())) {
91
- logger .info ("HttpClient doesn't allow setting the '" + name + "' header, ignoring!" );
99
+ logger .trace ("HttpClient doesn't allow setting the '" + name + "' header, ignoring!" );
92
100
return ;
93
101
}
94
- this .requestBuilder .header (name , value );
102
+
103
+ requestBuilder .header (name , value );
95
104
}
96
105
97
106
@ Override
98
107
public URI getUri () throws URISyntaxException {
99
- return this . uri ;
108
+ return uri ;
100
109
}
101
110
102
111
@ Override
103
112
protected int getResponseCode () throws IOException {
104
- return this . response != null ? this . response .statusCode () : 0 ;
113
+ return response != null ? response .statusCode () : 0 ;
105
114
}
106
115
107
116
@ Override
108
117
protected String getResponseMessage () throws IOException {
118
+
109
119
HttpStatus status = HttpStatus .resolve (getResponseCode ());
110
- return status != null ? status .getReasonPhrase () : "" ;
120
+
121
+ return status != null //
122
+ ? status .getReasonPhrase () //
123
+ : "" ;
111
124
}
112
125
113
126
@ Override
114
127
protected long getResponseContentLength () throws IOException {
115
- if (this .response != null ) {
116
- return this .response .headers ()
117
- .firstValueAsLong (HttpTransportConstants .HEADER_CONTENT_LENGTH )
128
+
129
+ if (response != null ) {
130
+
131
+ return response .headers () //
132
+ .firstValueAsLong (HttpTransportConstants .HEADER_CONTENT_LENGTH ) //
118
133
.orElse (-1 );
119
134
}
135
+
120
136
return 0 ;
121
137
}
122
138
123
139
@ Override
124
140
protected InputStream getRawResponseInputStream () throws IOException {
125
- return this . response .body ();
141
+ return response .body ();
126
142
}
127
143
128
144
@ Override
@@ -132,22 +148,24 @@ protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
132
148
133
149
@ Override
134
150
protected void onSendAfterWrite (WebServiceMessage message ) throws IOException {
135
- byte [] body = this .requestBuffer .toByteArray ();
136
- this .request = requestBuilder .POST (BodyPublishers .ofByteArray (body )).build ();
151
+
152
+ byte [] body = requestBuffer .toByteArray ();
153
+
154
+ request = requestBuilder .POST (BodyPublishers .ofByteArray (body )).build ();
155
+
137
156
try {
138
- this .response = this .client .send (this .request , BodyHandlers .ofInputStream ());
139
- }
140
- catch (InterruptedException ex )
141
- {
157
+ response = httpClient .send (request , BodyHandlers .ofInputStream ());
158
+ } catch (InterruptedException ex ) {
142
159
Thread .currentThread ().interrupt ();
143
160
throw new IllegalStateException (ex );
144
161
}
145
162
}
146
163
147
164
@ Override
148
165
protected void onClose () throws IOException {
149
- if (this .response != null ) {
150
- this .response .body ().close ();
166
+
167
+ if (response != null ) {
168
+ response .body ().close ();
151
169
}
152
170
}
153
171
}
0 commit comments