Search in sources :

Example 6 with Proxy

use of org.eclipse.ceylon.common.config.Proxies.Proxy in project ceylon by eclipse.

the class ProxiesTest method testProxyWithNonProxyHosts.

@Test
public void testProxyWithNonProxyHosts() throws Exception {
    CeylonConfig testConfig = loadTestConfig("proxy+non-proxy-hosts.config");
    Proxies proxies = getProxies(testConfig);
    Proxy proxy = proxies.getProxy();
    Assert.assertEquals("myproxy", proxy.getHost());
    Assert.assertEquals(1234, proxy.getPort());
    Authentication auth = Authentication.fromConfig(testConfig);
    Assert.assertNull(auth.getProxyAuthenticator());
    auth.installProxy();
    Assert.assertEquals(null, auth.getProxyAuthenticator());
    InetSocketAddress address = (InetSocketAddress) auth.getProxy().address();
    Assert.assertEquals("myproxy", address.getHostName());
    Assert.assertEquals(1234, address.getPort());
    List<java.net.Proxy> selectedProxies = auth.getProxySelector().select(URI.create("http://anything"));
    Assert.assertEquals(1, selectedProxies.size());
    Assert.assertEquals(java.net.Proxy.Type.HTTP, selectedProxies.get(0).type());
    Assert.assertEquals("myproxy", ((InetSocketAddress) selectedProxies.get(0).address()).getHostName());
    Assert.assertEquals(1234, ((InetSocketAddress) selectedProxies.get(0).address()).getPort());
    selectedProxies = auth.getProxySelector().select(URI.create("http://foo"));
    Assert.assertEquals(1, selectedProxies.size());
    Assert.assertEquals(java.net.Proxy.Type.DIRECT, selectedProxies.get(0).type());
    Assert.assertNull(selectedProxies.get(0).address());
    mockPrompt.assertSeenOnlyGivenPrompts();
}
Also used : Proxy(org.eclipse.ceylon.common.config.Proxies.Proxy) Proxies(org.eclipse.ceylon.common.config.Proxies) CeylonConfig(org.eclipse.ceylon.common.config.CeylonConfig) Authentication(org.eclipse.ceylon.common.config.Authentication) PasswordAuthentication(java.net.PasswordAuthentication) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 7 with Proxy

use of org.eclipse.ceylon.common.config.Proxies.Proxy in project ceylon by eclipse.

the class ProxyTool method main.

public static void main(String[] args) throws Exception {
    if (args.length != 3) {
        usage("Wrong number of arguments");
    }
    String mode = args[0];
    File configFile = new File(args[1]);
    if (!configFile.exists()) {
        usage("Config file doesn't exist");
        return;
    }
    final URL url;
    try {
        url = new URL(args[2]);
    } catch (MalformedURLException e) {
        usage("Malformed URL");
        return;
    }
    CeylonConfig config = CeylonConfigFinder.loadConfigFromFile(configFile);
    Authentication auth = Authentication.fromConfig(config);
    Proxy proxy = Proxies.withConfig(config).getProxy();
    final URLConnection conn;
    switch(mode) {
        case "proxy":
            java.net.Proxy netProxy = auth.getProxy();
            if (netProxy != null) {
                conn = url.openConnection(netProxy);
            } else {
                conn = url.openConnection();
            }
            break;
        case "install":
            auth.installProxy();
            conn = url.openConnection();
            break;
        default:
            throw new RuntimeException("Unsupported mode");
    }
    InputStream in = conn.getInputStream();
    try {
        int ch = in.read();
        while (ch != -1) {
            System.out.append((char) ch);
            ch = in.read();
        }
    } finally {
        in.close();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) URL(java.net.URL) URLConnection(java.net.URLConnection) Proxy(org.eclipse.ceylon.common.config.Proxies.Proxy) CeylonConfig(org.eclipse.ceylon.common.config.CeylonConfig) Authentication(org.eclipse.ceylon.common.config.Authentication) File(java.io.File)

Aggregations

Authentication (org.eclipse.ceylon.common.config.Authentication)7 CeylonConfig (org.eclipse.ceylon.common.config.CeylonConfig)7 Proxy (org.eclipse.ceylon.common.config.Proxies.Proxy)7 PasswordAuthentication (java.net.PasswordAuthentication)6 Proxies (org.eclipse.ceylon.common.config.Proxies)6 Test (org.junit.Test)6 InetSocketAddress (java.net.InetSocketAddress)5 File (java.io.File)3 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1