Java Code Examples for java.net.ProxySelector#select()

The following examples show how to use java.net.ProxySelector#select() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiThreadedSystemProxies.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("java.net.useSystemProxies", "true");
    final ProxySelector ps = ProxySelector.getDefault();
    final URI uri = new URI("http://ubuntu.com");
    Thread[] threads = new Thread[NUM_THREADS];

    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ps.select(uri);
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }
        });
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].start();
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].join();
    }
}
 
Example 2
Source File: MultiThreadedSystemProxies.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("java.net.useSystemProxies", "true");
    final ProxySelector ps = ProxySelector.getDefault();
    final URI uri = new URI("http://ubuntu.com");
    Thread[] threads = new Thread[NUM_THREADS];

    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ps.select(uri);
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }
        });
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].start();
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].join();
    }
}
 
Example 3
Source File: MultiThreadedSystemProxies.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("java.net.useSystemProxies", "true");
    final ProxySelector ps = ProxySelector.getDefault();
    final URI uri = new URI("http://ubuntu.com");
    Thread[] threads = new Thread[NUM_THREADS];

    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ps.select(uri);
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }
        });
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].start();
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].join();
    }
}
 
Example 4
Source File: MultiThreadedSystemProxies.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("java.net.useSystemProxies", "true");
    final ProxySelector ps = ProxySelector.getDefault();
    final URI uri = new URI("http://ubuntu.com");
    Thread[] threads = new Thread[NUM_THREADS];

    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ps.select(uri);
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }
        });
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].start();
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].join();
    }
}
 
Example 5
Source File: ProxySelectorTest.java    From AndriodVideoCache with Apache License 2.0 6 votes vote down vote up
@Test // https://github.com/danikula/AndroidVideoCache/issues/28
public void testIgnoring() throws Exception {
    InetSocketAddress proxyAddress = new InetSocketAddress("proxy.com", 80);
    Proxy systemProxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
    ProxySelector mockedProxySelector = Mockito.mock(ProxySelector.class);
    when(mockedProxySelector.select(Mockito.<URI>any())).thenReturn(Lists.newArrayList(systemProxy));
    ProxySelector.setDefault(mockedProxySelector);

    IgnoreHostProxySelector.install("localhost", 42);

    ProxySelector proxySelector = ProxySelector.getDefault();
    List<Proxy> githubProxies = proxySelector.select(new URI("http://github.com"));
    assertThat(githubProxies).hasSize(1);
    assertThat(githubProxies.get(0).address()).isEqualTo(proxyAddress);

    List<Proxy> localhostProxies = proxySelector.select(new URI("http://localhost:42"));
    assertThat(localhostProxies).hasSize(1);
    assertThat(localhostProxies.get(0)).isEqualTo(Proxy.NO_PROXY);

    List<Proxy> localhostPort69Proxies = proxySelector.select(new URI("http://localhost:69"));
    assertThat(localhostPort69Proxies).hasSize(1);
    assertThat(localhostPort69Proxies.get(0).address()).isEqualTo(proxyAddress);
}
 
Example 6
Source File: Env.java    From openvisualtraceroute with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static Proxy getProxy() {
	List<Proxy> l = null;
	try {
		final ProxySelector def = ProxySelector.getDefault();
		l = def.select(new URI("http://foo/bar"));
		ProxySelector.setDefault(null);
	} catch (final Exception e) {

	}
	if (l != null) {
		for (final Iterator<Proxy> iter = l.iterator(); iter.hasNext();) {
			final java.net.Proxy proxy = iter.next();
			return proxy;
		}
	}
	return null;
}
 
Example 7
Source File: B6563259.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("http.proxyHost", "myproxy");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("http.nonProxyHosts", "host1.*");
    ProxySelector sel = ProxySelector.getDefault();
    java.util.List<Proxy> l = sel.select(new URI("http://HOST1.sun.com/"));
    if (l.get(0) != Proxy.NO_PROXY) {
        throw new RuntimeException("ProxySelector returned the wrong proxy");
    }
}
 
Example 8
Source File: HttpClient.java    From GriefDefender with MIT License 5 votes vote down vote up
@Override
public List<Proxy> select(URI uri) {
    ProxySelector def = ProxySelector.getDefault();
    if (def == null) {
        return DIRECT;
    }
    return def.select(uri);
}
 
Example 9
Source File: B6563259.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("http.proxyHost", "myproxy");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("http.nonProxyHosts", "host1.*");
    ProxySelector sel = ProxySelector.getDefault();
    java.util.List<Proxy> l = sel.select(new URI("http://HOST1.sun.com/"));
    if (l.get(0) != Proxy.NO_PROXY) {
        throw new RuntimeException("ProxySelector returned the wrong proxy");
    }
}
 
Example 10
Source File: Pinger.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
private List<Proxy> getDefaultProxies() {
    try {
        ProxySelector defaultProxySelector = ProxySelector.getDefault();
        return defaultProxySelector.select(new URI(getPingUrl()));
    } catch (URISyntaxException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 11
Source File: B6563259.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("http.proxyHost", "myproxy");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("http.nonProxyHosts", "host1.*");
    ProxySelector sel = ProxySelector.getDefault();
    java.util.List<Proxy> l = sel.select(new URI("http://HOST1.sun.com/"));
    if (l.get(0) != Proxy.NO_PROXY) {
        throw new RuntimeException("ProxySelector returned the wrong proxy");
    }
}
 
Example 12
Source File: B6563259.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("http.proxyHost", "myproxy");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("http.nonProxyHosts", "host1.*");
    ProxySelector sel = ProxySelector.getDefault();
    java.util.List<Proxy> l = sel.select(new URI("http://HOST1.sun.com/"));
    if (l.get(0) != Proxy.NO_PROXY) {
        throw new RuntimeException("ProxySelector returned the wrong proxy");
    }
}
 
Example 13
Source File: HttpClientAdapter.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieve Proxy Selector
 *
 * @param uri target uri
 * @return proxy selector
 */
private static List<Proxy> getProxyForURI(java.net.URI uri) {
    LOGGER.debug("get Default proxy selector");
    ProxySelector proxySelector = ProxySelector.getDefault();
    LOGGER.debug("getProxyForURI(" + uri + ')');
    List<Proxy> proxies = proxySelector.select(uri);
    LOGGER.debug("got system proxies:" + proxies);
    return proxies;
}
 
Example 14
Source File: Pinger.java    From AndriodVideoCache with Apache License 2.0 5 votes vote down vote up
private List<Proxy> getDefaultProxies() {
    try {
        ProxySelector defaultProxySelector = ProxySelector.getDefault();
        return defaultProxySelector.select(new URI(getPingUrl()));
    } catch (URISyntaxException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 15
Source File: Pinger.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
private List<Proxy> getDefaultProxies() {
    try {
        ProxySelector defaultProxySelector = ProxySelector.getDefault();
        return defaultProxySelector.select(new URI(getPingUrl()));
    } catch (URISyntaxException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 16
Source File: B6563259.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("http.proxyHost", "myproxy");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("http.nonProxyHosts", "host1.*");
    ProxySelector sel = ProxySelector.getDefault();
    java.util.List<Proxy> l = sel.select(new URI("http://HOST1.sun.com/"));
    if (l.get(0) != Proxy.NO_PROXY) {
        throw new RuntimeException("ProxySelector returned the wrong proxy");
    }
}
 
Example 17
Source File: MultiThreadedSystemProxies.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("java.net.useSystemProxies", "true");
    final ProxySelector ps = ProxySelector.getDefault();
    final URI uri = new URI("http://ubuntu.com");
    Thread[] threads = new Thread[NUM_THREADS];

    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ps.select(uri);
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }
        });
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].start();
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].join();
    }
}
 
Example 18
Source File: B6563259.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("http.proxyHost", "myproxy");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("http.nonProxyHosts", "host1.*");
    ProxySelector sel = ProxySelector.getDefault();
    java.util.List<Proxy> l = sel.select(new URI("http://HOST1.sun.com/"));
    if (l.get(0) != Proxy.NO_PROXY) {
        throw new RuntimeException("ProxySelector returned the wrong proxy");
    }
}
 
Example 19
Source File: Proxy.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Return the proxy object to be used for the URL given as parameter.
 * @param ctx A Context used to get the settings for the proxy host.
 * @param url A URL to be accessed. Used to evaluate exclusion list.
 * @return Proxy (java.net) object containing the host name. If the
 *         user did not set a hostname it returns the default host.
 *         A null value means that no host is to be used.
 * {@hide}
 */
public static final java.net.Proxy getProxy(Context ctx, String url) {
    String host = "";
    if ((url != null) && !isLocalHost(host)) {
        URI uri = URI.create(url);
        ProxySelector proxySelector = ProxySelector.getDefault();

        List<java.net.Proxy> proxyList = proxySelector.select(uri);

        if (proxyList.size() > 0) {
            return proxyList.get(0);
        }
    }
    return java.net.Proxy.NO_PROXY;
}
 
Example 20
Source File: HttpClient.java    From GriefDefender with MIT License 5 votes vote down vote up
@Override
public List<Proxy> select(URI uri) {
    ProxySelector def = ProxySelector.getDefault();
    if (def == null) {
        return DIRECT;
    }
    return def.select(uri);
}