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

The following examples show how to use java.net.ProxySelector#setDefault() . 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: 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 2
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 3
Source File: GitTest.java    From steady with Apache License 2.0 6 votes vote down vote up
private void setupProxy() {

		final String phost = System.getProperty("http.proxyHost");
		final String pport = System.getProperty("http.proxyPort");

		try {
			if (phost != null && pport != null) {
				System.err.println("Using proxy " + phost + ":" + pport);
				ProxySelector.setDefault(new MyProxySelector(new Proxy(
						Proxy.Type.HTTP, new InetSocketAddress(phost, Integer
								.parseInt(pport)))));
			} else {
				System.err.printf("No proxy specified, connecting directly.");
			}
		} catch (Exception e) {
			System.err.println("Error setting proxy");
			e.printStackTrace();
		}

	}
 
Example 4
Source File: Proxy.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/** @hide */
public static final void setHttpProxySystemProperty(String host, String port, String exclList,
        Uri pacFileUrl) {
    if (exclList != null) exclList = exclList.replace(",", "|");
    if (false) Log.d(TAG, "setHttpProxySystemProperty :"+host+":"+port+" - "+exclList);
    if (host != null) {
        System.setProperty("http.proxyHost", host);
        System.setProperty("https.proxyHost", host);
    } else {
        System.clearProperty("http.proxyHost");
        System.clearProperty("https.proxyHost");
    }
    if (port != null) {
        System.setProperty("http.proxyPort", port);
        System.setProperty("https.proxyPort", port);
    } else {
        System.clearProperty("http.proxyPort");
        System.clearProperty("https.proxyPort");
    }
    if (exclList != null) {
        System.setProperty("http.nonProxyHosts", exclList);
        System.setProperty("https.nonProxyHosts", exclList);
    } else {
        System.clearProperty("http.nonProxyHosts");
        System.clearProperty("https.nonProxyHosts");
    }
    if (!Uri.EMPTY.equals(pacFileUrl)) {
        ProxySelector.setDefault(new PacProxySelector());
    } else {
        ProxySelector.setDefault(sDefaultProxySelector);
    }
}
 
Example 5
Source File: BadProxySelector.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 {
    ProxySelector.setDefault(new HTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }

   ProxySelector.setDefault(new NullHTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }
}
 
Example 6
Source File: BadProxySelector.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 {
    ProxySelector.setDefault(new HTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }

   ProxySelector.setDefault(new NullHTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }
}
 
Example 7
Source File: BadProxySelector.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ProxySelector.setDefault(new HTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }

   ProxySelector.setDefault(new NullHTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }
}
 
Example 8
Source File: ProxyCacheTestUtils.java    From AndriodVideoCache with Apache License 2.0 5 votes vote down vote up
public static void installExternalSystemProxy() {
    // see proxies list at http://proxylist.hidemyass.com/
    Proxy systemProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("162.8.230.7", 11180));
    ProxySelector mockedProxySelector = Mockito.mock(ProxySelector.class);
    when(mockedProxySelector.select(Mockito.<URI>any())).thenReturn(Lists.newArrayList(systemProxy));
    ProxySelector.setDefault(mockedProxySelector);
}
 
Example 9
Source File: BadProxySelector.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 {
    ProxySelector.setDefault(new HTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }

   ProxySelector.setDefault(new NullHTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }
}
 
Example 10
Source File: CoreBridgeImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Register NB specific property editors.
     *  Allows property editor unit tests to work correctly without 
     *  initializing full NetBeans environment.
     *  @since 1.98 */
    private static final void doRegisterPropertyEditors() {
        //issue 31879
//        if (editorsRegistered) return;
//        String[] syspesp = PropertyEditorManager.getEditorSearchPath();
//        String[] nbpesp = new String[] {
//            "org.netbeans.beaninfo.editors", // NOI18N
//            "org.openide.explorer.propertysheet.editors", // NOI18N
//        };
//        String[] allpesp = new String[syspesp.length + nbpesp.length];
//        System.arraycopy(nbpesp, 0, allpesp, 0, nbpesp.length);
//        System.arraycopy(syspesp, 0, allpesp, nbpesp.length, syspesp.length);
//        PropertyEditorManager.setEditorSearchPath(allpesp);
//        PropertyEditorManager.registerEditor (java.lang.Character.TYPE, org.netbeans.beaninfo.editors.CharEditor.class);
//        PropertyEditorManager.registerEditor(String[].class, org.netbeans.beaninfo.editors.StringArrayEditor.class); 
//        // use replacement hintable/internationalizable primitive editors - issues 20376, 5278
//        PropertyEditorManager.registerEditor (Integer.TYPE, org.netbeans.beaninfo.editors.IntEditor.class);
//        PropertyEditorManager.registerEditor (Boolean.TYPE, org.netbeans.beaninfo.editors.BoolEditor.class);
        
        NodeOp.registerPropertyEditors();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                NodeOp.registerPropertyEditors();
            }
        });

        ProxySelector selector = Lookup.getDefault().lookup(ProxySelector.class);
        if (selector != null) {
            // install java.net.ProxySelector
            ProxySelector.setDefault(selector);
        }

        editorsRegistered = true;
    }
 
Example 11
Source File: BadProxySelector.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 {
    ProxySelector.setDefault(new HTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }

   ProxySelector.setDefault(new NullHTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }
}
 
Example 12
Source File: BadProxySelector.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 {
    ProxySelector.setDefault(new HTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }

   ProxySelector.setDefault(new NullHTTPProxySelector());
    try (ServerSocket ss = new ServerSocket(0);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept()) {
    }
}
 
Example 13
Source File: ExceptionHandlerTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
protected void tearDown() throws Exception {
    super.tearDown();
    ProxySelector.setDefault(defaultPS);         
}
 
Example 14
Source File: SvnConfigFilesTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void tearDown() throws Exception {
    super.tearDown();
    ProxySelector.setDefault(defaultPS);
    System.setProperty("netbeans.t9y.svn.user.config.path", "");
    System.setProperty("netbeans.t9y.svn.nb.config.path", "");
}
 
Example 15
Source File: NetworkSettingsTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void tearDown() {
    ProxySelector.setDefault(defaultPS);
}
 
Example 16
Source File: IgnoreHostProxySelector.java    From DKVideoPlayer 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 17
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 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: ProxyCacheTestUtils.java    From AndriodVideoCache with Apache License 2.0 4 votes vote down vote up
public static void resetSystemProxy() {
    ProxySelector mockedProxySelector = Mockito.mock(ProxySelector.class);
    when(mockedProxySelector.select(Mockito.<URI>any())).thenReturn(Lists.newArrayList(Proxy.NO_PROXY));
    ProxySelector.setDefault(mockedProxySelector);
}
 
Example 20
Source File: IgnoreHostProxySelector.java    From VideoOS-Android-SDK 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);
}