1 19 20 package org.netbeans.core; 21 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 import java.util.HashSet ; 25 import java.util.Locale ; 26 import java.util.Set ; 27 import java.util.StringTokenizer ; 28 import java.util.prefs.PreferenceChangeListener ; 29 import java.util.prefs.Preferences ; 30 import org.openide.util.NbPreferences; 31 import org.openide.util.Utilities; 32 33 37 public class ProxySettings { 38 39 public static final String PROXY_HTTP_HOST = "proxyHttpHost"; 40 public static final String PROXY_HTTP_PORT = "proxyHttpPort"; 41 public static final String PROXY_HTTPS_HOST = "proxyHttpsHost"; 42 public static final String PROXY_HTTPS_PORT = "proxyHttpsPort"; 43 public static final String PROXY_SOCKS_HOST = "proxySocksHost"; 44 public static final String PROXY_SOCKS_PORT = "proxySocksPort"; 45 public static final String NOT_PROXY_HOSTS = "proxyNonProxyHosts"; 46 public static final String PROXY_TYPE = "proxyType"; 47 public static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication"; 48 public static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername"; 49 public static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword"; 50 public static final String USE_PROXY_ALL_PROTOCOLS = "useProxyAllProtocols"; 51 public static final String DIRECT = "DIRECT"; 52 53 private static String presetNonProxyHosts; 54 55 56 public static final int DIRECT_CONNECTION = 0; 57 58 59 public static final int AUTO_DETECT_PROXY = 1; 61 62 public static final int MANUAL_SET_PROXY = 2; 63 64 private static Preferences getPreferences() { 65 return NbPreferences.forModule (ProxySettings.class); 66 } 67 68 public static String getHttpHost () { 69 return normalizeProxyHost (getPreferences ().get (PROXY_HTTP_HOST, "")); 70 } 71 72 public static String getHttpPort () { 73 return getPreferences ().get (PROXY_HTTP_PORT, ""); 74 } 75 76 public static String getHttpsHost () { 77 return getPreferences ().get (PROXY_HTTPS_HOST, ""); 78 } 79 80 public static String getHttpsPort () { 81 return getPreferences ().get (PROXY_HTTPS_PORT, ""); 82 } 83 84 public static String getSocksHost () { 85 return getPreferences ().get (PROXY_SOCKS_HOST, ""); 86 } 87 88 public static String getSocksPort () { 89 return getPreferences ().get (PROXY_SOCKS_PORT, ""); 90 } 91 92 public static String getNonProxyHosts () { 93 return getPreferences ().get (NOT_PROXY_HOSTS, getDefaultUserNonProxyHosts ()); 94 } 95 96 public static int getProxyType () { 97 return getPreferences ().getInt (PROXY_TYPE, AUTO_DETECT_PROXY); 98 } 99 100 public static boolean useAuthentication () { 101 return getPreferences ().getBoolean (USE_PROXY_AUTHENTICATION, false); 102 } 103 104 public static boolean useProxyAllProtocols () { 105 return getPreferences ().getBoolean (USE_PROXY_ALL_PROTOCOLS, true); 106 } 107 108 public static String getAuthenticationUsername () { 109 return getPreferences ().get (PROXY_AUTHENTICATION_USERNAME, ""); 110 } 111 112 public static char[] getAuthenticationPassword () { 113 return getPreferences ().get (PROXY_AUTHENTICATION_PASSWORD, "").toCharArray (); 114 } 115 116 static void addPreferenceChangeListener (PreferenceChangeListener l) { 117 getPreferences ().addPreferenceChangeListener (l); 118 } 119 120 static void removePreferenceChangeListener (PreferenceChangeListener l) { 121 getPreferences ().removePreferenceChangeListener (l); 122 } 123 124 static class SystemProxySettings extends ProxySettings { 125 126 public static String getHttpHost () { 127 if (isSystemProxyDetect ()) { 128 return getSystemProxyHost (); 129 } else { 130 return ""; 131 } 132 } 133 134 public static String getHttpPort () { 135 if (isSystemProxyDetect ()) { 136 return getSystemProxyPort (); 137 } else { 138 return ""; 139 } 140 } 141 142 public static String getHttpsHost () { 143 if (isSystemProxyDetect ()) { 144 return getSystemProxyHost (); 145 } else { 146 return ""; 147 } 148 } 149 150 public static String getHttpsPort () { 151 if (isSystemProxyDetect ()) { 152 return getSystemProxyPort (); 153 } else { 154 return ""; 155 } 156 } 157 158 public static String getSocksHost () { 159 if (isSystemSocksServerDetect ()) { 160 return getSystemSocksServerHost (); 161 } else { 162 return ""; 163 } 164 } 165 166 public static String getSocksPort () { 167 if (isSystemSocksServerDetect ()) { 168 return getSystemSocksServerPort (); 169 } else { 170 return ""; 171 } 172 } 173 174 public static String getNonProxyHosts () { 175 return getDefaultUserNonProxyHosts (); 176 } 177 178 private static boolean isSystemProxyDetect () { 180 String s = System.getProperty ("netbeans.system_http_proxy"); return s != null && ! DIRECT.equals (s); } 183 184 private static String getSystemProxyHost () { 185 String systemProxy = System.getProperty ("netbeans.system_http_proxy"); if (systemProxy == null) { 187 return ""; } 189 190 int i = systemProxy.lastIndexOf (":"); if (i <= 0 || i >= systemProxy.length () - 1) { 192 return ""; } 194 195 return normalizeProxyHost (systemProxy.substring (0, i)); 196 } 197 198 private static String getSystemProxyPort () { 199 String systemProxy = System.getProperty ("netbeans.system_http_proxy"); if (systemProxy == null) { 201 return ""; } 203 204 int i = systemProxy.lastIndexOf (":"); if (i <= 0 || i >= systemProxy.length () - 1) { 206 return ""; } 208 209 String p = systemProxy.substring (i + 1); 210 if (p.indexOf ('/') >= 0) { 211 p = p.substring (0, p.indexOf ('/')); 212 } 213 214 return p; 215 } 216 217 private static boolean isSystemSocksServerDetect () { 218 return false; 220 } 221 222 private static String getSystemSocksServerHost () { 223 assert false : "SOCKS Server cannot be detected yet."; 224 return ""; 225 } 226 227 private static String getSystemSocksServerPort () { 228 assert false : "SOCKS Server cannot be detected yet."; 229 return ""; 230 } 231 232 } 233 234 private static String getSystemNonProxyHosts () { 235 String systemProxy = System.getProperty ("netbeans.system_http_non_proxy_hosts"); 237 return systemProxy == null ? "" : systemProxy; 238 } 239 240 private static String getPresetNonProxyHosts () { 241 if (presetNonProxyHosts == null) { 242 presetNonProxyHosts = System.getProperty ("http.nonProxyHosts", ""); 243 } 244 return presetNonProxyHosts; 245 } 246 247 private static String getDefaultUserNonProxyHosts () { 248 return getModifiedNonProxyHosts (getSystemNonProxyHosts ()); 249 } 250 251 private static String getModifiedNonProxyHosts (String systemPreset) { 252 String fromSystem = systemPreset.replaceAll (";", "|").replaceAll (",", "|"); String fromUser = getPresetNonProxyHosts () == null ? "" : getPresetNonProxyHosts ().replaceAll (";", "|").replaceAll (",", "|"); if (Utilities.isWindows ()) { 255 fromSystem = addReguralToNonProxyHosts (fromSystem); 256 } 257 String nonProxy = fromUser + (fromUser.length () == 0 ? "" : "|") + fromSystem + (fromSystem.length () == 0 ? "" : "|") + "localhost|127.0.0.1"; String localhost = ""; try { 260 localhost = InetAddress.getLocalHost().getHostName(); 261 if (!"localhost".equals(localhost)) { nonProxy = nonProxy + "|" + localhost; } else { 264 } 267 } 268 catch (UnknownHostException e) { 269 } 274 291 return compactNonProxyHosts (nonProxy); 292 } 293 294 295 private static String compactNonProxyHosts (String nonProxyHost) { 297 StringTokenizer st = new StringTokenizer (nonProxyHost, "|"); Set <String > s = new HashSet <String > (); 299 StringBuilder compactedProxyHosts = new StringBuilder (); 300 while (st.hasMoreTokens ()) { 301 String t = st.nextToken (); 302 if (s.add (t.toLowerCase (Locale.US))) { 303 if (compactedProxyHosts.length() > 0) 304 compactedProxyHosts.append('|'); 305 compactedProxyHosts.append(t); 306 } 307 } 308 return compactedProxyHosts.toString(); 309 } 310 311 private static String addReguralToNonProxyHosts (String nonProxyHost) { 312 StringTokenizer st = new StringTokenizer (nonProxyHost, "|"); 313 StringBuilder reguralProxyHosts = new StringBuilder (); 314 while (st.hasMoreTokens ()) { 315 String t = st.nextToken (); 316 if (t.indexOf ('*') == -1) { t = t + '*'; } 319 if (reguralProxyHosts.length() > 0) 320 reguralProxyHosts.append('|'); 321 reguralProxyHosts.append(t); 322 } 323 324 return reguralProxyHosts.toString(); 325 } 326 327 private static String normalizeProxyHost (String proxyHost) { 328 if (proxyHost.toLowerCase (Locale.US).startsWith ("http://")) { return proxyHost.substring (7, proxyHost.length ()); 330 } else { 331 return proxyHost; 332 } 333 } 334 335 } 336 | Popular Tags |