1 19 20 package org.netbeans.core; 21 22 import java.io.IOException ; 23 import java.lang.reflect.Method ; 24 import java.net.InetSocketAddress ; 25 import java.net.Proxy ; 26 import java.net.ProxySelector ; 27 import java.net.SocketAddress ; 28 import java.net.URI ; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.StringTokenizer ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 import java.util.prefs.PreferenceChangeEvent ; 37 import java.util.prefs.PreferenceChangeListener ; 38 39 43 public final class NbProxySelector extends ProxySelector { 44 45 private ProxySelector original = null; 46 private Logger log = Logger.getLogger (NbProxySelector.class.getName ()); 47 private Object useSystemProxies; 48 49 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 <Proxy > select(URI uri) { 59 List <Proxy > res = new ArrayList <Proxy > (); 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 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 if (dontUseProxy (ProxySettings.SystemProxySettings.getNonProxyHosts (), uri.getHost ())) { 72 res.add (Proxy.NO_PROXY); 73 } 74 String ports = ProxySettings.SystemProxySettings.getHttpPort (); 75 if (ports != null && ports.length () > 0 && ProxySettings.SystemProxySettings.getHttpHost ().length () > 0) { 76 int porti = Integer.parseInt(ports); 77 Proxy p = new Proxy (Proxy.Type.HTTP, new InetSocketAddress (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 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 if (dontUseProxy (ProxySettings.getNonProxyHosts (), uri.getHost ())) { 89 res.add (Proxy.NO_PROXY); 90 } 91 String hosts = ProxySettings.getHttpHost (); 92 String ports = ProxySettings.getHttpPort (); 93 if (ports != null && ports.length () > 0 && hosts.length () > 0) { 94 int porti = Integer.parseInt(ports); 95 Proxy p = new Proxy (Proxy.Type.HTTP, new InetSocketAddress (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 { String ports = ProxySettings.getSocksPort (); 104 String hosts = ProxySettings.getSocksHost (); 105 if (ports != null && ports.length () > 0 && hosts.length () > 0) { 106 int porti = Integer.parseInt(ports); 107 Proxy p = new Proxy (Proxy.Type.SOCKS, new InetSocketAddress (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 arg0, SocketAddress arg1, IOException arg2) { 124 log.log (Level.INFO, "connectionFailed(" + arg0 + ", " + arg1 +")", arg2); 125 } 126 127 private class ProxySettingsListener implements PreferenceChangeListener { 129 public void preferenceChange(PreferenceChangeEvent evt) { 130 if (evt.getKey ().startsWith ("proxy") || evt.getKey ().startsWith ("useProxy")) { 131 copySettingsToSystem (); 132 } 133 } 134 } 135 136 private void copySettingsToSystem () { 137 String host = null, port = null, nonProxyHosts = null; 138 String 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 key, String 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 nfe) { 182 log.log (Level.INFO, nfe.getMessage(), nfe); 183 } 184 } 185 System.setProperty (key, value); 186 } 187 } 188 189 private boolean dontUseProxy (String nonProxyHosts, String host) { 190 if (host == null) return false; 191 192 boolean dontUseProxy = false; 193 StringTokenizer st = new StringTokenizer (nonProxyHosts, "|", false); 194 while (st.hasMoreTokens () && !dontUseProxy) { 195 String 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 start = token.substring (0, star - 1 < 0 ? 0 : star - 1); 204 String 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 private boolean useSystemProxies () { 216 if (useSystemProxies == null) { 217 try { 218 Class clazz = Class.forName ("sun.net.NetProperties"); 219 Method getBoolean = clazz.getMethod ("getBoolean", String .class); 220 useSystemProxies = getBoolean.invoke (null, "java.net.useSystemProxies"); 221 } catch (Exception 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 |