Example usage for com.squareup.okhttp Authenticator Authenticator

List of usage examples for com.squareup.okhttp Authenticator Authenticator

Introduction

In this page you can find the example usage for com.squareup.okhttp Authenticator Authenticator.

Prototype

Authenticator

Source Link

Usage

From source file:net.codestory.rest.RestAssert.java

License:Apache License

public RestAssert withAuthentication(String login, String password) {
    return withClient(setAuthenticator(new Authenticator() {
        AtomicInteger tries = new AtomicInteger(0);

        @Override//from w w w. j  ava2s .  co m
        public Request authenticate(Proxy proxy, com.squareup.okhttp.Response response) {
            if (tries.getAndIncrement() > 0) {
                return null;
            }
            return addBasicAuthHeader(login, password).apply(response.request().newBuilder()).build();
        }

        @Override
        public Request authenticateProxy(Proxy proxy, com.squareup.okhttp.Response response) {
            return null;
        }
    }));
}

From source file:net.yatomiya.nicherry.services.bbs.BBSHttpClient.java

License:Open Source License

BBSHttpClient(IEclipseContext context) {
    super();//from ww  w  .jav a2  s . co  m

    this.context = context;

    //        setRetryOnConnectionFailure(false);
    //        setFollowRedirects(false);
    //        setFollowSslRedirects(false);

    getDispatcher().setMaxRequestsPerHost(1);

    cookieManager = context.get(PersistenceService.class).get(
            BBSHttpClient.class.getName() + "." + CookieManager.class.getName(), () -> new CookieManager());
    setCookieHandler(cookieManager);

    setAuthenticator(new Authenticator() {
        @Override
        public Request authenticate(Proxy proxy, Response response) throws IOException {
            return null;
        }

        @Override
        public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
            if (getProxy() != null && proxyCredentials != null) {
                if (!proxyCredentials.equals(response.request().header("Proxy-Authorization"))) {
                    return response.request().newBuilder().header("Proxy-Authorization", proxyCredentials)
                            .build();
                }
            }

            return null;
        }
    });

    intervalInterceptor = new AccessIntervalInterceptor();

    interceptors().add((chain) -> interceptRequest(chain));
    interceptors().add((chain) -> interceptGetDat(chain));

    {
        PreferenceService prefs = context.get(PreferenceService.class);
        prefs.subscribe(this, NPreferences.NODE, NPreferences.NETWORK_PROXY_ENABLE,
                event -> applyProxyPreference());
        prefs.subscribe(this, NPreferences.NODE, NPreferences.NETWORK_PROXY_HOST,
                event -> applyProxyPreference());
        prefs.subscribe(this, NPreferences.NODE, NPreferences.NETWORK_PROXY_PORT,
                event -> applyProxyPreference());
        prefs.subscribe(this, NPreferences.NODE, NPreferences.NETWORK_PROXY_BASIC_USERNAME,
                event -> applyProxyPreference());
        prefs.subscribe(this, NPreferences.NODE, NPreferences.NETWORK_PROXY_BASIC_PASSWORD,
                event -> applyProxyPreference());
        prefs.subscribe(this, NPreferences.NODE, NPreferences.BBS_NETWORK_CONNECT_INTERVAL_PER_HOST,
                event -> intervalInterceptor.interval = Integer.valueOf(event.getValue()));
    }
}

From source file:org.xbmc.kore.jsonrpc.HostConnection.java

License:Open Source License

/**
 * Initializes this class OkHttpClient/* w  w  w.  j  a v  a 2s.  c o m*/
 */
public OkHttpClient getOkHttpClient() {
    if (httpClient == null) {
        httpClient = new OkHttpClient();
        httpClient.setConnectTimeout(connectTimeout, TimeUnit.MILLISECONDS);

        httpClient.setAuthenticator(new Authenticator() {
            @Override
            public Request authenticate(Proxy proxy, Response response) throws IOException {
                if (TextUtils.isEmpty(hostInfo.getUsername()))
                    return null;

                String credential = Credentials.basic(hostInfo.getUsername(), hostInfo.getPassword());
                return response.request().newBuilder().header("Authorization", credential).build();
            }

            @Override
            public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
                return null;
            }
        });
    }
    return httpClient;
}