/** * Creates a proxy server instance from the given properties. * * <p>Currently the default http.* proxy properties are supported as well as properties specific * for AHC. * * @param properties the properties to evaluate. Must not be null. * @return a ProxyServer instance or null, if no valid properties were set. * @see <a * href="http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html">Networking * Properties</a> * @see #PROXY_HOST * @see #PROXY_PORT * @see #PROXY_PROTOCOL * @see #PROXY_NONPROXYHOSTS */ public static ProxyServer createProxy(Properties properties) { String host = System.getProperty(PROXY_HOST); if (host != null) { int port = Integer.valueOf(System.getProperty(PROXY_PORT, "80")); Protocol protocol; try { protocol = Protocol.valueOf(System.getProperty(PROXY_PROTOCOL, "HTTP")); } catch (IllegalArgumentException e) { protocol = Protocol.HTTP; } ProxyServer proxyServer = new ProxyServer( protocol, host, port, System.getProperty(PROXY_USER), System.getProperty(PROXY_PASSWORD)); String nonProxyHosts = System.getProperties().getProperty(PROXY_NONPROXYHOSTS); if (nonProxyHosts != null) { for (String spec : nonProxyHosts.split("\\|")) { proxyServer.addNonProxyHost(spec); } } return proxyServer; } return null; }
/** * Checks whether proxy should be used according to nonProxyHosts settings of it, or we want to go * directly to target host. If <code>null</code> proxy is passed in, this method returns true -- * since there is NO proxy, we should avoid to use it. Simple hostname pattern matching using "*" * are supported, but only as prefixes. See * http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html * * @param proxyServer * @param target the hostname * @return true if we have to avoid proxy use (obeying non-proxy hosts settings), false otherwise. */ public static boolean avoidProxy(final ProxyServer proxyServer, final String target) { if (proxyServer != null) { final String targetHost = target.toLowerCase(); List<String> nonProxyHosts = proxyServer.getNonProxyHosts(); if (nonProxyHosts != null && nonProxyHosts.size() > 0) { for (String nonProxyHost : nonProxyHosts) { if (nonProxyHost.startsWith("*") && nonProxyHost.length() > 1 && targetHost.endsWith(nonProxyHost.substring(1).toLowerCase())) { return true; } else if (nonProxyHost.equalsIgnoreCase(targetHost)) { return true; } } } return false; } else { return true; } }