< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  33 import java.util.StringTokenizer;
  34 import java.util.logging.Level;
  35 import java.util.logging.Logger;
  36 
  37 import javax.xml.soap.*;
  38 
  39 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
  40 import com.sun.xml.internal.messaging.saaj.util.*;
  41 
  42 /**
  43  * This represents a "connection" to the simple HTTP-based provider.
  44  *
  45  * @author Anil Vijendran (akv@eng.sun.com)
  46  * @author Rajiv Mordani (rajiv.mordani@sun.com)
  47  * @author Manveen Kaur (manveen.kaur@sun.com)
  48  *
  49  */
  50 class HttpSOAPConnection extends SOAPConnection {
  51 
  52     public static final String vmVendor = SAAJUtil.getSystemProperty("java.vendor.url");
  53     private static final String sunVmVendor = "http://java.sun.com/";
  54     private static final String ibmVmVendor = "http://www.ibm.com/";
  55     private static final boolean isSunVM = sunVmVendor.equals(vmVendor) ? true: false;
  56     private static final boolean isIBMVM = ibmVmVendor.equals(vmVendor) ? true : false;
  57     private static final String JAXM_URLENDPOINT="javax.xml.messaging.URLEndpoint";
  58 
  59     protected static final Logger log =
  60         Logger.getLogger(LogDomainConstants.HTTP_CONN_DOMAIN,
  61                          "com.sun.xml.internal.messaging.saaj.client.p2p.LocalStrings");
  62 
  63 
  64     MessageFactory messageFactory = null;
  65 
  66     boolean closed = false;
  67 
  68     public HttpSOAPConnection() throws SOAPException {
  69 
  70         try {
  71             messageFactory = MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL);
  72         } catch (NoSuchMethodError ex) {
  73             //fallback to default SOAP 1.1 in this case for backward compatibility
  74             messageFactory = MessageFactory.newInstance();
  75         } catch (Exception ex) {


  78         }
  79     }
  80 
  81     public void close() throws SOAPException {
  82         if (closed) {
  83             log.severe("SAAJ0002.p2p.close.already.closed.conn");
  84             throw new SOAPExceptionImpl("Connection already closed");
  85         }
  86 
  87         messageFactory = null;
  88         closed = true;
  89     }
  90 
  91    public SOAPMessage call(SOAPMessage message, Object endPoint)
  92         throws SOAPException {
  93         if (closed) {
  94             log.severe("SAAJ0003.p2p.call.already.closed.conn");
  95             throw new SOAPExceptionImpl("Connection is closed");
  96         }
  97 
  98         Class urlEndpointClass = null;
  99         ClassLoader loader = Thread.currentThread().getContextClassLoader();
 100         try {
 101             if (loader != null) {
 102                 urlEndpointClass = loader.loadClass(JAXM_URLENDPOINT);
 103             } else {
 104                 urlEndpointClass = Class.forName(JAXM_URLENDPOINT);
 105             }
 106         } catch (ClassNotFoundException ex) {
 107             //Do nothing. URLEndpoint is available only when JAXM is there.
 108             if (log.isLoggable(Level.FINEST))
 109                 log.finest("SAAJ0090.p2p.endpoint.available.only.for.JAXM");
 110         }
 111 
 112         if (urlEndpointClass != null) {
 113             if (urlEndpointClass.isInstance(endPoint)) {
 114                 String url = null;
 115 
 116                 try {
 117                     Method m = urlEndpointClass.getMethod("getURL", (Class[])null);
 118                     url = (String) m.invoke(endPoint, (Object[])null);


 181                 throw new IllegalArgumentException(
 182                     "Protocol "
 183                         + url.getProtocol()
 184                         + " not supported in URL "
 185                         + url);
 186             }
 187             httpConnection = (HttpURLConnection) createConnection(url);
 188 
 189             httpConnection.setRequestMethod("POST");
 190 
 191             httpConnection.setDoOutput(true);
 192             httpConnection.setDoInput(true);
 193             httpConnection.setUseCaches(false);
 194             httpConnection.setInstanceFollowRedirects(true);
 195 
 196             if (message.saveRequired())
 197                 message.saveChanges();
 198 
 199             MimeHeaders headers = message.getMimeHeaders();
 200 
 201             Iterator it = headers.getAllHeaders();
 202             boolean hasAuth = false; // true if we find explicit Auth header
 203             while (it.hasNext()) {
 204                 MimeHeader header = (MimeHeader) it.next();
 205 
 206                 String[] values = headers.getHeader(header.getName());
 207                 if (values.length == 1)
 208                     httpConnection.setRequestProperty(
 209                         header.getName(),
 210                         header.getValue());
 211                 else {
 212                     StringBuffer concat = new StringBuffer();
 213                     int i = 0;
 214                     while (i < values.length) {
 215                         if (i != 0)
 216                             concat.append(',');
 217                         concat.append(values[i]);
 218                         i++;
 219                     }
 220 
 221                     httpConnection.setRequestProperty(
 222                         header.getName(),
 223                         concat.toString());
 224                 }
 225 
 226                 if ("Authorization".equals(header.getName())) {
 227                     hasAuth = true;
 228                     if (log.isLoggable(Level.FINE))
 229                         log.fine("SAAJ0091.p2p.https.auth.in.POST.true");
 230                 }
 231             }
 232 


 338                 log.log(Level.SEVERE,"SAAJ0010.p2p.cannot.read.resp", ex);
 339                 throw new SOAPExceptionImpl(
 340                     "Unable to read response: " + ex.getMessage());
 341             } finally {
 342                if (httpIn != null)
 343                    httpIn.close();
 344                httpConnection.disconnect();
 345             }
 346         }
 347         return response;
 348     }
 349 
 350     // Object identifies where the request should be sent.
 351     // It is required to support objects of type String and java.net.URL.
 352 
 353     public SOAPMessage get(Object endPoint) throws SOAPException {
 354         if (closed) {
 355             log.severe("SAAJ0011.p2p.get.already.closed.conn");
 356             throw new SOAPExceptionImpl("Connection is closed");
 357         }
 358         Class urlEndpointClass = null;
 359 
 360         try {
 361             urlEndpointClass = Class.forName("javax.xml.messaging.URLEndpoint");
 362         } catch (Exception ex) {
 363             //Do nothing. URLEndpoint is available only when JAXM is there.
 364         }
 365 
 366         if (urlEndpointClass != null) {
 367             if (urlEndpointClass.isInstance(endPoint)) {
 368                 String url = null;
 369 
 370                 try {
 371                     Method m = urlEndpointClass.getMethod("getURL", (Class[])null);
 372                     url = (String) m.invoke(endPoint, (Object[])null);
 373                 } catch (Exception ex) {
 374                     log.severe("SAAJ0004.p2p.internal.err");
 375                     throw new SOAPExceptionImpl(
 376                         "Internal error: " + ex.getMessage());
 377                 }
 378                 try {


 424                 d("uri: " + userInfo + " " + url + " " + uri);
 425 
 426             // TBD
 427             //    Will deal with https later.
 428             if (!url.getProtocol().equalsIgnoreCase("http")
 429                 && !url.getProtocol().equalsIgnoreCase("https")) {
 430                 log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
 431                 throw new IllegalArgumentException(
 432                     "Protocol "
 433                         + url.getProtocol()
 434                         + " not supported in URL "
 435                         + url);
 436             }
 437             httpConnection = (HttpURLConnection) createConnection(url);
 438 
 439             httpConnection.setRequestMethod("GET");
 440 
 441             httpConnection.setDoOutput(true);
 442             httpConnection.setDoInput(true);
 443             httpConnection.setUseCaches(false);
 444             httpConnection.setFollowRedirects(true);
 445 
 446             httpConnection.connect();
 447 
 448             try {
 449 
 450                 responseCode = httpConnection.getResponseCode();
 451 
 452                 // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
 453                 if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
 454                     isFailure = true;
 455                 } else if ((responseCode / 100) != 2) {
 456                     log.log(Level.SEVERE,
 457                             "SAAJ0008.p2p.bad.response",
 458                             new String[] { httpConnection.getResponseMessage()});
 459                     throw new SOAPExceptionImpl(
 460                         "Bad response: ("
 461                             + responseCode
 462                             + httpConnection.getResponseMessage());
 463 
 464                 }


 574             SSL_PROVIDER ="com.sun.net.ssl.internal.ssl.Provider";
 575         }
 576     }
 577 
 578     private void initHttps() {
 579         //if(!setHttps) {
 580         String pkgs = SAAJUtil.getSystemProperty("java.protocol.handler.pkgs");
 581         if (log.isLoggable(Level.FINE))
 582             log.log(Level.FINE, "SAAJ0053.p2p.providers", new String[] { pkgs });
 583 
 584         if (pkgs == null || pkgs.indexOf(SSL_PKG) < 0) {
 585             if (pkgs == null)
 586                 pkgs = SSL_PKG;
 587             else
 588                 pkgs = pkgs + "|" + SSL_PKG;
 589             System.setProperty("java.protocol.handler.pkgs", pkgs);
 590             if (log.isLoggable(Level.FINE))
 591                 log.log(Level.FINE, "SAAJ0054.p2p.set.providers",
 592                         new String[] { pkgs });
 593             try {
 594                 Class c = Class.forName(SSL_PROVIDER);
 595                 Provider p = (Provider) c.newInstance();
 596                 Security.addProvider(p);
 597                 if (log.isLoggable(Level.FINE))
 598                     log.log(Level.FINE, "SAAJ0055.p2p.added.ssl.provider",
 599                             new String[] { SSL_PROVIDER });
 600                 //System.out.println("Added SSL_PROVIDER " + SSL_PROVIDER);
 601                 //setHttps = true;
 602             } catch (Exception ex) {
 603             }
 604         }
 605         //}
 606     }
 607 
 608     private void initAuthUserInfo(HttpURLConnection conn, String userInfo) {
 609         String user;
 610         String password;
 611         if (userInfo != null) { // get the user and password
 612             //System.out.println("UserInfo= " + userInfo );
 613             int delimiter = userInfo.indexOf(':');
 614             if (delimiter == -1) {


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  33 import java.util.StringTokenizer;
  34 import java.util.logging.Level;
  35 import java.util.logging.Logger;
  36 
  37 import javax.xml.soap.*;
  38 
  39 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
  40 import com.sun.xml.internal.messaging.saaj.util.*;
  41 
  42 /**
  43  * This represents a "connection" to the simple HTTP-based provider.
  44  *
  45  * @author Anil Vijendran (akv@eng.sun.com)
  46  * @author Rajiv Mordani (rajiv.mordani@sun.com)
  47  * @author Manveen Kaur (manveen.kaur@sun.com)
  48  *
  49  */
  50 class HttpSOAPConnection extends SOAPConnection {
  51 
  52     public static final String vmVendor = SAAJUtil.getSystemProperty("java.vendor.url");

  53     private static final String ibmVmVendor = "http://www.ibm.com/";

  54     private static final boolean isIBMVM = ibmVmVendor.equals(vmVendor) ? true : false;
  55     private static final String JAXM_URLENDPOINT="javax.xml.messaging.URLEndpoint";
  56 
  57     protected static final Logger log =
  58         Logger.getLogger(LogDomainConstants.HTTP_CONN_DOMAIN,
  59                          "com.sun.xml.internal.messaging.saaj.client.p2p.LocalStrings");
  60 
  61 
  62     MessageFactory messageFactory = null;
  63 
  64     boolean closed = false;
  65 
  66     public HttpSOAPConnection() throws SOAPException {
  67 
  68         try {
  69             messageFactory = MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL);
  70         } catch (NoSuchMethodError ex) {
  71             //fallback to default SOAP 1.1 in this case for backward compatibility
  72             messageFactory = MessageFactory.newInstance();
  73         } catch (Exception ex) {


  76         }
  77     }
  78 
  79     public void close() throws SOAPException {
  80         if (closed) {
  81             log.severe("SAAJ0002.p2p.close.already.closed.conn");
  82             throw new SOAPExceptionImpl("Connection already closed");
  83         }
  84 
  85         messageFactory = null;
  86         closed = true;
  87     }
  88 
  89    public SOAPMessage call(SOAPMessage message, Object endPoint)
  90         throws SOAPException {
  91         if (closed) {
  92             log.severe("SAAJ0003.p2p.call.already.closed.conn");
  93             throw new SOAPExceptionImpl("Connection is closed");
  94         }
  95 
  96         Class<?> urlEndpointClass = null;
  97         ClassLoader loader = Thread.currentThread().getContextClassLoader();
  98         try {
  99             if (loader != null) {
 100                 urlEndpointClass = loader.loadClass(JAXM_URLENDPOINT);
 101             } else {
 102                 urlEndpointClass = Class.forName(JAXM_URLENDPOINT);
 103             }
 104         } catch (ClassNotFoundException ex) {
 105             //Do nothing. URLEndpoint is available only when JAXM is there.
 106             if (log.isLoggable(Level.FINEST))
 107                 log.finest("SAAJ0090.p2p.endpoint.available.only.for.JAXM");
 108         }
 109 
 110         if (urlEndpointClass != null) {
 111             if (urlEndpointClass.isInstance(endPoint)) {
 112                 String url = null;
 113 
 114                 try {
 115                     Method m = urlEndpointClass.getMethod("getURL", (Class[])null);
 116                     url = (String) m.invoke(endPoint, (Object[])null);


 179                 throw new IllegalArgumentException(
 180                     "Protocol "
 181                         + url.getProtocol()
 182                         + " not supported in URL "
 183                         + url);
 184             }
 185             httpConnection = (HttpURLConnection) createConnection(url);
 186 
 187             httpConnection.setRequestMethod("POST");
 188 
 189             httpConnection.setDoOutput(true);
 190             httpConnection.setDoInput(true);
 191             httpConnection.setUseCaches(false);
 192             httpConnection.setInstanceFollowRedirects(true);
 193 
 194             if (message.saveRequired())
 195                 message.saveChanges();
 196 
 197             MimeHeaders headers = message.getMimeHeaders();
 198 
 199             Iterator<?> it = headers.getAllHeaders();
 200             boolean hasAuth = false; // true if we find explicit Auth header
 201             while (it.hasNext()) {
 202                 MimeHeader header = (MimeHeader) it.next();
 203 
 204                 String[] values = headers.getHeader(header.getName());
 205                 if (values.length == 1)
 206                     httpConnection.setRequestProperty(
 207                         header.getName(),
 208                         header.getValue());
 209                 else {
 210                     StringBuilder concat = new StringBuilder();
 211                     int i = 0;
 212                     while (i < values.length) {
 213                         if (i != 0)
 214                             concat.append(',');
 215                         concat.append(values[i]);
 216                         i++;
 217                     }
 218 
 219                     httpConnection.setRequestProperty(
 220                         header.getName(),
 221                         concat.toString());
 222                 }
 223 
 224                 if ("Authorization".equals(header.getName())) {
 225                     hasAuth = true;
 226                     if (log.isLoggable(Level.FINE))
 227                         log.fine("SAAJ0091.p2p.https.auth.in.POST.true");
 228                 }
 229             }
 230 


 336                 log.log(Level.SEVERE,"SAAJ0010.p2p.cannot.read.resp", ex);
 337                 throw new SOAPExceptionImpl(
 338                     "Unable to read response: " + ex.getMessage());
 339             } finally {
 340                if (httpIn != null)
 341                    httpIn.close();
 342                httpConnection.disconnect();
 343             }
 344         }
 345         return response;
 346     }
 347 
 348     // Object identifies where the request should be sent.
 349     // It is required to support objects of type String and java.net.URL.
 350 
 351     public SOAPMessage get(Object endPoint) throws SOAPException {
 352         if (closed) {
 353             log.severe("SAAJ0011.p2p.get.already.closed.conn");
 354             throw new SOAPExceptionImpl("Connection is closed");
 355         }
 356         Class<?> urlEndpointClass = null;
 357 
 358         try {
 359             urlEndpointClass = Class.forName("javax.xml.messaging.URLEndpoint");
 360         } catch (Exception ex) {
 361             //Do nothing. URLEndpoint is available only when JAXM is there.
 362         }
 363 
 364         if (urlEndpointClass != null) {
 365             if (urlEndpointClass.isInstance(endPoint)) {
 366                 String url = null;
 367 
 368                 try {
 369                     Method m = urlEndpointClass.getMethod("getURL", (Class[])null);
 370                     url = (String) m.invoke(endPoint, (Object[])null);
 371                 } catch (Exception ex) {
 372                     log.severe("SAAJ0004.p2p.internal.err");
 373                     throw new SOAPExceptionImpl(
 374                         "Internal error: " + ex.getMessage());
 375                 }
 376                 try {


 422                 d("uri: " + userInfo + " " + url + " " + uri);
 423 
 424             // TBD
 425             //    Will deal with https later.
 426             if (!url.getProtocol().equalsIgnoreCase("http")
 427                 && !url.getProtocol().equalsIgnoreCase("https")) {
 428                 log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
 429                 throw new IllegalArgumentException(
 430                     "Protocol "
 431                         + url.getProtocol()
 432                         + " not supported in URL "
 433                         + url);
 434             }
 435             httpConnection = (HttpURLConnection) createConnection(url);
 436 
 437             httpConnection.setRequestMethod("GET");
 438 
 439             httpConnection.setDoOutput(true);
 440             httpConnection.setDoInput(true);
 441             httpConnection.setUseCaches(false);
 442             httpConnection.setInstanceFollowRedirects(true);
 443 
 444             httpConnection.connect();
 445 
 446             try {
 447 
 448                 responseCode = httpConnection.getResponseCode();
 449 
 450                 // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
 451                 if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
 452                     isFailure = true;
 453                 } else if ((responseCode / 100) != 2) {
 454                     log.log(Level.SEVERE,
 455                             "SAAJ0008.p2p.bad.response",
 456                             new String[] { httpConnection.getResponseMessage()});
 457                     throw new SOAPExceptionImpl(
 458                         "Bad response: ("
 459                             + responseCode
 460                             + httpConnection.getResponseMessage());
 461 
 462                 }


 572             SSL_PROVIDER ="com.sun.net.ssl.internal.ssl.Provider";
 573         }
 574     }
 575 
 576     private void initHttps() {
 577         //if(!setHttps) {
 578         String pkgs = SAAJUtil.getSystemProperty("java.protocol.handler.pkgs");
 579         if (log.isLoggable(Level.FINE))
 580             log.log(Level.FINE, "SAAJ0053.p2p.providers", new String[] { pkgs });
 581 
 582         if (pkgs == null || pkgs.indexOf(SSL_PKG) < 0) {
 583             if (pkgs == null)
 584                 pkgs = SSL_PKG;
 585             else
 586                 pkgs = pkgs + "|" + SSL_PKG;
 587             System.setProperty("java.protocol.handler.pkgs", pkgs);
 588             if (log.isLoggable(Level.FINE))
 589                 log.log(Level.FINE, "SAAJ0054.p2p.set.providers",
 590                         new String[] { pkgs });
 591             try {
 592                 Class<?> c = Class.forName(SSL_PROVIDER);
 593                 Provider p = (Provider) c.newInstance();
 594                 Security.addProvider(p);
 595                 if (log.isLoggable(Level.FINE))
 596                     log.log(Level.FINE, "SAAJ0055.p2p.added.ssl.provider",
 597                             new String[] { SSL_PROVIDER });
 598                 //System.out.println("Added SSL_PROVIDER " + SSL_PROVIDER);
 599                 //setHttps = true;
 600             } catch (Exception ex) {
 601             }
 602         }
 603         //}
 604     }
 605 
 606     private void initAuthUserInfo(HttpURLConnection conn, String userInfo) {
 607         String user;
 608         String password;
 609         if (userInfo != null) { // get the user and password
 610             //System.out.println("UserInfo= " + userInfo );
 611             int delimiter = userInfo.indexOf(':');
 612             if (delimiter == -1) {


< prev index next >