Search in sources :

Example 26 with Route

use of com.google.cloud.compute.v1.Route in project okhttp by square.

the class RouteSelectorTest method failedRoutesAreLast.

@Test
public void failedRoutesAreLast() throws Exception {
    Address address = httpsAddress();
    RouteSelector routeSelector = new RouteSelector(address, routeDatabase);
    final int numberOfAddresses = 2;
    dns.set(uriHost, dns.allocate(numberOfAddresses));
    // Extract the regular sequence of routes from selector.
    List<Route> regularRoutes = new ArrayList<>();
    while (routeSelector.hasNext()) {
        regularRoutes.add(routeSelector.next());
    }
    // Check that we do indeed have more than one route.
    assertEquals(numberOfAddresses, regularRoutes.size());
    // Add first regular route as failed.
    routeDatabase.failed(regularRoutes.get(0));
    // Reset selector
    routeSelector = new RouteSelector(address, routeDatabase);
    List<Route> routesWithFailedRoute = new ArrayList<>();
    while (routeSelector.hasNext()) {
        routesWithFailedRoute.add(routeSelector.next());
    }
    assertEquals(regularRoutes.get(0), routesWithFailedRoute.get(routesWithFailedRoute.size() - 1));
    assertEquals(regularRoutes.size(), routesWithFailedRoute.size());
}
Also used : SocketAddress(java.net.SocketAddress) Address(okhttp3.Address) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) Route(okhttp3.Route) Test(org.junit.Test)

Example 27 with Route

use of com.google.cloud.compute.v1.Route in project okhttp by square.

the class RouteSelector method next.

public Route next() throws IOException {
    // Compute the next route to attempt.
    if (!hasNextInetSocketAddress()) {
        if (!hasNextProxy()) {
            if (!hasNextPostponed()) {
                throw new NoSuchElementException();
            }
            return nextPostponed();
        }
        lastProxy = nextProxy();
    }
    lastInetSocketAddress = nextInetSocketAddress();
    Route route = new Route(address, lastProxy, lastInetSocketAddress);
    if (routeDatabase.shouldPostpone(route)) {
        postponedRoutes.add(route);
        // We will only recurse in order to skip previously failed routes. They will be tried last.
        return next();
    }
    return route;
}
Also used : NoSuchElementException(java.util.NoSuchElementException) Route(okhttp3.Route)

Example 28 with Route

use of com.google.cloud.compute.v1.Route in project run-wallet-android by runplay.

the class IotaMsgCore method postConstruct.

/**
 * added header for IRI
 */
private void postConstruct() {
    final String nodeUrl = protocol + "://" + host + ":" + port;
    // Create OkHttpBuilder
    Authenticator auth = new Authenticator() {

        @Nullable
        @Override
        public Request authenticate(Route route, okhttp3.Response response) throws IOException {
            if (responseCount(response) >= 3) {
                // If we've failed 3 times, give up. - in real life, never give up!!
                return null;
            }
            String credential = Credentials.basic(uname, upassword);
            return response.request().newBuilder().header("Authorization", credential).build();
        }
    };
    final OkHttpClient client = new OkHttpClient.Builder().readTimeout(5000, TimeUnit.SECONDS).authenticator(auth).addInterceptor(new Interceptor() {

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Request newRequest;
            newRequest = request.newBuilder().addHeader(X_IOTA_API_VERSION_HEADER_NAME, X_IOTA_API_VERSION_HEADER_VALUE).build();
            return chain.proceed(newRequest);
        }
    }).connectTimeout(5000, TimeUnit.SECONDS).build();
    // use client to create Retrofit service
    final Retrofit retrofit = new Retrofit.Builder().baseUrl(nodeUrl).addConverterFactory(GsonConverterFactory.create()).client(client).build();
    service = retrofit.create(IotaAPIService.class);
    log.debug("Jota-API Java proxy pointing to node url: '{}'", nodeUrl);
}
Also used : IotaAPIService(jota.IotaAPIService) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route)

Example 29 with Route

use of com.google.cloud.compute.v1.Route in project TeamCityApp by vase4kin.

the class CreateAccountDataManagerImpl method authUser.

/**
 * {@inheritDoc}
 */
@Override
public void authUser(@NonNull final CustomOnLoadingListener<String> listener, final String url, final String userName, final String password) {
    // Creating okHttpClient with authenticator
    OkHttpClient okHttpClient = mOkHttpClient.newBuilder().authenticator(new Authenticator() {

        @Override
        public Request authenticate(Route route, Response response) throws IOException {
            String credential = Credentials.basic(userName, password);
            if (credential.equals(response.request().header(TeamCityService.AUTHORIZATION))) {
                // If we already failed with these credentials, don't retry.
                return null;
            }
            return response.request().newBuilder().header(TeamCityService.AUTHORIZATION, credential).build();
        }
    }).build();
    // Handling request
    handleAuthRequest(url, AUTH_URL, okHttpClient, listener);
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route)

Example 30 with Route

use of com.google.cloud.compute.v1.Route in project nzbhydra2 by theotherp.

the class HydraOkHttp3ClientHttpRequestFactory method getOkHttpClientBuilder.

public Builder getOkHttpClientBuilder(URI requestUri) {
    Builder builder = getBaseBuilder();
    if (!configProvider.getBaseConfig().getMain().isVerifySsl()) {
        builder = getUnsafeOkHttpClientBuilder(builder);
    } else {
        try {
            SSLSocketFactory sslSocketFactory = getSslSocketFactory(new TrustManager[] { getDefaultX509TrustManager() });
            builder = builder.sslSocketFactory(new SniWhitelistingSocketFactory(sslSocketFactory), getDefaultX509TrustManager());
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            throw new RuntimeException("Unable to create SSLSocketFactory", e);
        }
    }
    MainConfig main = configProvider.getBaseConfig().getMain();
    if (main.getProxyType() == ProxyType.NONE) {
        return builder;
    }
    if (isUriToBeIgnoredByProxy(requestUri.getHost())) {
        logger.debug("Not using proxy for request to {}", requestUri.getHost());
        return builder;
    }
    if (main.getProxyType() == ProxyType.SOCKS) {
        SockProxySocketFactory sockProxySocketFactory = new SockProxySocketFactory(main.getProxyHost(), main.getProxyPort(), main.getProxyUsername(), main.getProxyPassword());
        return builder.socketFactory(sockProxySocketFactory);
    } else if (main.getProxyType() == ProxyType.HTTP) {
        builder = builder.proxy(new Proxy(Type.HTTP, new InetSocketAddress(main.getProxyHost(), main.getProxyPort()))).proxyAuthenticator((Route route, Response response) -> {
            if (response.request().header("Proxy-Authorization") != null) {
                logger.warn("Authentication with proxy failed");
                // Give up, we've already failed to authenticate.
                return null;
            }
            String credential = Credentials.basic(main.getProxyUsername(), main.getProxyPassword());
            return response.request().newBuilder().header("Proxy-Authorization", credential).build();
        });
    }
    return builder;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Builder(okhttp3.OkHttpClient.Builder) MainConfig(org.nzbhydra.config.MainConfig) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) Response(okhttp3.Response) Proxy(java.net.Proxy) SocksProxy(sockslib.client.SocksProxy) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) DelegatingSSLSocketFactory(org.nzbhydra.misc.DelegatingSSLSocketFactory) Route(okhttp3.Route)

Aggregations

Route (okhttp3.Route)30 Response (okhttp3.Response)16 Authenticator (okhttp3.Authenticator)14 Request (okhttp3.Request)12 IOException (java.io.IOException)10 OkHttpClient (okhttp3.OkHttpClient)10 InetSocketAddress (java.net.InetSocketAddress)9 Route (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route)9 Proxy (java.net.Proxy)7 Route (io.fabric8.knative.serving.v1.Route)6 RouteBuilder (io.fabric8.knative.serving.v1.RouteBuilder)6 Test (org.junit.Test)6 Test (org.junit.jupiter.api.Test)6 InstanceIdentifier (org.opendaylight.yangtools.yang.binding.InstanceIdentifier)6 KeyedInstanceIdentifier (org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier)6 Address (okhttp3.Address)5 Connection (okhttp3.Connection)5 RIBSupport (org.opendaylight.protocol.bgp.rib.spi.RIBSupport)5 Identifier (org.opendaylight.yangtools.yang.binding.Identifier)5 Builder (okhttp3.OkHttpClient.Builder)4