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());
}
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;
}
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);
}
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);
}
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;
}
Aggregations