Search in sources :

Example 1 with Interceptor

use of com.squareup.okhttp.Interceptor in project xDrip by NightscoutFoundation.

the class ShareRest method getOkHttpClient.

private synchronized OkHttpClient getOkHttpClient() {
    try {
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        final OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.networkInterceptors().add(new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                try {
                    // Add user-agent and relevant headers.
                    Request original = chain.request();
                    Request copy = original.newBuilder().build();
                    Request modifiedRequest = original.newBuilder().header("User-Agent", "CGM-Store-1.2/22 CFNetwork/711.5.6 Darwin/14.0.0").header("Content-Type", "application/json").header("Accept", "application/json").build();
                    Log.d(TAG, "Sending request: " + modifiedRequest.toString());
                    Buffer buffer = new Buffer();
                    copy.body().writeTo(buffer);
                    Log.d(TAG, "Request body: " + buffer.readUtf8());
                    final Response response = chain.proceed(modifiedRequest);
                    Log.d(TAG, "Received response: " + response.toString());
                    if (response.body() != null) {
                        MediaType contentType = response.body().contentType();
                        String bodyString = response.body().string();
                        Log.d(TAG, "Response body: " + bodyString);
                        return response.newBuilder().body(ResponseBody.create(contentType, bodyString)).build();
                    } else
                        return response;
                } catch (NullPointerException e) {
                    Log.e(TAG, "Got null pointer exception: " + e);
                    return null;
                } catch (IllegalStateException e) {
                    UserError.Log.wtf(TAG, "Got illegal state exception: " + e);
                    return null;
                }
            }
        });
        okHttpClient.setSslSocketFactory(sslSocketFactory);
        okHttpClient.setHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException("Error occurred initializing OkHttp: ", e);
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) MediaType(com.squareup.okhttp.MediaType) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Interceptor(com.squareup.okhttp.Interceptor) Buffer(okio.Buffer) Request(com.squareup.okhttp.Request) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) Response(com.squareup.okhttp.Response) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 2 with Interceptor

use of com.squareup.okhttp.Interceptor in project xDrip-plus by jamorham.

the class ShareRest method getOkHttpClient.

private synchronized OkHttpClient getOkHttpClient() {
    try {
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        final OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.networkInterceptors().add(new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                try {
                    // Add user-agent and relevant headers.
                    Request original = chain.request();
                    Request copy = original.newBuilder().build();
                    Request modifiedRequest = original.newBuilder().header("User-Agent", "CGM-Store-1.2/22 CFNetwork/711.5.6 Darwin/14.0.0").header("Content-Type", "application/json").header("Accept", "application/json").build();
                    Log.d(TAG, "Sending request: " + modifiedRequest.toString());
                    Buffer buffer = new Buffer();
                    copy.body().writeTo(buffer);
                    Log.d(TAG, "Request body: " + buffer.readUtf8());
                    final Response response = chain.proceed(modifiedRequest);
                    Log.d(TAG, "Received response: " + response.toString());
                    if (response.body() != null) {
                        MediaType contentType = response.body().contentType();
                        String bodyString = response.body().string();
                        Log.d(TAG, "Response body: " + bodyString);
                        return response.newBuilder().body(ResponseBody.create(contentType, bodyString)).build();
                    } else
                        return response;
                } catch (NullPointerException e) {
                    Log.e(TAG, "Got null pointer exception: " + e);
                    return null;
                } catch (IllegalStateException e) {
                    UserError.Log.wtf(TAG, "Got illegal state exception: " + e);
                    return null;
                }
            }
        });
        okHttpClient.setSslSocketFactory(sslSocketFactory);
        okHttpClient.setHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException("Error occurred initializing OkHttp: ", e);
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) MediaType(com.squareup.okhttp.MediaType) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Interceptor(com.squareup.okhttp.Interceptor) Buffer(okio.Buffer) Request(com.squareup.okhttp.Request) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) Response(com.squareup.okhttp.Response) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 3 with Interceptor

use of com.squareup.okhttp.Interceptor in project android by testpress.

the class TestpressService method getRestAdapter.

private RestAdapter getRestAdapter() {
    if (authToken != null) {
        OkHttpClient client = new OkHttpClient();
        Interceptor interceptor = new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Request.Builder header = chain.request().newBuilder();
                header.addHeader("Authorization", getAuthToken());
                return chain.proceed(header.build());
            }
        };
        client.networkInterceptors().add(interceptor);
        restAdapter.setClient(new OkClient(client));
    }
    return restAdapter.build();
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) OkClient(retrofit.client.OkClient) Interceptor(com.squareup.okhttp.Interceptor)

Aggregations

Interceptor (com.squareup.okhttp.Interceptor)3 OkHttpClient (com.squareup.okhttp.OkHttpClient)3 Request (com.squareup.okhttp.Request)3 MediaType (com.squareup.okhttp.MediaType)2 Response (com.squareup.okhttp.Response)2 IOException (java.io.IOException)2 CertificateException (java.security.cert.CertificateException)2 ExecutionException (java.util.concurrent.ExecutionException)2 HostnameVerifier (javax.net.ssl.HostnameVerifier)2 SSLContext (javax.net.ssl.SSLContext)2 SSLSession (javax.net.ssl.SSLSession)2 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)2 TrustManager (javax.net.ssl.TrustManager)2 X509TrustManager (javax.net.ssl.X509TrustManager)2 Buffer (okio.Buffer)2 OkClient (retrofit.client.OkClient)1