Search in sources :

Example 41 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project mbed-cloud-sdk-java by ARMmbed.

the class GsonCustomConverterFactory method setApiKey.

/**
 * Helper method to configure the first api key found
 * @param apiKey API key
 * @return ApiClient
 */
public ApiClient setApiKey(String apiKey) {
    for (Interceptor apiAuthorization : apiAuthorizations.values()) {
        if (apiAuthorization instanceof ApiKeyAuth) {
            ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
            keyAuth.setApiKey(apiKey);
            return this;
        }
    }
    return this;
}
Also used : ApiKeyAuth(com.arm.mbed.cloud.sdk.internal.statistics.auth.ApiKeyAuth) Interceptor(okhttp3.Interceptor)

Example 42 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project mbed-cloud-sdk-java by ARMmbed.

the class GsonCustomConverterFactory method setAccessToken.

/**
 * Helper method to pre-set the oauth access token of the first oauth found in the apiAuthorizations (there should be only one)
 * @param accessToken Access token
 * @return ApiClient
 */
public ApiClient setAccessToken(String accessToken) {
    for (Interceptor apiAuthorization : apiAuthorizations.values()) {
        if (apiAuthorization instanceof OAuth) {
            OAuth oauth = (OAuth) apiAuthorization;
            oauth.setAccessToken(accessToken);
            return this;
        }
    }
    return this;
}
Also used : Interceptor(okhttp3.Interceptor) OAuth(com.arm.mbed.cloud.sdk.internal.statistics.auth.OAuth)

Example 43 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project okhttp-digest by rburgst.

the class AuthenticationCacheInterceptorTest method testCaching__whenNoConnectionExists__shouldNotBombOut.

@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);
}
Also used : CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Interceptor(okhttp3.Interceptor) Test(org.junit.Test)

Example 44 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project okhttp-digest by rburgst.

the class DispatchingAuthenticatorTest method createUnauthorizedServerResponse.

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();
        }
    });
}
Also used : Response(okhttp3.Response) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) Request(okhttp3.Request) IOException(java.io.IOException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Interceptor(okhttp3.Interceptor)

Example 45 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project autorest.java by Azure.

the class CustomBaseUriTests method getEmptyMultipleThreads.

@Test
public void getEmptyMultipleThreads() throws Exception {
    final CountDownLatch latch = new CountDownLatch(2);
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            // do nothing
            }
            return chain.proceed(chain.request());
        }
    });
    final AutoRestParameterizedHostTestClient client1 = new AutoRestParameterizedHostTestClientImpl(clientBuilder, new Retrofit.Builder());
    client1.withHost("host:3000");
    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                client1.paths().getEmpty("badlocal");
                fail();
            } catch (RuntimeException e) {
                latch.countDown();
            } catch (Exception e) {
                fail();
            }
        }
    });
    Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                client1.paths().getEmpty("local");
                latch.countDown();
            } catch (Exception ex) {
                fail();
            }
        }
    });
    t1.start();
    t2.start();
    Assert.assertTrue(latch.await(15, TimeUnit.SECONDS));
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) AutoRestParameterizedHostTestClientImpl(fixtures.custombaseuri.implementation.AutoRestParameterizedHostTestClientImpl) Interceptor(okhttp3.Interceptor) Test(org.junit.Test)

Aggregations

Interceptor (okhttp3.Interceptor)138 Request (okhttp3.Request)61 OkHttpClient (okhttp3.OkHttpClient)54 Response (okhttp3.Response)51 IOException (java.io.IOException)45 Test (org.junit.Test)29 Retrofit (retrofit2.Retrofit)27 File (java.io.File)15 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)13 Cache (okhttp3.Cache)9 HttpUrl (okhttp3.HttpUrl)8 Interceptor (com.pushtorefresh.storio3.Interceptor)7 Dispatcher (okhttp3.Dispatcher)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 X509TrustManager (javax.net.ssl.X509TrustManager)6 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)5 Provides (dagger.Provides)5 Singleton (javax.inject.Singleton)5 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)5 OAuth (com.arm.mbed.cloud.sdk.internal.devicedirectory.auth.OAuth)4