duke@1: /* tbell@45: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ tbell@45: /* tbell@45: * $Id: HttpSOAPConnection.java,v 1.41 2006/01/27 12:49:17 vj135062 Exp $ tbell@45: * $Revision: 1.41 $ tbell@45: * $Date: 2006/01/27 12:49:17 $ tbell@45: */ tbell@45: tbell@45: duke@1: package com.sun.xml.internal.messaging.saaj.client.p2p; duke@1: duke@1: import java.io.*; duke@1: import java.lang.reflect.Method; duke@1: import java.net.*; duke@1: import java.security.*; duke@1: import java.util.Iterator; duke@1: import java.util.StringTokenizer; duke@1: import java.util.logging.Level; duke@1: import java.util.logging.Logger; duke@1: duke@1: import javax.xml.soap.*; duke@1: duke@1: import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; duke@1: import com.sun.xml.internal.messaging.saaj.util.*; duke@1: duke@1: /** duke@1: * This represents a "connection" to the simple HTTP-based provider. duke@1: * duke@1: * @author Anil Vijendran (akv@eng.sun.com) duke@1: * @author Rajiv Mordani (rajiv.mordani@sun.com) duke@1: * @author Manveen Kaur (manveen.kaur@sun.com) duke@1: * duke@1: */ duke@1: public class HttpSOAPConnection extends SOAPConnection { duke@1: tbell@50: public static final String vmVendor = System.getProperty("java.vendor.url"); tbell@50: private static final String sunVmVendor = "http://java.sun.com/"; tbell@50: private static final String ibmVmVendor = "http://www.ibm.com/"; tbell@50: private static final boolean isSunVM = sunVmVendor.equals(vmVendor) ? true: false; tbell@50: private static final boolean isIBMVM = ibmVmVendor.equals(vmVendor) ? true : false; tbell@50: private static final String JAXM_URLENDPOINT="javax.xml.messaging.URLEndpoint"; tbell@50: tbell@50: protected static final Logger log = duke@1: Logger.getLogger(LogDomainConstants.HTTP_CONN_DOMAIN, duke@1: "com.sun.xml.internal.messaging.saaj.client.p2p.LocalStrings"); duke@1: tbell@50: public static final String defaultProxyHost = null; tbell@50: public static final int defaultProxyPort = -1; duke@1: duke@1: MessageFactory messageFactory = null; duke@1: duke@1: boolean closed = false; duke@1: duke@1: public HttpSOAPConnection() throws SOAPException { duke@1: proxyHost = defaultProxyHost; duke@1: proxyPort = defaultProxyPort; duke@1: duke@1: try { duke@1: messageFactory = MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL); tbell@50: } catch (NoSuchMethodError ex) { tbell@50: //fallback to default SOAP 1.1 in this case for backward compatibility tbell@50: messageFactory = MessageFactory.newInstance(); duke@1: } catch (Exception ex) { duke@1: log.log(Level.SEVERE, "SAAJ0001.p2p.cannot.create.msg.factory", ex); duke@1: throw new SOAPExceptionImpl("Unable to create message factory", ex); duke@1: } duke@1: } duke@1: duke@1: public void close() throws SOAPException { duke@1: if (closed) { duke@1: log.severe("SAAJ0002.p2p.close.already.closed.conn"); duke@1: throw new SOAPExceptionImpl("Connection already closed"); duke@1: } duke@1: duke@1: messageFactory = null; duke@1: closed = true; duke@1: } duke@1: duke@1: public SOAPMessage call(SOAPMessage message, Object endPoint) duke@1: throws SOAPException { duke@1: if (closed) { duke@1: log.severe("SAAJ0003.p2p.call.already.closed.conn"); duke@1: throw new SOAPExceptionImpl("Connection is closed"); duke@1: } duke@1: duke@1: Class urlEndpointClass = null; tbell@50: ClassLoader loader = Thread.currentThread().getContextClassLoader(); duke@1: duke@1: try { tbell@50: if (loader != null) { tbell@50: urlEndpointClass = loader.loadClass(JAXM_URLENDPOINT); tbell@50: } else { tbell@50: urlEndpointClass = Class.forName(JAXM_URLENDPOINT); tbell@50: } tbell@50: } catch (ClassNotFoundException ex) { tbell@50: //Do nothing. URLEndpoint is available only when JAXM is there. tbell@50: log.finest("SAAJ0090.p2p.endpoint.available.only.for.JAXM"); tbell@50: } duke@1: duke@1: if (urlEndpointClass != null) { duke@1: if (urlEndpointClass.isInstance(endPoint)) { duke@1: String url = null; duke@1: duke@1: try { duke@1: Method m = urlEndpointClass.getMethod("getURL", (Class[])null); duke@1: url = (String) m.invoke(endPoint, (Object[])null); duke@1: } catch (Exception ex) { duke@1: // TBD -- exception chaining duke@1: log.log(Level.SEVERE,"SAAJ0004.p2p.internal.err",ex); duke@1: throw new SOAPExceptionImpl( duke@1: "Internal error: " + ex.getMessage()); duke@1: } duke@1: try { duke@1: endPoint = new URL(url); duke@1: } catch (MalformedURLException mex) { duke@1: log.log(Level.SEVERE,"SAAJ0005.p2p.", mex); duke@1: throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage()); duke@1: } duke@1: } duke@1: } duke@1: duke@1: if (endPoint instanceof java.lang.String) { duke@1: try { duke@1: endPoint = new URL((String) endPoint); duke@1: } catch (MalformedURLException mex) { duke@1: log.log(Level.SEVERE, "SAAJ0006.p2p.bad.URL", mex); duke@1: throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage()); duke@1: } duke@1: } duke@1: duke@1: if (endPoint instanceof URL) duke@1: try { duke@1: PriviledgedPost pp = duke@1: new PriviledgedPost(this, message, (URL) endPoint); duke@1: SOAPMessage response = duke@1: (SOAPMessage) AccessController.doPrivileged(pp); duke@1: duke@1: return response; duke@1: } catch (Exception ex) { duke@1: // TBD -- chaining? duke@1: throw new SOAPExceptionImpl(ex); duke@1: } else { duke@1: log.severe("SAAJ0007.p2p.bad.endPoint.type"); duke@1: throw new SOAPExceptionImpl("Bad endPoint type " + endPoint); duke@1: } duke@1: } duke@1: duke@1: static class PriviledgedPost implements PrivilegedExceptionAction { duke@1: duke@1: HttpSOAPConnection c; duke@1: SOAPMessage message; duke@1: URL endPoint; duke@1: duke@1: PriviledgedPost( duke@1: HttpSOAPConnection c, duke@1: SOAPMessage message, duke@1: URL endPoint) { duke@1: this.c = c; duke@1: this.message = message; duke@1: this.endPoint = endPoint; duke@1: } duke@1: duke@1: public Object run() throws Exception { duke@1: return c.post(message, endPoint); duke@1: } duke@1: } duke@1: duke@1: // TBD duke@1: // Fix this to do things better. duke@1: duke@1: private String proxyHost = null; duke@1: duke@1: static class PriviledgedSetProxyAction implements PrivilegedExceptionAction { duke@1: duke@1: String proxyHost = null; duke@1: int proxyPort = 0; duke@1: duke@1: PriviledgedSetProxyAction(String host, int port) { duke@1: this.proxyHost = host; duke@1: this.proxyPort = port; duke@1: } duke@1: duke@1: public Object run() throws Exception { duke@1: System.setProperty("http.proxyHost", proxyHost); duke@1: System.setProperty("http.proxyPort", new Integer(proxyPort).toString()); duke@1: log.log(Level.FINE, "SAAJ0050.p2p.proxy.host", duke@1: new String[] { proxyHost }); duke@1: log.log(Level.FINE, "SAAJ0051.p2p.proxy.port", duke@1: new String[] { new Integer(proxyPort).toString() }); duke@1: return proxyHost; duke@1: } duke@1: } duke@1: duke@1: duke@1: public void setProxy(String host, int port) { duke@1: try { duke@1: proxyPort = port; duke@1: PriviledgedSetProxyAction ps = new PriviledgedSetProxyAction(host, port); duke@1: proxyHost = (String) AccessController.doPrivileged(ps); duke@1: } catch (Exception e) { duke@1: throw new RuntimeException(e); duke@1: } duke@1: } duke@1: duke@1: public String getProxyHost() { duke@1: return proxyHost; duke@1: } duke@1: duke@1: private int proxyPort = -1; duke@1: duke@1: public int getProxyPort() { duke@1: return proxyPort; duke@1: } duke@1: duke@1: SOAPMessage post(SOAPMessage message, URL endPoint) throws SOAPException { duke@1: boolean isFailure = false; duke@1: duke@1: URL url = null; duke@1: HttpURLConnection httpConnection = null; duke@1: duke@1: int responseCode = 0; duke@1: try { duke@1: if (endPoint.getProtocol().equals("https")) duke@1: //if(!setHttps) duke@1: initHttps(); duke@1: // Process the URL duke@1: JaxmURI uri = new JaxmURI(endPoint.toString()); duke@1: String userInfo = uri.getUserinfo(); duke@1: duke@1: url = endPoint; duke@1: duke@1: if (dL > 0) duke@1: d("uri: " + userInfo + " " + url + " " + uri); duke@1: duke@1: // TBD duke@1: // Will deal with https later. duke@1: if (!url.getProtocol().equalsIgnoreCase("http") duke@1: && !url.getProtocol().equalsIgnoreCase("https")) { duke@1: log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https"); duke@1: throw new IllegalArgumentException( duke@1: "Protocol " duke@1: + url.getProtocol() duke@1: + " not supported in URL " duke@1: + url); duke@1: } duke@1: httpConnection = (HttpURLConnection) createConnection(url); duke@1: duke@1: httpConnection.setRequestMethod("POST"); duke@1: duke@1: httpConnection.setDoOutput(true); duke@1: httpConnection.setDoInput(true); duke@1: httpConnection.setUseCaches(false); duke@1: HttpURLConnection.setFollowRedirects(true); duke@1: duke@1: if (message.saveRequired()) duke@1: message.saveChanges(); duke@1: duke@1: MimeHeaders headers = message.getMimeHeaders(); duke@1: duke@1: Iterator it = headers.getAllHeaders(); duke@1: boolean hasAuth = false; // true if we find explicit Auth header duke@1: while (it.hasNext()) { duke@1: MimeHeader header = (MimeHeader) it.next(); duke@1: duke@1: String[] values = headers.getHeader(header.getName()); duke@1: duke@1: if (values.length == 1) duke@1: httpConnection.setRequestProperty( duke@1: header.getName(), duke@1: header.getValue()); duke@1: else { duke@1: StringBuffer concat = new StringBuffer(); duke@1: int i = 0; duke@1: while (i < values.length) { duke@1: if (i != 0) duke@1: concat.append(','); duke@1: concat.append(values[i]); duke@1: i++; duke@1: } duke@1: duke@1: httpConnection.setRequestProperty( duke@1: header.getName(), duke@1: concat.toString()); duke@1: } duke@1: duke@1: if ("Authorization".equals(header.getName())) { duke@1: hasAuth = true; duke@1: log.fine("SAAJ0091.p2p.https.auth.in.POST.true"); duke@1: } duke@1: } duke@1: duke@1: if (!hasAuth && userInfo != null) { duke@1: initAuthUserInfo(httpConnection, userInfo); duke@1: } duke@1: duke@1: OutputStream out = httpConnection.getOutputStream(); duke@1: message.writeTo(out); duke@1: duke@1: out.flush(); duke@1: out.close(); duke@1: duke@1: httpConnection.connect(); duke@1: duke@1: try { duke@1: duke@1: responseCode = httpConnection.getResponseCode(); duke@1: duke@1: // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults duke@1: if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { duke@1: isFailure = true; duke@1: } duke@1: //else if (responseCode != HttpURLConnection.HTTP_OK) duke@1: //else if (!(responseCode >= HttpURLConnection.HTTP_OK && responseCode < 207)) duke@1: else if ((responseCode / 100) != 2) { duke@1: log.log(Level.SEVERE, duke@1: "SAAJ0008.p2p.bad.response", duke@1: new String[] {httpConnection.getResponseMessage()}); duke@1: throw new SOAPExceptionImpl( duke@1: "Bad response: (" duke@1: + responseCode duke@1: + httpConnection.getResponseMessage()); duke@1: duke@1: } duke@1: } catch (IOException e) { duke@1: // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds! duke@1: responseCode = httpConnection.getResponseCode(); duke@1: if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { duke@1: isFailure = true; duke@1: } else { duke@1: throw e; duke@1: } duke@1: duke@1: } duke@1: duke@1: } catch (SOAPException ex) { duke@1: throw ex; duke@1: } catch (Exception ex) { duke@1: log.severe("SAAJ0009.p2p.msg.send.failed"); duke@1: throw new SOAPExceptionImpl("Message send failed", ex); duke@1: } duke@1: duke@1: SOAPMessage response = null; duke@1: if (responseCode == HttpURLConnection.HTTP_OK || isFailure) { duke@1: try { duke@1: MimeHeaders headers = new MimeHeaders(); duke@1: duke@1: String key, value; duke@1: duke@1: // Header field 0 is the status line so we skip it. duke@1: duke@1: int i = 1; duke@1: duke@1: while (true) { duke@1: key = httpConnection.getHeaderFieldKey(i); duke@1: value = httpConnection.getHeaderField(i); duke@1: duke@1: if (key == null && value == null) duke@1: break; duke@1: duke@1: if (key != null) { duke@1: StringTokenizer values = duke@1: new StringTokenizer(value, ","); duke@1: while (values.hasMoreTokens()) duke@1: headers.addHeader(key, values.nextToken().trim()); duke@1: } duke@1: i++; duke@1: } duke@1: duke@1: InputStream httpIn = duke@1: (isFailure duke@1: ? httpConnection.getErrorStream() duke@1: : httpConnection.getInputStream()); duke@1: duke@1: byte[] bytes = readFully(httpIn); duke@1: duke@1: int length = duke@1: httpConnection.getContentLength() == -1 duke@1: ? bytes.length duke@1: : httpConnection.getContentLength(); duke@1: duke@1: // If no reply message is returned, duke@1: // content-Length header field value is expected to be zero. duke@1: if (length == 0) { duke@1: response = null; duke@1: log.warning("SAAJ0014.p2p.content.zero"); duke@1: } else { duke@1: ByteInputStream in = new ByteInputStream(bytes, length); duke@1: response = messageFactory.createMessage(headers, in); duke@1: } duke@1: duke@1: httpIn.close(); duke@1: httpConnection.disconnect(); duke@1: duke@1: } catch (SOAPException ex) { duke@1: throw ex; duke@1: } catch (Exception ex) { duke@1: log.log(Level.SEVERE,"SAAJ0010.p2p.cannot.read.resp", ex); duke@1: throw new SOAPExceptionImpl( duke@1: "Unable to read response: " + ex.getMessage()); duke@1: } duke@1: } duke@1: return response; duke@1: } duke@1: duke@1: // Object identifies where the request should be sent. duke@1: // It is required to support objects of type String and java.net.URL. duke@1: duke@1: public SOAPMessage get(Object endPoint) throws SOAPException { duke@1: if (closed) { duke@1: log.severe("SAAJ0011.p2p.get.already.closed.conn"); duke@1: throw new SOAPExceptionImpl("Connection is closed"); duke@1: } duke@1: Class urlEndpointClass = null; duke@1: duke@1: try { duke@1: urlEndpointClass = Class.forName("javax.xml.messaging.URLEndpoint"); duke@1: } catch (Exception ex) { duke@1: //Do nothing. URLEndpoint is available only when JAXM is there. duke@1: } duke@1: duke@1: if (urlEndpointClass != null) { duke@1: if (urlEndpointClass.isInstance(endPoint)) { duke@1: String url = null; duke@1: duke@1: try { duke@1: Method m = urlEndpointClass.getMethod("getURL", (Class[])null); duke@1: url = (String) m.invoke(endPoint, (Object[])null); duke@1: } catch (Exception ex) { duke@1: log.severe("SAAJ0004.p2p.internal.err"); duke@1: throw new SOAPExceptionImpl( duke@1: "Internal error: " + ex.getMessage()); duke@1: } duke@1: try { duke@1: endPoint = new URL(url); duke@1: } catch (MalformedURLException mex) { duke@1: log.severe("SAAJ0005.p2p."); duke@1: throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage()); duke@1: } duke@1: } duke@1: } duke@1: duke@1: if (endPoint instanceof java.lang.String) { duke@1: try { duke@1: endPoint = new URL((String) endPoint); duke@1: } catch (MalformedURLException mex) { duke@1: log.severe("SAAJ0006.p2p.bad.URL"); duke@1: throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage()); duke@1: } duke@1: } duke@1: duke@1: if (endPoint instanceof URL) duke@1: try { duke@1: PriviledgedGet pg = new PriviledgedGet(this, (URL) endPoint); duke@1: SOAPMessage response = duke@1: (SOAPMessage) AccessController.doPrivileged(pg); duke@1: duke@1: return response; duke@1: } catch (Exception ex) { duke@1: throw new SOAPExceptionImpl(ex); duke@1: } else duke@1: throw new SOAPExceptionImpl("Bad endPoint type " + endPoint); duke@1: } duke@1: duke@1: static class PriviledgedGet implements PrivilegedExceptionAction { duke@1: duke@1: HttpSOAPConnection c; duke@1: URL endPoint; duke@1: duke@1: PriviledgedGet(HttpSOAPConnection c, URL endPoint) { duke@1: this.c = c; duke@1: this.endPoint = endPoint; duke@1: } duke@1: duke@1: public Object run() throws Exception { duke@1: return c.get(endPoint); duke@1: } duke@1: } duke@1: duke@1: SOAPMessage get(URL endPoint) throws SOAPException { duke@1: boolean isFailure = false; duke@1: duke@1: URL url = null; duke@1: HttpURLConnection httpConnection = null; duke@1: duke@1: int responseCode = 0; duke@1: try { duke@1: /// Is https GET allowed?? duke@1: if (endPoint.getProtocol().equals("https")) duke@1: initHttps(); duke@1: // Process the URL duke@1: JaxmURI uri = new JaxmURI(endPoint.toString()); duke@1: String userInfo = uri.getUserinfo(); duke@1: duke@1: url = endPoint; duke@1: duke@1: if (dL > 0) duke@1: d("uri: " + userInfo + " " + url + " " + uri); duke@1: duke@1: // TBD duke@1: // Will deal with https later. duke@1: if (!url.getProtocol().equalsIgnoreCase("http") duke@1: && !url.getProtocol().equalsIgnoreCase("https")) { duke@1: log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https"); duke@1: throw new IllegalArgumentException( duke@1: "Protocol " duke@1: + url.getProtocol() duke@1: + " not supported in URL " duke@1: + url); duke@1: } duke@1: httpConnection = (HttpURLConnection) createConnection(url); duke@1: duke@1: httpConnection.setRequestMethod("GET"); duke@1: duke@1: httpConnection.setDoOutput(true); duke@1: httpConnection.setDoInput(true); duke@1: httpConnection.setUseCaches(false); duke@1: HttpURLConnection.setFollowRedirects(true); duke@1: duke@1: httpConnection.connect(); duke@1: duke@1: try { duke@1: duke@1: responseCode = httpConnection.getResponseCode(); duke@1: duke@1: // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults duke@1: if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { duke@1: isFailure = true; duke@1: } else if ((responseCode / 100) != 2) { duke@1: log.log(Level.SEVERE, duke@1: "SAAJ0008.p2p.bad.response", duke@1: new String[] { httpConnection.getResponseMessage()}); duke@1: throw new SOAPExceptionImpl( duke@1: "Bad response: (" duke@1: + responseCode duke@1: + httpConnection.getResponseMessage()); duke@1: duke@1: } duke@1: } catch (IOException e) { duke@1: // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds! duke@1: responseCode = httpConnection.getResponseCode(); duke@1: if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { duke@1: isFailure = true; duke@1: } else { duke@1: throw e; duke@1: } duke@1: duke@1: } duke@1: duke@1: } catch (SOAPException ex) { duke@1: throw ex; duke@1: } catch (Exception ex) { duke@1: log.severe("SAAJ0012.p2p.get.failed"); duke@1: throw new SOAPExceptionImpl("Get failed", ex); duke@1: } duke@1: duke@1: SOAPMessage response = null; duke@1: if (responseCode == HttpURLConnection.HTTP_OK || isFailure) { duke@1: try { duke@1: MimeHeaders headers = new MimeHeaders(); duke@1: duke@1: String key, value; duke@1: duke@1: // Header field 0 is the status line so we skip it. duke@1: duke@1: int i = 1; duke@1: duke@1: while (true) { duke@1: key = httpConnection.getHeaderFieldKey(i); duke@1: value = httpConnection.getHeaderField(i); duke@1: duke@1: if (key == null && value == null) duke@1: break; duke@1: duke@1: if (key != null) { duke@1: StringTokenizer values = duke@1: new StringTokenizer(value, ","); duke@1: while (values.hasMoreTokens()) duke@1: headers.addHeader(key, values.nextToken().trim()); duke@1: } duke@1: i++; duke@1: } duke@1: duke@1: InputStream httpIn = duke@1: (isFailure duke@1: ? httpConnection.getErrorStream() duke@1: : httpConnection.getInputStream()); duke@1: duke@1: byte[] bytes = readFully(httpIn); duke@1: duke@1: int length = duke@1: httpConnection.getContentLength() == -1 duke@1: ? bytes.length duke@1: : httpConnection.getContentLength(); duke@1: duke@1: // If no reply message is returned, duke@1: // content-Length header field value is expected to be zero. duke@1: if (length == 0) { duke@1: response = null; duke@1: log.warning("SAAJ0014.p2p.content.zero"); duke@1: } else { duke@1: duke@1: ByteInputStream in = new ByteInputStream(bytes, length); duke@1: response = messageFactory.createMessage(headers, in); duke@1: } duke@1: duke@1: httpIn.close(); duke@1: httpConnection.disconnect(); duke@1: duke@1: } catch (SOAPException ex) { duke@1: throw ex; duke@1: } catch (Exception ex) { duke@1: log.log(Level.SEVERE, duke@1: "SAAJ0010.p2p.cannot.read.resp", duke@1: ex); duke@1: throw new SOAPExceptionImpl( duke@1: "Unable to read response: " + ex.getMessage()); duke@1: } duke@1: } duke@1: return response; duke@1: } duke@1: duke@1: private byte[] readFully(InputStream istream) throws IOException { duke@1: ByteArrayOutputStream bout = new ByteArrayOutputStream(); duke@1: byte[] buf = new byte[1024]; duke@1: int num = 0; duke@1: duke@1: while ((num = istream.read(buf)) != -1) { duke@1: bout.write(buf, 0, num); duke@1: } duke@1: duke@1: byte[] ret = bout.toByteArray(); duke@1: duke@1: return ret; duke@1: } tbell@50: //private static String SSL_PKG = "com.sun.net.ssl.internal.www.protocol"; tbell@50: //private static String SSL_PROVIDER = tbell@50: // "com.sun.net.ssl.internal.ssl.Provider"; tbell@50: private static final String SSL_PKG; tbell@50: private static final String SSL_PROVIDER; duke@1: tbell@50: tbell@50: static { tbell@50: if (isIBMVM) { tbell@50: SSL_PKG ="com.ibm.net.ssl.internal.www.protocol"; tbell@50: SSL_PROVIDER ="com.ibm.net.ssl.internal.ssl.Provider"; tbell@50: } else { tbell@50: //if not IBM VM default to Sun. tbell@50: SSL_PKG = "com.sun.net.ssl.internal.www.protocol"; tbell@50: SSL_PROVIDER ="com.sun.net.ssl.internal.ssl.Provider"; tbell@50: } tbell@50: } duke@1: private void initHttps() { duke@1: //if(!setHttps) { duke@1: String pkgs = System.getProperty("java.protocol.handler.pkgs"); duke@1: log.log(Level.FINE, duke@1: "SAAJ0053.p2p.providers", duke@1: new String[] { pkgs }); duke@1: duke@1: if (pkgs == null || pkgs.indexOf(SSL_PKG) < 0) { duke@1: if (pkgs == null) duke@1: pkgs = SSL_PKG; duke@1: else duke@1: pkgs = pkgs + "|" + SSL_PKG; duke@1: System.setProperty("java.protocol.handler.pkgs", pkgs); duke@1: log.log(Level.FINE, duke@1: "SAAJ0054.p2p.set.providers", duke@1: new String[] { pkgs }); duke@1: try { duke@1: Class c = Class.forName(SSL_PROVIDER); duke@1: Provider p = (Provider) c.newInstance(); duke@1: Security.addProvider(p); duke@1: log.log(Level.FINE, duke@1: "SAAJ0055.p2p.added.ssl.provider", duke@1: new String[] { SSL_PROVIDER }); duke@1: //System.out.println("Added SSL_PROVIDER " + SSL_PROVIDER); duke@1: //setHttps = true; duke@1: } catch (Exception ex) { duke@1: } duke@1: } duke@1: //} duke@1: } duke@1: duke@1: private void initAuthUserInfo(HttpURLConnection conn, String userInfo) { duke@1: String user; duke@1: String password; duke@1: if (userInfo != null) { // get the user and password duke@1: //System.out.println("UserInfo= " + userInfo ); duke@1: int delimiter = userInfo.indexOf(':'); duke@1: if (delimiter == -1) { duke@1: user = ParseUtil.decode(userInfo); duke@1: password = null; duke@1: } else { duke@1: user = ParseUtil.decode(userInfo.substring(0, delimiter++)); duke@1: password = ParseUtil.decode(userInfo.substring(delimiter)); duke@1: } duke@1: duke@1: String plain = user + ":"; duke@1: byte[] nameBytes = plain.getBytes(); duke@1: byte[] passwdBytes = password.getBytes(); duke@1: duke@1: // concatenate user name and password bytes and encode them duke@1: byte[] concat = new byte[nameBytes.length + passwdBytes.length]; duke@1: duke@1: System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length); duke@1: System.arraycopy( duke@1: passwdBytes, duke@1: 0, duke@1: concat, duke@1: nameBytes.length, duke@1: passwdBytes.length); duke@1: String auth = "Basic " + new String(Base64.encode(concat)); duke@1: conn.setRequestProperty("Authorization", auth); duke@1: if (dL > 0) duke@1: d("Adding auth " + auth); duke@1: } duke@1: } duke@1: duke@1: private static final int dL = 0; duke@1: private void d(String s) { duke@1: log.log(Level.SEVERE, duke@1: "SAAJ0013.p2p.HttpSOAPConnection", duke@1: new String[] { s }); duke@1: System.err.println("HttpSOAPConnection: " + s); duke@1: } duke@1: duke@1: private java.net.HttpURLConnection createConnection(URL endpoint) duke@1: throws IOException { duke@1: return (HttpURLConnection) endpoint.openConnection(); duke@1: } duke@1: duke@1: }