001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core;
043:
044: import java.net.InetAddress;
045: import java.net.UnknownHostException;
046: import java.util.HashSet;
047: import java.util.Locale;
048: import java.util.Set;
049: import java.util.StringTokenizer;
050: import java.util.prefs.PreferenceChangeListener;
051: import java.util.prefs.Preferences;
052: import org.openide.util.NbPreferences;
053: import org.openide.util.Utilities;
054:
055: /**
056: *
057: * @author Jiri Rechtacek
058: */
059: public class ProxySettings {
060:
061: public static final String PROXY_HTTP_HOST = "proxyHttpHost";
062: public static final String PROXY_HTTP_PORT = "proxyHttpPort";
063: public static final String PROXY_HTTPS_HOST = "proxyHttpsHost";
064: public static final String PROXY_HTTPS_PORT = "proxyHttpsPort";
065: public static final String PROXY_SOCKS_HOST = "proxySocksHost";
066: public static final String PROXY_SOCKS_PORT = "proxySocksPort";
067: public static final String NOT_PROXY_HOSTS = "proxyNonProxyHosts";
068: public static final String PROXY_TYPE = "proxyType";
069: public static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication";
070: public static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername";
071: public static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword";
072: public static final String USE_PROXY_ALL_PROTOCOLS = "useProxyAllProtocols";
073: public static final String DIRECT = "DIRECT";
074:
075: private static String presetNonProxyHosts;
076:
077: /** No proxy is used to connect. */
078: public static final int DIRECT_CONNECTION = 0;
079:
080: /** Proxy setting is automaticaly detect in OS. */
081: public static final int AUTO_DETECT_PROXY = 1; // as default
082:
083: /** Manualy set proxy host and port. */
084: public static final int MANUAL_SET_PROXY = 2;
085:
086: private static Preferences getPreferences() {
087: return NbPreferences.forModule(ProxySettings.class);
088: }
089:
090: public static String getHttpHost() {
091: return normalizeProxyHost(getPreferences().get(PROXY_HTTP_HOST,
092: ""));
093: }
094:
095: public static String getHttpPort() {
096: return getPreferences().get(PROXY_HTTP_PORT, "");
097: }
098:
099: public static String getHttpsHost() {
100: if (useProxyAllProtocols()) {
101: return getHttpHost();
102: } else {
103: return getPreferences().get(PROXY_HTTPS_HOST, "");
104: }
105: }
106:
107: public static String getHttpsPort() {
108: if (useProxyAllProtocols()) {
109: return getHttpPort();
110: } else {
111: return getPreferences().get(PROXY_HTTPS_PORT, "");
112: }
113: }
114:
115: public static String getSocksHost() {
116: if (useProxyAllProtocols()) {
117: return getHttpHost();
118: } else {
119: return getPreferences().get(PROXY_SOCKS_HOST, "");
120: }
121: }
122:
123: public static String getSocksPort() {
124: if (useProxyAllProtocols()) {
125: return getHttpPort();
126: } else {
127: return getPreferences().get(PROXY_SOCKS_PORT, "");
128: }
129: }
130:
131: public static String getNonProxyHosts() {
132: return getPreferences().get(NOT_PROXY_HOSTS,
133: getDefaultUserNonProxyHosts());
134: }
135:
136: public static int getProxyType() {
137: return getPreferences().getInt(PROXY_TYPE, AUTO_DETECT_PROXY);
138: }
139:
140: public static boolean useAuthentication() {
141: return getPreferences().getBoolean(USE_PROXY_AUTHENTICATION,
142: false);
143: }
144:
145: public static boolean useProxyAllProtocols() {
146: return getPreferences().getBoolean(USE_PROXY_ALL_PROTOCOLS,
147: false);
148: }
149:
150: public static String getAuthenticationUsername() {
151: return getPreferences().get(PROXY_AUTHENTICATION_USERNAME, "");
152: }
153:
154: public static char[] getAuthenticationPassword() {
155: return getPreferences().get(PROXY_AUTHENTICATION_PASSWORD, "")
156: .toCharArray();
157: }
158:
159: static void addPreferenceChangeListener(PreferenceChangeListener l) {
160: getPreferences().addPreferenceChangeListener(l);
161: }
162:
163: static void removePreferenceChangeListener(
164: PreferenceChangeListener l) {
165: getPreferences().removePreferenceChangeListener(l);
166: }
167:
168: static class SystemProxySettings extends ProxySettings {
169:
170: public static String getHttpHost() {
171: if (isSystemProxyDetect()) {
172: return getSystemProxyHost();
173: } else {
174: return "";
175: }
176: }
177:
178: public static String getHttpPort() {
179: if (isSystemProxyDetect()) {
180: return getSystemProxyPort();
181: } else {
182: return "";
183: }
184: }
185:
186: public static String getHttpsHost() {
187: if (isSystemProxyDetect()) {
188: return getSystemProxyHost();
189: } else {
190: return "";
191: }
192: }
193:
194: public static String getHttpsPort() {
195: if (isSystemProxyDetect()) {
196: return getSystemProxyPort();
197: } else {
198: return "";
199: }
200: }
201:
202: public static String getSocksHost() {
203: if (isSystemSocksServerDetect()) {
204: return getSystemSocksServerHost();
205: } else {
206: return "";
207: }
208: }
209:
210: public static String getSocksPort() {
211: if (isSystemSocksServerDetect()) {
212: return getSystemSocksServerPort();
213: } else {
214: return "";
215: }
216: }
217:
218: public static String getNonProxyHosts() {
219: return getDefaultUserNonProxyHosts();
220: }
221:
222: // helper methods
223: private static boolean isSystemProxyDetect() {
224: String s = System.getProperty("netbeans.system_http_proxy"); // NOT_PROXY_HOSTS
225: return s != null && !DIRECT.equals(s); // NOI18N
226: }
227:
228: private static String getSystemProxyHost() {
229: String systemProxy = System
230: .getProperty("netbeans.system_http_proxy"); // NOI18N
231: if (systemProxy == null) {
232: return ""; // NOI18N
233: }
234:
235: int i = systemProxy.lastIndexOf(":"); // NOI18N
236: if (i <= 0 || i >= systemProxy.length() - 1) {
237: return ""; // NOI18N
238: }
239:
240: return normalizeProxyHost(systemProxy.substring(0, i));
241: }
242:
243: private static String getSystemProxyPort() {
244: String systemProxy = System
245: .getProperty("netbeans.system_http_proxy"); // NOI18N
246: if (systemProxy == null) {
247: return ""; // NOI18N
248: }
249:
250: int i = systemProxy.lastIndexOf(":"); // NOI18N
251: if (i <= 0 || i >= systemProxy.length() - 1) {
252: return ""; // NOI18N
253: }
254:
255: String p = systemProxy.substring(i + 1);
256: if (p.indexOf('/') >= 0) {
257: p = p.substring(0, p.indexOf('/'));
258: }
259:
260: return p;
261: }
262:
263: private static boolean isSystemSocksServerDetect() {
264: return isSystemProxyDetect()
265: && System
266: .getProperty("netbeans.system_socks_proxy") != null; // NOI18N
267: }
268:
269: private static String getSystemSocksServerHost() {
270: String systemProxy = System
271: .getProperty("netbeans.system_socks_proxy"); // NOI18N
272: if (systemProxy == null) {
273: return ""; // NOI18N
274: }
275:
276: int i = systemProxy.lastIndexOf(":"); // NOI18N
277: if (i <= 0 || i >= systemProxy.length() - 1) {
278: return ""; // NOI18N
279: }
280:
281: return normalizeProxyHost(systemProxy.substring(0, i));
282: }
283:
284: private static String getSystemSocksServerPort() {
285: String systemProxy = System
286: .getProperty("netbeans.system_socks_proxy"); // NOI18N
287: if (systemProxy == null) {
288: return ""; // NOI18N
289: }
290:
291: int i = systemProxy.lastIndexOf(":"); // NOI18N
292: if (i <= 0 || i >= systemProxy.length() - 1) {
293: return ""; // NOI18N
294: }
295:
296: String p = systemProxy.substring(i + 1);
297: if (p.indexOf('/') >= 0) {
298: p = p.substring(0, p.indexOf('/'));
299: }
300:
301: return p;
302: }
303:
304: }
305:
306: private static String getSystemNonProxyHosts() {
307: String systemProxy = System
308: .getProperty("netbeans.system_http_non_proxy_hosts"); // NOI18N
309:
310: return systemProxy == null ? "" : systemProxy;
311: }
312:
313: private static String getPresetNonProxyHosts() {
314: if (presetNonProxyHosts == null) {
315: presetNonProxyHosts = System.getProperty(
316: "http.nonProxyHosts", "");
317: }
318: return presetNonProxyHosts;
319: }
320:
321: private static String getDefaultUserNonProxyHosts() {
322: return getModifiedNonProxyHosts(getSystemNonProxyHosts());
323: }
324:
325: private static String getModifiedNonProxyHosts(String systemPreset) {
326: String fromSystem = systemPreset.replaceAll(";", "|")
327: .replaceAll(",", "|"); //NOI18N
328: String fromUser = getPresetNonProxyHosts() == null ? ""
329: : getPresetNonProxyHosts().replaceAll(";", "|")
330: .replaceAll(",", "|"); //NOI18N
331: if (Utilities.isWindows()) {
332: fromSystem = addReguralToNonProxyHosts(fromSystem);
333: }
334: String nonProxy = fromUser
335: + (fromUser.length() == 0 ? "" : "|") + fromSystem
336: + (fromSystem.length() == 0 ? "" : "|")
337: + "localhost|127.0.0.1"; // NOI18N
338: String localhost = ""; // NOI18N
339: try {
340: localhost = InetAddress.getLocalHost().getHostName();
341: if (!"localhost".equals(localhost)) { // NOI18N
342: nonProxy = nonProxy + "|" + localhost; // NOI18N
343: } else {
344: // Avoid this error when hostname == localhost:
345: // Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
346: }
347: } catch (UnknownHostException e) {
348: // OK. Sometimes a hostname is assigned by DNS, but a computer
349: // is later pulled off the network. It may then produce a bogus
350: // name for itself which can't actually be resolved. Normally
351: // "localhost" is aliased to 127.0.0.1 anyway.
352: }
353: /* per Milan's agreement it's removed. See issue #89868
354: try {
355: String localhost2 = InetAddress.getLocalHost().getCanonicalHostName();
356: if (!"localhost".equals(localhost2) && !localhost2.equals(localhost)) { // NOI18N
357: nonProxy = nonProxy + "|" + localhost2; // NOI18N
358: } else {
359: // Avoid this error when hostname == localhost:
360: // Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
361: }
362: }
363: catch (UnknownHostException e) {
364: // OK. Sometimes a hostname is assigned by DNS, but a computer
365: // is later pulled off the network. It may then produce a bogus
366: // name for itself which can't actually be resolved. Normally
367: // "localhost" is aliased to 127.0.0.1 anyway.
368: }
369: */
370: return compactNonProxyHosts(nonProxy);
371: }
372:
373: // avoid duplicate hosts
374: private static String compactNonProxyHosts(String nonProxyHost) {
375: StringTokenizer st = new StringTokenizer(nonProxyHost, "|"); //NOI18N
376: Set<String> s = new HashSet<String>();
377: StringBuilder compactedProxyHosts = new StringBuilder();
378: while (st.hasMoreTokens()) {
379: String t = st.nextToken();
380: if (s.add(t.toLowerCase(Locale.US))) {
381: if (compactedProxyHosts.length() > 0)
382: compactedProxyHosts.append('|');
383: compactedProxyHosts.append(t);
384: }
385: }
386: return compactedProxyHosts.toString();
387: }
388:
389: private static String addReguralToNonProxyHosts(String nonProxyHost) {
390: StringTokenizer st = new StringTokenizer(nonProxyHost, "|");
391: StringBuilder reguralProxyHosts = new StringBuilder();
392: while (st.hasMoreTokens()) {
393: String t = st.nextToken();
394: if (t.indexOf('*') == -1) { //NOI18N
395: t = t + '*'; //NOI18N
396: }
397: if (reguralProxyHosts.length() > 0)
398: reguralProxyHosts.append('|');
399: reguralProxyHosts.append(t);
400: }
401:
402: return reguralProxyHosts.toString();
403: }
404:
405: private static String normalizeProxyHost(String proxyHost) {
406: if (proxyHost.toLowerCase(Locale.US).startsWith("http://")) { // NOI18N
407: return proxyHost.substring(7, proxyHost.length());
408: } else {
409: return proxyHost;
410: }
411: }
412:
413: }
|