JaxWsProxySelector extends ProxySelector { @Override public List<Proxy> select(URI uri) { List<Proxy> list = Lists.newArrayList(); if (uri == null) { throw new IllegalArgumentException("URI can't be null."); } String scheme = uri.getScheme().toLowerCase(); if (SUPPORTED_SCHEMES.contains(scheme)){ Proxy proxy = getProxy(scheme, proxyProperties.getProperty(scheme + "." + PROXY_HOST), proxyProperties.getProperty(scheme + "." + PROXY_PORT), proxyProperties.getProperty(scheme + "." + PROXY_USER)); if (proxy != null){ list.add(proxy); } } if (scheme.startsWith("http") || "ftp".equals(scheme) || scheme.startsWith("socks")){ Proxy proxy = getProxy("socks", proxyProperties.getProperty(SOCKS_PROXY_HOST), proxyProperties.getProperty(SOCKS_PROXY_PORT), proxyProperties.getProperty(SOCKS_PROXY_USER)); if (proxy != null){ list.add(proxy); } } if (list.size() > 0) { return list; } return delegateSelect(uri); } JaxWsProxySelector(@Nullable ProxySelector proxySelector, Properties properties);  JaxWsProxySelector(@Nullable ProxySelector proxySelector); @Override List<Proxy> select(URI uri); @Override void connectFailed(URI uri, SocketAddress socketAddress, IOException ioException);  }
@Test public void testSelect_Direct() { JaxWsProxySelector ps = new JaxWsProxySelector(ProxySelector.getDefault()); ProxySelector.setDefault(ps); List<Proxy> list = ps.select(testHttpUri); assertEquals(list.get(0), Proxy.NO_PROXY); }
@Test public void testSelect_HTTP() throws UnknownHostException { systemProperties.setProperty(HTTP_PROXY_HOST, LOCALHOST); systemProperties.setProperty(HTTP_PROXY_PORT, TEST_PORT_STR); JaxWsProxySelector ps = new JaxWsProxySelector(ProxySelector.getDefault(), systemProperties); ProxySelector.setDefault(ps); List<Proxy> list = ps.select(testHttpUri); assertEquals(list.get(0), new Proxy(Type.HTTP, new InetSocketAddress(InetAddress.getByName(LOCALHOST), TEST_PORT_NUM))); }
@Test public void testSelect_HTTPS() throws UnknownHostException { systemProperties.setProperty(HTTPS_PROXY_HOST, LOCALHOST); systemProperties.setProperty(HTTPS_PROXY_PORT, TEST_PORT_STR); JaxWsProxySelector ps = new JaxWsProxySelector(ProxySelector.getDefault(), systemProperties); ProxySelector.setDefault(ps); List<Proxy> list = ps.select(testHttpsUri); assertEquals(list.get(0), new Proxy(Type.HTTP , new InetSocketAddress(InetAddress.getByName(LOCALHOST), TEST_PORT_NUM))); }
@Test public void testSelect_FTP() throws UnknownHostException { systemProperties.setProperty(FTP_PROXY_HOST, LOCALHOST); systemProperties.setProperty(FTP_PROXY_PORT, TEST_PORT_STR); JaxWsProxySelector ps = new JaxWsProxySelector(ProxySelector.getDefault(), systemProperties); ProxySelector.setDefault(ps); List<Proxy> list = ps.select(testFtpUri); assertEquals(list.get(0), new Proxy(Type.HTTP , new InetSocketAddress(InetAddress.getByName(LOCALHOST), TEST_PORT_NUM))); }
@Test public void testSelect_Socks() throws UnknownHostException { systemProperties.setProperty(SOCKS_PROXY_HOST, LOCALHOST); systemProperties.setProperty(SOCKS_PROXY_PORT, TEST_PORT_STR); JaxWsProxySelector ps = new JaxWsProxySelector(ProxySelector.getDefault(), systemProperties); ProxySelector.setDefault(ps); List<Proxy> list = ps.select(testSocksUri); assertEquals(list.get(0), new Proxy(Type.SOCKS , new InetSocketAddress(InetAddress.getByName(LOCALHOST), TEST_PORT_NUM))); }
@Test(expected = IllegalArgumentException.class) public void testSelect_IllegalArgumentException() { JaxWsProxySelector ps = new JaxWsProxySelector(ProxySelector.getDefault()); ps.select(null); }