KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > NbProxySelector


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.net.InetSocketAddress JavaDoc;
25 import java.net.Proxy JavaDoc;
26 import java.net.ProxySelector JavaDoc;
27 import java.net.SocketAddress JavaDoc;
28 import java.net.URI JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import java.util.prefs.PreferenceChangeEvent JavaDoc;
37 import java.util.prefs.PreferenceChangeListener JavaDoc;
38
39 /**
40  *
41  * @author Jiri Rechtacek
42  */

43 public final class NbProxySelector extends ProxySelector JavaDoc {
44     
45     private ProxySelector JavaDoc original = null;
46     private Logger JavaDoc log = Logger.getLogger (NbProxySelector.class.getName ());
47     private Object JavaDoc useSystemProxies;
48         
49     /** Creates a new instance of NbProxySelector */
50     public NbProxySelector () {
51         original = super.getDefault ();
52         log.fine ("Override the original ProxySelector: " + original);
53         log.fine ("java.net.useSystemProxies has been set to " + useSystemProxies ());
54         ProxySettings.addPreferenceChangeListener (new ProxySettingsListener ());
55         copySettingsToSystem ();
56     }
57     
58     public List JavaDoc<Proxy JavaDoc> select(URI JavaDoc uri) {
59         List JavaDoc<Proxy JavaDoc> res = new ArrayList JavaDoc<Proxy JavaDoc> ();
60         int proxyType = ProxySettings.getProxyType ();
61         if (ProxySettings.DIRECT_CONNECTION == proxyType) {
62             res = Collections.singletonList (Proxy.NO_PROXY);
63         } else if (ProxySettings.AUTO_DETECT_PROXY == proxyType) {
64             if (useSystemProxies ()) {
65                 res = original.select (uri);
66             } else {
67                 String JavaDoc protocol = uri.getScheme ();
68                 assert protocol != null : "Invalid scheme of uri " + uri + ". Scheme cannot be null!";
69                 if (protocol.toLowerCase (Locale.US).startsWith("http")) {
70                     // handling nonProxyHosts first
71
if (dontUseProxy (ProxySettings.SystemProxySettings.getNonProxyHosts (), uri.getHost ())) {
72                         res.add (Proxy.NO_PROXY);
73                     }
74                     String JavaDoc ports = ProxySettings.SystemProxySettings.getHttpPort ();
75                     if (ports != null && ports.length () > 0 && ProxySettings.SystemProxySettings.getHttpHost ().length () > 0) {
76                         int porti = Integer.parseInt(ports);
77                         Proxy JavaDoc p = new Proxy JavaDoc (Proxy.Type.HTTP, new InetSocketAddress JavaDoc (ProxySettings.SystemProxySettings.getHttpHost (), porti));
78                         res.add (p);
79                     }
80                 }
81                 res.addAll (original.select (uri));
82             }
83         } else if (ProxySettings.MANUAL_SET_PROXY == proxyType) {
84             String JavaDoc protocol = uri.getScheme ();
85             assert protocol != null : "Invalid scheme of uri " + uri + ". Scheme cannot be null!";
86             if (protocol.toLowerCase (Locale.US).startsWith("http")) {
87                 // handling nonProxyHosts first
88
if (dontUseProxy (ProxySettings.getNonProxyHosts (), uri.getHost ())) {
89                     res.add (Proxy.NO_PROXY);
90                 }
91                 String JavaDoc hosts = ProxySettings.getHttpHost ();
92                 String JavaDoc ports = ProxySettings.getHttpPort ();
93                 if (ports != null && ports.length () > 0 && hosts.length () > 0) {
94                     int porti = Integer.parseInt(ports);
95                     Proxy JavaDoc p = new Proxy JavaDoc (Proxy.Type.HTTP, new InetSocketAddress JavaDoc (hosts, porti));
96                     res.add (p);
97                 } else {
98                     log.info ("Incomplete HTTP Proxy [" + hosts + "/" + ports + "] found in ProxySelector[Type: " + ProxySettings.getProxyType () + "] for uri " + uri + ". ");
99                     log.finest ("Fallback to the default ProxySelector which returns " + original.select (uri));
100                     res.addAll (original.select (uri));
101                 }
102             } else { // supposed SOCKS
103
String JavaDoc ports = ProxySettings.getSocksPort ();
104                 String JavaDoc hosts = ProxySettings.getSocksHost ();
105                 if (ports != null && ports.length () > 0 && hosts.length () > 0) {
106                     int porti = Integer.parseInt(ports);
107                     Proxy JavaDoc p = new Proxy JavaDoc (Proxy.Type.SOCKS, new InetSocketAddress JavaDoc (ProxySettings.getSocksHost (), porti));
108                     res.add (p);
109                 } else {
110                     log.info ("Incomplete SOCKS Server [" + hosts + "/" + ports + "] found in ProxySelector[Type: " + ProxySettings.getProxyType () + "] for uri " + uri + ". ");
111                     log.finest ("Fallback to the default ProxySelector which returns " + original.select (uri));
112                     res.addAll (original.select (uri));
113                 }
114             }
115             res.add (Proxy.NO_PROXY);
116         } else {
117             assert false : "Invalid proxy type: " + ProxySettings.getProxyType ();
118         }
119         log.finest ("NbProxySelector[Type: " + ProxySettings.getProxyType () + "] returns " + res + " for URI " + uri);
120         return res;
121     }
122     
123     public void connectFailed (URI JavaDoc arg0, SocketAddress JavaDoc arg1, IOException JavaDoc arg2) {
124         log.log (Level.INFO, "connectionFailed(" + arg0 + ", " + arg1 +")", arg2);
125     }
126     
127     // several modules listenes on these properties and propagates it futher
128
private class ProxySettingsListener implements PreferenceChangeListener JavaDoc {
129         public void preferenceChange(PreferenceChangeEvent JavaDoc evt) {
130             if (evt.getKey ().startsWith ("proxy") || evt.getKey ().startsWith ("useProxy")) {
131                 copySettingsToSystem ();
132             }
133         }
134     }
135     
136     private void copySettingsToSystem () {
137         String JavaDoc host = null, port = null, nonProxyHosts = null;
138         String JavaDoc sHost = null, sPort = null;
139         int proxyType = ProxySettings.getProxyType ();
140         if (ProxySettings.DIRECT_CONNECTION == proxyType) {
141             host = null;
142             port = null;
143             nonProxyHosts = null;
144             sHost = null;
145             sPort = null;
146         } else if (ProxySettings.AUTO_DETECT_PROXY == proxyType) {
147             host = ProxySettings.SystemProxySettings.getHttpHost ();
148             port = ProxySettings.SystemProxySettings.getHttpPort ();
149             nonProxyHosts = ProxySettings.SystemProxySettings.getNonProxyHosts ();
150             sHost = ProxySettings.SystemProxySettings.getSocksHost ();
151             sPort = ProxySettings.SystemProxySettings.getSocksPort ();
152         } else if (ProxySettings.MANUAL_SET_PROXY == proxyType) {
153             host = ProxySettings.getHttpHost ();
154             port = ProxySettings.getHttpPort ();
155             nonProxyHosts = ProxySettings.getNonProxyHosts ();
156             sHost = ProxySettings.getSocksHost ();
157             sPort = ProxySettings.getSocksPort ();
158         } else {
159             assert false : "Invalid proxy type: " + proxyType;
160         }
161         setOrClearProperty ("http.proxyHost", host, false);
162         setOrClearProperty ("http.proxyPort", port, true);
163         setOrClearProperty ("http.nonProxyHosts", nonProxyHosts, false);
164         setOrClearProperty ("https.proxyHost", host, false);
165         setOrClearProperty ("https.proxyPort", port, true);
166         setOrClearProperty ("https.nonProxyHosts", nonProxyHosts, false);
167         setOrClearProperty ("socksProxyHost", sHost, false);
168         setOrClearProperty ("socksProxyPort", sPort, true);
169         log.finest ("Set System's http.proxyHost/Port/NonProxyHost to " + host + "/" + port + "/" + nonProxyHosts);
170         log.finest ("Set System's socksProxyHost/Port to " + sHost + "/" + sPort);
171     }
172     
173     private void setOrClearProperty (String JavaDoc key, String JavaDoc value, boolean isInteger) {
174         assert key != null;
175         if (value == null || value.length () == 0) {
176             System.clearProperty (key);
177         } else {
178             if (isInteger) {
179                 try {
180                     Integer.parseInt (value);
181                 } catch (NumberFormatException JavaDoc nfe) {
182                     log.log (Level.INFO, nfe.getMessage(), nfe);
183                 }
184             }
185             System.setProperty (key, value);
186         }
187     }
188
189     private boolean dontUseProxy (String JavaDoc nonProxyHosts, String JavaDoc host) {
190         if (host == null) return false;
191
192         boolean dontUseProxy = false;
193         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (nonProxyHosts, "|", false);
194         while (st.hasMoreTokens () && !dontUseProxy) {
195             String JavaDoc token = st.nextToken ();
196             int star = token.indexOf ("*");
197             if (star == -1) {
198                 dontUseProxy = token.equals (host);
199                 if (dontUseProxy) {
200                     log.finest ("NbProxySelector[Type: " + ProxySettings.getProxyType () + "]. Host " + host + " found in nonProxyHosts: " + nonProxyHosts);
201                 }
202             } else {
203                 String JavaDoc start = token.substring (0, star - 1 < 0 ? 0 : star - 1);
204                 String JavaDoc end = token.substring (star + 1 > token.length () ? token.length () : star + 1);
205                 dontUseProxy = host.startsWith(start) && host.endsWith(end);
206                 if (dontUseProxy) {
207                     log.finest ("NbProxySelector[Type: " + ProxySettings.getProxyType () + "]. Host " + host + " found in nonProxyHosts: " + nonProxyHosts);
208                 }
209             }
210         }
211         return dontUseProxy;
212     }
213     
214     // NetProperties is JDK vendor specific, access only by reflection
215
private boolean useSystemProxies () {
216         if (useSystemProxies == null) {
217             try {
218                 Class JavaDoc clazz = Class.forName ("sun.net.NetProperties");
219                 Method JavaDoc getBoolean = clazz.getMethod ("getBoolean", String JavaDoc.class);
220                 useSystemProxies = getBoolean.invoke (null, "java.net.useSystemProxies");
221             } catch (Exception JavaDoc x) {
222                 log.log (Level.FINEST, "Cannot get value of java.net.useSystemProxies bacause " + x.getMessage(), x);
223             }
224         }
225         return useSystemProxies != null && "true".equalsIgnoreCase (useSystemProxies.toString ());
226     }
227 }
228
Popular Tags