Example usage for com.squareup.okhttp OkHttpClient setSslSocketFactory

List of usage examples for com.squareup.okhttp OkHttpClient setSslSocketFactory

Introduction

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

Prototype

public OkHttpClient setSslSocketFactory(SSLSocketFactory sslSocketFactory) 

Source Link

Document

Sets the socket factory used to secure HTTPS connections.

Usage

From source file:io.jawg.osmcontributor.flickr.util.FlickrPhotoUtils.java

License:Open Source License

public static RestAdapter getAdapter() {
    if (adapter == null) {
        OkHttpClient okHttpClient = new OkHttpClient();
        try {//from   w w w .ja v a2 s  .c o m
            SSLSocketFactory NoSSLv3Factory = new NoSSLv3SocketFactory(
                    new URL("https://api.flickr.com/services/rest"));
            okHttpClient.setSslSocketFactory(NoSSLv3Factory);
            adapter = new RestAdapter.Builder().setConverter(new StringConverter())
                    .setEndpoint("https://api.flickr.com/services").setClient(new OkClient(okHttpClient))
                    .setLogLevel(RestAdapter.LogLevel.FULL).setLog(new AndroidLog("---------------------->"))
                    .build();
        } catch (MalformedURLException e) {

        } catch (IOException e) {

        }
    }
    return adapter;
}

From source file:io.jawg.osmcontributor.flickr.util.FlickrUploadUtils.java

License:Open Source License

public static RestAdapter getRestAdapter(final Map<String, String> oAuthParams) {
    if (adapter == null) {
        OkHttpClient okHttpClient = new OkHttpClient();
        try {/*from  ww  w.ja v a 2s. c om*/
            SSLSocketFactory NoSSLv3Factory = new NoSSLv3SocketFactory(
                    new URL("https://up.flickr.com/services"));
            okHttpClient.setSslSocketFactory(NoSSLv3Factory);
            adapter = new RestAdapter.Builder().setConverter(new StringConverter())
                    .setEndpoint("https://up.flickr.com/services").setClient(new OkClient(okHttpClient))
                    .setRequestInterceptor(new RequestInterceptor() {
                        @Override
                        public void intercept(RequestFacade request) {
                            request.addHeader("Authorization",
                                    FlickrSecurityUtils.getAuthorizationHeader(oAuthParams));
                        }
                    }).build();
        } catch (MalformedURLException e) {

        } catch (IOException e) {

        }
    }
    return adapter;
}

From source file:io.kodokojo.bdd.stage.cluster.ClusterApplicationThen.java

License:Open Source License

private OkHttpClient provideDefaultOkHttpClient() {
    OkHttpClient httpClient = new OkHttpClient();
    final TrustManager[] certs = new TrustManager[] { new X509TrustManager() {

        @Override/*from   w w  w  .jav  a2 s .c  o m*/
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkServerTrusted(final X509Certificate[] chain, final String authType)
                throws CertificateException {
        }

        @Override
        public void checkClientTrusted(final X509Certificate[] chain, final String authType)
                throws CertificateException {
        }
    } };

    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
    } catch (final java.security.GeneralSecurityException ex) {
    }
    httpClient.setHostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }
    });
    httpClient.setSslSocketFactory(ctx.getSocketFactory());
    return httpClient;
}

From source file:io.macgyver.core.util.CertChecker.java

License:Apache License

public List<X509Certificate> fetchCertificates(String httpUrl) throws IOException {
    OkHttpClient c = new OkHttpClient();

    CertExtractor extractor = new CertExtractor();
    c.setHostnameVerifier(extractor);// w w  w  .  ja va2s  .c o m
    c.setSslSocketFactory(SslTrust.withoutCertificateValidation().getSocketFactory());

    Request r = new Request.Builder().url(httpUrl).build();

    Response response = c.newCall(r).execute();
    response.body().close();

    return extractor.certList;

}

From source file:io.macgyver.neorx.rest.NeoRxClient.java

License:Apache License

public NeoRxClient(String url, String username, String password, boolean validateCertificates) {

    while (url.endsWith("/")) {
        url = url.substring(0, url.length() - 1);
    }//from w w  w.j  a  v a2 s . co m
    this.url = url;
    this.username = username;
    this.password = password;
    this.validateCertificates = validateCertificates;

    OkHttpClient client = new OkHttpClient();

    if (!validateCertificates) {
        client.setHostnameVerifier(SslTrust.withoutHostnameVerification());
        client.setSslSocketFactory(SslTrust.withoutCertificateValidation().getSocketFactory());

    }

    this.httpClient = client;
}

From source file:io.macgyver.plugin.elb.a10.A10ClientImpl.java

License:Apache License

protected OkHttpClient getClient() {

    // not guaranteed to be singleton, but close enough
    if (clientReference.get() == null) {
        OkHttpClient c = new OkHttpClient();

        c.setConnectTimeout(20, TimeUnit.SECONDS);

        c.setHostnameVerifier(withoutHostnameVerification());
        c.setSslSocketFactory(withoutCertificateValidation().getSocketFactory());

        c.setConnectionSpecs(getA10CompatibleConnectionSpecs());
        c.interceptors().add(LoggingInterceptor.create(A10ClientImpl.class));
        clientReference.set(c);/*from w ww.  j  a v  a  2  s . c  o m*/
    }
    return clientReference.get();
}

From source file:io.macgyver.plugin.pagerduty.PagerDutyClientImpl.java

License:Apache License

protected OkHttpClient newEventClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(10, TimeUnit.SECONDS);

    if (!getCertificateValidationEnabled()) {
        client = client.setHostnameVerifier(SslTrust.withoutHostnameVerification());
        client = client.setSslSocketFactory(SslTrust.withoutCertificateValidation().getSocketFactory());

    }// w w w.  jav  a  2s.co m

    return client;
}

From source file:io.takari.aether.okhttp.OkHttpAetherClient.java

License:Open Source License

public OkHttpAetherClient(AetherClientConfig config) {
    this.config = config;

    // headers are modified during http auth handshake
    // make a copy to avoid cross-talk among client instances
    headers = new HashMap<String, String>();
    if (config.getHeaders() != null) {
        headers.putAll(config.getHeaders());
    }/*  w  ww  .  java  2 s.c  om*/

    //
    // If the User-Agent has been overriden in the headers then we will use that
    //
    if (!headers.containsKey("User-Agent")) {
        headers.put("User-Agent", config.getUserAgent());
    }

    OkHttpClient httpClient = new OkHttpClient();
    httpClient.setProxy(getProxy(config.getProxy()));
    httpClient.setHostnameVerifier(OkHostnameVerifier.INSTANCE);
    httpClient.setAuthenticator(NOAUTH); // see #authenticate below
    httpClient.setConnectTimeout(config.getConnectionTimeout(), TimeUnit.MILLISECONDS);
    httpClient.setReadTimeout(config.getRequestTimeout(), TimeUnit.MILLISECONDS);
    httpClient.setSslSocketFactory(config.getSslSocketFactory());
    this.httpClient = httpClient;
}

From source file:it_minds.dk.eindberetningmobil_android.server.DebugOkHttpStack.java

License:Open Source License

private static OkHttpClient getUnsafeOkHttpClient(OkHttpClient client) {
    try {//from   w w  w  .  ja v a2 s.com
        // Create a trust manager that does not validate certificate chains
        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 new java.security.cert.X509Certificate[] {};
            }
        } };

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        client.setSslSocketFactory(sslSocketFactory);
        client.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        return client;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:net.qiujuer.common.okhttp.core.CertTool.java

License:Apache License

public static void setCertificates(OkHttpClient client, InputStream[] certificates, InputStream bksFile,
        String password) {//from   w  ww.  java2 s  .  co m
    try {
        TrustManager[] trustManagers = prepareTrustManager(certificates);
        KeyManager[] keyManagers = prepareKeyManager(bksFile, password);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagers,
                new TrustManager[] { new XTrustManager(chooseTrustManager(trustManagers)) },
                new SecureRandom());
        client.setSslSocketFactory(sslContext.getSocketFactory());
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
        e.printStackTrace();
    }
}