use of com.burgstaller.okhttp.basic.BasicAuthenticator in project okhttp-digest by rburgst.
the class AuthenticationCacheInterceptorTest method givenCachedAuthenticationFor.
private void givenCachedAuthenticationFor(String url, Map<String, CachingAuthenticator> authCache) throws IOException {
Authenticator decorator = new CachingAuthenticatorDecorator(new BasicAuthenticator(new Credentials("user1", "user1")), authCache);
Request dummyRequest = new Request.Builder().url(url).get().build();
Response response = new Response.Builder().request(dummyRequest).protocol(Protocol.HTTP_1_1).code(HTTP_UNAUTHORIZED).message("Unauthorized").header("WWW-Authenticate", "Basic realm=\"myrealm\"").build();
decorator.authenticate(null, response);
}
use of com.burgstaller.okhttp.basic.BasicAuthenticator in project okhttp-digest by rburgst.
the class DispatchingAuthenticatorTest method testCaching_withBasicAuthenticatorPreferredOrder.
@Test
public void testCaching_withBasicAuthenticatorPreferredOrder() throws Exception {
final Credentials credentials = new Credentials("user", "pwd");
final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("basic", basicAuthenticator).with("digest", digestAuthenticator).build();
Request request = authenticator.authenticate(mockRoute, createUnauthorizedServerResponse());
assertNotNull(request);
request = authenticator.authenticateWithState(mockRoute, createDummyRequest());
assertNotNull(request);
}
use of com.burgstaller.okhttp.basic.BasicAuthenticator in project okhttp-digest by rburgst.
the class DispatchingAuthenticatorTest method testCaching_withDigestAuthenticatorPreferredOrder.
@Test
public void testCaching_withDigestAuthenticatorPreferredOrder() throws Exception {
final Credentials credentials = new Credentials("user", "pwd");
final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("digest", digestAuthenticator).with("basic", basicAuthenticator).build();
Request request = authenticator.authenticate(mockRoute, createUnauthorizedServerResponse());
assertNotNull(request);
String authorizationHeader = request.header("Authorization");
assertThat(authorizationHeader, CoreMatchers.startsWith("Basic"));
request = authenticator.authenticateWithState(mockRoute, createDummyRequest());
assertNotNull(request);
}
use of com.burgstaller.okhttp.basic.BasicAuthenticator 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.basic.BasicAuthenticator in project LibreraReader by foobnix.
the class OPDS method buildProxy.
public static void buildProxy() {
new Thread(new Runnable() {
@Override
public void run() {
if (AppState.get().proxyEnable && TxtUtils.isNotEmpty(AppState.get().proxyServer) && AppState.get().proxyPort != 0) {
Type http = AppState.PROXY_SOCKS.equals(AppState.get().proxyType) ? Type.SOCKS : Type.HTTP;
LOG.d("Proxy: Server", http.name(), AppState.get().proxyServer, AppState.get().proxyPort);
builder.proxy(new Proxy(http, new InetSocketAddress(AppState.get().proxyServer, AppState.get().proxyPort)));
if (TxtUtils.isNotEmpty(AppState.get().proxyUser)) {
LOG.d("Proxy: User", AppState.get().proxyUser, AppState.get().proxyPassword);
builder.proxyAuthenticator(new BasicAuthenticator(new Credentials(AppState.get().proxyUser, AppState.get().proxyPassword)));
}
} else {
builder.proxy(null);
}
client = builder.build();
}
}).start();
}
Aggregations