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

changeset 1
0961a4a21176
child 45
31822b475baa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/xml/internal/messaging/saaj/client/p2p/HttpSOAPConnection.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,724 @@
     1.4 +/*
     1.5 + * $Id: HttpSOAPConnection.java,v 1.41 2006/01/27 12:49:17 vj135062 Exp $
     1.6 + * $Revision: 1.41 $
     1.7 + * $Date: 2006/01/27 12:49:17 $
     1.8 + */
     1.9 +
    1.10 +/*
    1.11 + * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
    1.12 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    1.13 + *
    1.14 + * This code is free software; you can redistribute it and/or modify it
    1.15 + * under the terms of the GNU General Public License version 2 only, as
    1.16 + * published by the Free Software Foundation.  Sun designates this
    1.17 + * particular file as subject to the "Classpath" exception as provided
    1.18 + * by Sun in the LICENSE file that accompanied this code.
    1.19 + *
    1.20 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.21 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.22 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.23 + * version 2 for more details (a copy is included in the LICENSE file that
    1.24 + * accompanied this code).
    1.25 + *
    1.26 + * You should have received a copy of the GNU General Public License version
    1.27 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.28 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.29 + *
    1.30 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.31 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.32 + * have any questions.
    1.33 + */
    1.34 +package com.sun.xml.internal.messaging.saaj.client.p2p;
    1.35 +
    1.36 +import java.io.*;
    1.37 +import java.lang.reflect.Method;
    1.38 +import java.net.*;
    1.39 +import java.security.*;
    1.40 +import java.util.Iterator;
    1.41 +import java.util.StringTokenizer;
    1.42 +import java.util.logging.Level;
    1.43 +import java.util.logging.Logger;
    1.44 +
    1.45 +import javax.xml.soap.*;
    1.46 +
    1.47 +import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
    1.48 +import com.sun.xml.internal.messaging.saaj.util.*;
    1.49 +
    1.50 +/**
    1.51 + * This represents a "connection" to the simple HTTP-based provider.
    1.52 + *
    1.53 + * @author Anil Vijendran (akv@eng.sun.com)
    1.54 + * @author Rajiv Mordani (rajiv.mordani@sun.com)
    1.55 + * @author Manveen Kaur (manveen.kaur@sun.com)
    1.56 + *
    1.57 + */
    1.58 +public class HttpSOAPConnection extends SOAPConnection {
    1.59 +
    1.60 +    protected static Logger log =
    1.61 +        Logger.getLogger(LogDomainConstants.HTTP_CONN_DOMAIN,
    1.62 +                         "com.sun.xml.internal.messaging.saaj.client.p2p.LocalStrings");
    1.63 +
    1.64 +    public static String defaultProxyHost = null;
    1.65 +    public static int defaultProxyPort = -1;
    1.66 +
    1.67 +    MessageFactory messageFactory = null;
    1.68 +
    1.69 +    boolean closed = false;
    1.70 +
    1.71 +    public HttpSOAPConnection() throws SOAPException {
    1.72 +        proxyHost = defaultProxyHost;
    1.73 +        proxyPort = defaultProxyPort;
    1.74 +
    1.75 +        try {
    1.76 +            messageFactory = MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL);
    1.77 +        } catch (Exception ex) {
    1.78 +            log.log(Level.SEVERE, "SAAJ0001.p2p.cannot.create.msg.factory", ex);
    1.79 +            throw new SOAPExceptionImpl("Unable to create message factory", ex);
    1.80 +        }
    1.81 +    }
    1.82 +
    1.83 +    public void close() throws SOAPException {
    1.84 +        if (closed) {
    1.85 +            log.severe("SAAJ0002.p2p.close.already.closed.conn");
    1.86 +            throw new SOAPExceptionImpl("Connection already closed");
    1.87 +        }
    1.88 +
    1.89 +        messageFactory = null;
    1.90 +        closed = true;
    1.91 +    }
    1.92 +
    1.93 +    public SOAPMessage call(SOAPMessage message, Object endPoint)
    1.94 +        throws SOAPException {
    1.95 +        if (closed) {
    1.96 +            log.severe("SAAJ0003.p2p.call.already.closed.conn");
    1.97 +            throw new SOAPExceptionImpl("Connection is closed");
    1.98 +        }
    1.99 +
   1.100 +        Class urlEndpointClass = null;
   1.101 +
   1.102 +        try {
   1.103 +            urlEndpointClass = Class.forName("javax.xml.messaging.URLEndpoint");
   1.104 +        } catch (Exception ex) {
   1.105 +            //Do nothing. URLEndpoint is available only when JAXM is there.
   1.106 +            log.finest("SAAJ0090.p2p.endpoint.available.only.for.JAXM");
   1.107 +        }
   1.108 +
   1.109 +        if (urlEndpointClass != null) {
   1.110 +            if (urlEndpointClass.isInstance(endPoint)) {
   1.111 +                String url = null;
   1.112 +
   1.113 +                try {
   1.114 +                    Method m = urlEndpointClass.getMethod("getURL", (Class[])null);
   1.115 +                    url = (String) m.invoke(endPoint, (Object[])null);
   1.116 +                } catch (Exception ex) {
   1.117 +                    // TBD -- exception chaining
   1.118 +                    log.log(Level.SEVERE,"SAAJ0004.p2p.internal.err",ex);
   1.119 +                    throw new SOAPExceptionImpl(
   1.120 +                        "Internal error: " + ex.getMessage());
   1.121 +                }
   1.122 +                try {
   1.123 +                    endPoint = new URL(url);
   1.124 +                } catch (MalformedURLException mex) {
   1.125 +                    log.log(Level.SEVERE,"SAAJ0005.p2p.", mex);
   1.126 +                    throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage());
   1.127 +                }
   1.128 +            }
   1.129 +        }
   1.130 +
   1.131 +        if (endPoint instanceof java.lang.String) {
   1.132 +            try {
   1.133 +                endPoint = new URL((String) endPoint);
   1.134 +            } catch (MalformedURLException mex) {
   1.135 +                log.log(Level.SEVERE, "SAAJ0006.p2p.bad.URL", mex);
   1.136 +                throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage());
   1.137 +            }
   1.138 +        }
   1.139 +
   1.140 +        if (endPoint instanceof URL)
   1.141 +            try {
   1.142 +                PriviledgedPost pp =
   1.143 +                    new PriviledgedPost(this, message, (URL) endPoint);
   1.144 +                SOAPMessage response =
   1.145 +                    (SOAPMessage) AccessController.doPrivileged(pp);
   1.146 +
   1.147 +                return response;
   1.148 +            } catch (Exception ex) {
   1.149 +                // TBD -- chaining?
   1.150 +                throw new SOAPExceptionImpl(ex);
   1.151 +            } else {
   1.152 +            log.severe("SAAJ0007.p2p.bad.endPoint.type");
   1.153 +            throw new SOAPExceptionImpl("Bad endPoint type " + endPoint);
   1.154 +        }
   1.155 +    }
   1.156 +
   1.157 +    static class PriviledgedPost implements PrivilegedExceptionAction {
   1.158 +
   1.159 +        HttpSOAPConnection c;
   1.160 +        SOAPMessage message;
   1.161 +        URL endPoint;
   1.162 +
   1.163 +        PriviledgedPost(
   1.164 +            HttpSOAPConnection c,
   1.165 +            SOAPMessage message,
   1.166 +            URL endPoint) {
   1.167 +            this.c = c;
   1.168 +            this.message = message;
   1.169 +            this.endPoint = endPoint;
   1.170 +        }
   1.171 +
   1.172 +        public Object run() throws Exception {
   1.173 +            return c.post(message, endPoint);
   1.174 +        }
   1.175 +    }
   1.176 +
   1.177 +    // TBD
   1.178 +    //    Fix this to do things better.
   1.179 +
   1.180 +    private String proxyHost = null;
   1.181 +
   1.182 +    static class PriviledgedSetProxyAction implements PrivilegedExceptionAction {
   1.183 +
   1.184 +        String proxyHost = null;
   1.185 +        int proxyPort = 0;
   1.186 +
   1.187 +        PriviledgedSetProxyAction(String host, int port) {
   1.188 +            this.proxyHost = host;
   1.189 +            this.proxyPort = port;
   1.190 +        }
   1.191 +
   1.192 +        public Object run() throws Exception {
   1.193 +            System.setProperty("http.proxyHost", proxyHost);
   1.194 +            System.setProperty("http.proxyPort", new Integer(proxyPort).toString());
   1.195 +            log.log(Level.FINE, "SAAJ0050.p2p.proxy.host",
   1.196 +                    new String[] { proxyHost });
   1.197 +            log.log(Level.FINE, "SAAJ0051.p2p.proxy.port",
   1.198 +                    new String[] { new Integer(proxyPort).toString() });
   1.199 +            return proxyHost;
   1.200 +        }
   1.201 +    }
   1.202 +
   1.203 +
   1.204 +    public void setProxy(String host, int port) {
   1.205 +        try {
   1.206 +            proxyPort = port;
   1.207 +            PriviledgedSetProxyAction ps = new PriviledgedSetProxyAction(host, port);
   1.208 +            proxyHost = (String) AccessController.doPrivileged(ps);
   1.209 +        } catch (Exception e) {
   1.210 +            throw new RuntimeException(e);
   1.211 +        }
   1.212 +    }
   1.213 +
   1.214 +    public String getProxyHost() {
   1.215 +        return proxyHost;
   1.216 +    }
   1.217 +
   1.218 +    private int proxyPort = -1;
   1.219 +
   1.220 +    public int getProxyPort() {
   1.221 +        return proxyPort;
   1.222 +    }
   1.223 +
   1.224 +    SOAPMessage post(SOAPMessage message, URL endPoint) throws SOAPException {
   1.225 +        boolean isFailure = false;
   1.226 +
   1.227 +        URL url = null;
   1.228 +        HttpURLConnection httpConnection = null;
   1.229 +
   1.230 +        int responseCode = 0;
   1.231 +        try {
   1.232 +            if (endPoint.getProtocol().equals("https"))
   1.233 +                //if(!setHttps)
   1.234 +                initHttps();
   1.235 +            // Process the URL
   1.236 +            JaxmURI uri = new JaxmURI(endPoint.toString());
   1.237 +            String userInfo = uri.getUserinfo();
   1.238 +
   1.239 +            url = endPoint;
   1.240 +
   1.241 +            if (dL > 0)
   1.242 +                d("uri: " + userInfo + " " + url + " " + uri);
   1.243 +
   1.244 +            // TBD
   1.245 +            //    Will deal with https later.
   1.246 +            if (!url.getProtocol().equalsIgnoreCase("http")
   1.247 +                && !url.getProtocol().equalsIgnoreCase("https")) {
   1.248 +                log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
   1.249 +                throw new IllegalArgumentException(
   1.250 +                    "Protocol "
   1.251 +                        + url.getProtocol()
   1.252 +                        + " not supported in URL "
   1.253 +                        + url);
   1.254 +            }
   1.255 +            httpConnection = (HttpURLConnection) createConnection(url);
   1.256 +
   1.257 +            httpConnection.setRequestMethod("POST");
   1.258 +
   1.259 +            httpConnection.setDoOutput(true);
   1.260 +            httpConnection.setDoInput(true);
   1.261 +            httpConnection.setUseCaches(false);
   1.262 +            HttpURLConnection.setFollowRedirects(true);
   1.263 +
   1.264 +            if (message.saveRequired())
   1.265 +                message.saveChanges();
   1.266 +
   1.267 +            MimeHeaders headers = message.getMimeHeaders();
   1.268 +
   1.269 +            Iterator it = headers.getAllHeaders();
   1.270 +            boolean hasAuth = false; // true if we find explicit Auth header
   1.271 +            while (it.hasNext()) {
   1.272 +                MimeHeader header = (MimeHeader) it.next();
   1.273 +
   1.274 +                String[] values = headers.getHeader(header.getName());
   1.275 +
   1.276 +                if (values.length == 1)
   1.277 +                    httpConnection.setRequestProperty(
   1.278 +                        header.getName(),
   1.279 +                        header.getValue());
   1.280 +                else {
   1.281 +                    StringBuffer concat = new StringBuffer();
   1.282 +                    int i = 0;
   1.283 +                    while (i < values.length) {
   1.284 +                        if (i != 0)
   1.285 +                            concat.append(',');
   1.286 +                        concat.append(values[i]);
   1.287 +                        i++;
   1.288 +                    }
   1.289 +
   1.290 +                    httpConnection.setRequestProperty(
   1.291 +                        header.getName(),
   1.292 +                        concat.toString());
   1.293 +                }
   1.294 +
   1.295 +                if ("Authorization".equals(header.getName())) {
   1.296 +                    hasAuth = true;
   1.297 +                    log.fine("SAAJ0091.p2p.https.auth.in.POST.true");
   1.298 +                }
   1.299 +            }
   1.300 +
   1.301 +            if (!hasAuth && userInfo != null) {
   1.302 +                initAuthUserInfo(httpConnection, userInfo);
   1.303 +            }
   1.304 +
   1.305 +            OutputStream out = httpConnection.getOutputStream();
   1.306 +            message.writeTo(out);
   1.307 +
   1.308 +            out.flush();
   1.309 +            out.close();
   1.310 +
   1.311 +            httpConnection.connect();
   1.312 +
   1.313 +            try {
   1.314 +
   1.315 +                responseCode = httpConnection.getResponseCode();
   1.316 +
   1.317 +                // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
   1.318 +                if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
   1.319 +                    isFailure = true;
   1.320 +                }
   1.321 +                //else if (responseCode != HttpURLConnection.HTTP_OK)
   1.322 +                //else if (!(responseCode >= HttpURLConnection.HTTP_OK && responseCode < 207))
   1.323 +                else if ((responseCode / 100) != 2) {
   1.324 +                    log.log(Level.SEVERE,
   1.325 +                            "SAAJ0008.p2p.bad.response",
   1.326 +                            new String[] {httpConnection.getResponseMessage()});
   1.327 +                    throw new SOAPExceptionImpl(
   1.328 +                        "Bad response: ("
   1.329 +                            + responseCode
   1.330 +                            + httpConnection.getResponseMessage());
   1.331 +
   1.332 +                }
   1.333 +            } catch (IOException e) {
   1.334 +                // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds!
   1.335 +                responseCode = httpConnection.getResponseCode();
   1.336 +                if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
   1.337 +                    isFailure = true;
   1.338 +                } else {
   1.339 +                    throw e;
   1.340 +                }
   1.341 +
   1.342 +            }
   1.343 +
   1.344 +        } catch (SOAPException ex) {
   1.345 +            throw ex;
   1.346 +        } catch (Exception ex) {
   1.347 +            log.severe("SAAJ0009.p2p.msg.send.failed");
   1.348 +            throw new SOAPExceptionImpl("Message send failed", ex);
   1.349 +        }
   1.350 +
   1.351 +        SOAPMessage response = null;
   1.352 +        if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
   1.353 +            try {
   1.354 +                MimeHeaders headers = new MimeHeaders();
   1.355 +
   1.356 +                String key, value;
   1.357 +
   1.358 +                // Header field 0 is the status line so we skip it.
   1.359 +
   1.360 +                int i = 1;
   1.361 +
   1.362 +                while (true) {
   1.363 +                    key = httpConnection.getHeaderFieldKey(i);
   1.364 +                    value = httpConnection.getHeaderField(i);
   1.365 +
   1.366 +                    if (key == null && value == null)
   1.367 +                        break;
   1.368 +
   1.369 +                    if (key != null) {
   1.370 +                        StringTokenizer values =
   1.371 +                            new StringTokenizer(value, ",");
   1.372 +                        while (values.hasMoreTokens())
   1.373 +                            headers.addHeader(key, values.nextToken().trim());
   1.374 +                    }
   1.375 +                    i++;
   1.376 +                }
   1.377 +
   1.378 +                InputStream httpIn =
   1.379 +                    (isFailure
   1.380 +                        ? httpConnection.getErrorStream()
   1.381 +                        : httpConnection.getInputStream());
   1.382 +
   1.383 +                byte[] bytes = readFully(httpIn);
   1.384 +
   1.385 +                int length =
   1.386 +                    httpConnection.getContentLength() == -1
   1.387 +                        ? bytes.length
   1.388 +                        : httpConnection.getContentLength();
   1.389 +
   1.390 +                // If no reply message is returned,
   1.391 +                // content-Length header field value is expected to be zero.
   1.392 +                if (length == 0) {
   1.393 +                    response = null;
   1.394 +                    log.warning("SAAJ0014.p2p.content.zero");
   1.395 +                } else {
   1.396 +                    ByteInputStream in = new ByteInputStream(bytes, length);
   1.397 +                    response = messageFactory.createMessage(headers, in);
   1.398 +                }
   1.399 +
   1.400 +                httpIn.close();
   1.401 +                httpConnection.disconnect();
   1.402 +
   1.403 +            } catch (SOAPException ex) {
   1.404 +                throw ex;
   1.405 +            } catch (Exception ex) {
   1.406 +                log.log(Level.SEVERE,"SAAJ0010.p2p.cannot.read.resp", ex);
   1.407 +                throw new SOAPExceptionImpl(
   1.408 +                    "Unable to read response: " + ex.getMessage());
   1.409 +            }
   1.410 +        }
   1.411 +        return response;
   1.412 +    }
   1.413 +
   1.414 +    // Object identifies where the request should be sent.
   1.415 +    // It is required to support objects of type String and java.net.URL.
   1.416 +
   1.417 +    public SOAPMessage get(Object endPoint) throws SOAPException {
   1.418 +        if (closed) {
   1.419 +            log.severe("SAAJ0011.p2p.get.already.closed.conn");
   1.420 +            throw new SOAPExceptionImpl("Connection is closed");
   1.421 +        }
   1.422 +        Class urlEndpointClass = null;
   1.423 +
   1.424 +        try {
   1.425 +            urlEndpointClass = Class.forName("javax.xml.messaging.URLEndpoint");
   1.426 +        } catch (Exception ex) {
   1.427 +            //Do nothing. URLEndpoint is available only when JAXM is there.
   1.428 +        }
   1.429 +
   1.430 +        if (urlEndpointClass != null) {
   1.431 +            if (urlEndpointClass.isInstance(endPoint)) {
   1.432 +                String url = null;
   1.433 +
   1.434 +                try {
   1.435 +                    Method m = urlEndpointClass.getMethod("getURL", (Class[])null);
   1.436 +                    url = (String) m.invoke(endPoint, (Object[])null);
   1.437 +                } catch (Exception ex) {
   1.438 +                    log.severe("SAAJ0004.p2p.internal.err");
   1.439 +                    throw new SOAPExceptionImpl(
   1.440 +                        "Internal error: " + ex.getMessage());
   1.441 +                }
   1.442 +                try {
   1.443 +                    endPoint = new URL(url);
   1.444 +                } catch (MalformedURLException mex) {
   1.445 +                    log.severe("SAAJ0005.p2p.");
   1.446 +                    throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage());
   1.447 +                }
   1.448 +            }
   1.449 +        }
   1.450 +
   1.451 +        if (endPoint instanceof java.lang.String) {
   1.452 +            try {
   1.453 +                endPoint = new URL((String) endPoint);
   1.454 +            } catch (MalformedURLException mex) {
   1.455 +                log.severe("SAAJ0006.p2p.bad.URL");
   1.456 +                throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage());
   1.457 +            }
   1.458 +        }
   1.459 +
   1.460 +        if (endPoint instanceof URL)
   1.461 +            try {
   1.462 +                PriviledgedGet pg = new PriviledgedGet(this, (URL) endPoint);
   1.463 +                SOAPMessage response =
   1.464 +                    (SOAPMessage) AccessController.doPrivileged(pg);
   1.465 +
   1.466 +                return response;
   1.467 +            } catch (Exception ex) {
   1.468 +                throw new SOAPExceptionImpl(ex);
   1.469 +            } else
   1.470 +            throw new SOAPExceptionImpl("Bad endPoint type " + endPoint);
   1.471 +    }
   1.472 +
   1.473 +    static class PriviledgedGet implements PrivilegedExceptionAction {
   1.474 +
   1.475 +        HttpSOAPConnection c;
   1.476 +        URL endPoint;
   1.477 +
   1.478 +        PriviledgedGet(HttpSOAPConnection c, URL endPoint) {
   1.479 +            this.c = c;
   1.480 +            this.endPoint = endPoint;
   1.481 +        }
   1.482 +
   1.483 +        public Object run() throws Exception {
   1.484 +            return c.get(endPoint);
   1.485 +        }
   1.486 +    }
   1.487 +
   1.488 +    SOAPMessage get(URL endPoint) throws SOAPException {
   1.489 +        boolean isFailure = false;
   1.490 +
   1.491 +        URL url = null;
   1.492 +        HttpURLConnection httpConnection = null;
   1.493 +
   1.494 +        int responseCode = 0;
   1.495 +        try {
   1.496 +            /// Is https GET allowed??
   1.497 +            if (endPoint.getProtocol().equals("https"))
   1.498 +                initHttps();
   1.499 +            // Process the URL
   1.500 +            JaxmURI uri = new JaxmURI(endPoint.toString());
   1.501 +            String userInfo = uri.getUserinfo();
   1.502 +
   1.503 +            url = endPoint;
   1.504 +
   1.505 +            if (dL > 0)
   1.506 +                d("uri: " + userInfo + " " + url + " " + uri);
   1.507 +
   1.508 +            // TBD
   1.509 +            //    Will deal with https later.
   1.510 +            if (!url.getProtocol().equalsIgnoreCase("http")
   1.511 +                && !url.getProtocol().equalsIgnoreCase("https")) {
   1.512 +                log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
   1.513 +                throw new IllegalArgumentException(
   1.514 +                    "Protocol "
   1.515 +                        + url.getProtocol()
   1.516 +                        + " not supported in URL "
   1.517 +                        + url);
   1.518 +            }
   1.519 +            httpConnection = (HttpURLConnection) createConnection(url);
   1.520 +
   1.521 +            httpConnection.setRequestMethod("GET");
   1.522 +
   1.523 +            httpConnection.setDoOutput(true);
   1.524 +            httpConnection.setDoInput(true);
   1.525 +            httpConnection.setUseCaches(false);
   1.526 +            HttpURLConnection.setFollowRedirects(true);
   1.527 +
   1.528 +            httpConnection.connect();
   1.529 +
   1.530 +            try {
   1.531 +
   1.532 +                responseCode = httpConnection.getResponseCode();
   1.533 +
   1.534 +                // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
   1.535 +                if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
   1.536 +                    isFailure = true;
   1.537 +                } else if ((responseCode / 100) != 2) {
   1.538 +                    log.log(Level.SEVERE,
   1.539 +                            "SAAJ0008.p2p.bad.response",
   1.540 +                            new String[] { httpConnection.getResponseMessage()});
   1.541 +                    throw new SOAPExceptionImpl(
   1.542 +                        "Bad response: ("
   1.543 +                            + responseCode
   1.544 +                            + httpConnection.getResponseMessage());
   1.545 +
   1.546 +                }
   1.547 +            } catch (IOException e) {
   1.548 +                // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds!
   1.549 +                responseCode = httpConnection.getResponseCode();
   1.550 +                if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
   1.551 +                    isFailure = true;
   1.552 +                } else {
   1.553 +                    throw e;
   1.554 +                }
   1.555 +
   1.556 +            }
   1.557 +
   1.558 +        } catch (SOAPException ex) {
   1.559 +            throw ex;
   1.560 +        } catch (Exception ex) {
   1.561 +            log.severe("SAAJ0012.p2p.get.failed");
   1.562 +            throw new SOAPExceptionImpl("Get failed", ex);
   1.563 +        }
   1.564 +
   1.565 +        SOAPMessage response = null;
   1.566 +        if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
   1.567 +            try {
   1.568 +                MimeHeaders headers = new MimeHeaders();
   1.569 +
   1.570 +                String key, value;
   1.571 +
   1.572 +                // Header field 0 is the status line so we skip it.
   1.573 +
   1.574 +                int i = 1;
   1.575 +
   1.576 +                while (true) {
   1.577 +                    key = httpConnection.getHeaderFieldKey(i);
   1.578 +                    value = httpConnection.getHeaderField(i);
   1.579 +
   1.580 +                    if (key == null && value == null)
   1.581 +                        break;
   1.582 +
   1.583 +                    if (key != null) {
   1.584 +                        StringTokenizer values =
   1.585 +                            new StringTokenizer(value, ",");
   1.586 +                        while (values.hasMoreTokens())
   1.587 +                            headers.addHeader(key, values.nextToken().trim());
   1.588 +                    }
   1.589 +                    i++;
   1.590 +                }
   1.591 +
   1.592 +                InputStream httpIn =
   1.593 +                    (isFailure
   1.594 +                        ? httpConnection.getErrorStream()
   1.595 +                        : httpConnection.getInputStream());
   1.596 +
   1.597 +                byte[] bytes = readFully(httpIn);
   1.598 +
   1.599 +                int length =
   1.600 +                    httpConnection.getContentLength() == -1
   1.601 +                        ? bytes.length
   1.602 +                        : httpConnection.getContentLength();
   1.603 +
   1.604 +                // If no reply message is returned,
   1.605 +                // content-Length header field value is expected to be zero.
   1.606 +                if (length == 0) {
   1.607 +                    response = null;
   1.608 +                    log.warning("SAAJ0014.p2p.content.zero");
   1.609 +                } else {
   1.610 +
   1.611 +                    ByteInputStream in = new ByteInputStream(bytes, length);
   1.612 +                    response = messageFactory.createMessage(headers, in);
   1.613 +                }
   1.614 +
   1.615 +                httpIn.close();
   1.616 +                httpConnection.disconnect();
   1.617 +
   1.618 +            } catch (SOAPException ex) {
   1.619 +                throw ex;
   1.620 +            } catch (Exception ex) {
   1.621 +                log.log(Level.SEVERE,
   1.622 +                        "SAAJ0010.p2p.cannot.read.resp",
   1.623 +                        ex);
   1.624 +                throw new SOAPExceptionImpl(
   1.625 +                    "Unable to read response: " + ex.getMessage());
   1.626 +            }
   1.627 +        }
   1.628 +        return response;
   1.629 +    }
   1.630 +
   1.631 +    private byte[] readFully(InputStream istream) throws IOException {
   1.632 +        ByteArrayOutputStream bout = new ByteArrayOutputStream();
   1.633 +        byte[] buf = new byte[1024];
   1.634 +        int num = 0;
   1.635 +
   1.636 +        while ((num = istream.read(buf)) != -1) {
   1.637 +            bout.write(buf, 0, num);
   1.638 +        }
   1.639 +
   1.640 +        byte[] ret = bout.toByteArray();
   1.641 +
   1.642 +        return ret;
   1.643 +    }
   1.644 +
   1.645 +    private static String SSL_PKG = "com.sun.net.ssl.internal.www.protocol";
   1.646 +    private static String SSL_PROVIDER =
   1.647 +        "com.sun.net.ssl.internal.ssl.Provider";
   1.648 +    private void initHttps() {
   1.649 +        //if(!setHttps) {
   1.650 +        String pkgs = System.getProperty("java.protocol.handler.pkgs");
   1.651 +        log.log(Level.FINE,
   1.652 +                "SAAJ0053.p2p.providers",
   1.653 +                new String[] { pkgs });
   1.654 +
   1.655 +        if (pkgs == null || pkgs.indexOf(SSL_PKG) < 0) {
   1.656 +            if (pkgs == null)
   1.657 +                pkgs = SSL_PKG;
   1.658 +            else
   1.659 +                pkgs = pkgs + "|" + SSL_PKG;
   1.660 +            System.setProperty("java.protocol.handler.pkgs", pkgs);
   1.661 +            log.log(Level.FINE,
   1.662 +                    "SAAJ0054.p2p.set.providers",
   1.663 +                    new String[] { pkgs });
   1.664 +            try {
   1.665 +                Class c = Class.forName(SSL_PROVIDER);
   1.666 +                Provider p = (Provider) c.newInstance();
   1.667 +                Security.addProvider(p);
   1.668 +                log.log(Level.FINE,
   1.669 +                        "SAAJ0055.p2p.added.ssl.provider",
   1.670 +                        new String[] { SSL_PROVIDER });
   1.671 +                //System.out.println("Added SSL_PROVIDER " + SSL_PROVIDER);
   1.672 +                //setHttps = true;
   1.673 +            } catch (Exception ex) {
   1.674 +            }
   1.675 +        }
   1.676 +        //}
   1.677 +    }
   1.678 +
   1.679 +    private void initAuthUserInfo(HttpURLConnection conn, String userInfo) {
   1.680 +        String user;
   1.681 +        String password;
   1.682 +        if (userInfo != null) { // get the user and password
   1.683 +            //System.out.println("UserInfo= " + userInfo );
   1.684 +            int delimiter = userInfo.indexOf(':');
   1.685 +            if (delimiter == -1) {
   1.686 +                user = ParseUtil.decode(userInfo);
   1.687 +                password = null;
   1.688 +            } else {
   1.689 +                user = ParseUtil.decode(userInfo.substring(0, delimiter++));
   1.690 +                password = ParseUtil.decode(userInfo.substring(delimiter));
   1.691 +            }
   1.692 +
   1.693 +            String plain = user + ":";
   1.694 +            byte[] nameBytes = plain.getBytes();
   1.695 +            byte[] passwdBytes = password.getBytes();
   1.696 +
   1.697 +            // concatenate user name and password bytes and encode them
   1.698 +            byte[] concat = new byte[nameBytes.length + passwdBytes.length];
   1.699 +
   1.700 +            System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length);
   1.701 +            System.arraycopy(
   1.702 +                passwdBytes,
   1.703 +                0,
   1.704 +                concat,
   1.705 +                nameBytes.length,
   1.706 +                passwdBytes.length);
   1.707 +            String auth = "Basic " + new String(Base64.encode(concat));
   1.708 +            conn.setRequestProperty("Authorization", auth);
   1.709 +            if (dL > 0)
   1.710 +                d("Adding auth " + auth);
   1.711 +        }
   1.712 +    }
   1.713 +
   1.714 +    private static final int dL = 0;
   1.715 +    private void d(String s) {
   1.716 +        log.log(Level.SEVERE,
   1.717 +                "SAAJ0013.p2p.HttpSOAPConnection",
   1.718 +                new String[] { s });
   1.719 +        System.err.println("HttpSOAPConnection: " + s);
   1.720 +    }
   1.721 +
   1.722 +    private java.net.HttpURLConnection createConnection(URL endpoint)
   1.723 +        throws IOException {
   1.724 +        return (HttpURLConnection) endpoint.openConnection();
   1.725 +    }
   1.726 +
   1.727 +}

mercurial