PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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