Example usage for com.squareup.okhttp ConnectionSpec MODERN_TLS

List of usage examples for com.squareup.okhttp ConnectionSpec MODERN_TLS

Introduction

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

Prototype

ConnectionSpec MODERN_TLS

To view the source code for com.squareup.okhttp ConnectionSpec MODERN_TLS.

Click Source Link

Document

A modern TLS connection with extensions like SNI and ALPN available.

Usage

From source file:org.jclouds.http.okhttp.OkHttpCommandExecutorServiceTest.java

License:Apache License

@Test
public void testBothProtocolsSucceedIfSSLAndHTTPConfigured() throws Exception {
    MockWebServer redirectTarget = mockWebServer(new MockResponse());
    MockWebServer server = mockWebServer(new MockResponse().setResponseCode(302).setHeader("Location",
            redirectTarget.getUrl("/").toString()));
    server.useHttps(sslContext.getSocketFactory(), false);
    Module httpConfigModule = new ConnectionSpecModule(ConnectionSpec.CLEARTEXT, ConnectionSpec.MODERN_TLS);
    PatchApi api = api(PatchApi.class, server.getUrl("/").toString(), httpConfigModule);
    try {/*from   w  ww . j a v  a  2  s .  c  o  m*/
        api.patchNothing("");
        assertEquals(server.getRequestCount(), 1);
        assertEquals(redirectTarget.getRequestCount(), 1);
    } finally {
        closeQuietly(api);
        server.shutdown();
        redirectTarget.shutdown();
    }
}

From source file:org.jclouds.http.okhttp.OkHttpCommandExecutorServiceTest.java

License:Apache License

@Test
public void testRestrictedSSLProtocols() throws Exception {
    MockWebServer server = mockWebServer(new MockResponse());
    server.useHttps(sslContext.getSocketFactory(), false);
    ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).tlsVersions(TlsVersion.TLS_1_2)
            .build();/*w  ww. j  av  a  2 s  .  co  m*/
    PatchApi api = api(PatchApi.class, server.getUrl("/").toString(), new ConnectionSpecModule(spec));
    try {
        api.patchNothing("");
        assertEquals(server.getRequestCount(), 1);
        RecordedRequest request = server.takeRequest();
        assertEquals(request.getSslProtocol(), "TLSv1.2");
    } finally {
        closeQuietly(api);
        server.shutdown();
    }
}

From source file:org.sonar.runner.impl.OkHttpClientFactory.java

License:Open Source License

static OkHttpClient create(JavaVersion javaVersion) {
    OkHttpClient httpClient = new OkHttpClient();
    httpClient.setConnectTimeout(CONNECT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
    httpClient.setReadTimeout(READ_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
    ConnectionSpec tls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).allEnabledTlsVersions()
            .allEnabledCipherSuites().supportsTlsExtensions(true).build();
    httpClient.setConnectionSpecs(asList(tls, ConnectionSpec.CLEARTEXT));
    if (javaVersion.isJava7()) {
        // OkHttp executes SSLContext.getInstance("TLS") by default (see
        // https://github.com/square/okhttp/blob/c358656/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java#L616)
        // As only TLS 1.0 is enabled by default in Java 7, the SSLContextFactory must be changed
        // in order to support all versions from 1.0 to 1.2.
        // Note that this is not overridden for Java 8 as TLS 1.2 is enabled by default.
        // Keeping getInstance("TLS") allows to support potential future versions of TLS on Java 8.
        try {// ww  w  . ja  v a  2s .  c  o m
            httpClient.setSslSocketFactory(
                    new Tls12Java7SocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault()));
        } catch (Exception e) {
            throw new IllegalStateException("Fail to init TLS context", e);
        }
    }
    return httpClient;
}

From source file:org.sonarqube.ws.client.HttpConnector.java

License:Open Source License

private HttpConnector(Builder builder, JavaVersion javaVersion) {
    this.baseUrl = HttpUrl.parse(builder.url.endsWith("/") ? builder.url : format("%s/", builder.url));
    this.userAgent = builder.userAgent;

    if (isNullOrEmpty(builder.login)) {
        // no login nor access token
        this.credentials = null;
    } else {//w ww . ja va  2s.c  om
        // password is null when login represents an access token. In this case
        // the Basic credentials consider an empty password.
        this.credentials = Credentials.basic(builder.login, nullToEmpty(builder.password));
    }

    if (builder.proxy != null) {
        this.okHttpClient.setProxy(builder.proxy);
    }
    // proxy credentials can be used on system-wide proxies, so even if builder.proxy is null
    if (isNullOrEmpty(builder.proxyLogin)) {
        this.proxyCredentials = null;
    } else {
        this.proxyCredentials = Credentials.basic(builder.proxyLogin, nullToEmpty(builder.proxyPassword));
    }

    this.okHttpClient.setConnectTimeout(builder.connectTimeoutMs, TimeUnit.MILLISECONDS);
    this.okHttpClient.setReadTimeout(builder.readTimeoutMs, TimeUnit.MILLISECONDS);

    ConnectionSpec tls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).allEnabledTlsVersions()
            .allEnabledCipherSuites().supportsTlsExtensions(true).build();
    this.okHttpClient.setConnectionSpecs(asList(tls, ConnectionSpec.CLEARTEXT));
    if (javaVersion.isJava7()) {
        // OkHttp executes SSLContext.getInstance("TLS") by default (see
        // https://github.com/square/okhttp/blob/c358656/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java#L616)
        // As only TLS 1.0 is enabled by default in Java 7, the SSLContextFactory must be changed
        // in order to support all versions from 1.0 to 1.2.
        // Note that this is not overridden for Java 8 as TLS 1.2 is enabled by default.
        // Keeping getInstance("TLS") allows to support potential future versions of TLS on Java 8.
        try {
            this.okHttpClient.setSslSocketFactory(
                    new Tls12Java7SocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault()));
        } catch (Exception e) {
            throw new IllegalStateException("Fail to init TLS context", e);
        }
    }
}