001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 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-2006 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: package org.netbeans.modules.mercurial.util;
042:
043: import java.util.prefs.Preferences;
044:
045: /**
046: *
047: * @author Tomas Stupka
048: */
049: public class HgProxySettings {
050:
051: private static final String PROXY_HTTP_HOST = "proxyHttpHost";
052: private static final String PROXY_HTTP_PORT = "proxyHttpPort";
053: private static final String PROXY_HTTPS_HOST = "proxyHttpsHost";
054: private static final String PROXY_HTTPS_PORT = "proxyHttpsPort";
055: private static final String NOT_PROXY_HOSTS = "proxyNonProxyHosts";
056: private static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication";
057: private static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername";
058: private static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword";
059:
060: private static final String PROXY_TYPE = "proxyType";
061: private static final String DIRECT_CONNECTION = "0";
062: private static final String AUTO_DETECT_PROXY = "1"; // as default
063: private static final String MANUAL_SET_PROXY = "2";
064:
065: private String username;
066: private String password;
067: private String notProxyHosts;
068: private boolean useAuth;
069: private String httpHost;
070: private String httpPort;
071: private String httpsHost;
072: private String httpsPort;
073: private String proxyType;
074:
075: private String toString = null;
076:
077: public HgProxySettings() {
078: init();
079: };
080:
081: private void init() {
082: Preferences prefs = org.openide.util.NbPreferences.root().node(
083: "org/netbeans/core"); // NOI18N
084: proxyType = prefs.get(PROXY_TYPE, ""); // NOI18N
085:
086: if (proxyType.equals(DIRECT_CONNECTION)) {
087: useAuth = false;
088: username = ""; // NOI18N
089: password = ""; // NOI18N
090:
091: notProxyHosts = ""; // NOI18N
092: httpHost = ""; // NOI18N
093: httpPort = ""; // NOI18N
094: httpsHost = ""; // NOI18N
095: httpsPort = ""; // NOI18N
096: } else if (isManualSetProxy()) {
097: useAuth = prefs.getBoolean(USE_PROXY_AUTHENTICATION, false); // NOI18N
098: username = prefs.get(PROXY_AUTHENTICATION_USERNAME, ""); // NOI18N
099: password = prefs.get(PROXY_AUTHENTICATION_PASSWORD, ""); // NOI18N
100:
101: notProxyHosts = prefs.get(NOT_PROXY_HOSTS, "").replace("|",
102: " ,"); // NOI18N
103: httpHost = prefs.get(PROXY_HTTP_HOST, ""); // NOI18N
104: httpPort = prefs.get(PROXY_HTTP_PORT, ""); // NOI18N
105: httpsHost = prefs.get(PROXY_HTTPS_HOST, ""); // NOI18N
106: httpsPort = prefs.get(PROXY_HTTPS_PORT, ""); // NOI18N
107: } else { // AUTO_DETECT_PROXY or DEFAULT
108: useAuth = false; // no way known yet!
109: username = ""; // NOI18N
110: password = ""; // NOI18N
111:
112: notProxyHosts = System
113: .getProperty("http.nonProxyHosts", ""); // NOI18N
114: httpHost = System.getProperty("http.proxyHost", ""); // NOI18N
115: httpPort = System.getProperty("http.proxyPort", ""); // NOI18N
116: httpsHost = System.getProperty("https.proxyHost", ""); // NOI18N
117: httpsPort = System.getProperty("https.proxyPort", ""); // NOI18N
118: }
119: }
120:
121: public boolean isDirect() {
122: return proxyType.equals(DIRECT_CONNECTION);
123: }
124:
125: public boolean isManualSetProxy() {
126: return proxyType.equals(MANUAL_SET_PROXY);
127: }
128:
129: public boolean hasAuth() {
130: return useAuth;
131: }
132:
133: public String getHttpHost() {
134: return httpHost;
135: }
136:
137: public int getHttpPort() {
138: if (httpPort.equals("")) {
139: return 8080;
140: }
141: return Integer.parseInt(httpPort);
142: }
143:
144: public String getHttpsHost() {
145: return httpsHost;
146: }
147:
148: public int getHttpsPort() {
149: if (httpsPort.equals("")) {
150: return 443;
151: }
152: return Integer.parseInt(httpsPort);
153: }
154:
155: public String getUsername() {
156: return username;
157: }
158:
159: public String getPassword() {
160: return password;
161: }
162:
163: public String getNotProxyHosts() {
164: return notProxyHosts;
165: }
166:
167: public boolean equals(Object obj) {
168: if (obj == null) {
169: return false;
170: }
171: if (!(obj instanceof HgProxySettings)) {
172: return false;
173: }
174: HgProxySettings ps = (HgProxySettings) obj;
175: return ps.httpHost.equals(httpHost)
176: && ps.httpPort.equals(httpPort)
177: && ps.httpsHost.equals(httpsHost)
178: && ps.httpsPort.equals(httpsPort)
179: && ps.notProxyHosts.equals(notProxyHosts)
180: && ps.password.equals(password)
181: && ps.proxyType.equals(proxyType)
182: && ps.username.equals(username)
183: && ps.useAuth == useAuth;
184: }
185:
186: public String toString() {
187: if (toString == null) {
188: StringBuffer sb = new StringBuffer();
189: sb.append("[");
190: sb.append(httpHost);
191: sb.append(",");
192: sb.append(httpPort);
193: sb.append(",");
194: sb.append(httpsHost);
195: sb.append(",");
196: sb.append(httpsPort);
197: sb.append(",");
198: sb.append(notProxyHosts);
199: sb.append(",");
200: sb.append(password);
201: sb.append(",");
202: sb.append(proxyType);
203: sb.append(",");
204: sb.append(username);
205: sb.append(",");
206: sb.append(useAuth);
207: sb.append("]");
208: toString = sb.toString();
209: }
210: return toString;
211: }
212:
213: public int hashCode() {
214: return toString().hashCode();
215: }
216:
217: }
|