Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.messaging.saaj.client.p2p; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.*; |
aoqi@0 | 29 | import java.lang.reflect.Method; |
aoqi@0 | 30 | import java.net.*; |
aoqi@0 | 31 | import java.security.*; |
aoqi@0 | 32 | import java.util.Iterator; |
aoqi@0 | 33 | import java.util.StringTokenizer; |
aoqi@0 | 34 | import java.util.logging.Level; |
aoqi@0 | 35 | import java.util.logging.Logger; |
aoqi@0 | 36 | |
aoqi@0 | 37 | import javax.xml.soap.*; |
aoqi@0 | 38 | |
aoqi@0 | 39 | import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; |
aoqi@0 | 40 | import com.sun.xml.internal.messaging.saaj.util.*; |
aoqi@0 | 41 | |
aoqi@0 | 42 | /** |
aoqi@0 | 43 | * This represents a "connection" to the simple HTTP-based provider. |
aoqi@0 | 44 | * |
aoqi@0 | 45 | * @author Anil Vijendran (akv@eng.sun.com) |
aoqi@0 | 46 | * @author Rajiv Mordani (rajiv.mordani@sun.com) |
aoqi@0 | 47 | * @author Manveen Kaur (manveen.kaur@sun.com) |
aoqi@0 | 48 | * |
aoqi@0 | 49 | */ |
aoqi@0 | 50 | class HttpSOAPConnection extends SOAPConnection { |
aoqi@0 | 51 | |
aoqi@0 | 52 | public static final String vmVendor = SAAJUtil.getSystemProperty("java.vendor.url"); |
aoqi@0 | 53 | private static final String sunVmVendor = "http://java.sun.com/"; |
aoqi@0 | 54 | private static final String ibmVmVendor = "http://www.ibm.com/"; |
aoqi@0 | 55 | private static final boolean isSunVM = sunVmVendor.equals(vmVendor) ? true: false; |
aoqi@0 | 56 | private static final boolean isIBMVM = ibmVmVendor.equals(vmVendor) ? true : false; |
aoqi@0 | 57 | private static final String JAXM_URLENDPOINT="javax.xml.messaging.URLEndpoint"; |
aoqi@0 | 58 | |
aoqi@0 | 59 | protected static final Logger log = |
aoqi@0 | 60 | Logger.getLogger(LogDomainConstants.HTTP_CONN_DOMAIN, |
aoqi@0 | 61 | "com.sun.xml.internal.messaging.saaj.client.p2p.LocalStrings"); |
aoqi@0 | 62 | |
aoqi@0 | 63 | |
aoqi@0 | 64 | MessageFactory messageFactory = null; |
aoqi@0 | 65 | |
aoqi@0 | 66 | boolean closed = false; |
aoqi@0 | 67 | |
aoqi@0 | 68 | public HttpSOAPConnection() throws SOAPException { |
aoqi@0 | 69 | |
aoqi@0 | 70 | try { |
aoqi@0 | 71 | messageFactory = MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL); |
aoqi@0 | 72 | } catch (NoSuchMethodError ex) { |
aoqi@0 | 73 | //fallback to default SOAP 1.1 in this case for backward compatibility |
aoqi@0 | 74 | messageFactory = MessageFactory.newInstance(); |
aoqi@0 | 75 | } catch (Exception ex) { |
aoqi@0 | 76 | log.log(Level.SEVERE, "SAAJ0001.p2p.cannot.create.msg.factory", ex); |
aoqi@0 | 77 | throw new SOAPExceptionImpl("Unable to create message factory", ex); |
aoqi@0 | 78 | } |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | public void close() throws SOAPException { |
aoqi@0 | 82 | if (closed) { |
aoqi@0 | 83 | log.severe("SAAJ0002.p2p.close.already.closed.conn"); |
aoqi@0 | 84 | throw new SOAPExceptionImpl("Connection already closed"); |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | messageFactory = null; |
aoqi@0 | 88 | closed = true; |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | public SOAPMessage call(SOAPMessage message, Object endPoint) |
aoqi@0 | 92 | throws SOAPException { |
aoqi@0 | 93 | if (closed) { |
aoqi@0 | 94 | log.severe("SAAJ0003.p2p.call.already.closed.conn"); |
aoqi@0 | 95 | throw new SOAPExceptionImpl("Connection is closed"); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | Class urlEndpointClass = null; |
aoqi@0 | 99 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
aoqi@0 | 100 | try { |
aoqi@0 | 101 | if (loader != null) { |
aoqi@0 | 102 | urlEndpointClass = loader.loadClass(JAXM_URLENDPOINT); |
aoqi@0 | 103 | } else { |
aoqi@0 | 104 | urlEndpointClass = Class.forName(JAXM_URLENDPOINT); |
aoqi@0 | 105 | } |
aoqi@0 | 106 | } catch (ClassNotFoundException ex) { |
aoqi@0 | 107 | //Do nothing. URLEndpoint is available only when JAXM is there. |
aoqi@0 | 108 | if (log.isLoggable(Level.FINEST)) |
aoqi@0 | 109 | log.finest("SAAJ0090.p2p.endpoint.available.only.for.JAXM"); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | if (urlEndpointClass != null) { |
aoqi@0 | 113 | if (urlEndpointClass.isInstance(endPoint)) { |
aoqi@0 | 114 | String url = null; |
aoqi@0 | 115 | |
aoqi@0 | 116 | try { |
aoqi@0 | 117 | Method m = urlEndpointClass.getMethod("getURL", (Class[])null); |
aoqi@0 | 118 | url = (String) m.invoke(endPoint, (Object[])null); |
aoqi@0 | 119 | } catch (Exception ex) { |
aoqi@0 | 120 | // TBD -- exception chaining |
aoqi@0 | 121 | log.log(Level.SEVERE,"SAAJ0004.p2p.internal.err",ex); |
aoqi@0 | 122 | throw new SOAPExceptionImpl( |
aoqi@0 | 123 | "Internal error: " + ex.getMessage()); |
aoqi@0 | 124 | } |
aoqi@0 | 125 | try { |
aoqi@0 | 126 | endPoint = new URL(url); |
aoqi@0 | 127 | } catch (MalformedURLException mex) { |
aoqi@0 | 128 | log.log(Level.SEVERE,"SAAJ0005.p2p.", mex); |
aoqi@0 | 129 | throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage()); |
aoqi@0 | 130 | } |
aoqi@0 | 131 | } |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | if (endPoint instanceof java.lang.String) { |
aoqi@0 | 135 | try { |
aoqi@0 | 136 | endPoint = new URL((String) endPoint); |
aoqi@0 | 137 | } catch (MalformedURLException mex) { |
aoqi@0 | 138 | log.log(Level.SEVERE, "SAAJ0006.p2p.bad.URL", mex); |
aoqi@0 | 139 | throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage()); |
aoqi@0 | 140 | } |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | if (endPoint instanceof URL) |
aoqi@0 | 144 | try { |
aoqi@0 | 145 | SOAPMessage response = post(message, (URL)endPoint); |
aoqi@0 | 146 | return response; |
aoqi@0 | 147 | } catch (Exception ex) { |
aoqi@0 | 148 | // TBD -- chaining? |
aoqi@0 | 149 | throw new SOAPExceptionImpl(ex); |
aoqi@0 | 150 | } else { |
aoqi@0 | 151 | log.severe("SAAJ0007.p2p.bad.endPoint.type"); |
aoqi@0 | 152 | throw new SOAPExceptionImpl("Bad endPoint type " + endPoint); |
aoqi@0 | 153 | } |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | SOAPMessage post(SOAPMessage message, URL endPoint) throws SOAPException, IOException { |
aoqi@0 | 157 | boolean isFailure = false; |
aoqi@0 | 158 | |
aoqi@0 | 159 | URL url = null; |
aoqi@0 | 160 | HttpURLConnection httpConnection = null; |
aoqi@0 | 161 | |
aoqi@0 | 162 | int responseCode = 0; |
aoqi@0 | 163 | try { |
aoqi@0 | 164 | if (endPoint.getProtocol().equals("https")) |
aoqi@0 | 165 | //if(!setHttps) |
aoqi@0 | 166 | initHttps(); |
aoqi@0 | 167 | // Process the URL |
aoqi@0 | 168 | URI uri = new URI(endPoint.toString()); |
aoqi@0 | 169 | String userInfo = uri.getRawUserInfo(); |
aoqi@0 | 170 | |
aoqi@0 | 171 | url = endPoint; |
aoqi@0 | 172 | |
aoqi@0 | 173 | if (dL > 0) |
aoqi@0 | 174 | d("uri: " + userInfo + " " + url + " " + uri); |
aoqi@0 | 175 | |
aoqi@0 | 176 | // TBD |
aoqi@0 | 177 | // Will deal with https later. |
aoqi@0 | 178 | if (!url.getProtocol().equalsIgnoreCase("http") |
aoqi@0 | 179 | && !url.getProtocol().equalsIgnoreCase("https")) { |
aoqi@0 | 180 | log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https"); |
aoqi@0 | 181 | throw new IllegalArgumentException( |
aoqi@0 | 182 | "Protocol " |
aoqi@0 | 183 | + url.getProtocol() |
aoqi@0 | 184 | + " not supported in URL " |
aoqi@0 | 185 | + url); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | httpConnection = (HttpURLConnection) createConnection(url); |
aoqi@0 | 188 | |
aoqi@0 | 189 | httpConnection.setRequestMethod("POST"); |
aoqi@0 | 190 | |
aoqi@0 | 191 | httpConnection.setDoOutput(true); |
aoqi@0 | 192 | httpConnection.setDoInput(true); |
aoqi@0 | 193 | httpConnection.setUseCaches(false); |
aoqi@0 | 194 | httpConnection.setInstanceFollowRedirects(true); |
aoqi@0 | 195 | |
aoqi@0 | 196 | if (message.saveRequired()) |
aoqi@0 | 197 | message.saveChanges(); |
aoqi@0 | 198 | |
aoqi@0 | 199 | MimeHeaders headers = message.getMimeHeaders(); |
aoqi@0 | 200 | |
aoqi@0 | 201 | Iterator it = headers.getAllHeaders(); |
aoqi@0 | 202 | boolean hasAuth = false; // true if we find explicit Auth header |
aoqi@0 | 203 | while (it.hasNext()) { |
aoqi@0 | 204 | MimeHeader header = (MimeHeader) it.next(); |
aoqi@0 | 205 | |
aoqi@0 | 206 | String[] values = headers.getHeader(header.getName()); |
aoqi@0 | 207 | if (values.length == 1) |
aoqi@0 | 208 | httpConnection.setRequestProperty( |
aoqi@0 | 209 | header.getName(), |
aoqi@0 | 210 | header.getValue()); |
aoqi@0 | 211 | else { |
aoqi@0 | 212 | StringBuffer concat = new StringBuffer(); |
aoqi@0 | 213 | int i = 0; |
aoqi@0 | 214 | while (i < values.length) { |
aoqi@0 | 215 | if (i != 0) |
aoqi@0 | 216 | concat.append(','); |
aoqi@0 | 217 | concat.append(values[i]); |
aoqi@0 | 218 | i++; |
aoqi@0 | 219 | } |
aoqi@0 | 220 | |
aoqi@0 | 221 | httpConnection.setRequestProperty( |
aoqi@0 | 222 | header.getName(), |
aoqi@0 | 223 | concat.toString()); |
aoqi@0 | 224 | } |
aoqi@0 | 225 | |
aoqi@0 | 226 | if ("Authorization".equals(header.getName())) { |
aoqi@0 | 227 | hasAuth = true; |
aoqi@0 | 228 | if (log.isLoggable(Level.FINE)) |
aoqi@0 | 229 | log.fine("SAAJ0091.p2p.https.auth.in.POST.true"); |
aoqi@0 | 230 | } |
aoqi@0 | 231 | } |
aoqi@0 | 232 | |
aoqi@0 | 233 | if (!hasAuth && userInfo != null) { |
aoqi@0 | 234 | initAuthUserInfo(httpConnection, userInfo); |
aoqi@0 | 235 | } |
aoqi@0 | 236 | |
aoqi@0 | 237 | OutputStream out = httpConnection.getOutputStream(); |
aoqi@0 | 238 | try { |
aoqi@0 | 239 | message.writeTo(out); |
aoqi@0 | 240 | out.flush(); |
aoqi@0 | 241 | } finally { |
aoqi@0 | 242 | out.close(); |
aoqi@0 | 243 | } |
aoqi@0 | 244 | |
aoqi@0 | 245 | httpConnection.connect(); |
aoqi@0 | 246 | |
aoqi@0 | 247 | try { |
aoqi@0 | 248 | |
aoqi@0 | 249 | responseCode = httpConnection.getResponseCode(); |
aoqi@0 | 250 | |
aoqi@0 | 251 | // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults |
aoqi@0 | 252 | if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { |
aoqi@0 | 253 | isFailure = true; |
aoqi@0 | 254 | } |
aoqi@0 | 255 | //else if (responseCode != HttpURLConnection.HTTP_OK) |
aoqi@0 | 256 | //else if (!(responseCode >= HttpURLConnection.HTTP_OK && responseCode < 207)) |
aoqi@0 | 257 | else if ((responseCode / 100) != 2) { |
aoqi@0 | 258 | log.log(Level.SEVERE, |
aoqi@0 | 259 | "SAAJ0008.p2p.bad.response", |
aoqi@0 | 260 | new String[] {httpConnection.getResponseMessage()}); |
aoqi@0 | 261 | throw new SOAPExceptionImpl( |
aoqi@0 | 262 | "Bad response: (" |
aoqi@0 | 263 | + responseCode |
aoqi@0 | 264 | + httpConnection.getResponseMessage()); |
aoqi@0 | 265 | |
aoqi@0 | 266 | } |
aoqi@0 | 267 | } catch (IOException e) { |
aoqi@0 | 268 | // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds! |
aoqi@0 | 269 | responseCode = httpConnection.getResponseCode(); |
aoqi@0 | 270 | if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { |
aoqi@0 | 271 | isFailure = true; |
aoqi@0 | 272 | } else { |
aoqi@0 | 273 | throw e; |
aoqi@0 | 274 | } |
aoqi@0 | 275 | |
aoqi@0 | 276 | } |
aoqi@0 | 277 | |
aoqi@0 | 278 | } catch (SOAPException ex) { |
aoqi@0 | 279 | throw ex; |
aoqi@0 | 280 | } catch (Exception ex) { |
aoqi@0 | 281 | log.severe("SAAJ0009.p2p.msg.send.failed"); |
aoqi@0 | 282 | throw new SOAPExceptionImpl("Message send failed", ex); |
aoqi@0 | 283 | } |
aoqi@0 | 284 | |
aoqi@0 | 285 | SOAPMessage response = null; |
aoqi@0 | 286 | InputStream httpIn = null; |
aoqi@0 | 287 | if (responseCode == HttpURLConnection.HTTP_OK || isFailure) { |
aoqi@0 | 288 | try { |
aoqi@0 | 289 | MimeHeaders headers = new MimeHeaders(); |
aoqi@0 | 290 | |
aoqi@0 | 291 | String key, value; |
aoqi@0 | 292 | |
aoqi@0 | 293 | // Header field 0 is the status line so we skip it. |
aoqi@0 | 294 | |
aoqi@0 | 295 | int i = 1; |
aoqi@0 | 296 | |
aoqi@0 | 297 | while (true) { |
aoqi@0 | 298 | key = httpConnection.getHeaderFieldKey(i); |
aoqi@0 | 299 | value = httpConnection.getHeaderField(i); |
aoqi@0 | 300 | |
aoqi@0 | 301 | if (key == null && value == null) |
aoqi@0 | 302 | break; |
aoqi@0 | 303 | |
aoqi@0 | 304 | if (key != null) { |
aoqi@0 | 305 | StringTokenizer values = |
aoqi@0 | 306 | new StringTokenizer(value, ","); |
aoqi@0 | 307 | while (values.hasMoreTokens()) |
aoqi@0 | 308 | headers.addHeader(key, values.nextToken().trim()); |
aoqi@0 | 309 | } |
aoqi@0 | 310 | i++; |
aoqi@0 | 311 | } |
aoqi@0 | 312 | |
aoqi@0 | 313 | httpIn = |
aoqi@0 | 314 | (isFailure |
aoqi@0 | 315 | ? httpConnection.getErrorStream() |
aoqi@0 | 316 | : httpConnection.getInputStream()); |
aoqi@0 | 317 | |
aoqi@0 | 318 | byte[] bytes = readFully(httpIn); |
aoqi@0 | 319 | |
aoqi@0 | 320 | int length = |
aoqi@0 | 321 | httpConnection.getContentLength() == -1 |
aoqi@0 | 322 | ? bytes.length |
aoqi@0 | 323 | : httpConnection.getContentLength(); |
aoqi@0 | 324 | |
aoqi@0 | 325 | // If no reply message is returned, |
aoqi@0 | 326 | // content-Length header field value is expected to be zero. |
aoqi@0 | 327 | if (length == 0) { |
aoqi@0 | 328 | response = null; |
aoqi@0 | 329 | log.warning("SAAJ0014.p2p.content.zero"); |
aoqi@0 | 330 | } else { |
aoqi@0 | 331 | ByteInputStream in = new ByteInputStream(bytes, length); |
aoqi@0 | 332 | response = messageFactory.createMessage(headers, in); |
aoqi@0 | 333 | } |
aoqi@0 | 334 | |
aoqi@0 | 335 | } catch (SOAPException ex) { |
aoqi@0 | 336 | throw ex; |
aoqi@0 | 337 | } catch (Exception ex) { |
aoqi@0 | 338 | log.log(Level.SEVERE,"SAAJ0010.p2p.cannot.read.resp", ex); |
aoqi@0 | 339 | throw new SOAPExceptionImpl( |
aoqi@0 | 340 | "Unable to read response: " + ex.getMessage()); |
aoqi@0 | 341 | } finally { |
aoqi@0 | 342 | if (httpIn != null) |
aoqi@0 | 343 | httpIn.close(); |
aoqi@0 | 344 | httpConnection.disconnect(); |
aoqi@0 | 345 | } |
aoqi@0 | 346 | } |
aoqi@0 | 347 | return response; |
aoqi@0 | 348 | } |
aoqi@0 | 349 | |
aoqi@0 | 350 | // Object identifies where the request should be sent. |
aoqi@0 | 351 | // It is required to support objects of type String and java.net.URL. |
aoqi@0 | 352 | |
aoqi@0 | 353 | public SOAPMessage get(Object endPoint) throws SOAPException { |
aoqi@0 | 354 | if (closed) { |
aoqi@0 | 355 | log.severe("SAAJ0011.p2p.get.already.closed.conn"); |
aoqi@0 | 356 | throw new SOAPExceptionImpl("Connection is closed"); |
aoqi@0 | 357 | } |
aoqi@0 | 358 | Class urlEndpointClass = null; |
aoqi@0 | 359 | |
aoqi@0 | 360 | try { |
aoqi@0 | 361 | urlEndpointClass = Class.forName("javax.xml.messaging.URLEndpoint"); |
aoqi@0 | 362 | } catch (Exception ex) { |
aoqi@0 | 363 | //Do nothing. URLEndpoint is available only when JAXM is there. |
aoqi@0 | 364 | } |
aoqi@0 | 365 | |
aoqi@0 | 366 | if (urlEndpointClass != null) { |
aoqi@0 | 367 | if (urlEndpointClass.isInstance(endPoint)) { |
aoqi@0 | 368 | String url = null; |
aoqi@0 | 369 | |
aoqi@0 | 370 | try { |
aoqi@0 | 371 | Method m = urlEndpointClass.getMethod("getURL", (Class[])null); |
aoqi@0 | 372 | url = (String) m.invoke(endPoint, (Object[])null); |
aoqi@0 | 373 | } catch (Exception ex) { |
aoqi@0 | 374 | log.severe("SAAJ0004.p2p.internal.err"); |
aoqi@0 | 375 | throw new SOAPExceptionImpl( |
aoqi@0 | 376 | "Internal error: " + ex.getMessage()); |
aoqi@0 | 377 | } |
aoqi@0 | 378 | try { |
aoqi@0 | 379 | endPoint = new URL(url); |
aoqi@0 | 380 | } catch (MalformedURLException mex) { |
aoqi@0 | 381 | log.severe("SAAJ0005.p2p."); |
aoqi@0 | 382 | throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage()); |
aoqi@0 | 383 | } |
aoqi@0 | 384 | } |
aoqi@0 | 385 | } |
aoqi@0 | 386 | |
aoqi@0 | 387 | if (endPoint instanceof java.lang.String) { |
aoqi@0 | 388 | try { |
aoqi@0 | 389 | endPoint = new URL((String) endPoint); |
aoqi@0 | 390 | } catch (MalformedURLException mex) { |
aoqi@0 | 391 | log.severe("SAAJ0006.p2p.bad.URL"); |
aoqi@0 | 392 | throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage()); |
aoqi@0 | 393 | } |
aoqi@0 | 394 | } |
aoqi@0 | 395 | |
aoqi@0 | 396 | if (endPoint instanceof URL) |
aoqi@0 | 397 | try { |
aoqi@0 | 398 | SOAPMessage response = doGet((URL)endPoint); |
aoqi@0 | 399 | return response; |
aoqi@0 | 400 | } catch (Exception ex) { |
aoqi@0 | 401 | throw new SOAPExceptionImpl(ex); |
aoqi@0 | 402 | } else |
aoqi@0 | 403 | throw new SOAPExceptionImpl("Bad endPoint type " + endPoint); |
aoqi@0 | 404 | } |
aoqi@0 | 405 | |
aoqi@0 | 406 | SOAPMessage doGet(URL endPoint) throws SOAPException, IOException { |
aoqi@0 | 407 | boolean isFailure = false; |
aoqi@0 | 408 | |
aoqi@0 | 409 | URL url = null; |
aoqi@0 | 410 | HttpURLConnection httpConnection = null; |
aoqi@0 | 411 | |
aoqi@0 | 412 | int responseCode = 0; |
aoqi@0 | 413 | try { |
aoqi@0 | 414 | /// Is https GET allowed?? |
aoqi@0 | 415 | if (endPoint.getProtocol().equals("https")) |
aoqi@0 | 416 | initHttps(); |
aoqi@0 | 417 | // Process the URL |
aoqi@0 | 418 | URI uri = new URI(endPoint.toString()); |
aoqi@0 | 419 | String userInfo = uri.getRawUserInfo(); |
aoqi@0 | 420 | |
aoqi@0 | 421 | url = endPoint; |
aoqi@0 | 422 | |
aoqi@0 | 423 | if (dL > 0) |
aoqi@0 | 424 | d("uri: " + userInfo + " " + url + " " + uri); |
aoqi@0 | 425 | |
aoqi@0 | 426 | // TBD |
aoqi@0 | 427 | // Will deal with https later. |
aoqi@0 | 428 | if (!url.getProtocol().equalsIgnoreCase("http") |
aoqi@0 | 429 | && !url.getProtocol().equalsIgnoreCase("https")) { |
aoqi@0 | 430 | log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https"); |
aoqi@0 | 431 | throw new IllegalArgumentException( |
aoqi@0 | 432 | "Protocol " |
aoqi@0 | 433 | + url.getProtocol() |
aoqi@0 | 434 | + " not supported in URL " |
aoqi@0 | 435 | + url); |
aoqi@0 | 436 | } |
aoqi@0 | 437 | httpConnection = (HttpURLConnection) createConnection(url); |
aoqi@0 | 438 | |
aoqi@0 | 439 | httpConnection.setRequestMethod("GET"); |
aoqi@0 | 440 | |
aoqi@0 | 441 | httpConnection.setDoOutput(true); |
aoqi@0 | 442 | httpConnection.setDoInput(true); |
aoqi@0 | 443 | httpConnection.setUseCaches(false); |
aoqi@0 | 444 | httpConnection.setFollowRedirects(true); |
aoqi@0 | 445 | |
aoqi@0 | 446 | httpConnection.connect(); |
aoqi@0 | 447 | |
aoqi@0 | 448 | try { |
aoqi@0 | 449 | |
aoqi@0 | 450 | responseCode = httpConnection.getResponseCode(); |
aoqi@0 | 451 | |
aoqi@0 | 452 | // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults |
aoqi@0 | 453 | if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { |
aoqi@0 | 454 | isFailure = true; |
aoqi@0 | 455 | } else if ((responseCode / 100) != 2) { |
aoqi@0 | 456 | log.log(Level.SEVERE, |
aoqi@0 | 457 | "SAAJ0008.p2p.bad.response", |
aoqi@0 | 458 | new String[] { httpConnection.getResponseMessage()}); |
aoqi@0 | 459 | throw new SOAPExceptionImpl( |
aoqi@0 | 460 | "Bad response: (" |
aoqi@0 | 461 | + responseCode |
aoqi@0 | 462 | + httpConnection.getResponseMessage()); |
aoqi@0 | 463 | |
aoqi@0 | 464 | } |
aoqi@0 | 465 | } catch (IOException e) { |
aoqi@0 | 466 | // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds! |
aoqi@0 | 467 | responseCode = httpConnection.getResponseCode(); |
aoqi@0 | 468 | if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { |
aoqi@0 | 469 | isFailure = true; |
aoqi@0 | 470 | } else { |
aoqi@0 | 471 | throw e; |
aoqi@0 | 472 | } |
aoqi@0 | 473 | |
aoqi@0 | 474 | } |
aoqi@0 | 475 | |
aoqi@0 | 476 | } catch (SOAPException ex) { |
aoqi@0 | 477 | throw ex; |
aoqi@0 | 478 | } catch (Exception ex) { |
aoqi@0 | 479 | log.severe("SAAJ0012.p2p.get.failed"); |
aoqi@0 | 480 | throw new SOAPExceptionImpl("Get failed", ex); |
aoqi@0 | 481 | } |
aoqi@0 | 482 | |
aoqi@0 | 483 | SOAPMessage response = null; |
aoqi@0 | 484 | InputStream httpIn = null; |
aoqi@0 | 485 | if (responseCode == HttpURLConnection.HTTP_OK || isFailure) { |
aoqi@0 | 486 | try { |
aoqi@0 | 487 | MimeHeaders headers = new MimeHeaders(); |
aoqi@0 | 488 | |
aoqi@0 | 489 | String key, value; |
aoqi@0 | 490 | |
aoqi@0 | 491 | // Header field 0 is the status line so we skip it. |
aoqi@0 | 492 | |
aoqi@0 | 493 | int i = 1; |
aoqi@0 | 494 | |
aoqi@0 | 495 | while (true) { |
aoqi@0 | 496 | key = httpConnection.getHeaderFieldKey(i); |
aoqi@0 | 497 | value = httpConnection.getHeaderField(i); |
aoqi@0 | 498 | |
aoqi@0 | 499 | if (key == null && value == null) |
aoqi@0 | 500 | break; |
aoqi@0 | 501 | |
aoqi@0 | 502 | if (key != null) { |
aoqi@0 | 503 | StringTokenizer values = |
aoqi@0 | 504 | new StringTokenizer(value, ","); |
aoqi@0 | 505 | while (values.hasMoreTokens()) |
aoqi@0 | 506 | headers.addHeader(key, values.nextToken().trim()); |
aoqi@0 | 507 | } |
aoqi@0 | 508 | i++; |
aoqi@0 | 509 | } |
aoqi@0 | 510 | |
aoqi@0 | 511 | httpIn = |
aoqi@0 | 512 | (isFailure |
aoqi@0 | 513 | ? httpConnection.getErrorStream() |
aoqi@0 | 514 | : httpConnection.getInputStream()); |
aoqi@0 | 515 | // If no reply message is returned, |
aoqi@0 | 516 | // content-Length header field value is expected to be zero. |
aoqi@0 | 517 | // java SE 6 documentation says : |
aoqi@0 | 518 | // available() : an estimate of the number of bytes that can be read |
aoqi@0 | 519 | //(or skipped over) from this input stream without blocking |
aoqi@0 | 520 | //or 0 when it reaches the end of the input stream. |
aoqi@0 | 521 | if ((httpIn == null ) |
aoqi@0 | 522 | || (httpConnection.getContentLength() == 0) |
aoqi@0 | 523 | || (httpIn.available() == 0)) { |
aoqi@0 | 524 | response = null; |
aoqi@0 | 525 | log.warning("SAAJ0014.p2p.content.zero"); |
aoqi@0 | 526 | } else { |
aoqi@0 | 527 | response = messageFactory.createMessage(headers, httpIn); |
aoqi@0 | 528 | } |
aoqi@0 | 529 | |
aoqi@0 | 530 | } catch (SOAPException ex) { |
aoqi@0 | 531 | throw ex; |
aoqi@0 | 532 | } catch (Exception ex) { |
aoqi@0 | 533 | log.log(Level.SEVERE, |
aoqi@0 | 534 | "SAAJ0010.p2p.cannot.read.resp", |
aoqi@0 | 535 | ex); |
aoqi@0 | 536 | throw new SOAPExceptionImpl( |
aoqi@0 | 537 | "Unable to read response: " + ex.getMessage()); |
aoqi@0 | 538 | } finally { |
aoqi@0 | 539 | if (httpIn != null) |
aoqi@0 | 540 | httpIn.close(); |
aoqi@0 | 541 | httpConnection.disconnect(); |
aoqi@0 | 542 | } |
aoqi@0 | 543 | } |
aoqi@0 | 544 | return response; |
aoqi@0 | 545 | } |
aoqi@0 | 546 | |
aoqi@0 | 547 | private byte[] readFully(InputStream istream) throws IOException { |
aoqi@0 | 548 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
aoqi@0 | 549 | byte[] buf = new byte[1024]; |
aoqi@0 | 550 | int num = 0; |
aoqi@0 | 551 | |
aoqi@0 | 552 | while ((num = istream.read(buf)) != -1) { |
aoqi@0 | 553 | bout.write(buf, 0, num); |
aoqi@0 | 554 | } |
aoqi@0 | 555 | |
aoqi@0 | 556 | byte[] ret = bout.toByteArray(); |
aoqi@0 | 557 | |
aoqi@0 | 558 | return ret; |
aoqi@0 | 559 | } |
aoqi@0 | 560 | |
aoqi@0 | 561 | //private static String SSL_PKG = "com.sun.net.ssl.internal.www.protocol"; |
aoqi@0 | 562 | //private static String SSL_PROVIDER = |
aoqi@0 | 563 | // "com.sun.net.ssl.internal.ssl.Provider"; |
aoqi@0 | 564 | private static final String SSL_PKG; |
aoqi@0 | 565 | private static final String SSL_PROVIDER; |
aoqi@0 | 566 | |
aoqi@0 | 567 | static { |
aoqi@0 | 568 | if (isIBMVM) { |
aoqi@0 | 569 | SSL_PKG ="com.ibm.net.ssl.internal.www.protocol"; |
aoqi@0 | 570 | SSL_PROVIDER ="com.ibm.net.ssl.internal.ssl.Provider"; |
aoqi@0 | 571 | } else { |
aoqi@0 | 572 | //if not IBM VM default to Sun. |
aoqi@0 | 573 | SSL_PKG = "com.sun.net.ssl.internal.www.protocol"; |
aoqi@0 | 574 | SSL_PROVIDER ="com.sun.net.ssl.internal.ssl.Provider"; |
aoqi@0 | 575 | } |
aoqi@0 | 576 | } |
aoqi@0 | 577 | |
aoqi@0 | 578 | private void initHttps() { |
aoqi@0 | 579 | //if(!setHttps) { |
aoqi@0 | 580 | String pkgs = SAAJUtil.getSystemProperty("java.protocol.handler.pkgs"); |
aoqi@0 | 581 | if (log.isLoggable(Level.FINE)) |
aoqi@0 | 582 | log.log(Level.FINE, "SAAJ0053.p2p.providers", new String[] { pkgs }); |
aoqi@0 | 583 | |
aoqi@0 | 584 | if (pkgs == null || pkgs.indexOf(SSL_PKG) < 0) { |
aoqi@0 | 585 | if (pkgs == null) |
aoqi@0 | 586 | pkgs = SSL_PKG; |
aoqi@0 | 587 | else |
aoqi@0 | 588 | pkgs = pkgs + "|" + SSL_PKG; |
aoqi@0 | 589 | System.setProperty("java.protocol.handler.pkgs", pkgs); |
aoqi@0 | 590 | if (log.isLoggable(Level.FINE)) |
aoqi@0 | 591 | log.log(Level.FINE, "SAAJ0054.p2p.set.providers", |
aoqi@0 | 592 | new String[] { pkgs }); |
aoqi@0 | 593 | try { |
aoqi@0 | 594 | Class c = Class.forName(SSL_PROVIDER); |
aoqi@0 | 595 | Provider p = (Provider) c.newInstance(); |
aoqi@0 | 596 | Security.addProvider(p); |
aoqi@0 | 597 | if (log.isLoggable(Level.FINE)) |
aoqi@0 | 598 | log.log(Level.FINE, "SAAJ0055.p2p.added.ssl.provider", |
aoqi@0 | 599 | new String[] { SSL_PROVIDER }); |
aoqi@0 | 600 | //System.out.println("Added SSL_PROVIDER " + SSL_PROVIDER); |
aoqi@0 | 601 | //setHttps = true; |
aoqi@0 | 602 | } catch (Exception ex) { |
aoqi@0 | 603 | } |
aoqi@0 | 604 | } |
aoqi@0 | 605 | //} |
aoqi@0 | 606 | } |
aoqi@0 | 607 | |
aoqi@0 | 608 | private void initAuthUserInfo(HttpURLConnection conn, String userInfo) { |
aoqi@0 | 609 | String user; |
aoqi@0 | 610 | String password; |
aoqi@0 | 611 | if (userInfo != null) { // get the user and password |
aoqi@0 | 612 | //System.out.println("UserInfo= " + userInfo ); |
aoqi@0 | 613 | int delimiter = userInfo.indexOf(':'); |
aoqi@0 | 614 | if (delimiter == -1) { |
aoqi@0 | 615 | user = ParseUtil.decode(userInfo); |
aoqi@0 | 616 | password = null; |
aoqi@0 | 617 | } else { |
aoqi@0 | 618 | user = ParseUtil.decode(userInfo.substring(0, delimiter++)); |
aoqi@0 | 619 | password = ParseUtil.decode(userInfo.substring(delimiter)); |
aoqi@0 | 620 | } |
aoqi@0 | 621 | |
aoqi@0 | 622 | String plain = user + ":"; |
aoqi@0 | 623 | byte[] nameBytes = plain.getBytes(); |
aoqi@0 | 624 | byte[] passwdBytes = password.getBytes(); |
aoqi@0 | 625 | |
aoqi@0 | 626 | // concatenate user name and password bytes and encode them |
aoqi@0 | 627 | byte[] concat = new byte[nameBytes.length + passwdBytes.length]; |
aoqi@0 | 628 | |
aoqi@0 | 629 | System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length); |
aoqi@0 | 630 | System.arraycopy( |
aoqi@0 | 631 | passwdBytes, |
aoqi@0 | 632 | 0, |
aoqi@0 | 633 | concat, |
aoqi@0 | 634 | nameBytes.length, |
aoqi@0 | 635 | passwdBytes.length); |
aoqi@0 | 636 | String auth = "Basic " + new String(Base64.encode(concat)); |
aoqi@0 | 637 | conn.setRequestProperty("Authorization", auth); |
aoqi@0 | 638 | if (dL > 0) |
aoqi@0 | 639 | d("Adding auth " + auth); |
aoqi@0 | 640 | } |
aoqi@0 | 641 | } |
aoqi@0 | 642 | |
aoqi@0 | 643 | private static final int dL = 0; |
aoqi@0 | 644 | private void d(String s) { |
aoqi@0 | 645 | log.log(Level.SEVERE, |
aoqi@0 | 646 | "SAAJ0013.p2p.HttpSOAPConnection", |
aoqi@0 | 647 | new String[] { s }); |
aoqi@0 | 648 | System.err.println("HttpSOAPConnection: " + s); |
aoqi@0 | 649 | } |
aoqi@0 | 650 | |
aoqi@0 | 651 | private java.net.HttpURLConnection createConnection(URL endpoint) |
aoqi@0 | 652 | throws IOException { |
aoqi@0 | 653 | return (HttpURLConnection) endpoint.openConnection(); |
aoqi@0 | 654 | } |
aoqi@0 | 655 | |
aoqi@0 | 656 | } |