KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > URLConnector


1 /*
2  * $Id: URLConnector.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.io.IOException JavaDoc;
27 import java.net.HttpURLConnection JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLConnection JavaDoc;
30 import java.security.GeneralSecurityException JavaDoc;
31
32 import javax.net.ssl.*;
33
34 /**
35  * URLConnector.java
36  *
37  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
38  * @version $Rev: 5462 $
39  * @since 2.0
40  */

41 public class URLConnector {
42     
43     public static final String JavaDoc module = URLConnector.class.getName();
44
45     private URLConnection JavaDoc connection = null;
46     private URL JavaDoc url = null;
47     private String JavaDoc clientCertAlias = null;
48     private boolean timedOut = false;
49
50     protected URLConnector() {}
51     protected URLConnector(URL JavaDoc url, String JavaDoc clientCertAlias) {
52         this.clientCertAlias = clientCertAlias;
53         this.url = url;
54     }
55     
56     protected synchronized URLConnection JavaDoc openConnection(int timeout) throws IOException JavaDoc {
57         Thread JavaDoc t = new Thread JavaDoc(new URLConnectorThread());
58         t.start();
59               
60         try {
61             this.wait(timeout);
62         } catch (InterruptedException JavaDoc e) {
63             if (connection == null) {
64                 timedOut = true;
65             } else {
66                 close(connection);
67             }
68             throw new IOException JavaDoc("Connection never established");
69         }
70
71         if (connection != null) {
72             return connection;
73         } else {
74             timedOut = true;
75             throw new IOException JavaDoc("Connection timed out");
76         }
77     }
78     
79     public static URLConnection JavaDoc openConnection(URL JavaDoc url) throws IOException JavaDoc {
80         return openConnection(url, 30000);
81     }
82     
83     public static URLConnection JavaDoc openConnection(URL JavaDoc url, int timeout) throws IOException JavaDoc {
84         return openConnection(url, timeout, null);
85     }
86     
87     public static URLConnection JavaDoc openConnection(URL JavaDoc url, String JavaDoc clientCertAlias) throws IOException JavaDoc {
88         return openConnection(url, 30000, clientCertAlias);
89     }
90       
91     public static URLConnection JavaDoc openConnection(URL JavaDoc url, int timeout, String JavaDoc clientCertAlias) throws IOException JavaDoc {
92         URLConnector uc = new URLConnector(url, clientCertAlias);
93         return uc.openConnection(timeout);
94     }
95
96     // special thread to open the connection
97
private class URLConnectorThread implements Runnable JavaDoc {
98         public void run() {
99             URLConnection JavaDoc con = null;
100             try {
101                 con = url.openConnection();
102                 if ("HTTPS".equalsIgnoreCase(url.getProtocol())) {
103                     HttpsURLConnection scon = (HttpsURLConnection) con;
104                     try {
105                         scon.setSSLSocketFactory(SSLUtil.getSSLSocketFactory(clientCertAlias));
106                     } catch (GeneralSecurityException JavaDoc gse) {
107                         Debug.logError(gse, module);
108                     }
109                 }
110             } catch (IOException JavaDoc e) {
111                 Debug.logError(e, module);
112             }
113
114             synchronized (URLConnector.this) {
115                 if (timedOut && con != null) {
116                     close(con);
117                 } else {
118                     connection = con;
119                     URLConnector.this.notify();
120                 }
121             }
122         }
123     }
124     
125     // closes the HttpURLConnection does nothing to others
126
private static void close(URLConnection JavaDoc con) {
127         if (con instanceof HttpURLConnection JavaDoc) {
128             ((HttpURLConnection JavaDoc) con).disconnect();
129         }
130     }
131 }
132
Popular Tags