PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/netbeans-7.3/j2ee.sun.appsrv/src/org/netbeans/modules/j2ee/sun/ide/editors/ProxySettings.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 402 lines | 267 code | 59 blank | 76 comment | 61 complexity | e1b06ad0151dff26dbee8e1224a9e23d MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.j2ee.sun.ide.editors;
  45. import java.net.InetAddress;
  46. import java.net.UnknownHostException;
  47. import java.util.HashSet;
  48. import java.util.Locale;
  49. import java.util.Set;
  50. import java.util.StringTokenizer;
  51. import java.util.prefs.PreferenceChangeListener;
  52. import java.util.prefs.Preferences;
  53. import org.openide.util.NbPreferences;
  54. import org.openide.util.Utilities;
  55. /**
  56. * Stolen from core by vkraemer
  57. *
  58. * @author Jiri Rechtacek
  59. */
  60. public class ProxySettings {
  61. public static final String PROXY_HTTP_HOST = "proxyHttpHost";
  62. public static final String PROXY_HTTP_PORT = "proxyHttpPort";
  63. public static final String PROXY_HTTPS_HOST = "proxyHttpsHost";
  64. public static final String PROXY_HTTPS_PORT = "proxyHttpsPort";
  65. public static final String PROXY_SOCKS_HOST = "proxySocksHost";
  66. public static final String PROXY_SOCKS_PORT = "proxySocksPort";
  67. public static final String NOT_PROXY_HOSTS = "proxyNonProxyHosts";
  68. public static final String PROXY_TYPE = "proxyType";
  69. public static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication";
  70. public static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername";
  71. public static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword";
  72. public static final String USE_PROXY_ALL_PROTOCOLS = "useProxyAllProtocols";
  73. public static final String DIRECT = "DIRECT";
  74. private static String presetNonProxyHosts;
  75. /** No proxy is used to connect. */
  76. public static final int DIRECT_CONNECTION = 0;
  77. /** Proxy setting is automaticaly detect in OS. */
  78. public static final int AUTO_DETECT_PROXY = 1; // as default
  79. /** Manualy set proxy host and port. */
  80. public static final int MANUAL_SET_PROXY = 2;
  81. private static Preferences getPreferences() {
  82. return NbPreferences.forModule (ProxySettings.class);
  83. }
  84. public static String getHttpHost () {
  85. return normalizeProxyHost (getPreferences ().get (PROXY_HTTP_HOST, ""));
  86. }
  87. public static String getHttpPort () {
  88. return getPreferences ().get (PROXY_HTTP_PORT, "");
  89. }
  90. public static String getHttpsHost () {
  91. if (useProxyAllProtocols ()) {
  92. return getHttpHost ();
  93. } else {
  94. return getPreferences ().get (PROXY_HTTPS_HOST, "");
  95. }
  96. }
  97. public static String getHttpsPort () {
  98. if (useProxyAllProtocols ()) {
  99. return getHttpPort ();
  100. } else {
  101. return getPreferences ().get (PROXY_HTTPS_PORT, "");
  102. }
  103. }
  104. public static String getSocksHost () {
  105. if (useProxyAllProtocols ()) {
  106. return getHttpHost ();
  107. } else {
  108. return getPreferences ().get (PROXY_SOCKS_HOST, "");
  109. }
  110. }
  111. public static String getSocksPort () {
  112. if (useProxyAllProtocols ()) {
  113. return getHttpPort ();
  114. } else {
  115. return getPreferences ().get (PROXY_SOCKS_PORT, "");
  116. }
  117. }
  118. public static String getNonProxyHosts () {
  119. return getPreferences ().get (NOT_PROXY_HOSTS, getDefaultUserNonProxyHosts ());
  120. }
  121. public static int getProxyType () {
  122. return getPreferences ().getInt (PROXY_TYPE, AUTO_DETECT_PROXY);
  123. }
  124. public static boolean useAuthentication () {
  125. return getPreferences ().getBoolean (USE_PROXY_AUTHENTICATION, false);
  126. }
  127. public static boolean useProxyAllProtocols () {
  128. return getPreferences ().getBoolean (USE_PROXY_ALL_PROTOCOLS, false);
  129. }
  130. public static String getAuthenticationUsername () {
  131. return getPreferences ().get (PROXY_AUTHENTICATION_USERNAME, "");
  132. }
  133. public static char[] getAuthenticationPassword () {
  134. return getPreferences ().get (PROXY_AUTHENTICATION_PASSWORD, "").toCharArray ();
  135. }
  136. static void addPreferenceChangeListener (PreferenceChangeListener l) {
  137. getPreferences ().addPreferenceChangeListener (l);
  138. }
  139. static void removePreferenceChangeListener (PreferenceChangeListener l) {
  140. getPreferences ().removePreferenceChangeListener (l);
  141. }
  142. static class SystemProxySettings extends ProxySettings {
  143. public static String getHttpHost () {
  144. if (isSystemProxyDetect ()) {
  145. return getSystemProxyHost ();
  146. } else {
  147. return "";
  148. }
  149. }
  150. public static String getHttpPort () {
  151. if (isSystemProxyDetect ()) {
  152. return getSystemProxyPort ();
  153. } else {
  154. return "";
  155. }
  156. }
  157. public static String getHttpsHost () {
  158. if (isSystemProxyDetect ()) {
  159. return getSystemProxyHost ();
  160. } else {
  161. return "";
  162. }
  163. }
  164. public static String getHttpsPort () {
  165. if (isSystemProxyDetect ()) {
  166. return getSystemProxyPort ();
  167. } else {
  168. return "";
  169. }
  170. }
  171. public static String getSocksHost () {
  172. if (isSystemSocksServerDetect ()) {
  173. return getSystemSocksServerHost ();
  174. } else {
  175. return "";
  176. }
  177. }
  178. public static String getSocksPort () {
  179. if (isSystemSocksServerDetect ()) {
  180. return getSystemSocksServerPort ();
  181. } else {
  182. return "";
  183. }
  184. }
  185. public static String getNonProxyHosts () {
  186. return getDefaultUserNonProxyHosts ();
  187. }
  188. // helper methods
  189. private static boolean isSystemProxyDetect () {
  190. if (NbProxySelector.useSystemProxies ()) {
  191. return true;
  192. }
  193. String s = System.getProperty ("netbeans.system_http_proxy"); // NOI18N
  194. return s != null && ! DIRECT.equals (s); // NOI18N
  195. }
  196. private static String getSystemProxyHost () {
  197. String systemProxy = System.getProperty ("netbeans.system_http_proxy"); // NOI18N
  198. if (systemProxy == null) {
  199. return ""; // NOI18N
  200. }
  201. int i = systemProxy.lastIndexOf (":"); // NOI18N
  202. if (i <= 0 || i >= systemProxy.length () - 1) {
  203. return ""; // NOI18N
  204. }
  205. return normalizeProxyHost (systemProxy.substring (0, i));
  206. }
  207. private static String getSystemProxyPort () {
  208. String systemProxy = System.getProperty ("netbeans.system_http_proxy"); // NOI18N
  209. if (systemProxy == null) {
  210. return ""; // NOI18N
  211. }
  212. int i = systemProxy.lastIndexOf (":"); // NOI18N
  213. if (i <= 0 || i >= systemProxy.length () - 1) {
  214. return ""; // NOI18N
  215. }
  216. String p = systemProxy.substring (i + 1);
  217. if (p.indexOf ('/') >= 0) {
  218. p = p.substring (0, p.indexOf ('/'));
  219. }
  220. return p;
  221. }
  222. private static boolean isSystemSocksServerDetect () {
  223. return isSystemProxyDetect () && System.getProperty ("netbeans.system_socks_proxy") != null; // NOI18N
  224. }
  225. private static String getSystemSocksServerHost () {
  226. String systemProxy = System.getProperty ("netbeans.system_socks_proxy"); // NOI18N
  227. if (systemProxy == null) {
  228. return ""; // NOI18N
  229. }
  230. int i = systemProxy.lastIndexOf (":"); // NOI18N
  231. if (i <= 0 || i >= systemProxy.length () - 1) {
  232. return ""; // NOI18N
  233. }
  234. return normalizeProxyHost (systemProxy.substring (0, i));
  235. }
  236. private static String getSystemSocksServerPort () {
  237. String systemProxy = System.getProperty ("netbeans.system_socks_proxy"); // NOI18N
  238. if (systemProxy == null) {
  239. return ""; // NOI18N
  240. }
  241. int i = systemProxy.lastIndexOf (":"); // NOI18N
  242. if (i <= 0 || i >= systemProxy.length () - 1) {
  243. return ""; // NOI18N
  244. }
  245. String p = systemProxy.substring (i + 1);
  246. if (p.indexOf ('/') >= 0) {
  247. p = p.substring (0, p.indexOf ('/'));
  248. }
  249. return p;
  250. }
  251. }
  252. private static String getSystemNonProxyHosts () {
  253. String systemProxy = System.getProperty ("netbeans.system_http_non_proxy_hosts"); // NOI18N
  254. return systemProxy == null ? "" : systemProxy;
  255. }
  256. private static String getPresetNonProxyHosts () {
  257. if (presetNonProxyHosts == null) {
  258. presetNonProxyHosts = System.getProperty ("http.nonProxyHosts", "");
  259. }
  260. return presetNonProxyHosts;
  261. }
  262. private static String getDefaultUserNonProxyHosts () {
  263. return getModifiedNonProxyHosts (getSystemNonProxyHosts ());
  264. }
  265. private static String getModifiedNonProxyHosts (String systemPreset) {
  266. String fromSystem = systemPreset.replaceAll (";", "|").replaceAll (",", "|"); //NOI18N
  267. String fromUser = getPresetNonProxyHosts () == null ? "" : getPresetNonProxyHosts ().replaceAll (";", "|").replaceAll (",", "|"); //NOI18N
  268. if (Utilities.isWindows ()) {
  269. fromSystem = addReguralToNonProxyHosts (fromSystem);
  270. }
  271. String nonProxy = fromUser + (fromUser.length () == 0 ? "" : "|") + fromSystem + (fromSystem.length () == 0 ? "" : "|") + "localhost|127.0.0.1"; // NOI18N
  272. String localhost = ""; // NOI18N
  273. try {
  274. localhost = InetAddress.getLocalHost().getHostName();
  275. if (!"localhost".equals(localhost)) { // NOI18N
  276. nonProxy = nonProxy + "|" + localhost; // NOI18N
  277. } else {
  278. // Avoid this error when hostname == localhost:
  279. // Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
  280. }
  281. }
  282. catch (UnknownHostException e) {
  283. // OK. Sometimes a hostname is assigned by DNS, but a computer
  284. // is later pulled off the network. It may then produce a bogus
  285. // name for itself which can't actually be resolved. Normally
  286. // "localhost" is aliased to 127.0.0.1 anyway.
  287. }
  288. /* per Milan's agreement it's removed. See issue #89868
  289. try {
  290. String localhost2 = InetAddress.getLocalHost().getCanonicalHostName();
  291. if (!"localhost".equals(localhost2) && !localhost2.equals(localhost)) { // NOI18N
  292. nonProxy = nonProxy + "|" + localhost2; // NOI18N
  293. } else {
  294. // Avoid this error when hostname == localhost:
  295. // Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
  296. }
  297. }
  298. catch (UnknownHostException e) {
  299. // OK. Sometimes a hostname is assigned by DNS, but a computer
  300. // is later pulled off the network. It may then produce a bogus
  301. // name for itself which can't actually be resolved. Normally
  302. // "localhost" is aliased to 127.0.0.1 anyway.
  303. }
  304. */
  305. return compactNonProxyHosts (nonProxy);
  306. }
  307. // avoid duplicate hosts
  308. private static String compactNonProxyHosts (String nonProxyHost) {
  309. StringTokenizer st = new StringTokenizer (nonProxyHost, "|"); //NOI18N
  310. Set<String> s = new HashSet<String> ();
  311. StringBuilder compactedProxyHosts = new StringBuilder();
  312. while (st.hasMoreTokens ()) {
  313. String t = st.nextToken ();
  314. if (s.add (t.toLowerCase (Locale.US))) {
  315. if (compactedProxyHosts.length() > 0)
  316. compactedProxyHosts.append('|');
  317. compactedProxyHosts.append(t);
  318. }
  319. }
  320. return compactedProxyHosts.toString();
  321. }
  322. private static String addReguralToNonProxyHosts (String nonProxyHost) {
  323. StringTokenizer st = new StringTokenizer (nonProxyHost, "|");
  324. StringBuilder reguralProxyHosts = new StringBuilder();
  325. while (st.hasMoreTokens ()) {
  326. String t = st.nextToken ();
  327. if (t.indexOf ('*') == -1) { //NOI18N
  328. t = t + '*'; //NOI18N
  329. }
  330. if (reguralProxyHosts.length() > 0)
  331. reguralProxyHosts.append('|');
  332. reguralProxyHosts.append(t);
  333. }
  334. return reguralProxyHosts.toString();
  335. }
  336. private static String normalizeProxyHost (String proxyHost) {
  337. if (proxyHost.toLowerCase (Locale.US).startsWith ("http://")) { // NOI18N
  338. return proxyHost.substring (7, proxyHost.length ());
  339. } else {
  340. return proxyHost;
  341. }
  342. }
  343. }