-
Notifications
You must be signed in to change notification settings - Fork 40
/
HttpTransportPipe.java
451 lines (402 loc) · 17.9 KB
/
HttpTransportPipe.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.ws.transport.http.client;
import com.sun.istack.NotNull;
import com.sun.xml.ws.api.SOAPVersion;
import com.sun.xml.ws.api.WSBinding;
import com.sun.xml.ws.api.ha.StickyFeature;
import com.sun.xml.ws.api.message.Packet;
import com.sun.xml.ws.api.pipe.*;
import com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl;
import com.sun.xml.ws.client.ClientTransportException;
import com.sun.xml.ws.developer.HttpConfigFeature;
import com.sun.xml.ws.resources.ClientMessages;
import com.sun.xml.ws.resources.WsservletMessages;
import com.sun.xml.ws.transport.Headers;
import com.sun.xml.ws.transport.http.HttpAdapter;
import com.sun.xml.ws.util.ByteArrayBuffer;
import com.sun.xml.ws.util.RuntimeVersion;
import com.sun.xml.ws.util.StreamUtils;
import javax.xml.bind.DatatypeConverter;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.soap.SOAPBinding;
import java.io.*;
import java.net.CookieHandler;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* {@link Tube} that sends a request to a remote HTTP server.
*
* TODO: need to create separate HTTP transport pipes for binding. SOAP1.1, SOAP1.2,
* TODO: XML/HTTP differ in handling status codes.
*
* @author Jitendra Kotamraju
*/
public class HttpTransportPipe extends AbstractTubeImpl {
private static final List<String> USER_AGENT = Collections.singletonList(RuntimeVersion.VERSION.toString());
private static final Logger LOGGER = Logger.getLogger(HttpTransportPipe.class.getName());
/**
* Dumps what goes across HTTP transport.
*/
public static boolean dump;
private final Codec codec;
private final WSBinding binding;
private final CookieHandler cookieJar; // shared object among the tubes
private final boolean sticky;
static {
try {
dump = Boolean.getBoolean(HttpTransportPipe.class.getName() + ".dump");
} catch (SecurityException se) {
if (LOGGER.isLoggable(Level.CONFIG)) {
LOGGER.log(Level.CONFIG, "Cannot read ''{0}'' property, using defaults.",
new Object[]{HttpTransportPipe.class.getName() + ".dump"});
}
}
}
public HttpTransportPipe(Codec codec, WSBinding binding) {
this.codec = codec;
this.binding = binding;
this.sticky = isSticky(binding);
HttpConfigFeature configFeature = binding.getFeature(HttpConfigFeature.class);
if (configFeature == null) {
configFeature = new HttpConfigFeature();
}
this.cookieJar = configFeature.getCookieHandler();
}
private static boolean isSticky(WSBinding binding) {
boolean tSticky = false;
WebServiceFeature[] features = binding.getFeatures().toArray();
for(WebServiceFeature f : features) {
if (f instanceof StickyFeature) {
tSticky = true;
break;
}
}
return tSticky;
}
/*
* Copy constructor for {@link Tube#copy(TubeCloner)}.
*/
private HttpTransportPipe(HttpTransportPipe that, TubeCloner cloner) {
this(that.codec.copy(), that.binding);
cloner.add(that,this);
}
@Override
public NextAction processException(@NotNull Throwable t) {
return doThrow(t);
}
@Override
public NextAction processRequest(@NotNull Packet request) {
return doReturnWith(process(request));
}
@Override
public NextAction processResponse(@NotNull Packet response) {
return doReturnWith(response);
}
protected HttpClientTransport getTransport(Packet request, Map<String, List<String>> reqHeaders) {
return new HttpClientTransport(request, reqHeaders);
}
@Override
public Packet process(Packet request) {
HttpClientTransport con;
try {
// get transport headers from message
Map<String, List<String>> reqHeaders = new Headers();
@SuppressWarnings("unchecked")
Map<String, List<String>> userHeaders = (Map<String, List<String>>) request.invocationProperties.get(MessageContext.HTTP_REQUEST_HEADERS);
boolean addUserAgent = true;
if (userHeaders != null) {
// userHeaders may not be modifiable like SingletonMap, just copy them
reqHeaders.putAll(userHeaders);
// application wants to use its own User-Agent header
if (userHeaders.get("User-Agent") != null) {
addUserAgent = false;
}
}
if (addUserAgent) {
reqHeaders.put("User-Agent", USER_AGENT);
}
addBasicAuth(request, reqHeaders);
addCookies(request, reqHeaders);
con = getTransport(request, reqHeaders);
request.addSatellite(new HttpResponseProperties(con));
ContentType ct = codec.getStaticContentType(request);
if (ct == null) {
ByteArrayBuffer buf = new ByteArrayBuffer();
ct = codec.encode(request, buf);
// data size is available, set it as Content-Length
reqHeaders.put("Content-Length", Collections.singletonList(Integer.toString(buf.size())));
reqHeaders.put("Content-Type", Collections.singletonList(ct.getContentType()));
if (ct.getAcceptHeader() != null) {
reqHeaders.put("Accept", Collections.singletonList(ct.getAcceptHeader()));
}
if (binding instanceof SOAPBinding) {
writeSOAPAction(reqHeaders, ct.getSOAPActionHeader());
}
if (dump || LOGGER.isLoggable(Level.FINER)) {
dump(buf, "HTTP request", reqHeaders);
}
buf.writeTo(con.getOutput());
} else {
// Set static Content-Type
reqHeaders.put("Content-Type", Collections.singletonList(ct.getContentType()));
if (ct.getAcceptHeader() != null) {
reqHeaders.put("Accept", Collections.singletonList(ct.getAcceptHeader()));
}
if (binding instanceof SOAPBinding) {
writeSOAPAction(reqHeaders, ct.getSOAPActionHeader());
}
if(dump || LOGGER.isLoggable(Level.FINER)) {
ByteArrayBuffer buf = new ByteArrayBuffer();
codec.encode(request, buf);
dump(buf, "HTTP request - "+request.endpointAddress, reqHeaders);
OutputStream out = con.getOutput();
if (out != null) {
buf.writeTo(out);
}
} else {
OutputStream os = con.getOutput();
if (os != null) {
codec.encode(request, os);
}
}
}
con.closeOutput();
return createResponsePacket(request, con);
} catch(WebServiceException wex) {
throw wex;
} catch(Exception ex) {
throw new WebServiceException(ex);
}
}
private Packet createResponsePacket(Packet request, HttpClientTransport con) throws IOException {
con.readResponseCodeAndMessage(); // throws IOE
recordCookies(request, con);
InputStream responseStream = con.getInput();
if (dump || LOGGER.isLoggable(Level.FINER)) {
ByteArrayBuffer buf = new ByteArrayBuffer();
if (responseStream != null) {
buf.write(responseStream);
responseStream.close();
}
dump(buf,"HTTP response - "+request.endpointAddress+" - "+con.statusCode, con.getHeaders());
responseStream = buf.newInputStream();
}
// Check if stream contains any data
int cl = con.contentLength;
InputStream tempIn = null;
if (cl == -1) { // No Content-Length header
tempIn = StreamUtils.hasSomeData(responseStream);
if (tempIn != null) {
responseStream = tempIn;
}
}
if (cl == 0 || (cl == -1 && tempIn == null)) {
if(responseStream != null) {
responseStream.close(); // No data, so close the stream
responseStream = null;
}
}
// Allows only certain http status codes for a binding. For all
// other status codes, throws exception
checkStatusCode(responseStream, con); // throws ClientTransportException
//To avoid zero-length chunk for One-Way
if (cl ==-1 && con.statusCode == 202 && "Accepted".equals(con.statusMessage) && responseStream != null) {
ByteArrayBuffer buf = new ByteArrayBuffer();
buf.write(responseStream); //What is within the responseStream?
responseStream.close();
responseStream = (buf.size()==0)? null : buf.newInputStream();
buf.close();
}
Packet reply = request.createClientResponse(null);
reply.wasTransportSecure = con.isSecure();
if (responseStream != null) {
String contentType = con.getContentType();
if (contentType != null && contentType.contains("text/html") && binding instanceof SOAPBinding) {
throw new ClientTransportException(ClientMessages.localizableHTTP_STATUS_CODE(con.statusCode, con.statusMessage));
}
codec.decode(responseStream, contentType, reply);
}
return reply;
}
/*
* Allows the following HTTP status codes.
* SOAP 1.1/HTTP - 200, 202, 500
* SOAP 1.2/HTTP - 200, 202, 400, 500
* XML/HTTP - all
*
* For all other status codes, it throws an exception
*/
private void checkStatusCode(InputStream in, HttpClientTransport con) throws IOException {
int statusCode = con.statusCode;
String statusMessage = con.statusMessage;
// SOAP1.1 and SOAP1.2 differ here
if (binding instanceof SOAPBinding) {
if (binding.getSOAPVersion() == SOAPVersion.SOAP_12) {
//In SOAP 1.2, Fault messages can be sent with 4xx and 5xx error codes
if (statusCode == HttpURLConnection.HTTP_OK || statusCode == HttpURLConnection.HTTP_ACCEPTED || isErrorCode(statusCode)) {
// acceptable status codes for SOAP 1.2
if (isErrorCode(statusCode) && in == null) {
// No envelope for the error, so throw an exception with http error details
throw new ClientTransportException(ClientMessages.localizableHTTP_STATUS_CODE(statusCode, statusMessage));
}
return;
}
} else {
// SOAP 1.1
if (statusCode == HttpURLConnection.HTTP_OK || statusCode == HttpURLConnection.HTTP_ACCEPTED || statusCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
// acceptable status codes for SOAP 1.1
if (statusCode == HttpURLConnection.HTTP_INTERNAL_ERROR && in == null) {
// No envelope for the error, so throw an exception with http error details
throw new ClientTransportException(ClientMessages.localizableHTTP_STATUS_CODE(statusCode, statusMessage));
}
return;
}
}
if (in != null) {
in.close();
}
throw new ClientTransportException(ClientMessages.localizableHTTP_STATUS_CODE(statusCode, statusMessage));
}
// Every status code is OK for XML/HTTP
}
private boolean isErrorCode(int code) {
//if(code/100 == 5/*Server-side error*/ || code/100 == 4 /*client error*/ ) {
return code == 500 || code == 400;
}
private void addCookies(Packet context, Map<String, List<String>> reqHeaders) throws IOException {
Boolean shouldMaintainSessionProperty =
(Boolean) context.invocationProperties.get(BindingProvider.SESSION_MAINTAIN_PROPERTY);
if (shouldMaintainSessionProperty != null && !shouldMaintainSessionProperty) {
return; // explicitly turned off
}
if (sticky || (shouldMaintainSessionProperty != null && shouldMaintainSessionProperty)) {
Map<String, List<String>> rememberedCookies = cookieJar.get(context.endpointAddress.getURI(), reqHeaders);
processCookieHeaders(reqHeaders, rememberedCookies, "Cookie");
processCookieHeaders(reqHeaders, rememberedCookies, "Cookie2");
}
}
private void processCookieHeaders(Map<String, List<String>> requestHeaders, Map<String, List<String>> rememberedCookies, String cookieHeader) {
List<String> jarCookies = rememberedCookies.get(cookieHeader);
if (jarCookies != null && !jarCookies.isEmpty()) {
List<String> resultCookies = mergeUserCookies(jarCookies, requestHeaders.get(cookieHeader));
requestHeaders.put(cookieHeader, resultCookies);
}
}
private List<String> mergeUserCookies(List<String> rememberedCookies, List<String> userCookies) {
// nothing to merge
if (userCookies == null || userCookies.isEmpty()) {
return rememberedCookies;
}
Map<String, String> map = new HashMap<String, String>();
cookieListToMap(rememberedCookies, map);
cookieListToMap(userCookies, map);
return new ArrayList<String>(map.values());
}
private void cookieListToMap(List<String> cookieList, Map<String, String> targetMap) {
for(String cookie : cookieList) {
int index = cookie.indexOf("=");
String cookieName = cookie.substring(0, index);
targetMap.put(cookieName, cookie);
}
}
private void recordCookies(Packet context, HttpClientTransport con) throws IOException {
Boolean shouldMaintainSessionProperty =
(Boolean) context.invocationProperties.get(BindingProvider.SESSION_MAINTAIN_PROPERTY);
if (shouldMaintainSessionProperty != null && !shouldMaintainSessionProperty) {
return; // explicitly turned off
}
if (sticky || (shouldMaintainSessionProperty != null && shouldMaintainSessionProperty)) {
cookieJar.put(context.endpointAddress.getURI(), con.getHeaders());
}
}
private void addBasicAuth(Packet context, Map<String, List<String>> reqHeaders) {
String user = (String) context.invocationProperties.get(BindingProvider.USERNAME_PROPERTY);
if (user != null) {
String pw = (String) context.invocationProperties.get(BindingProvider.PASSWORD_PROPERTY);
if (pw != null) {
StringBuilder buf = new StringBuilder(user);
buf.append(":");
buf.append(pw);
String creds = DatatypeConverter.printBase64Binary(buf.toString().getBytes());
reqHeaders.put("Authorization", Collections.singletonList("Basic "+creds));
}
}
}
/*
* write SOAPAction header if the soapAction parameter is non-null or BindingProvider properties set.
* BindingProvider properties take precedence.
*/
private void writeSOAPAction(Map<String, List<String>> reqHeaders, String soapAction) {
//dont write SOAPAction HTTP header for SOAP 1.2 messages.
if(SOAPVersion.SOAP_12.equals(binding.getSOAPVersion())) {
return;
}
if (soapAction != null) {
reqHeaders.put("SOAPAction", Collections.singletonList(soapAction));
} else {
reqHeaders.put("SOAPAction", Collections.singletonList("\"\""));
}
}
@Override
public void preDestroy() {
// nothing to do. Intentionally left empty.
}
@Override
public HttpTransportPipe copy(TubeCloner cloner) {
return new HttpTransportPipe(this,cloner);
}
private void dump(ByteArrayBuffer buf, String caption, Map<String, List<String>> headers) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos, true);
pw.println("---["+caption +"]---");
for (Entry<String,List<String>> header : headers.entrySet()) {
if(header.getValue().isEmpty()) {
// I don't think this is legal, but let's just dump it,
// as the point of the dump is to uncover problems.
pw.println(header.getValue());
} else {
for (String value : header.getValue()) {
pw.println(header.getKey()+": "+value);
}
}
}
if (buf.size() > HttpAdapter.dump_threshold) {
byte[] b = buf.getRawData();
baos.write(b, 0, HttpAdapter.dump_threshold);
pw.println();
pw.println(WsservletMessages.MESSAGE_TOO_LONG(HttpAdapter.class.getName() + ".dumpTreshold"));
} else {
buf.writeTo(baos);
pw.println();
}
pw.println("--------------------");
String msg = baos.toString();
if (dump) {
System.out.println(msg);
}
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, msg);
}
}
public static void setDump(boolean dumpMessages) {
HttpTransportPipe.dump = dumpMessages;
}
}