Search in sources :

Example 41 with ProxySelector

use of java.net.ProxySelector in project okhttp-digest by rburgst.

the class DigestAuthenticatorTest method beforeMethod.

@Before
public void beforeMethod() {
    Connection mockConnection = mock(Connection.class);
    Dns mockDns = mock(Dns.class);
    SocketFactory socketFactory = mock(SocketFactory.class);
    Authenticator proxyAuthenticator = mock(Authenticator.class);
    ProxySelector proxySelector = mock(ProxySelector.class);
    Proxy proxy = mock(Proxy.class);
    // setup some dummy data so that we dont get NPEs
    Address address = new Address("localhost", 8080, mockDns, socketFactory, null, null, null, proxyAuthenticator, null, Collections.singletonList(Protocol.HTTP_1_1), Collections.singletonList(ConnectionSpec.MODERN_TLS), proxySelector);
    InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 8080);
    mockRoute = new Route(address, proxy, inetSocketAddress);
    given(mockConnection.route()).willReturn(mockRoute);
    authenticator = new DigestAuthenticator(new Credentials("user1", "user1"));
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) Address(okhttp3.Address) InetSocketAddress(java.net.InetSocketAddress) SocketFactory(javax.net.SocketFactory) InetSocketAddress(java.net.InetSocketAddress) Connection(okhttp3.Connection) Dns(okhttp3.Dns) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route) Before(org.junit.Before)

Example 42 with ProxySelector

use of java.net.ProxySelector in project okhttp by square.

the class SocksProxyTest method proxySelector.

@Test
public void proxySelector() throws Exception {
    server.enqueue(new MockResponse().setBody("abc"));
    ProxySelector proxySelector = new ProxySelector() {

        @Override
        public List<Proxy> select(URI uri) {
            return Collections.singletonList(socksProxy.proxy());
        }

        @Override
        public void connectFailed(URI uri, SocketAddress socketAddress, IOException e) {
            throw new AssertionError();
        }
    };
    OkHttpClient client = clientTestRule.newClientBuilder().proxySelector(proxySelector).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    Response response = client.newCall(request).execute();
    assertThat(response.body().string()).isEqualTo("abc");
    assertThat(socksProxy.connectionCount()).isEqualTo(1);
}
Also used : ProxySelector(java.net.ProxySelector) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Proxy(java.net.Proxy) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 43 with ProxySelector

use of java.net.ProxySelector in project dropwizard by dropwizard.

the class HttpClientBuilderTest method usesACustomRoutePlanner.

@Test
void usesACustomRoutePlanner() throws Exception {
    final HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(new ProxySelector() {

        @Override
        public List<Proxy> select(URI uri) {
            return Collections.singletonList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.52.1", 8080)));
        }

        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        }
    });
    final CloseableHttpClient httpClient = builder.using(configuration).using(routePlanner).createClient(apacheBuilder, connectionManager, "test").getClient();
    assertThat(httpClient).isNotNull();
    assertThat(spyHttpClientBuilderField("routePlanner", apacheBuilder)).isSameAs(routePlanner);
    assertThat(spyHttpClientField("routePlanner", httpClient)).isSameAs(routePlanner);
}
Also used : ProxySelector(java.net.ProxySelector) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Proxy(java.net.Proxy) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) InetSocketAddress(java.net.InetSocketAddress) List(java.util.List) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner) Test(org.junit.jupiter.api.Test)

Example 44 with ProxySelector

use of java.net.ProxySelector in project open-ecard by ecsec.

the class UpdatingProxySelector method loadSelector.

private synchronized void loadSelector() {
    long now = System.currentTimeMillis();
    long diff = now - lastUpdate;
    if (diff > UPDATE_DELTA) {
        lastUpdate = now;
        ProxySelector selector = supplier.find();
        if (selector == null) {
            selector = new NoProxySelector();
        }
        this.lastSelector = selector;
    }
}
Also used : ProxySelector(java.net.ProxySelector)

Example 45 with ProxySelector

use of java.net.ProxySelector in project scout.rt by eclipse.

the class ConfigurableProxySelector method select.

@Override
public List<Proxy> select(URI uri) {
    LOG.trace("Selecting proxy for {}.", uri);
    if (!m_proxyIgnoreList.isEmpty()) {
        for (Pattern ignorePattern : m_proxyIgnoreList) {
            if (ignorePattern.matcher(uri.toString()).matches()) {
                LOG.trace("Using no proxy for {} specified by proxy ignore list.", uri);
                return Collections.singletonList(Proxy.NO_PROXY);
            }
        }
    }
    if (!m_proxyMap.isEmpty()) {
        List<Proxy> proxyList = new ArrayList<Proxy>();
        for (Entry<Pattern, Proxy> entry : m_proxyMap.entrySet()) {
            if (entry.getKey().matcher(uri.toString()).matches()) {
                proxyList.add(entry.getValue());
            }
        }
        if (!proxyList.isEmpty()) {
            LOG.trace("Using {} for {} (configured by properties).", proxyList, uri);
            return proxyList;
        }
    }
    ProxySelector fallbackProxySelector = getFallbackProxySelector();
    if (fallbackProxySelector != null) {
        List<Proxy> proxyList = fallbackProxySelector.select(uri);
        if (proxyList != null && !proxyList.isEmpty()) {
            LOG.trace("Using {} for {} (configured by fallback proxy selector).", proxyList, uri);
            return proxyList;
        }
    }
    if (isUseSystemDefaultProxySelectorAsFallback()) {
        fallbackProxySelector = ProxySelector.getDefault();
        if (fallbackProxySelector != null && fallbackProxySelector != this) {
            List<Proxy> proxyList = fallbackProxySelector.select(uri);
            if (proxyList != null && !proxyList.isEmpty()) {
                LOG.trace("Using {} for {} (configured by system default proxy selector).", proxyList, uri);
                return proxyList;
            }
        }
    }
    // no proxy found (not in configuration and also not in fallback lists)
    LOG.trace("No proxy found for {} using a direct connection.", uri);
    return Collections.singletonList(Proxy.NO_PROXY);
}
Also used : ProxySelector(java.net.ProxySelector) Pattern(java.util.regex.Pattern) Proxy(java.net.Proxy) ArrayList(java.util.ArrayList)

Aggregations

ProxySelector (java.net.ProxySelector)53 URI (java.net.URI)33 Proxy (java.net.Proxy)32 InetSocketAddress (java.net.InetSocketAddress)21 IOException (java.io.IOException)19 SocketAddress (java.net.SocketAddress)13 List (java.util.List)7 InetAddress (java.net.InetAddress)6 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 HttpURLConnection (java.net.HttpURLConnection)3 URISyntaxException (java.net.URISyntaxException)3 HttpHost (org.apache.http.HttpHost)3 Test (org.junit.Test)3 ManualProxySelector (org.kse.utilities.net.ManualProxySelector)3 NoProxySelector (org.kse.utilities.net.NoProxySelector)3 PacProxySelector (org.kse.utilities.net.PacProxySelector)3 ProxyAddress (org.kse.utilities.net.ProxyAddress)3 SystemProxySelector (org.kse.utilities.net.SystemProxySelector)3 InterruptedIOException (java.io.InterruptedIOException)2