本文整理汇总了Java中com.burgstaller.okhttp.digest.CachingAuthenticator类的典型用法代码示例。如果您正苦于以下问题:Java CachingAuthenticator类的具体用法?Java CachingAuthenticator怎么用?Java CachingAuthenticator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CachingAuthenticator类属于com.burgstaller.okhttp.digest包,在下文中一共展示了CachingAuthenticator类的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: testAuthenticateWithState__shouldCallAuthenticatorsInExpectedOrder
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
/**
* Makes sure that in the case of cached authenticators the authenticators are called in the
* order in which they were registered.
*
* @throws Exception
*/
@Test
public void testAuthenticateWithState__shouldCallAuthenticatorsInExpectedOrder() throws Exception {
// given
CachingAuthenticator auth1 = mock(CachingAuthenticator.class);
CachingAuthenticator auth2 = mock(CachingAuthenticator.class);
DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder()
.with("digest", auth1)
.with("basic", auth2)
.build();
Request request = createDummyRequest();
// make sure that the 2nd authenticator will not be called
given(auth2.authenticateWithState(eq(mockRoute), eq(request))).willThrow(IllegalStateException.class);
given(auth1.authenticateWithState(eq(mockRoute), eq(request))).willReturn(request);
// when
Request result = authenticator.authenticateWithState(mockRoute, request);
// then
assertEquals(request, result);
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:28,代码来源:DispatchingAuthenticatorTest.java
示例2: createUnauthorizedServerResponse
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
private Response createUnauthorizedServerResponse() throws IOException {
final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
Interceptor interceptor = new AuthenticationCacheInterceptor(authCache);
return interceptor.intercept(new ChainAdapter(createDummyRequest(), mockConnection) {
@Override
public Response proceed(Request request) throws IOException {
return new Response.Builder()
.body(ResponseBody.create(MediaType.parse("text/plain"), "Unauthorized"))
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(HTTP_UNAUTHORIZED)
.message("Unauthorized")
.header("WWW-Authenticate", "Basic realm=\"myrealm\"")
.build();
}
});
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:19,代码来源:DispatchingAuthenticatorTest.java
示例3: testCaching_withExpiredAuthentication
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
@Test
public void testCaching_withExpiredAuthentication() throws Exception {
Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
final String dummyUrl = "https://myhost.com/path";
// Fill in authCache.
// https://myhost.com => basic auth user1:user1
givenCachedAuthenticationFor(dummyUrl, authCache);
assertEquals(1, authCache.size());
Interceptor interceptor = new AuthenticationCacheInterceptor(authCache);
// Check that unauthorized response (e.g. credentials changed or expired)
// removes cached authenticator
whenServerReturns401(dummyUrl, interceptor);
thenAuthCacheShouldBeEmpty(authCache);
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:20,代码来源:AuthenticationCacheInterceptorTest.java
示例4: testCaching_withDifferentPorts
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
@Test
public void testCaching_withDifferentPorts() throws Exception {
Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
// Fill in authCache.
// https://myhost.com => basic auth user1:user1
givenCachedAuthenticationFor("https://myhost.com", authCache);
assertEquals(1, authCache.size());
Interceptor interceptor = new AuthenticationCacheInterceptor(authCache);
// Check that authenticator exists for https://myhost.com:443
final String authorization = whenInterceptAuthenticationForUrl(interceptor, "https://myhost.com:443");
thenAuthorizationHeaderShouldBePresent(authorization);
// Check that authenticator does not exist for http://myhost.com:8080
final String authorization2 = whenInterceptAuthenticationForUrl(interceptor, "http://myhost.com:8080");
thenNoAuthorizationHeaderShouldBePresent(authorization2);
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:20,代码来源:AuthenticationCacheInterceptorTest.java
示例5: givenCachedAuthenticationFor
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
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);
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:18,代码来源:AuthenticationCacheInterceptorTest.java
示例6: OkHttpBuilder
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
protected OkHttpBuilder(final HttpObjectConfig config) {
super(config);
this.config = new HttpConfigs.ThreadSafeHttpConfig(config.getChainedConfig());
this.clientConfig = config.getClient();
this.executor = config.getExecution().getExecutor();
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
final SSLContext sslContext = config.getExecution().getSslContext();
if (sslContext != null) {
builder.sslSocketFactory(sslContext.getSocketFactory()/*, (X509TrustManager) TRUST_MANAGERS[0]*/);
builder.hostnameVerifier(config.getExecution().getHostnameVerifier());
}
// DIGEST support - defining this here only allows DIGEST config on the HttpBuilder configuration, not for individual methods.
final HttpConfig.Auth auth = config.getRequest().getAuth();
if (auth != null && auth.getAuthType() == DIGEST) {
Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
builder.addInterceptor(new AuthenticationCacheInterceptor(authCache));
builder.authenticator(new CachingAuthenticatorDecorator(
new DigestAuthenticator(new com.burgstaller.okhttp.digest.Credentials(auth.getUser(), auth.getPassword())), authCache)
);
}
final Consumer<Object> clientCustomizer = clientConfig.getClientCustomizer();
if (clientCustomizer != null) {
clientCustomizer.accept(builder);
}
final ProxyInfo pinfo = config.getExecution().getProxyInfo();
if (usesProxy(pinfo)) {
builder.proxy(pinfo.getProxy());
}
this.client = builder.build();
}
开发者ID:http-builder-ng,项目名称:http-builder-ng,代码行数:39,代码来源:OkHttpBuilder.java
示例7: authenticate
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
@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;
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:14,代码来源:CachingAuthenticatorDecorator.java
示例8: intercept
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
@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;
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:31,代码来源:AuthenticationCacheInterceptor.java
示例9: DispatchingAuthenticator
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
private DispatchingAuthenticator(Map<String, Authenticator> registry) {
authenticatorRegistry = registry;
cachingRegistry = new LinkedHashMap<>();
for (Map.Entry<String, Authenticator> entry : authenticatorRegistry.entrySet()) {
if (entry.getValue() instanceof CachingAuthenticator) {
cachingRegistry.put(entry.getKey(), (CachingAuthenticator) entry.getValue());
}
}
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:10,代码来源:DispatchingAuthenticator.java
示例10: authenticateWithState
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
@Override
public Request authenticateWithState(Route route, Request request) throws IOException {
for (Map.Entry<String, CachingAuthenticator> authenticatorEntry : cachingRegistry.entrySet()) {
final Request authRequest = authenticatorEntry.getValue().authenticateWithState(route, request);
if (authRequest != null) {
return authRequest;
}
}
return null;
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:11,代码来源:DispatchingAuthenticator.java
示例11: testCaching__whenNoConnectionExists__shouldNotBombOut
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
@Test
public void testCaching__whenNoConnectionExists__shouldNotBombOut() throws IOException {
Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
Interceptor interceptor = new AuthenticationCacheInterceptor(authCache);
String auth = whenInterceptAuthenticationForUrlWithNoConnection(interceptor, "https://myhost.com:443");
assertNull(auth);
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:9,代码来源:AuthenticationCacheInterceptorTest.java
示例12: testCaching__whenNoConnectionExistsButCachedInfo__shouldNotBombOut
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
@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);
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:11,代码来源:AuthenticationCacheInterceptorTest.java
示例13: getClient
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
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;
}
开发者ID:PhilippC,项目名称:keepass2android,代码行数:44,代码来源:WebDavStorage.java
示例14: GerritApiClient
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
GerritApiClient(String endpoint, Authorization authorization,
PlatformAbstractionLayer abstractionLayer) {
mAbstractionLayer = abstractionLayer;
mEndPoint = endpoint;
mCookieManager = new CookieManager(toUnauthenticatedEndpoint(mEndPoint));
Authorization auth = authorization;
if (auth == null) {
auth = new Authorization();
}
DispatchingAuthenticator authenticator = null;
if (!auth.isAnonymousUser()) {
final Credentials credentials = new Credentials(auth.mUsername, auth.mPassword);
final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
authenticator = new DispatchingAuthenticator.Builder()
.with("digest", digestAuthenticator)
.with("basic", basicAuthenticator)
.requireAuthScheme(false)
.build();
}
// OkHttp client
OkHttpClient.Builder clientBuilder = auth.mTrustAllCertificates
? OkHttpHelper.getUnsafeClientBuilder() : OkHttpHelper.getSafeClientBuilder();
clientBuilder
.readTimeout(20000, java.util.concurrent.TimeUnit.MILLISECONDS)
.cookieJar(mCookieManager)
.followRedirects(true)
.followSslRedirects(true)
.addInterceptor(createConnectivityCheckInterceptor())
.addInterceptor(createLoggingInterceptor())
.addInterceptor(createHeadersInterceptor(auth));
if (!auth.isAnonymousUser()) {
final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
clientBuilder
.authenticator(new CachingAuthenticatorDecorator(authenticator, authCache))
.addInterceptor(new AuthenticationCacheInterceptor(authCache));
}
OkHttpClient client = clientBuilder.build();
// Gson adapter
GsonConverterFactory gsonFactory = GsonConverterFactory.create(
GsonHelper.createGerritGsonBuilder(true, mAbstractionLayer).create());
// RxJava adapter
RxJava2CallAdapterFactory rxAdapter = RxJava2CallAdapterFactory.create();
// Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(endpoint)
.client(client)
.addConverterFactory(gsonFactory)
.addCallAdapterFactory(rxAdapter)
.build();
// Build the api
mService = retrofit.create(GerritRestApi.class);
}
开发者ID:jruesga,项目名称:rview,代码行数:61,代码来源:GerritApiClient.java
示例15: CachingAuthenticatorDecorator
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
public CachingAuthenticatorDecorator(Authenticator innerAuthenticator, Map<String, CachingAuthenticator> authCache) {
this.innerAuthenticator = innerAuthenticator;
this.authCache = authCache;
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:5,代码来源:CachingAuthenticatorDecorator.java
示例16: AuthenticationCacheInterceptor
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
public AuthenticationCacheInterceptor(Map<String, CachingAuthenticator> authCache) {
this.authCache = authCache;
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:4,代码来源:AuthenticationCacheInterceptor.java
示例17: thenAuthCacheShouldBeEmpty
import com.burgstaller.okhttp.digest.CachingAuthenticator; //导入依赖的package包/类
private void thenAuthCacheShouldBeEmpty(Map<String, CachingAuthenticator> authCache) {
// No cached authenticator anymore
assertEquals(0, authCache.size());
}
开发者ID:rburgst,项目名称:okhttp-digest,代码行数:5,代码来源:AuthenticationCacheInterceptorTest.java
注:本文中的com.burgstaller.okhttp.digest.CachingAuthenticator类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论