PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/glassfish.common/src/org/netbeans/modules/glassfish/common/ProxySettings.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 403 lines | 268 code | 59 blank | 76 comment | 61 complexity | 335d721a70927b3ee7a34a0d1e77751f 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.glassfish.common;
  45. import org.netbeans.modules.glassfish.spi.*;
  46. import java.net.InetAddress;
  47. import java.net.UnknownHostException;
  48. import java.util.HashSet;
  49. import java.util.Locale;
  50. import java.util.Set;
  51. import java.util.StringTokenizer;
  52. import java.util.prefs.PreferenceChangeListener;
  53. import java.util.prefs.Preferences;
  54. import org.openide.util.NbPreferences;
  55. import org.openide.util.Utilities;
  56. /**
  57. * Stolen from core by vkraemer
  58. *
  59. * @author Jiri Rechtacek
  60. */
  61. class ProxySettings {
  62. public static final String PROXY_HTTP_HOST = "proxyHttpHost";
  63. public static final String PROXY_HTTP_PORT = "proxyHttpPort";
  64. public static final String PROXY_HTTPS_HOST = "proxyHttpsHost";
  65. public static final String PROXY_HTTPS_PORT = "proxyHttpsPort";
  66. public static final String PROXY_SOCKS_HOST = "proxySocksHost";
  67. public static final String PROXY_SOCKS_PORT = "proxySocksPort";
  68. public static final String NOT_PROXY_HOSTS = "proxyNonProxyHosts";
  69. public static final String PROXY_TYPE = "proxyType";
  70. public static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication";
  71. public static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername";
  72. public static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword";
  73. public static final String USE_PROXY_ALL_PROTOCOLS = "useProxyAllProtocols";
  74. public static final String DIRECT = "DIRECT";
  75. private static String presetNonProxyHosts;
  76. /** No proxy is used to connect. */
  77. public static final int DIRECT_CONNECTION = 0;
  78. /** Proxy setting is automaticaly detect in OS. */
  79. public static final int AUTO_DETECT_PROXY = 1; // as default
  80. /** Manualy set proxy host and port. */
  81. public static final int MANUAL_SET_PROXY = 2;
  82. private static Preferences getPreferences() {
  83. return NbPreferences.forModule (ProxySettings.class);
  84. }
  85. public static String getHttpHost () {
  86. return normalizeProxyHost (getPreferences ().get (PROXY_HTTP_HOST, ""));
  87. }
  88. public static String getHttpPort () {
  89. return getPreferences ().get (PROXY_HTTP_PORT, "");
  90. }
  91. public static String getHttpsHost () {
  92. if (useProxyAllProtocols ()) {
  93. return getHttpHost ();
  94. } else {
  95. return getPreferences ().get (PROXY_HTTPS_HOST, "");
  96. }
  97. }
  98. public static String getHttpsPort () {
  99. if (useProxyAllProtocols ()) {
  100. return getHttpPort ();
  101. } else {
  102. return getPreferences ().get (PROXY_HTTPS_PORT, "");
  103. }
  104. }
  105. public static String getSocksHost () {
  106. if (useProxyAllProtocols ()) {
  107. return getHttpHost ();
  108. } else {
  109. return getPreferences ().get (PROXY_SOCKS_HOST, "");
  110. }
  111. }
  112. public static String getSocksPort () {
  113. if (useProxyAllProtocols ()) {
  114. return getHttpPort ();
  115. } else {
  116. return getPreferences ().get (PROXY_SOCKS_PORT, "");
  117. }
  118. }
  119. public static String getNonProxyHosts () {
  120. return getPreferences ().get (NOT_PROXY_HOSTS, getDefaultUserNonProxyHosts ());
  121. }
  122. public static int getProxyType () {
  123. return getPreferences ().getInt (PROXY_TYPE, AUTO_DETECT_PROXY);
  124. }
  125. public static boolean useAuthentication () {
  126. return getPreferences ().getBoolean (USE_PROXY_AUTHENTICATION, false);
  127. }
  128. public static boolean useProxyAllProtocols () {
  129. return getPreferences ().getBoolean (USE_PROXY_ALL_PROTOCOLS, false);
  130. }
  131. public static String getAuthenticationUsername () {
  132. return getPreferences ().get (PROXY_AUTHENTICATION_USERNAME, "");
  133. }
  134. public static char[] getAuthenticationPassword () {
  135. return getPreferences ().get (PROXY_AUTHENTICATION_PASSWORD, "").toCharArray ();
  136. }
  137. static void addPreferenceChangeListener (PreferenceChangeListener l) {
  138. getPreferences ().addPreferenceChangeListener (l);
  139. }
  140. static void removePreferenceChangeListener (PreferenceChangeListener l) {
  141. getPreferences ().removePreferenceChangeListener (l);
  142. }
  143. static class SystemProxySettings extends ProxySettings {
  144. public static String getHttpHost () {
  145. if (isSystemProxyDetect ()) {
  146. return getSystemProxyHost ();
  147. } else {
  148. return "";
  149. }
  150. }
  151. public static String getHttpPort () {
  152. if (isSystemProxyDetect ()) {
  153. return getSystemProxyPort ();
  154. } else {
  155. return "";
  156. }
  157. }
  158. public static String getHttpsHost () {
  159. if (isSystemProxyDetect ()) {
  160. return getSystemProxyHost ();
  161. } else {
  162. return "";
  163. }
  164. }
  165. public static String getHttpsPort () {
  166. if (isSystemProxyDetect ()) {
  167. return getSystemProxyPort ();
  168. } else {
  169. return "";
  170. }
  171. }
  172. public static String getSocksHost () {
  173. if (isSystemSocksServerDetect ()) {
  174. return getSystemSocksServerHost ();
  175. } else {
  176. return "";
  177. }
  178. }
  179. public static String getSocksPort () {
  180. if (isSystemSocksServerDetect ()) {
  181. return getSystemSocksServerPort ();
  182. } else {
  183. return "";
  184. }
  185. }
  186. public static String getNonProxyHosts () {
  187. return getDefaultUserNonProxyHosts ();
  188. }
  189. // helper methods
  190. private static boolean isSystemProxyDetect () {
  191. if (NbProxySelector.useSystemProxies ()) {
  192. return true;
  193. }
  194. String s = System.getProperty ("netbeans.system_http_proxy"); // NOI18N
  195. return s != null && ! DIRECT.equals (s); // NOI18N
  196. }
  197. private static String getSystemProxyHost () {
  198. String systemProxy = System.getProperty ("netbeans.system_http_proxy"); // NOI18N
  199. if (systemProxy == null) {
  200. return ""; // NOI18N
  201. }
  202. int i = systemProxy.lastIndexOf (":"); // NOI18N
  203. if (i <= 0 || i >= systemProxy.length () - 1) {
  204. return ""; // NOI18N
  205. }
  206. return normalizeProxyHost (systemProxy.substring (0, i));
  207. }
  208. private static String getSystemProxyPort () {
  209. String systemProxy = System.getProperty ("netbeans.system_http_proxy"); // NOI18N
  210. if (systemProxy == null) {
  211. return ""; // NOI18N
  212. }
  213. int i = systemProxy.lastIndexOf (":"); // NOI18N
  214. if (i <= 0 || i >= systemProxy.length () - 1) {
  215. return ""; // NOI18N
  216. }
  217. String p = systemProxy.substring (i + 1);
  218. if (p.indexOf ('/') >= 0) {
  219. p = p.substring (0, p.indexOf ('/'));
  220. }
  221. return p;
  222. }
  223. private static boolean isSystemSocksServerDetect () {
  224. return isSystemProxyDetect () && System.getProperty ("netbeans.system_socks_proxy") != null; // NOI18N
  225. }
  226. private static String getSystemSocksServerHost () {
  227. String systemProxy = System.getProperty ("netbeans.system_socks_proxy"); // NOI18N
  228. if (systemProxy == null) {
  229. return ""; // NOI18N
  230. }
  231. int i = systemProxy.lastIndexOf (":"); // NOI18N
  232. if (i <= 0 || i >= systemProxy.length () - 1) {
  233. return ""; // NOI18N
  234. }
  235. return normalizeProxyHost (systemProxy.substring (0, i));
  236. }
  237. private static String getSystemSocksServerPort () {
  238. String systemProxy = System.getProperty ("netbeans.system_socks_proxy"); // NOI18N
  239. if (systemProxy == null) {
  240. return ""; // NOI18N
  241. }
  242. int i = systemProxy.lastIndexOf (":"); // NOI18N
  243. if (i <= 0 || i >= systemProxy.length () - 1) {
  244. return ""; // NOI18N
  245. }
  246. String p = systemProxy.substring (i + 1);
  247. if (p.indexOf ('/') >= 0) {
  248. p = p.substring (0, p.indexOf ('/'));
  249. }
  250. return p;
  251. }
  252. }
  253. private static String getSystemNonProxyHosts () {
  254. String systemProxy = System.getProperty ("netbeans.system_http_non_proxy_hosts"); // NOI18N
  255. return systemProxy == null ? "" : systemProxy;
  256. }
  257. private static String getPresetNonProxyHosts () {
  258. if (presetNonProxyHosts == null) {
  259. presetNonProxyHosts = System.getProperty ("http.nonProxyHosts", "");
  260. }
  261. return presetNonProxyHosts;
  262. }
  263. private static String getDefaultUserNonProxyHosts () {
  264. return getModifiedNonProxyHosts (getSystemNonProxyHosts ());
  265. }
  266. private static String getModifiedNonProxyHosts (String systemPreset) {
  267. String fromSystem = systemPreset.replaceAll (";", "|").replaceAll (",", "|"); //NOI18N
  268. String fromUser = getPresetNonProxyHosts () == null ? "" : getPresetNonProxyHosts ().replaceAll (";", "|").replaceAll (",", "|"); //NOI18N
  269. if (Utilities.isWindows ()) {
  270. fromSystem = addReguralToNonProxyHosts (fromSystem);
  271. }
  272. String nonProxy = fromUser + (fromUser.length () == 0 ? "" : "|") + fromSystem + (fromSystem.length () == 0 ? "" : "|") + "localhost|127.0.0.1"; // NOI18N
  273. String localhost = ""; // NOI18N
  274. try {
  275. localhost = InetAddress.getLocalHost().getHostName();
  276. if (!"localhost".equals(localhost)) { // NOI18N
  277. nonProxy = nonProxy + "|" + localhost; // NOI18N
  278. } else {
  279. // Avoid this error when hostname == localhost:
  280. // Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
  281. }
  282. }
  283. catch (UnknownHostException e) {
  284. // OK. Sometimes a hostname is assigned by DNS, but a computer
  285. // is later pulled off the network. It may then produce a bogus
  286. // name for itself which can't actually be resolved. Normally
  287. // "localhost" is aliased to 127.0.0.1 anyway.
  288. }
  289. /* per Milan's agreement it's removed. See issue #89868
  290. try {
  291. String localhost2 = InetAddress.getLocalHost().getCanonicalHostName();
  292. if (!"localhost".equals(localhost2) && !localhost2.equals(localhost)) { // NOI18N
  293. nonProxy = nonProxy + "|" + localhost2; // NOI18N
  294. } else {
  295. // Avoid this error when hostname == localhost:
  296. // Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
  297. }
  298. }
  299. catch (UnknownHostException e) {
  300. // OK. Sometimes a hostname is assigned by DNS, but a computer
  301. // is later pulled off the network. It may then produce a bogus
  302. // name for itself which can't actually be resolved. Normally
  303. // "localhost" is aliased to 127.0.0.1 anyway.
  304. }
  305. */
  306. return compactNonProxyHosts (nonProxy);
  307. }
  308. // avoid duplicate hosts
  309. private static String compactNonProxyHosts (String nonProxyHost) {
  310. StringTokenizer st = new StringTokenizer (nonProxyHost, "|"); //NOI18N
  311. Set<String> s = new HashSet<String> ();
  312. StringBuilder compactedProxyHosts = new StringBuilder();
  313. while (st.hasMoreTokens ()) {
  314. String t = st.nextToken ();
  315. if (s.add (t.toLowerCase (Locale.US))) {
  316. if (compactedProxyHosts.length() > 0)
  317. compactedProxyHosts.append('|');
  318. compactedProxyHosts.append(t);
  319. }
  320. }
  321. return compactedProxyHosts.toString();
  322. }
  323. private static String addReguralToNonProxyHosts (String nonProxyHost) {
  324. StringTokenizer st = new StringTokenizer (nonProxyHost, "|");
  325. StringBuilder reguralProxyHosts = new StringBuilder();
  326. while (st.hasMoreTokens ()) {
  327. String t = st.nextToken ();
  328. if (t.indexOf ('*') == -1) { //NOI18N
  329. t = t + '*'; //NOI18N
  330. }
  331. if (reguralProxyHosts.length() > 0)
  332. reguralProxyHosts.append('|');
  333. reguralProxyHosts.append(t);
  334. }
  335. return reguralProxyHosts.toString();
  336. }
  337. private static String normalizeProxyHost (String proxyHost) {
  338. if (proxyHost.toLowerCase (Locale.US).startsWith ("http://")) { // NOI18N
  339. return proxyHost.substring (7, proxyHost.length ());
  340. } else {
  341. return proxyHost;
  342. }
  343. }
  344. }