@Test public void testUseProxyForSocketsSettingShouldntAffectHTTP() throws Exception { ProxyConfig config = new MyProxyConfig(false, false, Proxy.Type.HTTP, hostAndPort, creds); ProxyForURI proxy = new ProxyForURI(config); Field useProxyForSockets = proxy.getClass().getDeclaredField("useProxyForSockets"); useProxyForSockets.setAccessible(true); useProxyForSockets.setBoolean(proxy, false); URI uri = new URI("http: assertEquals(proxy.apply(uri), new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080))); }
@Override public Proxy apply(URI endpoint) { if (!useProxyForSockets && "socket".equals(endpoint.getScheme())) { return Proxy.NO_PROXY; } if (config.getProxy().isPresent()) { SocketAddress addr = new InetSocketAddress(config.getProxy().get().getHostText(), config.getProxy().get() .getPort()); Proxy proxy = new Proxy(config.getType(), addr); final Optional<Credentials> creds = config.getCredentials(); if (creds.isPresent()) { Authenticator authenticator = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(creds.get().identity, creds.get().credential.toCharArray()); } }; Authenticator.setDefault(authenticator); } return proxy; } if (config.isJvmProxyEnabled()) { return getDefaultProxy(endpoint); } if (config.useSystem()) { System.setProperty("java.net.useSystemProxies", "true"); return getDefaultProxy(endpoint); } return Proxy.NO_PROXY; }
ProxyForURI implements Function<URI, Proxy> { @Override public Proxy apply(URI endpoint) { if (!useProxyForSockets && "socket".equals(endpoint.getScheme())) { return Proxy.NO_PROXY; } if (config.getProxy().isPresent()) { SocketAddress addr = new InetSocketAddress(config.getProxy().get().getHostText(), config.getProxy().get() .getPort()); Proxy proxy = new Proxy(config.getType(), addr); final Optional<Credentials> creds = config.getCredentials(); if (creds.isPresent()) { Authenticator authenticator = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(creds.get().identity, creds.get().credential.toCharArray()); } }; Authenticator.setDefault(authenticator); } return proxy; } if (config.isJvmProxyEnabled()) { return getDefaultProxy(endpoint); } if (config.useSystem()) { System.setProperty("java.net.useSystemProxies", "true"); return getDefaultProxy(endpoint); } return Proxy.NO_PROXY; } }
ProxyForURI implements Function<URI, Proxy> { @Override public Proxy apply(URI endpoint) { if (!useProxyForSockets && "socket".equals(endpoint.getScheme())) { return Proxy.NO_PROXY; } if (config.getProxy().isPresent()) { SocketAddress addr = new InetSocketAddress(config.getProxy().get().getHostText(), config.getProxy().get() .getPort()); Proxy proxy = new Proxy(config.getType(), addr); final Optional<Credentials> creds = config.getCredentials(); if (creds.isPresent()) { Authenticator authenticator = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(creds.get().identity, creds.get().credential.toCharArray()); } }; Authenticator.setDefault(authenticator); } return proxy; } if (config.isJvmProxyEnabled()) { return getDefaultProxy(endpoint); } if (config.useSystem()) { System.setProperty("java.net.useSystemProxies", "true"); return getDefaultProxy(endpoint); } return Proxy.NO_PROXY; } @VisibleForTesting @Inject  ProxyForURI(ProxyConfig config); }
ProxyForURI implements Function<URI, Proxy> { @Override public Proxy apply(URI endpoint) { if (!useProxyForSockets && "socket".equals(endpoint.getScheme())) { return Proxy.NO_PROXY; } if (config.getProxy().isPresent()) { SocketAddress addr = new InetSocketAddress(config.getProxy().get().getHostText(), config.getProxy().get() .getPort()); Proxy proxy = new Proxy(config.getType(), addr); final Optional<Credentials> creds = config.getCredentials(); if (creds.isPresent()) { Authenticator authenticator = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(creds.get().identity, creds.get().credential.toCharArray()); } }; Authenticator.setDefault(authenticator); } return proxy; } if (config.isJvmProxyEnabled()) { return getDefaultProxy(endpoint); } if (config.useSystem()) { System.setProperty("java.net.useSystemProxies", "true"); return getDefaultProxy(endpoint); } return Proxy.NO_PROXY; } @VisibleForTesting @Inject  ProxyForURI(ProxyConfig config); @Override Proxy apply(URI endpoint); }
ProxyForURI implements Function<URI, Proxy> { @Override public Proxy apply(URI endpoint) { if (!useProxyForSockets && "socket".equals(endpoint.getScheme())) { return Proxy.NO_PROXY; } if (config.getProxy().isPresent()) { SocketAddress addr = new InetSocketAddress(config.getProxy().get().getHostText(), config.getProxy().get() .getPort()); Proxy proxy = new Proxy(config.getType(), addr); final Optional<Credentials> creds = config.getCredentials(); if (creds.isPresent()) { Authenticator authenticator = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(creds.get().identity, creds.get().credential.toCharArray()); } }; Authenticator.setDefault(authenticator); } return proxy; } if (config.isJvmProxyEnabled()) { return getDefaultProxy(endpoint); } if (config.useSystem()) { System.setProperty("java.net.useSystemProxies", "true"); return getDefaultProxy(endpoint); } return Proxy.NO_PROXY; } @VisibleForTesting @Inject  ProxyForURI(ProxyConfig config); @Override Proxy apply(URI endpoint);  }