use of com.burgstaller.okhttp.DispatchingAuthenticator in project LibreraReader by foobnix.
the class OPDS method getHttpResponse.
public static String getHttpResponse(String url) throws IOException {
Request request = //
new Request.Builder().header("User-Agent", USER_AGENT).cacheControl(//
new CacheControl.Builder().maxAge(10, //
TimeUnit.MINUTES).build()).url(//
url).build();
Response response = //
client.newCall(//
request).execute();
LOG.d("Header: >>", url);
LOG.d("Header: Status code:", response.code());
for (int i = 0; i < response.headers().size(); i++) {
String name = response.headers().name(i);
String value = response.headers().value(i);
LOG.d("Header: ", name, value);
}
if (response.code() == 401 && TxtUtils.isEmpty(TempHolder.get().login)) {
return CODE_401;
} else {
Credentials credentials = new Credentials(TempHolder.get().login, TempHolder.get().password);
final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
DispatchingAuthenticator authenticator = //
new DispatchingAuthenticator.Builder().with("digest", //
digestAuthenticator).with("basic", //
basicAuthenticator).build();
client = //
builder.authenticator(//
new CachingAuthenticatorDecorator(authenticator, authCache)).addInterceptor(//
new AuthenticationCacheInterceptor(authCache)).build();
response = client.newCall(request).execute();
if (response.code() == 401) {
return CODE_401;
}
}
String string = response.body().string();
return string;
}
use of com.burgstaller.okhttp.DispatchingAuthenticator in project keepass2android by PhilippC.
the class WebDavStorage method getClient.
private OkHttpClient getClient(ConnectionInfo ci) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
com.burgstaller.okhttp.digest.Credentials credentials = new com.burgstaller.okhttp.digest.Credentials(ci.username, ci.password);
final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
// note that all auth schemes should be registered as lowercase!
DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("digest", digestAuthenticator).with("basic", basicAuthenticator).build();
builder = builder.authenticator(new CachingAuthenticatorDecorator(authenticator, authCache)).addInterceptor(new AuthenticationCacheInterceptor(authCache));
if ((mCertificateErrorHandler != null) && (!mCertificateErrorHandler.alwaysFailOnValidationError())) {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
trustManager = new DecoratedTrustManager(trustManager, mCertificateErrorHandler);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { trustManager }, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
builder = builder.sslSocketFactory(sslSocketFactory, trustManager).hostnameVerifier(new DecoratedHostnameVerifier(OkHostnameVerifier.INSTANCE, mCertificateErrorHandler));
}
OkHttpClient client = builder.build();
return client;
}
Aggregations