PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 341 lines | 264 code | 21 blank | 56 comment | 95 complexity | 129398ab732c9a88fb79514d5bdeba69 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.io.IOException;
  46. import java.lang.reflect.Method;
  47. import java.net.InetAddress;
  48. import java.net.InetSocketAddress;
  49. import java.net.Proxy;
  50. import java.net.ProxySelector;
  51. import java.net.SocketAddress;
  52. import java.net.URI;
  53. import java.net.UnknownHostException;
  54. import java.util.ArrayList;
  55. import java.util.Collections;
  56. import java.util.List;
  57. import java.util.Locale;
  58. import java.util.StringTokenizer;
  59. import java.util.logging.Level;
  60. import java.util.logging.Logger;
  61. import java.util.prefs.PreferenceChangeEvent;
  62. import java.util.prefs.PreferenceChangeListener;
  63. import java.util.regex.Pattern;
  64. import java.util.regex.PatternSyntaxException;
  65. /**
  66. * Stolen from core by vkraemer.
  67. *
  68. * @author Jiri Rechtacek
  69. */
  70. public final class NbProxySelector extends ProxySelector {
  71. private ProxySelector original = null;
  72. private static Logger log = Logger.getLogger (NbProxySelector.class.getName ());
  73. private static Object useSystemProxies;
  74. /** Creates a new instance of NbProxySelector */
  75. public NbProxySelector () {
  76. original = ProxySelector.getDefault ();
  77. if (original == null) {
  78. log.warning ("No default system ProxySelector was found thus NetBeans ProxySelector won't delegate on it");
  79. } else {
  80. log.fine ("Override the original ProxySelector: " + original);
  81. }
  82. log.fine ("java.net.useSystemProxies has been set to " + useSystemProxies ());
  83. log.fine ("In launcher was detected netbeans.system_http_proxy: " + System.getProperty ("netbeans.system_http_proxy", "N/A"));
  84. log.fine ("In launcher was detected netbeans.system_socks_proxy: " + System.getProperty ("netbeans.system_socks_proxy", "N/A"));
  85. ProxySettings.addPreferenceChangeListener (new ProxySettingsListener ());
  86. copySettingsToSystem ();
  87. }
  88. public List<Proxy> select(URI uri) {
  89. List<Proxy> res = new ArrayList<Proxy> ();
  90. int proxyType = ProxySettings.getProxyType ();
  91. if (ProxySettings.DIRECT_CONNECTION == proxyType) {
  92. res = Collections.singletonList (Proxy.NO_PROXY);
  93. } else if (ProxySettings.AUTO_DETECT_PROXY == proxyType) {
  94. if (useSystemProxies ()) {
  95. if (original != null) {
  96. res = original.select (uri);
  97. }
  98. } else {
  99. String protocol = uri.getScheme ();
  100. assert protocol != null : "Invalid scheme of uri " + uri + ". Scheme cannot be null!";
  101. if (dontUseProxy (ProxySettings.SystemProxySettings.getNonProxyHosts (), uri.getHost ())) {
  102. res.add (Proxy.NO_PROXY);
  103. }
  104. if (protocol.toLowerCase (Locale.US).startsWith("http")) {
  105. String ports = ProxySettings.SystemProxySettings.getHttpPort ();
  106. if (ports != null && ports.length () > 0 && ProxySettings.SystemProxySettings.getHttpHost ().length () > 0) {
  107. int porti = Integer.parseInt(ports);
  108. Proxy p = new Proxy (Proxy.Type.HTTP, new InetSocketAddress (ProxySettings.SystemProxySettings.getHttpHost (), porti));
  109. res.add (p);
  110. }
  111. } else { // supposed SOCKS
  112. String ports = ProxySettings.SystemProxySettings.getSocksPort ();
  113. String hosts = ProxySettings.SystemProxySettings.getSocksHost ();
  114. if (ports != null && ports.length () > 0 && hosts.length () > 0) {
  115. int porti = Integer.parseInt(ports);
  116. Proxy p = new Proxy (Proxy.Type.SOCKS, new InetSocketAddress (hosts, porti));
  117. res.add (p);
  118. }
  119. }
  120. if (original != null) {
  121. res.addAll (original.select (uri));
  122. }
  123. }
  124. } else if (ProxySettings.MANUAL_SET_PROXY == proxyType) {
  125. String protocol = uri.getScheme ();
  126. assert protocol != null : "Invalid scheme of uri " + uri + ". Scheme cannot be null!";
  127. // handling nonProxyHosts first
  128. if (dontUseProxy (ProxySettings.getNonProxyHosts (), uri.getHost ())) {
  129. res.add (Proxy.NO_PROXY);
  130. }
  131. if (protocol.toLowerCase (Locale.US).startsWith("http")) {
  132. String hosts = ProxySettings.getHttpHost ();
  133. String ports = ProxySettings.getHttpPort ();
  134. if (ports != null && ports.length () > 0 && hosts.length () > 0) {
  135. int porti = Integer.parseInt(ports);
  136. Proxy p = new Proxy (Proxy.Type.HTTP, new InetSocketAddress (hosts, porti));
  137. res.add (p);
  138. } else {
  139. log.info ("Incomplete HTTP Proxy [" + hosts + "/" + ports + "] found in ProxySelector[Type: " + ProxySettings.getProxyType () + "] for uri " + uri + ". ");
  140. if (original != null) {
  141. log.finest ("Fallback to the default ProxySelector which returns " + original.select (uri));
  142. res.addAll (original.select (uri));
  143. }
  144. }
  145. } else { // supposed SOCKS
  146. String ports = ProxySettings.getSocksPort ();
  147. String hosts = ProxySettings.getSocksHost ();
  148. if (ports != null && ports.length () > 0 && hosts.length () > 0) {
  149. int porti = Integer.parseInt(ports);
  150. Proxy p = new Proxy (Proxy.Type.SOCKS, new InetSocketAddress (hosts, porti));
  151. res.add (p);
  152. } else {
  153. log.info ("Incomplete SOCKS Server [" + hosts + "/" + ports + "] found in ProxySelector[Type: " + ProxySettings.getProxyType () + "] for uri " + uri + ". ");
  154. if (original != null) {
  155. log.finest ("Fallback to the default ProxySelector which returns " + original.select (uri));
  156. res.addAll (original.select (uri));
  157. }
  158. }
  159. }
  160. res.add (Proxy.NO_PROXY);
  161. } else {
  162. assert false : "Invalid proxy type: " + ProxySettings.getProxyType ();
  163. }
  164. log.finest ("NbProxySelector[Type: " + ProxySettings.getProxyType () +
  165. ", Use HTTP for all protocols: " + ProxySettings.useProxyAllProtocols ()+
  166. "] returns " + res + " for URI " + uri);
  167. return res;
  168. }
  169. public void connectFailed (URI arg0, SocketAddress arg1, IOException arg2) {
  170. log.log (Level.INFO, "connectionFailed(" + arg0 + ", " + arg1 +")", arg2);
  171. }
  172. // several modules listenes on these properties and propagates it futher
  173. private class ProxySettingsListener implements PreferenceChangeListener {
  174. public void preferenceChange(PreferenceChangeEvent evt) {
  175. if (evt.getKey ().startsWith ("proxy") || evt.getKey ().startsWith ("useProxy")) {
  176. copySettingsToSystem ();
  177. }
  178. }
  179. }
  180. private void copySettingsToSystem () {
  181. String host = null, port = null, nonProxyHosts = null;
  182. String sHost = null, sPort = null;
  183. String httpsHost = null, httpsPort = null;
  184. int proxyType = ProxySettings.getProxyType ();
  185. if (ProxySettings.DIRECT_CONNECTION == proxyType) {
  186. host = null;
  187. port = null;
  188. httpsHost = null;
  189. httpsPort = null;
  190. nonProxyHosts = null;
  191. sHost = null;
  192. sPort = null;
  193. } else if (ProxySettings.AUTO_DETECT_PROXY == proxyType) {
  194. host = ProxySettings.SystemProxySettings.getHttpHost ();
  195. port = ProxySettings.SystemProxySettings.getHttpPort ();
  196. httpsHost = ProxySettings.SystemProxySettings.getHttpsHost ();
  197. httpsPort = ProxySettings.SystemProxySettings.getHttpsPort ();
  198. nonProxyHosts = ProxySettings.SystemProxySettings.getNonProxyHosts ();
  199. sHost = ProxySettings.SystemProxySettings.getSocksHost ();
  200. sPort = ProxySettings.SystemProxySettings.getSocksPort ();
  201. } else if (ProxySettings.MANUAL_SET_PROXY == proxyType) {
  202. host = ProxySettings.getHttpHost ();
  203. port = ProxySettings.getHttpPort ();
  204. httpsHost = ProxySettings.getHttpsHost ();
  205. httpsPort = ProxySettings.getHttpsPort ();
  206. nonProxyHosts = ProxySettings.getNonProxyHosts ();
  207. sHost = ProxySettings.getSocksHost ();
  208. sPort = ProxySettings.getSocksPort ();
  209. } else {
  210. assert false : "Invalid proxy type: " + proxyType;
  211. }
  212. setOrClearProperty ("http.proxyHost", host, false);
  213. setOrClearProperty ("http.proxyPort", port, true);
  214. setOrClearProperty ("http.nonProxyHosts", nonProxyHosts, false);
  215. setOrClearProperty ("https.proxyHost", httpsHost, false);
  216. setOrClearProperty ("https.proxyPort", httpsPort, true);
  217. setOrClearProperty ("https.nonProxyHosts", nonProxyHosts, false);
  218. setOrClearProperty ("socksProxyHost", sHost, false);
  219. setOrClearProperty ("socksProxyPort", sPort, true);
  220. log.fine ("Set System's http.proxyHost/Port/NonProxyHost to " + host + "/" + port + "/" + nonProxyHosts);
  221. log.fine ("Set System's https.proxyHost/Port to " + httpsHost + "/" + httpsPort);
  222. log.fine ("Set System's socksProxyHost/Port to " + sHost + "/" + sPort);
  223. }
  224. private void setOrClearProperty (String key, String value, boolean isInteger) {
  225. assert key != null;
  226. if (value == null || value.length () == 0) {
  227. System.clearProperty (key);
  228. } else {
  229. if (isInteger) {
  230. try {
  231. Integer.parseInt (value);
  232. } catch (NumberFormatException nfe) {
  233. log.log (Level.INFO, nfe.getMessage(), nfe);
  234. }
  235. }
  236. System.setProperty (key, value);
  237. }
  238. }
  239. // package-private for unit-testing
  240. static boolean dontUseProxy (String nonProxyHosts, String host) {
  241. if (host == null) return false;
  242. // try IP adress first
  243. if (dontUseIp (nonProxyHosts, host)) {
  244. return true;
  245. } else {
  246. return dontUseHostName (nonProxyHosts, host);
  247. }
  248. }
  249. private static boolean dontUseHostName (String nonProxyHosts, String host) {
  250. if (host == null) return false;
  251. boolean dontUseProxy = false;
  252. StringTokenizer st = new StringTokenizer (nonProxyHosts, "|", false);
  253. while (st.hasMoreTokens () && !dontUseProxy) {
  254. String token = st.nextToken ();
  255. int star = token.indexOf ("*");
  256. if (star == -1) {
  257. dontUseProxy = token.equals (host);
  258. if (dontUseProxy) {
  259. log.finest ("NbProxySelector[Type: " + ProxySettings.getProxyType () + "]. Host " + host + " found in nonProxyHosts: " + nonProxyHosts);
  260. }
  261. } else {
  262. String start = token.substring (0, star - 1 < 0 ? 0 : star - 1);
  263. String end = token.substring (star + 1 > token.length () ? token.length () : star + 1);
  264. dontUseProxy = host.startsWith(start) && host.endsWith(end);
  265. if (dontUseProxy) {
  266. log.finest ("NbProxySelector[Type: " + ProxySettings.getProxyType () + "]. Host " + host + " found in nonProxyHosts: " + nonProxyHosts);
  267. }
  268. }
  269. }
  270. return dontUseProxy;
  271. }
  272. private static boolean dontUseIp (String nonProxyHosts, String host) {
  273. if (host == null) return false;
  274. String ip = null;
  275. try {
  276. ip = InetAddress.getByName (host).getHostAddress ();
  277. } catch (UnknownHostException ex) {
  278. log.log (Level.FINE, ex.getLocalizedMessage (), ex);
  279. }
  280. if (ip == null) {
  281. return false;
  282. }
  283. boolean dontUseProxy = false;
  284. StringTokenizer st = new StringTokenizer (nonProxyHosts, "|", false);
  285. while (st.hasMoreTokens () && !dontUseProxy) {
  286. String nonProxyHost = st.nextToken ();
  287. int star = nonProxyHost.indexOf ("*");
  288. if (star == -1) {
  289. dontUseProxy = nonProxyHost.equals (ip);
  290. if (dontUseProxy) {
  291. log.finest ("NbProxySelector[Type: " + ProxySettings.getProxyType () + "]. Host's IP " + ip + " found in nonProxyHosts: " + nonProxyHosts);
  292. }
  293. } else {
  294. // match with given dotted-quad IP
  295. try {
  296. dontUseProxy = Pattern.matches (nonProxyHost, ip);
  297. if (dontUseProxy) {
  298. log.finest ("NbProxySelector[Type: " + ProxySettings.getProxyType () + "]. Host's IP" + ip + " found in nonProxyHosts: " + nonProxyHosts);
  299. }
  300. } catch (PatternSyntaxException pse) {
  301. // may ignore it here
  302. }
  303. }
  304. }
  305. return dontUseProxy;
  306. }
  307. // NetProperties is JDK vendor specific, access only by reflection
  308. static boolean useSystemProxies () {
  309. if (useSystemProxies == null) {
  310. try {
  311. Class clazz = Class.forName ("sun.net.NetProperties");
  312. Method getBoolean = clazz.getMethod ("getBoolean", String.class);
  313. useSystemProxies = getBoolean.invoke (null, "java.net.useSystemProxies");
  314. } catch (Exception x) {
  315. log.log (Level.FINEST, "Cannot get value of java.net.useSystemProxies bacause " + x.getMessage(), x);
  316. }
  317. }
  318. return useSystemProxies != null && "true".equalsIgnoreCase (useSystemProxies.toString ());
  319. }
  320. }