|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
* yyyymmdd bug Email and other contact information |
| 11 |
* -------- -------- ----------------------------------------------------------- |
| 12 |
* 20060131 125777 jesper@selskabet.org - Jesper S Moller |
| 13 |
* 20060222 118019 andyzhai@ca.ibm.com - Andy Zhai |
| 14 |
* 20060222 128564 jesper@selskabet.org - Jesper S Moller |
| 15 |
* 20060823 99034 makandre@ca.ibm.com - Andrew Mak, WSE support for basic-authenticating firewalls |
| 16 |
* 20070413 176493 makandre@ca.ibm.com - Andrew Mak, WSE: Make message/transport stack pluggable |
| 17 |
*******************************************************************************/ |
| 18 |
package org.eclipse.wst.ws.internal.explorer.platform.wsdl.transport; |
| 19 |
|
| 20 |
import java.io.BufferedReader; |
| 21 |
import java.io.ByteArrayInputStream; |
| 22 |
import java.io.ByteArrayOutputStream; |
| 23 |
import java.io.IOException; |
| 24 |
import java.io.InputStream; |
| 25 |
import java.io.InputStreamReader; |
| 26 |
import java.io.OutputStream; |
| 27 |
import java.net.Socket; |
| 28 |
import java.net.SocketTimeoutException; |
| 29 |
import java.net.URL; |
| 30 |
import java.net.UnknownHostException; |
| 31 |
import java.util.Hashtable; |
| 32 |
import javax.net.ssl.SSLSocketFactory; |
| 33 |
|
| 34 |
import org.eclipse.wst.ws.internal.explorer.platform.util.XMLUtils; |
| 35 |
import org.eclipse.wst.ws.internal.explorer.transport.HTTPTransportException; |
| 36 |
import org.w3c.dom.Element; |
| 37 |
import sun.misc.BASE64Encoder; |
| 38 |
|
| 39 |
public class HTTPTransport |
| 40 |
{ |
| 41 |
private final String SYS_PROP_HTTPS_PROXY_HOST = "https.proxyHost"; |
| 42 |
private final String SYS_PROP_HTTPS_PROXY_PORT = "https.proxyPort"; |
| 43 |
private final String SYS_PROP_HTTPS_NON_PROXY_HOSTS = "https.nonProxyHosts"; |
| 44 |
private final String SYS_PROP_HTTP_PROXY_HOST = "http.proxyHost"; |
| 45 |
private final String SYS_PROP_HTTP_PROXY_PORT = "http.proxyPort"; |
| 46 |
private final String SYS_PROP_HTTP_PROXY_USER_NAME = "http.proxyUserName"; |
| 47 |
private final String SYS_PROP_HTTP_PROXY_PASSWORD = "http.proxyPassword"; |
| 48 |
private final String SYS_PROP_HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts"; |
| 49 |
|
| 50 |
private final String HTTP_METHOD = "POST"; |
| 51 |
private final String HTTP_CONNECT = "CONNECT"; |
| 52 |
private final String HTTP_VERSION_1_0 = "HTTP/1.0"; |
| 53 |
private final String HTTP_VERSION = "HTTP/1.1"; |
| 54 |
private final String HTTP_HEADER_SOAP_ACTION = "SOAPAction"; |
| 55 |
public static final String HTTP_HEADER_AUTH = "Authorization"; |
| 56 |
public static final String HTTP_HEADER_WWW_AUTH = "WWW-Authenticate"; |
| 57 |
private final String HTTP_HEADER_PROXY_AUTH = "Proxy-authorization"; |
| 58 |
private final String HTTP_HEADER_COOKIE = "Cookie"; |
| 59 |
private final String HTTP_HEADER_COOKIE2 = "Cookie2"; |
| 60 |
private final String HTTP_HEADER_HOST = "Host"; |
| 61 |
private final String HTTP_HEADER_CONTENT_TYPE = "Content-Type"; |
| 62 |
public static final String HTTP_HEADER_CONTENT_LENGTH = "Content-Length"; |
| 63 |
private final String HTTP_HEADER_ACCEPT = "Accept"; |
| 64 |
private final String HTTP_HEADER_USER_AGENT = "User-Agent"; |
| 65 |
private final String HTTP_HEADER_CACHE_CONTROL = "Cache-Control"; |
| 66 |
private final String HTTP_HEADER_PRAGMA = "Pragma"; |
| 67 |
private final String HTTP_HEADER_TRANSFER_ENCODEING = "Transfer-Encoding"; |
| 68 |
private final String HTTP_HEADER_CONNECTION = "Connection"; |
| 69 |
|
| 70 |
private final int HTTP_CODE_CONTINUE = 100; |
| 71 |
private final int HTTP_CODE_OK = 200; |
| 72 |
private final int HTTP_CODE_EXCEPTION = 300; |
| 73 |
|
| 74 |
private final String IBM_WEB_SERVICES_EXPLORER = "IBM Web Services Explorer"; |
| 75 |
private final String TEXT_XML = "text/xml"; |
| 76 |
private final String CONTENT_TYPE_VALUE = "text/xml; charset=utf-8"; |
| 77 |
private final String ACCEPT_VALUE = "application/soap+xml, application/dime, multipart/related, text/*"; |
| 78 |
public static final String BASIC = "Basic"; |
| 79 |
private final String NO_CACHE = "no-cache"; |
| 80 |
private final String CHUNKED = "chunked"; |
| 81 |
private final String CLOSE = "close"; |
| 82 |
private final String HTTPS = "https"; |
| 83 |
private final char QUOTE = '\"'; |
| 84 |
public static final char COLON = ':'; |
| 85 |
private final char SPACE = ' '; |
| 86 |
private final char TAB = '\t'; |
| 87 |
private final char R = '\r'; |
| 88 |
private final char N = '\n'; |
| 89 |
private final char ROOT = '/'; |
| 90 |
private final String NEW_LINE = "\r\n"; |
| 91 |
private final String EMPTY_STRING = ""; |
| 92 |
|
| 93 |
private final int DEFAULT_HTTP_PORT = 80; |
| 94 |
private final int DEFAULT_HTTPS_PORT = 443; |
| 95 |
private final String DEFAULT_SOAP_ENCODING = "UTF-8"; |
| 96 |
private final String DEFAULT_HTTP_HEADER_ENCODING = "iso-8859-1"; |
| 97 |
private final boolean DEFAULT_CASE_SENSITIVE_FOR_HOST_NAME = false; |
| 98 |
|
| 99 |
private String httpBasicAuthUsername; |
| 100 |
private String httpBasicAuthPassword; |
| 101 |
private boolean maintainSession = false; |
| 102 |
private String cookie; |
| 103 |
private String cookie2; |
| 104 |
private HTTPResponse httpResponse; |
| 105 |
|
| 106 |
public String getHttpBasicAuthUsername() |
| 107 |
{ |
| 108 |
return httpBasicAuthUsername; |
| 109 |
} |
| 110 |
|
| 111 |
public void setHttpBasicAuthUsername(String httpBasicAuthUsername) |
| 112 |
{ |
| 113 |
this.httpBasicAuthUsername = httpBasicAuthUsername; |
| 114 |
} |
| 115 |
|
| 116 |
public String getHttpBasicAuthPassword() |
| 117 |
{ |
| 118 |
return httpBasicAuthPassword; |
| 119 |
} |
| 120 |
|
| 121 |
public void setHttpBasicAuthPassword(String httpBasicAuthPassword) |
| 122 |
{ |
| 123 |
this.httpBasicAuthPassword = httpBasicAuthPassword; |
| 124 |
} |
| 125 |
|
| 126 |
private String getMethod(URL url) |
| 127 |
{ |
| 128 |
StringBuffer sb = new StringBuffer(HTTP_METHOD); |
| 129 |
sb.append(SPACE); |
| 130 |
String protocol = url.getProtocol(); |
| 131 |
String httpProxyHost = System.getProperty(SYS_PROP_HTTP_PROXY_HOST); |
| 132 |
String httpsProxyHost = System.getProperty(SYS_PROP_HTTPS_PROXY_HOST); |
| 133 |
if (protocol.equalsIgnoreCase("http") && httpProxyHost != null && httpProxyHost.length() > 0) |
| 134 |
{ |
| 135 |
sb.append(url.toString()); |
| 136 |
} |
| 137 |
else if (protocol.equalsIgnoreCase("https") && httpsProxyHost != null && httpsProxyHost.length() > 0) |
| 138 |
{ |
| 139 |
sb.append(url.toString()); |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
String file = url.getFile(); |
| 144 |
if (file != null && file.length() > 0) |
| 145 |
sb.append(file); |
| 146 |
else |
| 147 |
sb.append(ROOT); |
| 148 |
} |
| 149 |
sb.append(SPACE); |
| 150 |
sb.append(HTTP_VERSION); |
| 151 |
sb.append(NEW_LINE); |
| 152 |
return sb.toString(); |
| 153 |
} |
| 154 |
|
| 155 |
private String getHost(URL url) |
| 156 |
{ |
| 157 |
StringBuffer sb = new StringBuffer(HTTP_HEADER_HOST); |
| 158 |
sb.append(COLON); |
| 159 |
sb.append(SPACE); |
| 160 |
sb.append(url.getHost()); |
| 161 |
sb.append(COLON); |
| 162 |
int port = url.getPort(); |
| 163 |
if (port > 0) |
| 164 |
sb.append(String.valueOf(port)); |
| 165 |
else if (url.getProtocol().equalsIgnoreCase(HTTPS)) |
| 166 |
sb.append(DEFAULT_HTTPS_PORT); |
| 167 |
else |
| 168 |
sb.append(DEFAULT_HTTP_PORT); |
| 169 |
sb.append(NEW_LINE); |
| 170 |
return sb.toString(); |
| 171 |
} |
| 172 |
|
| 173 |
private String getContentType() |
| 174 |
{ |
| 175 |
return getHTTPHeader(HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_VALUE); |
| 176 |
} |
| 177 |
|
| 178 |
private String getContentLength(byte[] payloadAsBytes) |
| 179 |
{ |
| 180 |
return getHTTPHeader(HTTP_HEADER_CONTENT_LENGTH, String.valueOf(payloadAsBytes.length)); |
| 181 |
} |
| 182 |
|
| 183 |
private String getSOAPAction(String soapAction) |
| 184 |
{ |
| 185 |
StringBuffer sb = new StringBuffer(HTTP_HEADER_SOAP_ACTION); |
| 186 |
sb.append(COLON); |
| 187 |
sb.append(SPACE); |
| 188 |
sb.append(QUOTE); |
| 189 |
if (soapAction != null) |
| 190 |
sb.append(soapAction); |
| 191 |
sb.append(QUOTE); |
| 192 |
sb.append(NEW_LINE); |
| 193 |
return sb.toString(); |
| 194 |
} |
| 195 |
|
| 196 |
private String getCookie() |
| 197 |
{ |
| 198 |
if (maintainSession) |
| 199 |
return getHTTPHeader(HTTP_HEADER_COOKIE, cookie); |
| 200 |
else |
| 201 |
return EMPTY_STRING; |
| 202 |
} |
| 203 |
|
| 204 |
private String getCookie2() |
| 205 |
{ |
| 206 |
if (maintainSession) |
| 207 |
return getHTTPHeader(HTTP_HEADER_COOKIE2, cookie2); |
| 208 |
else |
| 209 |
return EMPTY_STRING; |
| 210 |
} |
| 211 |
|
| 212 |
private String getWWWAuthentication() |
| 213 |
{ |
| 214 |
if (httpBasicAuthUsername != null && httpBasicAuthPassword != null) |
| 215 |
{ |
| 216 |
StringBuffer sb = new StringBuffer(httpBasicAuthUsername); |
| 217 |
sb.append(COLON); |
| 218 |
sb.append(httpBasicAuthPassword); |
| 219 |
BASE64Encoder encoder = new BASE64Encoder(); |
| 220 |
String encodedUserNamePassword = encoder.encode(sb.toString().getBytes()); |
| 221 |
sb.setLength(0); |
| 222 |
sb.append(HTTP_HEADER_AUTH); |
| 223 |
sb.append(COLON); |
| 224 |
sb.append(SPACE); |
| 225 |
sb.append(BASIC); |
| 226 |
sb.append(SPACE); |
| 227 |
sb.append(encodedUserNamePassword); |
| 228 |
sb.append(NEW_LINE); |
| 229 |
return sb.toString(); |
| 230 |
} |
| 231 |
else |
| 232 |
return EMPTY_STRING; |
| 233 |
} |
| 234 |
|
| 235 |
private String getProxyAuthentication() |
| 236 |
{ |
| 237 |
String proxyUserName = System.getProperty(SYS_PROP_HTTP_PROXY_USER_NAME); |
| 238 |
String proxyPassword = System.getProperty(SYS_PROP_HTTP_PROXY_PASSWORD); |
| 239 |
if (proxyUserName != null && proxyPassword != null) |
| 240 |
{ |
| 241 |
StringBuffer sb = new StringBuffer(proxyUserName); |
| 242 |
sb.append(COLON); |
| 243 |
sb.append(proxyPassword); |
| 244 |
BASE64Encoder encoder = new BASE64Encoder(); |
| 245 |
String encodedUserNamePassword = encoder.encode(sb.toString().getBytes()); |
| 246 |
sb.setLength(0); |
| 247 |
sb.append(HTTP_HEADER_PROXY_AUTH); |
| 248 |
sb.append(COLON); |
| 249 |
sb.append(SPACE); |
| 250 |
sb.append(BASIC); |
| 251 |
sb.append(SPACE); |
| 252 |
sb.append(encodedUserNamePassword); |
| 253 |
sb.append(NEW_LINE); |
| 254 |
return sb.toString(); |
| 255 |
} |
| 256 |
else |
| 257 |
return EMPTY_STRING; |
| 258 |
} |
| 259 |
|
| 260 |
private String getAccept() |
| 261 |
{ |
| 262 |
return getHTTPHeader(HTTP_HEADER_ACCEPT, ACCEPT_VALUE); |
| 263 |
} |
| 264 |
|
| 265 |
private String getUserAgent() |
| 266 |
{ |
| 267 |
return getHTTPHeader(HTTP_HEADER_USER_AGENT, IBM_WEB_SERVICES_EXPLORER); |
| 268 |
} |
| 269 |
|
| 270 |
private String getCacheControl() |
| 271 |
{ |
| 272 |
return getHTTPHeader(HTTP_HEADER_CACHE_CONTROL, NO_CACHE); |
| 273 |
} |
| 274 |
|
| 275 |
private String getPragma() |
| 276 |
{ |
| 277 |
return getHTTPHeader(HTTP_HEADER_PRAGMA, NO_CACHE); |
| 278 |
} |
| 279 |
|
| 280 |
private String getConnection() |
| 281 |
{ |
| 282 |
return getHTTPHeader(HTTP_HEADER_CONNECTION, CLOSE); |
| 283 |
} |
| 284 |
|
| 285 |
private String getHTTPHeader(String key, String value) |
| 286 |
{ |
| 287 |
if (value != null) |
| 288 |
{ |
| 289 |
StringBuffer sb = new StringBuffer(key); |
| 290 |
sb.append(COLON); |
| 291 |
sb.append(SPACE); |
| 292 |
sb.append(value); |
| 293 |
sb.append(NEW_LINE); |
| 294 |
return sb.toString(); |
| 295 |
} |
| 296 |
else |
| 297 |
return EMPTY_STRING; |
| 298 |
} |
| 299 |
|
| 300 |
public void send(URL url, String soapAction, String payload) throws UnknownHostException, IOException |
| 301 |
{ |
| 302 |
byte[] payloadAsUTF8 = payload.getBytes(DEFAULT_SOAP_ENCODING); |
| 303 |
|
| 304 |
StringBuffer httpHeader = new StringBuffer(); |
| 305 |
httpHeader.append(getMethod(url)); |
| 306 |
httpHeader.append(getHost(url)); |
| 307 |
httpHeader.append(getContentType()); |
| 308 |
httpHeader.append(getContentLength(payloadAsUTF8)); |
| 309 |
httpHeader.append(getAccept()); |
| 310 |
httpHeader.append(getUserAgent()); |
| 311 |
httpHeader.append(getCacheControl()); |
| 312 |
httpHeader.append(getPragma()); |
| 313 |
httpHeader.append(getSOAPAction(soapAction)); |
| 314 |
httpHeader.append(getWWWAuthentication()); |
| 315 |
httpHeader.append(getProxyAuthentication()); |
| 316 |
httpHeader.append(getCookie()); |
| 317 |
httpHeader.append(getCookie2()); |
| 318 |
httpHeader.append(getConnection()); |
| 319 |
httpHeader.append(NEW_LINE); // new line between the HTTP header and the payload |
| 320 |
Socket socket = buildSocket(url); |
| 321 |
InputStream is = socket.getInputStream(); |
| 322 |
OutputStream os = socket.getOutputStream(); |
| 323 |
os.write(httpHeader.toString().getBytes(DEFAULT_HTTP_HEADER_ENCODING)); |
| 324 |
os.write(payloadAsUTF8); |
| 325 |
os.flush(); |
| 326 |
httpResponse = new HTTPResponse(); |
| 327 |
readHTTPResponseHeader(is, httpResponse); |
| 328 |
int code = httpResponse.getStatusCode(); |
| 329 |
if (code == HTTP_CODE_CONTINUE) |
| 330 |
{ |
| 331 |
httpResponse.reset(); |
| 332 |
readHTTPResponseHeader(is, httpResponse); |
| 333 |
} |
| 334 |
readHTTPResponsePayload(is, httpResponse); |
| 335 |
os.close(); |
| 336 |
is.close(); |
| 337 |
socket.close(); |
| 338 |
code = httpResponse.getStatusCode(); |
| 339 |
String contentType = httpResponse.getHeader(HTTP_HEADER_CONTENT_TYPE.toLowerCase()); |
| 340 |
if (code >= HTTP_CODE_EXCEPTION && (contentType == null || contentType.toLowerCase().indexOf(TEXT_XML.toLowerCase()) == -1)) |
| 341 |
throw new HTTPTransportException(code, httpResponse.getStatusMessage(), httpResponse.getHeaders()); |
| 342 |
} |
| 343 |
|
| 344 |
private void readHTTPResponseHeader(InputStream is, HTTPResponse resp) throws IOException |
| 345 |
{ |
| 346 |
byte b = 0; |
| 347 |
int len = 0; |
| 348 |
int colonIndex = -1; |
| 349 |
String key; |
| 350 |
String value; |
| 351 |
boolean readTooMuch = false; |
| 352 |
ByteArrayOutputStream baos; |
| 353 |
for (baos = new ByteArrayOutputStream(4096);;) |
| 354 |
{ |
| 355 |
if (!readTooMuch) |
| 356 |
b = (byte)is.read(); |
| 357 |
if (b == -1) |
| 358 |
break; |
| 359 |
readTooMuch = false; |
| 360 |
if ((b != R) && (b != N)) |
| 361 |
{ |
| 362 |
if ((b == COLON) && (colonIndex == -1)) |
| 363 |
colonIndex = len; |
| 364 |
len++; |
| 365 |
baos.write(b); |
| 366 |
} |
| 367 |
else if (b == R) |
| 368 |
continue; |
| 369 |
else // b == N |
| 370 |
{ |
| 371 |
if (len == 0) |
| 372 |
break; |
| 373 |
b = (byte)is.read(); |
| 374 |
readTooMuch = true; |
| 375 |
// A space or tab at the begining of a line means the header continues. |
| 376 |
if ((b == SPACE) || (b == TAB)) |
| 377 |
continue; |
| 378 |
baos.close(); |
| 379 |
byte[] bArray = baos.toByteArray(); |
| 380 |
baos.reset(); |
| 381 |
if (colonIndex != -1) |
| 382 |
{ |
| 383 |
key = new String(bArray, 0, colonIndex, DEFAULT_HTTP_HEADER_ENCODING); |
| 384 |
value = new String(bArray, colonIndex + 1, len - 1 - colonIndex, DEFAULT_HTTP_HEADER_ENCODING); |
| 385 |
colonIndex = -1; |
| 386 |
} |
| 387 |
else |
| 388 |
{ |
| 389 |
key = new String(bArray, 0, len, DEFAULT_HTTP_HEADER_ENCODING); |
| 390 |
value = EMPTY_STRING; |
| 391 |
} |
| 392 |
if (!resp.isStatusSet()) |
| 393 |
{ |
| 394 |
// Reader status code |
| 395 |
int start = key.indexOf(SPACE) + 1; |
| 396 |
String s = key.substring(start).trim(); |
| 397 |
int end = s.indexOf(SPACE); |
| 398 |
if (end != -1) |
| 399 |
s = s.substring(0, end); |
| 400 |
try |
| 401 |
{ |
| 402 |
resp.setStatusCode(Integer.parseInt(s)); |
| 403 |
} |
| 404 |
catch (NumberFormatException nfe) |
| 405 |
{ |
| 406 |
resp.setStatusCode(-1); |
| 407 |
} |
| 408 |
resp.setStatusMessage(key.substring(start + end + 1)); |
| 409 |
} |
| 410 |
else |
| 411 |
resp.addHeader(key.toLowerCase().trim(), value.trim()); |
| 412 |
len = 0; |
| 413 |
} |
| 414 |
} |
| 415 |
baos.close(); |
| 416 |
} |
| 417 |
|
| 418 |
private void readHTTPResponsePayload(InputStream is, HTTPResponse resp) throws IOException |
| 419 |
{ |
| 420 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 421 |
try |
| 422 |
{ |
| 423 |
byte b = (byte)is.read(); |
| 424 |
while (b != -1) |
| 425 |
{ |
| 426 |
baos.write(b); |
| 427 |
b = (byte)is.read(); |
| 428 |
} |
| 429 |
} |
| 430 |
catch (SocketTimeoutException ste) |
| 431 |
{ |
| 432 |
} |
| 433 |
baos.close(); |
| 434 |
resp.setPayload(baos.toByteArray()); |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Receives the bytes from the last web service invocation. This is used by WSE's default |
| 439 |
* SOAP transport. |
| 440 |
* |
| 441 |
* @return A byte array. |
| 442 |
* @throws IOException |
| 443 |
*/ |
| 444 |
byte[] receiveBytes() throws IOException |
| 445 |
{ |
| 446 |
if (httpResponse != null) |
| 447 |
{ |
| 448 |
byte[] payload = httpResponse.getPayload(); |
| 449 |
if (CHUNKED.equalsIgnoreCase(httpResponse.getHeader(HTTP_HEADER_TRANSFER_ENCODEING.toLowerCase()))) |
| 450 |
{ |
| 451 |
ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 452 |
ChunkedInputStream cis = new ChunkedInputStream(bais); |
| 453 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 454 |
byte b; |
| 455 |
while ((b = (byte)cis.read()) != -1) |
| 456 |
baos.write(b); |
| 457 |
baos.close(); |
| 458 |
cis.close(); |
| 459 |
bais.close(); |
| 460 |
return baos.toByteArray(); |
| 461 |
} |
| 462 |
else |
| 463 |
{ |
| 464 |
return payload; |
| 465 |
} |
| 466 |
} |
| 467 |
return null; |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* This method is deprecated after WSE swtiches to using a pluggable transport via enhancement 176493. |
| 472 |
* The core functionality of this method is refactored to the receiveBytes() method. |
| 473 |
* |
| 474 |
* @deprecated |
| 475 |
*/ |
| 476 |
public BufferedReader receive() |
| 477 |
{ |
| 478 |
try |
| 479 |
{ |
| 480 |
byte[] payload = receiveBytes(); |
| 481 |
if (payload != null) { |
| 482 |
Element soapEnvelope = XMLUtils.byteArrayToElement(payload, false); |
| 483 |
// remove XML namespace declaration |
| 484 |
if (soapEnvelope != null) |
| 485 |
return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(XMLUtils.serialize(soapEnvelope, true).getBytes(DEFAULT_SOAP_ENCODING)), DEFAULT_SOAP_ENCODING)); |
| 486 |
} |
| 487 |
} |
| 488 |
catch (Throwable t) |
| 489 |
{ |
| 490 |
} |
| 491 |
return null; |
| 492 |
} |
| 493 |
|
| 494 |
public Hashtable getHeaders() |
| 495 |
{ |
| 496 |
Hashtable headers = new Hashtable(); |
| 497 |
if (httpResponse != null) |
| 498 |
headers.putAll(httpResponse.getHeaders()); |
| 499 |
return headers; |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Builds the first line of a connection request to the proxy server |
| 504 |
* |
| 505 |
* @param url The URL that we want to ultimately connect to. |
| 506 |
* @return A string of the form "CONNECT <hostname>:<port> HTTP/1.0". |
| 507 |
*/ |
| 508 |
private StringBuffer getConnectMethod(URL url) { |
| 509 |
|
| 510 |
StringBuffer sb = new StringBuffer(HTTP_CONNECT); |
| 511 |
sb.append(SPACE); |
| 512 |
sb.append(url.getHost()); |
| 513 |
sb.append(COLON); |
| 514 |
sb.append(url.getPort()); |
| 515 |
sb.append(SPACE); |
| 516 |
sb.append(HTTP_VERSION_1_0); |
| 517 |
sb.append(NEW_LINE); |
| 518 |
return sb; |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Construct a socket to the proxy server which be used to tunnel through. |
| 523 |
* |
| 524 |
* @param url The URL that we want to ultimately connect to. |
| 525 |
* @param proxyHost The proxy host. |
| 526 |
* @param proxyPort The proxy port. |
| 527 |
* @return A connected socket to the proxy server. |
| 528 |
* @throws IOException |
| 529 |
*/ |
| 530 |
private Socket buildTunnelSocket(URL url, String proxyHost, int proxyPort) throws IOException { |
| 531 |
|
| 532 |
StringBuffer httpHeader = new StringBuffer(); |
| 533 |
httpHeader.append(getConnectMethod(url)); |
| 534 |
httpHeader.append(getProxyAuthentication()); |
| 535 |
httpHeader.append(NEW_LINE); |
| 536 |
|
| 537 |
Socket tunnel = new Socket(proxyHost, proxyPort); |
| 538 |
|
| 539 |
InputStream is = tunnel.getInputStream(); |
| 540 |
OutputStream os = tunnel.getOutputStream(); |
| 541 |
|
| 542 |
os.write(httpHeader.toString().getBytes(DEFAULT_HTTP_HEADER_ENCODING)); |
| 543 |
os.flush(); |
| 544 |
|
| 545 |
HTTPResponse httpResponse = new HTTPResponse(); |
| 546 |
readHTTPResponseHeader(is, httpResponse); |
| 547 |
|
| 548 |
int code = httpResponse.getStatusCode(); |
| 549 |
|
| 550 |
// ensure we are successfully connected to the proxy |
| 551 |
if (code != HTTP_CODE_OK) |
| 552 |
throw new HTTPTransportException(code, httpResponse.getStatusMessage(), httpResponse.getHeaders()); |
| 553 |
|
| 554 |
return tunnel; |
| 555 |
} |
| 556 |
|
| 557 |
private Socket buildSocket(URL url) throws UnknownHostException, IOException |
| 558 |
{ |
| 559 |
Socket s = null; |
| 560 |
String host = url.getHost(); |
| 561 |
int port = url.getPort(); |
| 562 |
String proxyHost = System.getProperty(SYS_PROP_HTTP_PROXY_HOST); |
| 563 |
int proxyPort = Integer.getInteger(SYS_PROP_HTTP_PROXY_PORT, DEFAULT_HTTP_PORT).intValue(); |
| 564 |
|
| 565 |
String nonProxyHosts = System.getProperty(SYS_PROP_HTTP_NON_PROXY_HOSTS); |
| 566 |
|
| 567 |
// String proxyUserName = System.getProperty(SYS_PROP_HTTP_PROXY_USER_NAME); |
| 568 |
// String proxyPassword = System.getProperty(SYS_PROP_HTTP_PROXY_PASSWORD); |
| 569 |
if (url.getProtocol().equalsIgnoreCase(HTTPS)) |
| 570 |
{ |
| 571 |
proxyHost = System.getProperty(SYS_PROP_HTTPS_PROXY_HOST); |
| 572 |
proxyPort = Integer.getInteger(SYS_PROP_HTTPS_PROXY_PORT, DEFAULT_HTTPS_PORT).intValue(); |
| 573 |
nonProxyHosts = System.getProperty(SYS_PROP_HTTPS_NON_PROXY_HOSTS); |
| 574 |
|
| 575 |
if (proxyHost != null && proxyHost.length() > 0 && !isHostInNonProxyHosts(host, nonProxyHosts, DEFAULT_CASE_SENSITIVE_FOR_HOST_NAME)) |
| 576 |
{ |
| 577 |
// SSL with proxy server |
| 578 |
Socket tunnel = buildTunnelSocket(url, proxyHost, proxyPort); |
| 579 |
s = ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(tunnel, host, port, true); |
| 580 |
} |
| 581 |
else |
| 582 |
s = SSLSocketFactory.getDefault().createSocket(host, (port > 0 ? port : DEFAULT_HTTPS_PORT)); |
| 583 |
// Removing dependency on soap.jar |
| 584 |
// s = SSLUtils.buildSSLSocket(host, (port > 0 ? port : DEFAULT_HTTPS_PORT), proxyHost, proxyPort); |
| 585 |
// TODO: |
| 586 |
// Build an SSL socket that supports proxyUser and proxyPassword, |
| 587 |
// as demonstrated in the following (original) line of code: |
| 588 |
// s = SSLUtils.buildSSLSocket(host, (port > 0 ? port : DEFAULT_HTTPS_PORT), proxyHost, proxyPort, proxyUserName, proxyPassword); |
| 589 |
} |
| 590 |
else if (proxyHost != null && proxyHost.length() > 0 && !isHostInNonProxyHosts(host, nonProxyHosts, DEFAULT_CASE_SENSITIVE_FOR_HOST_NAME)) |
| 591 |
s = new Socket(proxyHost, proxyPort); |
| 592 |
else |
| 593 |
s = new Socket(host, (port > 0 ? port : DEFAULT_HTTP_PORT)); |
| 594 |
return s; |
| 595 |
} |
| 596 |
|
| 597 |
private boolean isHostInNonProxyHosts(String host, String nonProxyHosts, boolean caseSensitive) |
| 598 |
{ |
| 599 |
if (caseSensitive) return host.matches(createPatternFromString(nonProxyHosts)); |
| 600 |
else return host.toLowerCase().matches(createPatternFromString(nonProxyHosts.toLowerCase())); |
| 601 |
} |
| 602 |
|
| 603 |
/* |
| 604 |
* This method is used to generate a valid regular expression for a |
| 605 |
* normal java String used in the proxy mechanism. |
| 606 |
* For example, the http.nonProxyHosts contains following String: |
| 607 |
* "192.168.2.*|localhost|*.ibm.com" . It would be |
| 608 |
* transformed into: "192\.168\.2\.\w*|localhost|\w*\.ibm\.com" |
| 609 |
* Thus, following host string would match above pattern: |
| 610 |
* 192.168.2.5 192.168.2. 192.168.2.666 192.168.2.w |
| 611 |
* localhost |
| 612 |
* torolab.ibm.com .ibm.com 123.ibm.com |
| 613 |
* |
| 614 |
* Two transformations are done: |
| 615 |
* 1. replace all "." into "\." As in regular expression, '.' represents |
| 616 |
* any charater. "\.' represents the real character '.' |
| 617 |
* 2. In order to get the real meaning of "*" used in property |
| 618 |
* http.nonProxyHosts, "\w*" is used to replace "*" |
| 619 |
* "\w" represent a word character: [a-zA-Z_0-9] |
| 620 |
* |
| 621 |
* Restriction: |
| 622 |
* The validity of address is not checked. |
| 623 |
* (192.168.2.555 and 192.168.2.com are OK) |
| 624 |
* |
| 625 |
* TODO check whether * occurs in address or hostname. |
| 626 |
* if it occuus in address representation, replace "*" with "\d*" |
| 627 |
* and check: value < 256 ? |
| 628 |
*/ |
| 629 |
private String createPatternFromString(String str) |
| 630 |
{ |
| 631 |
/* This is the same as following more understandable way: |
| 632 |
* return str.replace(".", "\\.").replace("*", "\\w*"); |
| 633 |
* But, replace(CharSequence target, CharSequence replacement) can only be |
| 634 |
* supported after j2se 1.5, on the other hand, |
| 635 |
* replaceAll(String regex, String replacement) can be supported before |
| 636 |
* j2se 1.5. |
| 637 |
*/ |
| 638 |
return str == null ? null : str.replaceAll("\\.", "\\.").replaceAll("\\*", "\\w*"); |
| 639 |
} |
| 640 |
} |