001: /*
002: * $Id: URLConnector.java,v 1.3 2003/10/29 21:21:58 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.base.util;
025:
026: import java.io.IOException;
027: import java.net.HttpURLConnection;
028: import java.net.URL;
029: import java.net.URLConnection;
030: import java.security.GeneralSecurityException;
031:
032: import javax.net.ssl.*;
033:
034: /**
035: * URLConnector.java
036: *
037: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
038: * @version $Revision: 1.3 $
039: * @since 2.0
040: */
041: public class URLConnector {
042:
043: public static final String module = URLConnector.class.getName();
044:
045: private URLConnection connection = null;
046: private URL url = null;
047: private String clientCertAlias = null;
048: private boolean timedOut = false;
049:
050: protected URLConnector() {
051: }
052:
053: protected URLConnector(URL url, String clientCertAlias) {
054: this .clientCertAlias = clientCertAlias;
055: this .url = url;
056: }
057:
058: protected synchronized URLConnection openConnection(int timeout)
059: throws IOException {
060: Thread t = new Thread(new URLConnectorThread());
061: t.start();
062:
063: try {
064: this .wait(timeout);
065: } catch (InterruptedException e) {
066: if (connection == null) {
067: timedOut = true;
068: } else {
069: close(connection);
070: }
071: throw new IOException("Connection never established");
072: }
073:
074: if (connection != null) {
075: return connection;
076: } else {
077: timedOut = true;
078: throw new IOException("Connection timed out");
079: }
080: }
081:
082: public static URLConnection openConnection(URL url)
083: throws IOException {
084: return openConnection(url, 30000);
085: }
086:
087: public static URLConnection openConnection(URL url, int timeout)
088: throws IOException {
089: return openConnection(url, timeout, null);
090: }
091:
092: public static URLConnection openConnection(URL url,
093: String clientCertAlias) throws IOException {
094: return openConnection(url, 30000, clientCertAlias);
095: }
096:
097: public static URLConnection openConnection(URL url, int timeout,
098: String clientCertAlias) throws IOException {
099: URLConnector uc = new URLConnector(url, clientCertAlias);
100: return uc.openConnection(timeout);
101: }
102:
103: // special thread to open the connection
104: private class URLConnectorThread implements Runnable {
105: public void run() {
106: URLConnection con = null;
107: try {
108: con = url.openConnection();
109: if ("HTTPS".equalsIgnoreCase(url.getProtocol())) {
110: HttpsURLConnection scon = (HttpsURLConnection) con;
111: try {
112: scon.setSSLSocketFactory(SSLUtil
113: .getSSLSocketFactory(clientCertAlias));
114: } catch (GeneralSecurityException gse) {
115: Debug.logError(gse, module);
116: }
117: }
118: } catch (IOException e) {
119: Debug.logError(e, module);
120: }
121:
122: synchronized (URLConnector.this ) {
123: if (timedOut && con != null) {
124: close(con);
125: } else {
126: connection = con;
127: URLConnector.this .notify();
128: }
129: }
130: }
131: }
132:
133: // closes the HttpURLConnection does nothing to others
134: private static void close(URLConnection con) {
135: if (con instanceof HttpURLConnection) {
136: ((HttpURLConnection) con).disconnect();
137: }
138: }
139: }
|