use of com.burgstaller.okhttp.digest.CachingAuthenticator in project okhttp-digest by rburgst.
the class AuthenticationCacheInterceptorTest method testCaching__whenNoConnectionExistsButCachedInfo__shouldNotBombOut.
@Test
public void testCaching__whenNoConnectionExistsButCachedInfo__shouldNotBombOut() throws IOException {
Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
givenCachedAuthenticationFor("https://myhost.com:443", authCache);
Interceptor interceptor = new AuthenticationCacheInterceptor(authCache);
// when
String auth = whenInterceptAuthenticationForUrlWithNoConnection(interceptor, "https://myhost.com:443");
thenAuthorizationHeaderShouldBePresent(auth);
}
use of com.burgstaller.okhttp.digest.CachingAuthenticator in project okhttp-digest by rburgst.
the class AuthenticationCacheInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
final Request request = chain.request();
final HttpUrl url = request.url();
final String key = CachingUtils.getCachingKey(url);
CachingAuthenticator authenticator = authCache.get(key);
Request authRequest = null;
Connection connection = chain.connection();
Route route = connection != null ? connection.route() : null;
if (authenticator != null) {
authRequest = authenticator.authenticateWithState(route, request);
}
if (authRequest == null) {
authRequest = request;
}
Response response = chain.proceed(authRequest);
// Cached response was used, but it produced unauthorized response (cache expired).
int responseCode = response != null ? response.code() : 0;
if (authenticator != null && (responseCode == HTTP_UNAUTHORIZED || responseCode == HTTP_PROXY_AUTH)) {
// Remove cached authenticator and resend request
if (authCache.remove(key) != null) {
response.body().close();
Platform.get().log(Platform.INFO, "Cached authentication expired. Sending a new request.", null);
// Force sending a new request without "Authorization" header
response = chain.proceed(request);
}
}
return response;
}
use of com.burgstaller.okhttp.digest.CachingAuthenticator in project okhttp-digest by rburgst.
the class CachingAuthenticatorDecorator method authenticate.
@Override
public Request authenticate(Route route, Response response) throws IOException {
Request authenticated = innerAuthenticator.authenticate(route, response);
if (authenticated != null) {
String authorizationValue = authenticated.header("Authorization");
if (authorizationValue != null && innerAuthenticator instanceof CachingAuthenticator) {
final HttpUrl url = authenticated.url();
final String key = CachingUtils.getCachingKey(url);
authCache.put(key, (CachingAuthenticator) innerAuthenticator);
}
}
return authenticated;
}
Aggregations