KickJava   Java API By Example, From Geeks To Geeks.

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


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import java.util.prefs.PreferenceChangeListener JavaDoc;
29 import java.util.prefs.Preferences JavaDoc;
30 import org.openide.util.NbPreferences;
31 import org.openide.util.Utilities;
32
33 /**
34  *
35  * @author Jiri Rechtacek
36  */

37 public class ProxySettings {
38     
39     public static final String JavaDoc PROXY_HTTP_HOST = "proxyHttpHost";
40     public static final String JavaDoc PROXY_HTTP_PORT = "proxyHttpPort";
41     public static final String JavaDoc PROXY_HTTPS_HOST = "proxyHttpsHost";
42     public static final String JavaDoc PROXY_HTTPS_PORT = "proxyHttpsPort";
43     public static final String JavaDoc PROXY_SOCKS_HOST = "proxySocksHost";
44     public static final String JavaDoc PROXY_SOCKS_PORT = "proxySocksPort";
45     public static final String JavaDoc NOT_PROXY_HOSTS = "proxyNonProxyHosts";
46     public static final String JavaDoc PROXY_TYPE = "proxyType";
47     public static final String JavaDoc USE_PROXY_AUTHENTICATION = "useProxyAuthentication";
48     public static final String JavaDoc PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername";
49     public static final String JavaDoc PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword";
50     public static final String JavaDoc USE_PROXY_ALL_PROTOCOLS = "useProxyAllProtocols";
51     public static final String JavaDoc DIRECT = "DIRECT";
52     
53     private static String JavaDoc presetNonProxyHosts;
54
55     /** No proxy is used to connect. */
56     public static final int DIRECT_CONNECTION = 0;
57     
58     /** Proxy setting is automaticaly detect in OS. */
59     public static final int AUTO_DETECT_PROXY = 1; // as default
60

61     /** Manualy set proxy host and port. */
62     public static final int MANUAL_SET_PROXY = 2;
63     
64     private static Preferences JavaDoc getPreferences() {
65         return NbPreferences.forModule (ProxySettings.class);
66     }
67     
68     public static String JavaDoc getHttpHost () {
69         return normalizeProxyHost (getPreferences ().get (PROXY_HTTP_HOST, ""));
70     }
71     
72     public static String JavaDoc getHttpPort () {
73         return getPreferences ().get (PROXY_HTTP_PORT, "");
74     }
75     
76     public static String JavaDoc getHttpsHost () {
77         return getPreferences ().get (PROXY_HTTPS_HOST, "");
78     }
79     
80     public static String JavaDoc getHttpsPort () {
81         return getPreferences ().get (PROXY_HTTPS_PORT, "");
82     }
83     
84     public static String JavaDoc getSocksHost () {
85         return getPreferences ().get (PROXY_SOCKS_HOST, "");
86     }
87     
88     public static String JavaDoc getSocksPort () {
89         return getPreferences ().get (PROXY_SOCKS_PORT, "");
90     }
91     
92     public static String JavaDoc 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 JavaDoc 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 JavaDoc l) {
117         getPreferences ().addPreferenceChangeListener (l);
118     }
119     
120     static void removePreferenceChangeListener (PreferenceChangeListener JavaDoc l) {
121         getPreferences ().removePreferenceChangeListener (l);
122     }
123     
124     static class SystemProxySettings extends ProxySettings {
125         
126         public static String JavaDoc getHttpHost () {
127             if (isSystemProxyDetect ()) {
128                 return getSystemProxyHost ();
129             } else {
130                 return "";
131             }
132         }
133
134         public static String JavaDoc getHttpPort () {
135             if (isSystemProxyDetect ()) {
136                 return getSystemProxyPort ();
137             } else {
138                 return "";
139             }
140         }
141
142         public static String JavaDoc getHttpsHost () {
143             if (isSystemProxyDetect ()) {
144                 return getSystemProxyHost ();
145             } else {
146                 return "";
147             }
148         }
149
150         public static String JavaDoc getHttpsPort () {
151             if (isSystemProxyDetect ()) {
152                 return getSystemProxyPort ();
153             } else {
154                 return "";
155             }
156         }
157
158         public static String JavaDoc getSocksHost () {
159             if (isSystemSocksServerDetect ()) {
160                 return getSystemSocksServerHost ();
161             } else {
162                 return "";
163             }
164         }
165
166         public static String JavaDoc getSocksPort () {
167             if (isSystemSocksServerDetect ()) {
168                 return getSystemSocksServerPort ();
169             } else {
170                 return "";
171             }
172         }
173
174         public static String JavaDoc getNonProxyHosts () {
175             return getDefaultUserNonProxyHosts ();
176         }
177
178         // helper methods
179
private static boolean isSystemProxyDetect () {
180             String JavaDoc s = System.getProperty ("netbeans.system_http_proxy"); // NOT_PROXY_HOSTS
181
return s != null && ! DIRECT.equals (s); // NOI18N
182
}
183
184         private static String JavaDoc getSystemProxyHost () {
185             String JavaDoc systemProxy = System.getProperty ("netbeans.system_http_proxy"); // NOI18N
186
if (systemProxy == null) {
187                 return ""; // NOI18N
188
}
189
190             int i = systemProxy.lastIndexOf (":"); // NOI18N
191
if (i <= 0 || i >= systemProxy.length () - 1) {
192                 return ""; // NOI18N
193
}
194
195             return normalizeProxyHost (systemProxy.substring (0, i));
196         }
197
198         private static String JavaDoc getSystemProxyPort () {
199             String JavaDoc systemProxy = System.getProperty ("netbeans.system_http_proxy"); // NOI18N
200
if (systemProxy == null) {
201                 return ""; // NOI18N
202
}
203
204             int i = systemProxy.lastIndexOf (":"); // NOI18N
205
if (i <= 0 || i >= systemProxy.length () - 1) {
206                 return ""; // NOI18N
207
}
208             
209             String JavaDoc 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             // no SOCKS detection yet
219
return false;
220         }
221         
222         private static String JavaDoc getSystemSocksServerHost () {
223             assert false : "SOCKS Server cannot be detected yet.";
224             return "";
225         }
226
227         private static String JavaDoc getSystemSocksServerPort () {
228             assert false : "SOCKS Server cannot be detected yet.";
229             return "";
230         }
231
232     }
233
234     private static String JavaDoc getSystemNonProxyHosts () {
235         String JavaDoc systemProxy = System.getProperty ("netbeans.system_http_non_proxy_hosts"); // NOI18N
236

237         return systemProxy == null ? "" : systemProxy;
238     }
239     
240     private static String JavaDoc getPresetNonProxyHosts () {
241         if (presetNonProxyHosts == null) {
242             presetNonProxyHosts = System.getProperty ("http.nonProxyHosts", "");
243         }
244         return presetNonProxyHosts;
245     }
246     
247     private static String JavaDoc getDefaultUserNonProxyHosts () {
248         return getModifiedNonProxyHosts (getSystemNonProxyHosts ());
249     }
250
251     private static String JavaDoc getModifiedNonProxyHosts (String JavaDoc systemPreset) {
252         String JavaDoc fromSystem = systemPreset.replaceAll (";", "|").replaceAll (",", "|"); //NOI18N
253
String JavaDoc fromUser = getPresetNonProxyHosts () == null ? "" : getPresetNonProxyHosts ().replaceAll (";", "|").replaceAll (",", "|"); //NOI18N
254
if (Utilities.isWindows ()) {
255             fromSystem = addReguralToNonProxyHosts (fromSystem);
256         }
257         String JavaDoc nonProxy = fromUser + (fromUser.length () == 0 ? "" : "|") + fromSystem + (fromSystem.length () == 0 ? "" : "|") + "localhost|127.0.0.1"; // NOI18N
258
String JavaDoc localhost = ""; // NOI18N
259
try {
260             localhost = InetAddress.getLocalHost().getHostName();
261             if (!"localhost".equals(localhost)) { // NOI18N
262
nonProxy = nonProxy + "|" + localhost; // NOI18N
263
} else {
264                 // Avoid this error when hostname == localhost:
265
// Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
266
}
267         }
268         catch (UnknownHostException JavaDoc e) {
269             // OK. Sometimes a hostname is assigned by DNS, but a computer
270
// is later pulled off the network. It may then produce a bogus
271
// name for itself which can't actually be resolved. Normally
272
// "localhost" is aliased to 127.0.0.1 anyway.
273
}
274         /* per Milan's agreement it's removed. See issue #89868
275         try {
276             String localhost2 = InetAddress.getLocalHost().getCanonicalHostName();
277             if (!"localhost".equals(localhost2) && !localhost2.equals(localhost)) { // NOI18N
278                 nonProxy = nonProxy + "|" + localhost2; // NOI18N
279             } else {
280                 // Avoid this error when hostname == localhost:
281                 // Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
282             }
283         }
284         catch (UnknownHostException e) {
285             // OK. Sometimes a hostname is assigned by DNS, but a computer
286             // is later pulled off the network. It may then produce a bogus
287             // name for itself which can't actually be resolved. Normally
288             // "localhost" is aliased to 127.0.0.1 anyway.
289         }
290          */

291         return compactNonProxyHosts (nonProxy);
292     }
293
294
295     // avoid duplicate hosts
296
private static String JavaDoc compactNonProxyHosts (String JavaDoc nonProxyHost) {
297         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (nonProxyHost, "|"); //NOI18N
298
Set JavaDoc<String JavaDoc> s = new HashSet JavaDoc<String JavaDoc> ();
299         StringBuilder JavaDoc compactedProxyHosts = new StringBuilder JavaDoc();
300         while (st.hasMoreTokens ()) {
301             String JavaDoc 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 JavaDoc addReguralToNonProxyHosts (String JavaDoc nonProxyHost) {
312         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (nonProxyHost, "|");
313         StringBuilder JavaDoc reguralProxyHosts = new StringBuilder JavaDoc();
314         while (st.hasMoreTokens ()) {
315             String JavaDoc t = st.nextToken ();
316             if (t.indexOf ('*') == -1) { //NOI18N
317
t = t + '*'; //NOI18N
318
}
319             if (reguralProxyHosts.length() > 0)
320                 reguralProxyHosts.append('|');
321             reguralProxyHosts.append(t);
322         }
323
324         return reguralProxyHosts.toString();
325     }
326
327     private static String JavaDoc normalizeProxyHost (String JavaDoc proxyHost) {
328         if (proxyHost.toLowerCase (Locale.US).startsWith ("http://")) { // NOI18N
329
return proxyHost.substring (7, proxyHost.length ());
330         } else {
331             return proxyHost;
332         }
333     }
334     
335 }
336
Popular Tags