001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: HttpSOAPConnection.java,v 1.5 2007/11/22 10:19:18 kumarjayanti Exp $
022: * $Revision: 1.5 $
023: * $Date: 2007/11/22 10:19:18 $
024: */
025:
026: /*
027: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
028: *
029: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
030: *
031: * The contents of this file are subject to the terms of either the GNU
032: * General Public License Version 2 only ("GPL") or the Common Development
033: * and Distribution License("CDDL") (collectively, the "License"). You
034: * may not use this file except in compliance with the License. You can obtain
035: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
036: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
037: * language governing permissions and limitations under the License.
038: *
039: * When distributing the software, include this License Header Notice in each
040: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
041: * Sun designates this particular file as subject to the "Classpath" exception
042: * as provided by Sun in the GPL Version 2 section of the License file that
043: * accompanied this code. If applicable, add the following below the License
044: * Header, with the fields enclosed by brackets [] replaced by your own
045: * identifying information: "Portions Copyrighted [year]
046: * [name of copyright owner]"
047: *
048: * Contributor(s):
049: *
050: * If you wish your version of this file to be governed by only the CDDL or
051: * only the GPL Version 2, indicate your decision by adding "[Contributor]
052: * elects to include this software in this distribution under the [CDDL or GPL
053: * Version 2] license." If you don't indicate a single choice of license, a
054: * recipient has the option to distribute your version of this file under
055: * either the CDDL, the GPL Version 2 or to extend the choice of license to
056: * its licensees as provided above. However, if you add GPL Version 2 code
057: * and therefore, elected the GPL Version 2 license, then the option applies
058: * only if the new code is made subject to such option by the copyright
059: * holder.
060: */
061: package com.sun.xml.messaging.saaj.client.p2p;
062:
063: import java.io.*;
064: import java.lang.reflect.Method;
065: import java.net.*;
066: import java.security.*;
067: import java.util.Iterator;
068: import java.util.StringTokenizer;
069: import java.util.logging.Level;
070: import java.util.logging.Logger;
071:
072: import javax.xml.soap.*;
073:
074: import com.sun.xml.messaging.saaj.SOAPExceptionImpl;
075: import com.sun.xml.messaging.saaj.util.*;
076:
077: /**
078: * This represents a "connection" to the simple HTTP-based provider.
079: *
080: * @author Anil Vijendran (akv@eng.sun.com)
081: * @author Rajiv Mordani (rajiv.mordani@sun.com)
082: * @author Manveen Kaur (manveen.kaur@sun.com)
083: *
084: */
085: public class HttpSOAPConnection extends SOAPConnection {
086:
087: public static final String vmVendor = System
088: .getProperty("java.vendor.url");
089: private static final String sunVmVendor = "http://java.sun.com/";
090: private static final String ibmVmVendor = "http://www.ibm.com/";
091: private static final boolean isSunVM = sunVmVendor.equals(vmVendor) ? true
092: : false;
093: private static final boolean isIBMVM = ibmVmVendor.equals(vmVendor) ? true
094: : false;
095: private static final String JAXM_URLENDPOINT = "javax.xml.messaging.URLEndpoint";
096:
097: protected static Logger log = Logger.getLogger(
098: LogDomainConstants.HTTP_CONN_DOMAIN,
099: "com.sun.xml.messaging.saaj.client.p2p.LocalStrings");
100:
101: public static String defaultProxyHost = null;
102: public static int defaultProxyPort = -1;
103:
104: MessageFactory messageFactory = null;
105:
106: boolean closed = false;
107:
108: public HttpSOAPConnection() throws SOAPException {
109: proxyHost = defaultProxyHost;
110: proxyPort = defaultProxyPort;
111:
112: try {
113: messageFactory = MessageFactory
114: .newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL);
115: } catch (NoSuchMethodError ex) {
116: //fallback to default SOAP 1.1 in this case for backward compatibility
117: messageFactory = MessageFactory.newInstance();
118: } catch (Exception ex) {
119: log.log(Level.SEVERE,
120: "SAAJ0001.p2p.cannot.create.msg.factory", ex);
121: throw new SOAPExceptionImpl(
122: "Unable to create message factory", ex);
123: }
124: }
125:
126: public void close() throws SOAPException {
127: if (closed) {
128: log.severe("SAAJ0002.p2p.close.already.closed.conn");
129: throw new SOAPExceptionImpl("Connection already closed");
130: }
131:
132: messageFactory = null;
133: closed = true;
134: }
135:
136: public SOAPMessage call(SOAPMessage message, Object endPoint)
137: throws SOAPException {
138: if (closed) {
139: log.severe("SAAJ0003.p2p.call.already.closed.conn");
140: throw new SOAPExceptionImpl("Connection is closed");
141: }
142:
143: Class urlEndpointClass = null;
144: ClassLoader loader = Thread.currentThread()
145: .getContextClassLoader();
146: try {
147: if (loader != null) {
148: urlEndpointClass = loader.loadClass(JAXM_URLENDPOINT);
149: } else {
150: urlEndpointClass = Class.forName(JAXM_URLENDPOINT);
151: }
152: } catch (ClassNotFoundException ex) {
153: //Do nothing. URLEndpoint is available only when JAXM is there.
154: log.finest("SAAJ0090.p2p.endpoint.available.only.for.JAXM");
155: }
156:
157: if (urlEndpointClass != null) {
158: if (urlEndpointClass.isInstance(endPoint)) {
159: String url = null;
160:
161: try {
162: Method m = urlEndpointClass.getMethod("getURL",
163: (Class[]) null);
164: url = (String) m.invoke(endPoint, (Object[]) null);
165: } catch (Exception ex) {
166: // TBD -- exception chaining
167: log.log(Level.SEVERE, "SAAJ0004.p2p.internal.err",
168: ex);
169: throw new SOAPExceptionImpl("Internal error: "
170: + ex.getMessage());
171: }
172: try {
173: endPoint = new URL(url);
174: } catch (MalformedURLException mex) {
175: log.log(Level.SEVERE, "SAAJ0005.p2p.", mex);
176: throw new SOAPExceptionImpl("Bad URL: "
177: + mex.getMessage());
178: }
179: }
180: }
181:
182: if (endPoint instanceof java.lang.String) {
183: try {
184: endPoint = new URL((String) endPoint);
185: } catch (MalformedURLException mex) {
186: log.log(Level.SEVERE, "SAAJ0006.p2p.bad.URL", mex);
187: throw new SOAPExceptionImpl("Bad URL: "
188: + mex.getMessage());
189: }
190: }
191:
192: if (endPoint instanceof URL)
193: try {
194: PriviledgedPost pp = new PriviledgedPost(this , message,
195: (URL) endPoint);
196: SOAPMessage response = (SOAPMessage) AccessController
197: .doPrivileged(pp);
198:
199: return response;
200: } catch (Exception ex) {
201: // TBD -- chaining?
202: throw new SOAPExceptionImpl(ex);
203: }
204: else {
205: log.severe("SAAJ0007.p2p.bad.endPoint.type");
206: throw new SOAPExceptionImpl("Bad endPoint type " + endPoint);
207: }
208: }
209:
210: static class PriviledgedPost implements PrivilegedExceptionAction {
211:
212: HttpSOAPConnection c;
213: SOAPMessage message;
214: URL endPoint;
215:
216: PriviledgedPost(HttpSOAPConnection c, SOAPMessage message,
217: URL endPoint) {
218: this .c = c;
219: this .message = message;
220: this .endPoint = endPoint;
221: }
222:
223: public Object run() throws Exception {
224: return c.post(message, endPoint);
225: }
226: }
227:
228: // TBD
229: // Fix this to do things better.
230:
231: private String proxyHost = null;
232:
233: static class PriviledgedSetProxyAction implements
234: PrivilegedExceptionAction {
235:
236: String proxyHost = null;
237: int proxyPort = 0;
238:
239: PriviledgedSetProxyAction(String host, int port) {
240: this .proxyHost = host;
241: this .proxyPort = port;
242: }
243:
244: public Object run() throws Exception {
245: System.setProperty("http.proxyHost", proxyHost);
246: System.setProperty("http.proxyPort", new Integer(proxyPort)
247: .toString());
248: log.log(Level.FINE, "SAAJ0050.p2p.proxy.host",
249: new String[] { proxyHost });
250: log.log(Level.FINE, "SAAJ0051.p2p.proxy.port",
251: new String[] { new Integer(proxyPort).toString() });
252: return proxyHost;
253: }
254: }
255:
256: public void setProxy(String host, int port) {
257: try {
258: proxyPort = port;
259: PriviledgedSetProxyAction ps = new PriviledgedSetProxyAction(
260: host, port);
261: proxyHost = (String) AccessController.doPrivileged(ps);
262: } catch (Exception e) {
263: throw new RuntimeException(e);
264: }
265: }
266:
267: public String getProxyHost() {
268: return proxyHost;
269: }
270:
271: private int proxyPort = -1;
272:
273: public int getProxyPort() {
274: return proxyPort;
275: }
276:
277: SOAPMessage post(SOAPMessage message, URL endPoint)
278: throws SOAPException {
279: boolean isFailure = false;
280:
281: URL url = null;
282: HttpURLConnection httpConnection = null;
283:
284: int responseCode = 0;
285: try {
286: if (endPoint.getProtocol().equals("https"))
287: //if(!setHttps)
288: initHttps();
289: // Process the URL
290: JaxmURI uri = new JaxmURI(endPoint.toString());
291: String userInfo = uri.getUserinfo();
292:
293: url = endPoint;
294:
295: if (dL > 0)
296: d("uri: " + userInfo + " " + url + " " + uri);
297:
298: // TBD
299: // Will deal with https later.
300: if (!url.getProtocol().equalsIgnoreCase("http")
301: && !url.getProtocol().equalsIgnoreCase("https")) {
302: log
303: .severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
304: throw new IllegalArgumentException("Protocol "
305: + url.getProtocol() + " not supported in URL "
306: + url);
307: }
308: httpConnection = (HttpURLConnection) createConnection(url);
309:
310: httpConnection.setRequestMethod("POST");
311:
312: httpConnection.setDoOutput(true);
313: httpConnection.setDoInput(true);
314: httpConnection.setUseCaches(false);
315: HttpURLConnection.setFollowRedirects(true);
316:
317: if (message.saveRequired())
318: message.saveChanges();
319:
320: MimeHeaders headers = message.getMimeHeaders();
321:
322: Iterator it = headers.getAllHeaders();
323: boolean hasAuth = false; // true if we find explicit Auth header
324: while (it.hasNext()) {
325: MimeHeader header = (MimeHeader) it.next();
326:
327: String[] values = headers.getHeader(header.getName());
328:
329: if (values.length == 1)
330: httpConnection.setRequestProperty(header.getName(),
331: header.getValue());
332: else {
333: StringBuffer concat = new StringBuffer();
334: int i = 0;
335: while (i < values.length) {
336: if (i != 0)
337: concat.append(',');
338: concat.append(values[i]);
339: i++;
340: }
341:
342: httpConnection.setRequestProperty(header.getName(),
343: concat.toString());
344: }
345:
346: if ("Authorization".equals(header.getName())) {
347: hasAuth = true;
348: log.fine("SAAJ0091.p2p.https.auth.in.POST.true");
349: }
350: }
351:
352: if (!hasAuth && userInfo != null) {
353: initAuthUserInfo(httpConnection, userInfo);
354: }
355:
356: OutputStream out = httpConnection.getOutputStream();
357: message.writeTo(out);
358:
359: out.flush();
360: out.close();
361:
362: httpConnection.connect();
363:
364: try {
365:
366: responseCode = httpConnection.getResponseCode();
367:
368: // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
369: if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
370: isFailure = true;
371: }
372: //else if (responseCode != HttpURLConnection.HTTP_OK)
373: //else if (!(responseCode >= HttpURLConnection.HTTP_OK && responseCode < 207))
374: else if ((responseCode / 100) != 2) {
375: log.log(Level.SEVERE, "SAAJ0008.p2p.bad.response",
376: new String[] { httpConnection
377: .getResponseMessage() });
378: throw new SOAPExceptionImpl("Bad response: ("
379: + responseCode
380: + httpConnection.getResponseMessage());
381:
382: }
383: } catch (IOException e) {
384: // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds!
385: responseCode = httpConnection.getResponseCode();
386: if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
387: isFailure = true;
388: } else {
389: throw e;
390: }
391:
392: }
393:
394: } catch (SOAPException ex) {
395: throw ex;
396: } catch (Exception ex) {
397: log.severe("SAAJ0009.p2p.msg.send.failed");
398: throw new SOAPExceptionImpl("Message send failed", ex);
399: }
400:
401: SOAPMessage response = null;
402: if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
403: try {
404: MimeHeaders headers = new MimeHeaders();
405:
406: String key, value;
407:
408: // Header field 0 is the status line so we skip it.
409:
410: int i = 1;
411:
412: while (true) {
413: key = httpConnection.getHeaderFieldKey(i);
414: value = httpConnection.getHeaderField(i);
415:
416: if (key == null && value == null)
417: break;
418:
419: if (key != null) {
420: StringTokenizer values = new StringTokenizer(
421: value, ",");
422: while (values.hasMoreTokens())
423: headers.addHeader(key, values.nextToken()
424: .trim());
425: }
426: i++;
427: }
428:
429: InputStream httpIn = (isFailure ? httpConnection
430: .getErrorStream() : httpConnection
431: .getInputStream());
432:
433: byte[] bytes = readFully(httpIn);
434:
435: int length = httpConnection.getContentLength() == -1 ? bytes.length
436: : httpConnection.getContentLength();
437:
438: // If no reply message is returned,
439: // content-Length header field value is expected to be zero.
440: if (length == 0) {
441: response = null;
442: log.warning("SAAJ0014.p2p.content.zero");
443: } else {
444: ByteInputStream in = new ByteInputStream(bytes,
445: length);
446: response = messageFactory
447: .createMessage(headers, in);
448: }
449:
450: httpIn.close();
451: httpConnection.disconnect();
452:
453: } catch (SOAPException ex) {
454: throw ex;
455: } catch (Exception ex) {
456: log.log(Level.SEVERE, "SAAJ0010.p2p.cannot.read.resp",
457: ex);
458: throw new SOAPExceptionImpl("Unable to read response: "
459: + ex.getMessage());
460: }
461: }
462: return response;
463: }
464:
465: // Object identifies where the request should be sent.
466: // It is required to support objects of type String and java.net.URL.
467:
468: public SOAPMessage get(Object endPoint) throws SOAPException {
469: if (closed) {
470: log.severe("SAAJ0011.p2p.get.already.closed.conn");
471: throw new SOAPExceptionImpl("Connection is closed");
472: }
473: Class urlEndpointClass = null;
474:
475: try {
476: urlEndpointClass = Class
477: .forName("javax.xml.messaging.URLEndpoint");
478: } catch (Exception ex) {
479: //Do nothing. URLEndpoint is available only when JAXM is there.
480: }
481:
482: if (urlEndpointClass != null) {
483: if (urlEndpointClass.isInstance(endPoint)) {
484: String url = null;
485:
486: try {
487: Method m = urlEndpointClass.getMethod("getURL",
488: (Class[]) null);
489: url = (String) m.invoke(endPoint, (Object[]) null);
490: } catch (Exception ex) {
491: log.severe("SAAJ0004.p2p.internal.err");
492: throw new SOAPExceptionImpl("Internal error: "
493: + ex.getMessage());
494: }
495: try {
496: endPoint = new URL(url);
497: } catch (MalformedURLException mex) {
498: log.severe("SAAJ0005.p2p.");
499: throw new SOAPExceptionImpl("Bad URL: "
500: + mex.getMessage());
501: }
502: }
503: }
504:
505: if (endPoint instanceof java.lang.String) {
506: try {
507: endPoint = new URL((String) endPoint);
508: } catch (MalformedURLException mex) {
509: log.severe("SAAJ0006.p2p.bad.URL");
510: throw new SOAPExceptionImpl("Bad URL: "
511: + mex.getMessage());
512: }
513: }
514:
515: if (endPoint instanceof URL)
516: try {
517: PriviledgedGet pg = new PriviledgedGet(this ,
518: (URL) endPoint);
519: SOAPMessage response = (SOAPMessage) AccessController
520: .doPrivileged(pg);
521:
522: return response;
523: } catch (Exception ex) {
524: throw new SOAPExceptionImpl(ex);
525: }
526: else
527: throw new SOAPExceptionImpl("Bad endPoint type " + endPoint);
528: }
529:
530: static class PriviledgedGet implements PrivilegedExceptionAction {
531:
532: HttpSOAPConnection c;
533: URL endPoint;
534:
535: PriviledgedGet(HttpSOAPConnection c, URL endPoint) {
536: this .c = c;
537: this .endPoint = endPoint;
538: }
539:
540: public Object run() throws Exception {
541: return c.get(endPoint);
542: }
543: }
544:
545: SOAPMessage get(URL endPoint) throws SOAPException {
546: boolean isFailure = false;
547:
548: URL url = null;
549: HttpURLConnection httpConnection = null;
550:
551: int responseCode = 0;
552: try {
553: /// Is https GET allowed??
554: if (endPoint.getProtocol().equals("https"))
555: initHttps();
556: // Process the URL
557: JaxmURI uri = new JaxmURI(endPoint.toString());
558: String userInfo = uri.getUserinfo();
559:
560: url = endPoint;
561:
562: if (dL > 0)
563: d("uri: " + userInfo + " " + url + " " + uri);
564:
565: // TBD
566: // Will deal with https later.
567: if (!url.getProtocol().equalsIgnoreCase("http")
568: && !url.getProtocol().equalsIgnoreCase("https")) {
569: log
570: .severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
571: throw new IllegalArgumentException("Protocol "
572: + url.getProtocol() + " not supported in URL "
573: + url);
574: }
575: httpConnection = (HttpURLConnection) createConnection(url);
576:
577: httpConnection.setRequestMethod("GET");
578:
579: httpConnection.setDoOutput(true);
580: httpConnection.setDoInput(true);
581: httpConnection.setUseCaches(false);
582: HttpURLConnection.setFollowRedirects(true);
583:
584: httpConnection.connect();
585:
586: try {
587:
588: responseCode = httpConnection.getResponseCode();
589:
590: // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
591: if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
592: isFailure = true;
593: } else if ((responseCode / 100) != 2) {
594: log.log(Level.SEVERE, "SAAJ0008.p2p.bad.response",
595: new String[] { httpConnection
596: .getResponseMessage() });
597: throw new SOAPExceptionImpl("Bad response: ("
598: + responseCode
599: + httpConnection.getResponseMessage());
600:
601: }
602: } catch (IOException e) {
603: // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds!
604: responseCode = httpConnection.getResponseCode();
605: if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
606: isFailure = true;
607: } else {
608: throw e;
609: }
610:
611: }
612:
613: } catch (SOAPException ex) {
614: throw ex;
615: } catch (Exception ex) {
616: log.severe("SAAJ0012.p2p.get.failed");
617: throw new SOAPExceptionImpl("Get failed", ex);
618: }
619:
620: SOAPMessage response = null;
621: if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
622: try {
623: MimeHeaders headers = new MimeHeaders();
624:
625: String key, value;
626:
627: // Header field 0 is the status line so we skip it.
628:
629: int i = 1;
630:
631: while (true) {
632: key = httpConnection.getHeaderFieldKey(i);
633: value = httpConnection.getHeaderField(i);
634:
635: if (key == null && value == null)
636: break;
637:
638: if (key != null) {
639: StringTokenizer values = new StringTokenizer(
640: value, ",");
641: while (values.hasMoreTokens())
642: headers.addHeader(key, values.nextToken()
643: .trim());
644: }
645: i++;
646: }
647:
648: InputStream httpIn = (isFailure ? httpConnection
649: .getErrorStream() : httpConnection
650: .getInputStream());
651:
652: byte[] bytes = readFully(httpIn);
653:
654: int length = httpConnection.getContentLength() == -1 ? bytes.length
655: : httpConnection.getContentLength();
656:
657: // If no reply message is returned,
658: // content-Length header field value is expected to be zero.
659: if (length == 0) {
660: response = null;
661: log.warning("SAAJ0014.p2p.content.zero");
662: } else {
663:
664: ByteInputStream in = new ByteInputStream(bytes,
665: length);
666: response = messageFactory
667: .createMessage(headers, in);
668: }
669:
670: httpIn.close();
671: httpConnection.disconnect();
672:
673: } catch (SOAPException ex) {
674: throw ex;
675: } catch (Exception ex) {
676: log.log(Level.SEVERE, "SAAJ0010.p2p.cannot.read.resp",
677: ex);
678: throw new SOAPExceptionImpl("Unable to read response: "
679: + ex.getMessage());
680: }
681: }
682: return response;
683: }
684:
685: private byte[] readFully(InputStream istream) throws IOException {
686: ByteArrayOutputStream bout = new ByteArrayOutputStream();
687: byte[] buf = new byte[1024];
688: int num = 0;
689:
690: while ((num = istream.read(buf)) != -1) {
691: bout.write(buf, 0, num);
692: }
693:
694: byte[] ret = bout.toByteArray();
695:
696: return ret;
697: }
698:
699: //private static String SSL_PKG = "com.sun.net.ssl.internal.www.protocol";
700: //private static String SSL_PROVIDER =
701: // "com.sun.net.ssl.internal.ssl.Provider";
702: private static final String SSL_PKG;
703: private static final String SSL_PROVIDER;
704:
705: static {
706: if (isIBMVM) {
707: SSL_PKG = "com.ibm.net.ssl.internal.www.protocol";
708: SSL_PROVIDER = "com.ibm.net.ssl.internal.ssl.Provider";
709: } else {
710: //if not IBM VM default to Sun.
711: SSL_PKG = "com.sun.net.ssl.internal.www.protocol";
712: SSL_PROVIDER = "com.sun.net.ssl.internal.ssl.Provider";
713: }
714: }
715:
716: private void initHttps() {
717: //if(!setHttps) {
718: String pkgs = System.getProperty("java.protocol.handler.pkgs");
719: log.log(Level.FINE, "SAAJ0053.p2p.providers",
720: new String[] { pkgs });
721:
722: if (pkgs == null || pkgs.indexOf(SSL_PKG) < 0) {
723: if (pkgs == null)
724: pkgs = SSL_PKG;
725: else
726: pkgs = pkgs + "|" + SSL_PKG;
727: System.setProperty("java.protocol.handler.pkgs", pkgs);
728: log.log(Level.FINE, "SAAJ0054.p2p.set.providers",
729: new String[] { pkgs });
730: try {
731: Class c = Class.forName(SSL_PROVIDER);
732: Provider p = (Provider) c.newInstance();
733: Security.addProvider(p);
734: log.log(Level.FINE, "SAAJ0055.p2p.added.ssl.provider",
735: new String[] { SSL_PROVIDER });
736: //System.out.println("Added SSL_PROVIDER " + SSL_PROVIDER);
737: //setHttps = true;
738: } catch (Exception ex) {
739: }
740: }
741: //}
742: }
743:
744: private void initAuthUserInfo(HttpURLConnection conn,
745: String userInfo) {
746: String user;
747: String password;
748: if (userInfo != null) { // get the user and password
749: //System.out.println("UserInfo= " + userInfo );
750: int delimiter = userInfo.indexOf(':');
751: if (delimiter == -1) {
752: user = ParseUtil.decode(userInfo);
753: password = null;
754: } else {
755: user = ParseUtil.decode(userInfo.substring(0,
756: delimiter++));
757: password = ParseUtil.decode(userInfo
758: .substring(delimiter));
759: }
760:
761: String plain = user + ":";
762: byte[] nameBytes = plain.getBytes();
763: byte[] passwdBytes = password.getBytes();
764:
765: // concatenate user name and password bytes and encode them
766: byte[] concat = new byte[nameBytes.length
767: + passwdBytes.length];
768:
769: System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length);
770: System.arraycopy(passwdBytes, 0, concat, nameBytes.length,
771: passwdBytes.length);
772: String auth = "Basic " + new String(Base64.encode(concat));
773: conn.setRequestProperty("Authorization", auth);
774: if (dL > 0)
775: d("Adding auth " + auth);
776: }
777: }
778:
779: private static final int dL = 0;
780:
781: private void d(String s) {
782: log.log(Level.SEVERE, "SAAJ0013.p2p.HttpSOAPConnection",
783: new String[] { s });
784: System.err.println("HttpSOAPConnection: " + s);
785: }
786:
787: private java.net.HttpURLConnection createConnection(URL endpoint)
788: throws IOException {
789: return (HttpURLConnection) endpoint.openConnection();
790: }
791:
792: }
|