src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/client/p2p/HttpSOAPConnection.java

changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
equal deleted inserted replaced
366:8c0b6bccfe47 368:0989ad8c0860
1 /* 1 /*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
103 } else { 103 } else {
104 urlEndpointClass = Class.forName(JAXM_URLENDPOINT); 104 urlEndpointClass = Class.forName(JAXM_URLENDPOINT);
105 } 105 }
106 } catch (ClassNotFoundException ex) { 106 } catch (ClassNotFoundException ex) {
107 //Do nothing. URLEndpoint is available only when JAXM is there. 107 //Do nothing. URLEndpoint is available only when JAXM is there.
108 log.finest("SAAJ0090.p2p.endpoint.available.only.for.JAXM"); 108 if (log.isLoggable(Level.FINEST))
109 log.finest("SAAJ0090.p2p.endpoint.available.only.for.JAXM");
109 } 110 }
110 111
111 if (urlEndpointClass != null) { 112 if (urlEndpointClass != null) {
112 if (urlEndpointClass.isInstance(endPoint)) { 113 if (urlEndpointClass.isInstance(endPoint)) {
113 String url = null; 114 String url = null;
150 log.severe("SAAJ0007.p2p.bad.endPoint.type"); 151 log.severe("SAAJ0007.p2p.bad.endPoint.type");
151 throw new SOAPExceptionImpl("Bad endPoint type " + endPoint); 152 throw new SOAPExceptionImpl("Bad endPoint type " + endPoint);
152 } 153 }
153 } 154 }
154 155
155 SOAPMessage post(SOAPMessage message, URL endPoint) throws SOAPException { 156 SOAPMessage post(SOAPMessage message, URL endPoint) throws SOAPException, IOException {
156 boolean isFailure = false; 157 boolean isFailure = false;
157 158
158 URL url = null; 159 URL url = null;
159 HttpURLConnection httpConnection = null; 160 HttpURLConnection httpConnection = null;
160 161
162 try { 163 try {
163 if (endPoint.getProtocol().equals("https")) 164 if (endPoint.getProtocol().equals("https"))
164 //if(!setHttps) 165 //if(!setHttps)
165 initHttps(); 166 initHttps();
166 // Process the URL 167 // Process the URL
167 JaxmURI uri = new JaxmURI(endPoint.toString()); 168 URI uri = new URI(endPoint.toString());
168 String userInfo = uri.getUserinfo(); 169 String userInfo = uri.getRawUserInfo();
169 170
170 url = endPoint; 171 url = endPoint;
171 172
172 if (dL > 0) 173 if (dL > 0)
173 d("uri: " + userInfo + " " + url + " " + uri); 174 d("uri: " + userInfo + " " + url + " " + uri);
222 concat.toString()); 223 concat.toString());
223 } 224 }
224 225
225 if ("Authorization".equals(header.getName())) { 226 if ("Authorization".equals(header.getName())) {
226 hasAuth = true; 227 hasAuth = true;
227 log.fine("SAAJ0091.p2p.https.auth.in.POST.true"); 228 if (log.isLoggable(Level.FINE))
229 log.fine("SAAJ0091.p2p.https.auth.in.POST.true");
228 } 230 }
229 } 231 }
230 232
231 if (!hasAuth && userInfo != null) { 233 if (!hasAuth && userInfo != null) {
232 initAuthUserInfo(httpConnection, userInfo); 234 initAuthUserInfo(httpConnection, userInfo);
233 } 235 }
234 236
235 OutputStream out = httpConnection.getOutputStream(); 237 OutputStream out = httpConnection.getOutputStream();
236 message.writeTo(out); 238 try {
237 239 message.writeTo(out);
238 out.flush(); 240 out.flush();
239 out.close(); 241 } finally {
242 out.close();
243 }
240 244
241 httpConnection.connect(); 245 httpConnection.connect();
242 246
243 try { 247 try {
244 248
277 log.severe("SAAJ0009.p2p.msg.send.failed"); 281 log.severe("SAAJ0009.p2p.msg.send.failed");
278 throw new SOAPExceptionImpl("Message send failed", ex); 282 throw new SOAPExceptionImpl("Message send failed", ex);
279 } 283 }
280 284
281 SOAPMessage response = null; 285 SOAPMessage response = null;
286 InputStream httpIn = null;
282 if (responseCode == HttpURLConnection.HTTP_OK || isFailure) { 287 if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
283 try { 288 try {
284 MimeHeaders headers = new MimeHeaders(); 289 MimeHeaders headers = new MimeHeaders();
285 290
286 String key, value; 291 String key, value;
303 headers.addHeader(key, values.nextToken().trim()); 308 headers.addHeader(key, values.nextToken().trim());
304 } 309 }
305 i++; 310 i++;
306 } 311 }
307 312
308 InputStream httpIn = 313 httpIn =
309 (isFailure 314 (isFailure
310 ? httpConnection.getErrorStream() 315 ? httpConnection.getErrorStream()
311 : httpConnection.getInputStream()); 316 : httpConnection.getInputStream());
312 317
313 byte[] bytes = readFully(httpIn); 318 byte[] bytes = readFully(httpIn);
325 } else { 330 } else {
326 ByteInputStream in = new ByteInputStream(bytes, length); 331 ByteInputStream in = new ByteInputStream(bytes, length);
327 response = messageFactory.createMessage(headers, in); 332 response = messageFactory.createMessage(headers, in);
328 } 333 }
329 334
330 httpIn.close();
331 httpConnection.disconnect();
332
333 } catch (SOAPException ex) { 335 } catch (SOAPException ex) {
334 throw ex; 336 throw ex;
335 } catch (Exception ex) { 337 } catch (Exception ex) {
336 log.log(Level.SEVERE,"SAAJ0010.p2p.cannot.read.resp", ex); 338 log.log(Level.SEVERE,"SAAJ0010.p2p.cannot.read.resp", ex);
337 throw new SOAPExceptionImpl( 339 throw new SOAPExceptionImpl(
338 "Unable to read response: " + ex.getMessage()); 340 "Unable to read response: " + ex.getMessage());
341 } finally {
342 if (httpIn != null)
343 httpIn.close();
344 httpConnection.disconnect();
339 } 345 }
340 } 346 }
341 return response; 347 return response;
342 } 348 }
343 349
395 throw new SOAPExceptionImpl(ex); 401 throw new SOAPExceptionImpl(ex);
396 } else 402 } else
397 throw new SOAPExceptionImpl("Bad endPoint type " + endPoint); 403 throw new SOAPExceptionImpl("Bad endPoint type " + endPoint);
398 } 404 }
399 405
400 SOAPMessage doGet(URL endPoint) throws SOAPException { 406 SOAPMessage doGet(URL endPoint) throws SOAPException, IOException {
401 boolean isFailure = false; 407 boolean isFailure = false;
402 408
403 URL url = null; 409 URL url = null;
404 HttpURLConnection httpConnection = null; 410 HttpURLConnection httpConnection = null;
405 411
407 try { 413 try {
408 /// Is https GET allowed?? 414 /// Is https GET allowed??
409 if (endPoint.getProtocol().equals("https")) 415 if (endPoint.getProtocol().equals("https"))
410 initHttps(); 416 initHttps();
411 // Process the URL 417 // Process the URL
412 JaxmURI uri = new JaxmURI(endPoint.toString()); 418 URI uri = new URI(endPoint.toString());
413 String userInfo = uri.getUserinfo(); 419 String userInfo = uri.getRawUserInfo();
414 420
415 url = endPoint; 421 url = endPoint;
416 422
417 if (dL > 0) 423 if (dL > 0)
418 d("uri: " + userInfo + " " + url + " " + uri); 424 d("uri: " + userInfo + " " + url + " " + uri);
473 log.severe("SAAJ0012.p2p.get.failed"); 479 log.severe("SAAJ0012.p2p.get.failed");
474 throw new SOAPExceptionImpl("Get failed", ex); 480 throw new SOAPExceptionImpl("Get failed", ex);
475 } 481 }
476 482
477 SOAPMessage response = null; 483 SOAPMessage response = null;
484 InputStream httpIn = null;
478 if (responseCode == HttpURLConnection.HTTP_OK || isFailure) { 485 if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
479 try { 486 try {
480 MimeHeaders headers = new MimeHeaders(); 487 MimeHeaders headers = new MimeHeaders();
481 488
482 String key, value; 489 String key, value;
499 headers.addHeader(key, values.nextToken().trim()); 506 headers.addHeader(key, values.nextToken().trim());
500 } 507 }
501 i++; 508 i++;
502 } 509 }
503 510
504 InputStream httpIn = 511 httpIn =
505 (isFailure 512 (isFailure
506 ? httpConnection.getErrorStream() 513 ? httpConnection.getErrorStream()
507 : httpConnection.getInputStream()); 514 : httpConnection.getInputStream());
508 // If no reply message is returned, 515 // If no reply message is returned,
509 // content-Length header field value is expected to be zero. 516 // content-Length header field value is expected to be zero.
518 log.warning("SAAJ0014.p2p.content.zero"); 525 log.warning("SAAJ0014.p2p.content.zero");
519 } else { 526 } else {
520 response = messageFactory.createMessage(headers, httpIn); 527 response = messageFactory.createMessage(headers, httpIn);
521 } 528 }
522 529
523 httpIn.close();
524 httpConnection.disconnect();
525
526 } catch (SOAPException ex) { 530 } catch (SOAPException ex) {
527 throw ex; 531 throw ex;
528 } catch (Exception ex) { 532 } catch (Exception ex) {
529 log.log(Level.SEVERE, 533 log.log(Level.SEVERE,
530 "SAAJ0010.p2p.cannot.read.resp", 534 "SAAJ0010.p2p.cannot.read.resp",
531 ex); 535 ex);
532 throw new SOAPExceptionImpl( 536 throw new SOAPExceptionImpl(
533 "Unable to read response: " + ex.getMessage()); 537 "Unable to read response: " + ex.getMessage());
538 } finally {
539 if (httpIn != null)
540 httpIn.close();
541 httpConnection.disconnect();
534 } 542 }
535 } 543 }
536 return response; 544 return response;
537 } 545 }
538 546
568 } 576 }
569 577
570 private void initHttps() { 578 private void initHttps() {
571 //if(!setHttps) { 579 //if(!setHttps) {
572 String pkgs = SAAJUtil.getSystemProperty("java.protocol.handler.pkgs"); 580 String pkgs = SAAJUtil.getSystemProperty("java.protocol.handler.pkgs");
573 log.log(Level.FINE, 581 if (log.isLoggable(Level.FINE))
574 "SAAJ0053.p2p.providers", 582 log.log(Level.FINE, "SAAJ0053.p2p.providers", new String[] { pkgs });
575 new String[] { pkgs });
576 583
577 if (pkgs == null || pkgs.indexOf(SSL_PKG) < 0) { 584 if (pkgs == null || pkgs.indexOf(SSL_PKG) < 0) {
578 if (pkgs == null) 585 if (pkgs == null)
579 pkgs = SSL_PKG; 586 pkgs = SSL_PKG;
580 else 587 else
581 pkgs = pkgs + "|" + SSL_PKG; 588 pkgs = pkgs + "|" + SSL_PKG;
582 System.setProperty("java.protocol.handler.pkgs", pkgs); 589 System.setProperty("java.protocol.handler.pkgs", pkgs);
583 log.log(Level.FINE, 590 if (log.isLoggable(Level.FINE))
584 "SAAJ0054.p2p.set.providers", 591 log.log(Level.FINE, "SAAJ0054.p2p.set.providers",
585 new String[] { pkgs }); 592 new String[] { pkgs });
586 try { 593 try {
587 Class c = Class.forName(SSL_PROVIDER); 594 Class c = Class.forName(SSL_PROVIDER);
588 Provider p = (Provider) c.newInstance(); 595 Provider p = (Provider) c.newInstance();
589 Security.addProvider(p); 596 Security.addProvider(p);
590 log.log(Level.FINE, 597 if (log.isLoggable(Level.FINE))
591 "SAAJ0055.p2p.added.ssl.provider", 598 log.log(Level.FINE, "SAAJ0055.p2p.added.ssl.provider",
592 new String[] { SSL_PROVIDER }); 599 new String[] { SSL_PROVIDER });
593 //System.out.println("Added SSL_PROVIDER " + SSL_PROVIDER); 600 //System.out.println("Added SSL_PROVIDER " + SSL_PROVIDER);
594 //setHttps = true; 601 //setHttps = true;
595 } catch (Exception ex) { 602 } catch (Exception ex) {
596 } 603 }
597 } 604 }

mercurial