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

The following examples show how to use java.net.ProxySelector#getDefault() . 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 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 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 3
Source File: ExceptionHandlerTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();
    System.setProperty("netbeans.user", getWorkDir().getAbsolutePath());
    
    // reset
    Method m = MylynSupport.class.getDeclaredMethod("reset", new Class[0]);
    m.setAccessible(true);
    m.invoke(MylynSupport.class);
            
    Field f = Bugzilla.class.getDeclaredField("instance");
    f.setAccessible(true);
    f.set(Bugzilla.class, null);
    
    brc = Bugzilla.getInstance().getRepositoryConnector();
    
    WebUtil.init();
    
    if (defaultPS == null) {
        defaultPS = ProxySelector.getDefault();
    }
}
 
Example 4
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 5
Source File: MultiThreadedSystemProxies.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("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 6
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 7
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 8
Source File: NonProxyHostsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp () throws Exception {
    super.setUp ();
    System.setProperty ("netbeans.system_http_proxy", SYSTEM_PROXY_HOST + ":" + SYSTEM_PROXY_PORT);
    System.setProperty ("netbeans.system_socks_proxy", SYSTEM_PROXY_HOST + ":" + SYSTEM_PROXY_PORT);
    System.setProperty ("netbeans.system_http_non_proxy_hosts", "*.other.org");
    System.setProperty ("http.nonProxyHosts", "*.netbeans.org");
    selector = ProxySelector.getDefault ();
    proxyPreferences  = NbPreferences.root ().node ("/org/netbeans/core");
    proxyPreferences.addPreferenceChangeListener (new PreferenceChangeListener () {
        public void preferenceChange (PreferenceChangeEvent arg0) {
            isWaiting = false;
        }
    });
    proxyPreferences.put ("proxyHttpHost", USER_PROXY_HOST);
    proxyPreferences.put ("proxyHttpPort", USER_PROXY_PORT);
    proxyPreferences.put ("proxySocksHost", USER_PROXY_HOST);
    proxyPreferences.put ("proxySocksPort", USER_PROXY_PORT);
    while (isWaiting);
    isWaiting = true;
    TO_LOCALHOST = new URI ("http://localhost");
    TO_LOCAL_DOMAIN_1 = new URI ("http://core.netbeans.org");
    TO_LOCAL_DOMAIN_2 = new URI ("http://core.other.org");
    TO_EXTERNAL = new URI ("http://worldwide.net");
    
    SOCKS_TO_LOCALHOST = new URI ("socket://localhost:8041");
    SOCKS_TO_LOCAL_DOMAIN_1 = new URI ("socket://core.netbeans.org");
    SOCKS_TO_LOCAL_DOMAIN_2 = new URI ("socket://core.other.org");
    SOCKS_TO_EXTERNAL = new URI ("socket://worldwide.net");
}
 
Example 9
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 10
Source File: Pinger.java    From HaoReader 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 11
Source File: HttpClientFactory.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
private HttpRoutePlanner createRoutePlanner() {
    // If http.nonProxyHosts is not set, then "localhost" is used as a default value, which prevents setting a proxy on localhost.
    if (System.getProperty("http.nonProxyHosts") == null) {
        System.setProperty("http.nonProxyHosts", "");
    }
    return new SystemDefaultRoutePlanner(ProxySelector.getDefault());
}
 
Example 12
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 13
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 14
Source File: HttpClient.java    From GriefDefender with MIT License 5 votes vote down vote up
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
    ProxySelector def = ProxySelector.getDefault();
    if (def != null) {
        def.connectFailed(uri, sa, ioe);
    }
}
 
Example 15
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 16
Source File: HttpClient.java    From GriefDefender with MIT License 5 votes vote down vote up
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
    ProxySelector def = ProxySelector.getDefault();
    if (def != null) {
        def.connectFailed(uri, sa, ioe);
    }
}
 
Example 17
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 18
Source File: IgnoreHostProxySelector.java    From AndriodVideoCache with Apache License 2.0 4 votes vote down vote up
static void install(String hostToIgnore, int portToIgnore) {
    ProxySelector defaultProxySelector = ProxySelector.getDefault();
    ProxySelector ignoreHostProxySelector = new IgnoreHostProxySelector(defaultProxySelector, hostToIgnore, portToIgnore);
    ProxySelector.setDefault(ignoreHostProxySelector);
}
 
Example 19
Source File: IgnoreHostProxySelector.java    From HaoReader with GNU General Public License v3.0 4 votes vote down vote up
static void install(String hostToIgnore, int portToIgnore) {
    ProxySelector defaultProxySelector = ProxySelector.getDefault();
    ProxySelector ignoreHostProxySelector = new IgnoreHostProxySelector(defaultProxySelector, hostToIgnore, portToIgnore);
    ProxySelector.setDefault(ignoreHostProxySelector);
}
 
Example 20
Source File: ProxyDetectorImpl.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public ProxySelector get() {
  // TODO(spencerfang): consider using java.security.AccessController here
  return ProxySelector.getDefault();
}