@Override public List<Proxy> select(URI uri) { boolean ignored = hostToIgnore.equals(uri.getHost()) && portToIgnore == uri.getPort(); return ignored ? NO_PROXY_LIST : defaultProxySelector.select(uri); }
/** * Selects a proxy for the given URL. * * @param requestTokenURL The URL * @return The proxy. */ protected Proxy selectProxy(URL requestTokenURL) { try { List<Proxy> selectedProxies = getProxySelector().select(requestTokenURL.toURI()); return selectedProxies.isEmpty() ? Proxy.NO_PROXY : selectedProxies.get(0); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } }
private List<Proxy> getDefaultProxies() { try { ProxySelector defaultProxySelector = ProxySelector.getDefault(); return defaultProxySelector.select(new URI(getPingUrl())); } catch (URISyntaxException e) { throw new IllegalStateException(e); } }
/** Prepares the proxy servers to try. */ private void resetNextProxy(HttpUrl url, Proxy proxy) { if (proxy != null) { // If the user specifies a proxy, try that and only that. proxies = Collections.singletonList(proxy); } else { // Try each of the ProxySelector choices until one connection succeeds. List<Proxy> proxiesOrNull = address.proxySelector().select(url.uri()); proxies = proxiesOrNull != null && !proxiesOrNull.isEmpty() ? Util.immutableList(proxiesOrNull) : Util.immutableList(Proxy.NO_PROXY); } nextProxyIndex = 0; }
@VisibleForTesting static String getProxySynthesis(URI uri, ProxySelector proxySelector) { List<Proxy> proxies = proxySelector.select(uri); if (proxies.size() == 1 && proxies.get(0).type().equals(Proxy.Type.DIRECT)) { return "no proxy"; } List<String> descriptions = Lists.newArrayList(); for (Proxy proxy : proxies) { if (proxy.type() != Proxy.Type.DIRECT) { descriptions.add(proxy.type() + " proxy: " + proxy.address()); } } return Joiner.on(", ").join(descriptions); }
/** Prepares the proxy servers to try. */ private void resetNextProxy(HttpUrl url, Proxy proxy) { if (proxy != null) { // If the user specifies a proxy, try that and only that. proxies = Collections.singletonList(proxy); } else { // Try each of the ProxySelector choices until one connection succeeds. List<Proxy> proxiesOrNull = address.proxySelector().select(url.uri()); proxies = proxiesOrNull != null && !proxiesOrNull.isEmpty() ? Util.immutableList(proxiesOrNull) : Util.immutableList(Proxy.NO_PROXY); } nextProxyIndex = 0; }
System.setProperty("java.net.useSystemProxies", "true"); System.out.println("detecting proxies"); List l = null; try { l = ProxySelector.getDefault().select(new URI("http://foo/bar")); } catch (URISyntaxException e) { e.printStackTrace(); } if (l != null) { for (Iterator iter = l.iterator(); iter.hasNext();) { java.net.Proxy proxy = (java.net.Proxy) iter.next(); System.out.println("proxy type: " + proxy.type()); InetSocketAddress addr = (InetSocketAddress) proxy.address(); if (addr == null) { System.out.println("No Proxy"); } else { System.out.println("proxy hostname: " + addr.getHostName()); System.setProperty("http.proxyHost", addr.getHostName()); System.out.println("proxy port: " + addr.getPort()); System.setProperty("http.proxyPort", Integer.toString(addr.getPort())); } } }
ProxySelector selector = ProxySelector.getDefault(); StringBuilder result = new StringBuilder(); for (Proxy proxy : selector.select(destURI)) { SocketAddress address = proxy.address(); if (address == null) {
try { System.setProperty("java.net.useSystemProxies","true"); List<Proxy> l = ProxySelector.getDefault().select( new URI("http://www.yahoo.com/"));
/** Prepares the proxy servers to try. */ private void resetNextProxy(HttpUrl url, Proxy proxy) { if (proxy != null) { // If the user specifies a proxy, try that and only that. proxies = Collections.singletonList(proxy); } else { // Try each of the ProxySelector choices until one connection succeeds. List<Proxy> proxiesOrNull = address.proxySelector().select(url.uri()); proxies = proxiesOrNull != null && !proxiesOrNull.isEmpty() ? Util.immutableList(proxiesOrNull) : Util.immutableList(Proxy.NO_PROXY); } nextProxyIndex = 0; }
@Test public void shouldGetDirectProxySynthesis() throws URISyntaxException { ProxySelector proxySelector = mock(ProxySelector.class); when(proxySelector.select(any(URI.class))).thenReturn(Arrays.asList(Proxy.NO_PROXY)); assertThat(DefaultHttpDownloader.BaseHttpDownloader.getProxySynthesis(new URI("http://an_url"), proxySelector)).isEqualTo("no proxy"); }
private void configureProxy(ClientConfig clientConfig, URI originalUri, String protocol) { List<Proxy> proxies = ProxySelector.getDefault().select(originalUri); for (Proxy proxy : proxies) { InetSocketAddress address = (InetSocketAddress) proxy.address(); if (address != null) { String hostname = address.getHostName(); int port = address.getPort(); clientConfig.property(ClientProperties.PROXY_URI, "http://" + hostname + ":" + port); String httpProxyUser = System.getProperty(protocol + ".proxyUser"); if (httpProxyUser != null) { clientConfig.property(ClientProperties.PROXY_USERNAME, httpProxyUser); String httpProxyPassword = System.getProperty(protocol + ".proxyPassword"); if (httpProxyPassword != null) { clientConfig.property(ClientProperties.PROXY_PASSWORD, httpProxyPassword); } } } } }
@Test public void shouldGetProxySynthesis() throws URISyntaxException { ProxySelector proxySelector = mock(ProxySelector.class); when(proxySelector.select(any(URI.class))).thenReturn(Arrays.<Proxy>asList(new FakeProxy())); assertThat(DefaultHttpDownloader.BaseHttpDownloader.getProxySynthesis(new URI("http://an_url"), proxySelector)).isEqualTo("HTTP proxy: /123.45.67.89:4040"); }
ProxySelector selector = ProxySelector.getDefault(); if (selector != null) { proxyList = selector.select(uri);
URI javaUri = uri.toJavaNetURI(); List<Proxy> proxies = proxySelector.select(javaUri); if (proxies != null) {
("Cannot convert host to URI: " + target, usx); List<Proxy> proxies = psel.select(targetURI);
private static Proxy chooseProxy(URI uri) { ProxySelector sel = java.security.AccessController .doPrivileged(new java.security.PrivilegedAction<ProxySelector>() { @Override public ProxySelector run() { return ProxySelector.getDefault(); } }); if (sel == null) { return Proxy.NO_PROXY; } //detect usage of user-defined proxy and avoid optimizations in that case if (!"sun.net.spi.DefaultProxySelector".equals(sel.getClass().getName())) { return null; } Iterator<Proxy> it = sel.select(uri).iterator(); if (it.hasNext()) { return it.next(); } return Proxy.NO_PROXY; } }
ProxySearch proxySearch = new ProxySearch(); proxySearch.addStrategy(Strategy.OS_DEFAULT); proxySearch.addStrategy(Strategy.JAVA); proxySearch.addStrategy(Strategy.BROWSER); ProxySelector proxySelector = proxySearch.getProxySelector(); ProxySelector.setDefault(proxySelector); URI home = URI.create("http://www.google.com"); System.out.println("ProxySelector: " + proxySelector); System.out.println("URI: " + home); List<Proxy> proxyList = proxySelector.select(home); if (proxyList != null && !proxyList.isEmpty()) { for (Proxy proxy : proxyList) { System.out.println(proxy); SocketAddress address = proxy.address(); if (address instanceof InetSocketAddress) { String host = ((InetSocketAddress) address).getHostName(); String port = Integer.toString(((InetSocketAddress) address).getPort()); System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", port); } } }
/** * Determine the proxy server (if any) needed to obtain a URL. * * @param proxySelector * proxy support for the caller. * @param u * location of the server caller wants to talk to. * @return proxy to communicate with the supplied URL. * @throws java.net.ConnectException * the proxy could not be computed as the supplied URL could not * be read. This failure should never occur. */ public static Proxy proxyFor(ProxySelector proxySelector, URL u) throws ConnectException { try { return proxySelector.select(u.toURI()).get(0); } catch (URISyntaxException e) { final ConnectException err; err = new ConnectException(MessageFormat.format(JGitText.get().cannotDetermineProxyFor, u)); err.initCause(e); throw err; } }
public Proxy setupProxy(URL url) { List<Proxy> proxies = null; try { proxies = ProxySelector.getDefault().select(url.toURI()); } catch (URISyntaxException e) { // this can't happen } if (proxies != null) { for (Proxy proxy : proxies) { if (proxy.type().equals(Proxy.Type.HTTP)) { return proxy; } } } return null; }