Example usage for org.apache.http.conn.ssl SSLConnectionSocketFactory SSLConnectionSocketFactory

List of usage examples for org.apache.http.conn.ssl SSLConnectionSocketFactory SSLConnectionSocketFactory

Introduction

In this page you can find the example usage for org.apache.http.conn.ssl SSLConnectionSocketFactory SSLConnectionSocketFactory.

Prototype

public SSLConnectionSocketFactory(final SSLContext sslContext) 

Source Link

Usage

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

@Test
public void sslGetScheme() throws Exception { // gh-2232
    AbstractServletWebServerFactory factory = getFactory();
    factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks"));
    this.webServer = factory
            .getWebServer(new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello"));
    this.webServer.start();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);/*w  w w  . j  ava2 s  .  c o  m*/
    assertThat(getResponse(getLocalUrl("https", "/hello"), requestFactory)).contains("scheme=https");
}

From source file:br.com.autonomiccs.apacheCloudStack.client.ApacheCloudStackClient.java

/**
 * This method creates an insecure SSL factory that will trust on self signed certificates.
 * For that we use {@link TrustSelfSignedStrategy}.
 *///from   w  w  w.  j  a  v  a2s.  co m
protected SSLConnectionSocketFactory createInsecureSslFactory() {
    SSLContextBuilder builder = new SSLContextBuilder();
    try {
        builder.loadTrustMaterial(new TrustSelfSignedStrategy());
        return new SSLConnectionSocketFactory(builder.build());
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
        throw new ApacheCloudStackClientRuntimeException(e);
    }
}

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

@Test
public void sslKeyAlias() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    Ssl ssl = getSsl(null, "password", "test-alias", "src/test/resources/test.jks");
    factory.setSsl(ssl);//from ww w  .jav  a 2s .  c  o  m
    ServletRegistrationBean<ExampleServlet> registration = new ServletRegistrationBean<>(
            new ExampleServlet(true, false), "/hello");
    this.webServer = factory.getWebServer(registration);
    this.webServer.start();
    TrustStrategy trustStrategy = new SerialNumberValidatingTrustSelfSignedStrategy("77e7c302");
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext))
            .build();
    String response = getResponse(getLocalUrl("https", "/hello"),
            new HttpComponentsClientHttpRequestFactory(httpClient));
    assertThat(response).contains("scheme=https");
}

From source file:io.fabric8.kubernetes.jolokia.JolokiaClients.java

protected J4pClient createJolokiaClient(Container container, String jolokiaUrl) {
    String name = container.getName();
    LOG.debug("Creating jolokia client for : " + name + " at URL: " + jolokiaUrl);
    J4pClientBuilder builder = J4pClient.url(jolokiaUrl);

    if (useKubeProxy) {
        // When using the https proxy, inject the Kubernetes client's SSL context
        URL masterUrl = getKubernetes().getMasterUrl();
        if (masterUrl != null && masterUrl.toString().startsWith("https")) {
            try {
                SSLContext sslCtx = SSLUtils.sslContext(kubernetes.getConfiguration());
                ConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslCtx);
                builder = builder.sslConnectionSocketFactory(factory);
            } catch (Exception e) {
                LOG.warn(// w  w  w.j a  v a2s .  c o m
                        "Unable to inject the Kubernetes SSL context into the Jolokia client. Using the default context",
                        e);
            }
        }
    }

    AuthenticationMode mode = locateAuthenticationMode();
    switch (mode) {
    case BEARER:
        builder = builder.authenticator(new BearerTokenAuthenticator());

        String token = kubernetes.getConfiguration().getOauthToken();
        builder = builder.user(token);
        break;
    case BASIC:
        builder = builder.authenticator(new BasicAuthenticator());
        if (Strings.isNotBlank(user)) {
            builder = builder.user(user);
        }
        if (Strings.isNotBlank(password)) {
            builder = builder.password(password);
        }
        break;
    default:
        throw new IllegalStateException("Unsupported authentication mode: " + mode);
    }

    return builder.build();
}

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

@Test
public void serverHeaderIsDisabledByDefaultWhenUsingSsl() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks"));
    this.webServer = factory
            .getWebServer(new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello"));
    this.webServer.start();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    ClientHttpResponse response = getClientResponse(getLocalUrl("https", "/hello"), HttpMethod.GET,
            new HttpComponentsClientHttpRequestFactory(httpClient));
    assertThat(response.getHeaders().get("Server")).isNullOrEmpty();
}

From source file:org.apache.gobblin.service.modules.orchestration.AzkabanAjaxAPIClient.java

private static CloseableHttpClient getHttpClient() throws IOException {
    try {/*from   w ww.j  a v  a  2s.c  o m*/
        // Self sign SSL
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, (TrustStrategy) new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());

        // Create client
        return HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCookieStore(new BasicCookieStore())
                .build();
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        throw new IOException("Issue with creating http client", e);
    }
}

From source file:org.apache.sling.etcd.client.impl.EtcdClientImplTest.java

private void buildSecureEtcdClient(int port, @Nonnull String keyStorePath, @Nullable String keyStorePwd,
        @Nonnull String trustStorePath, @Nullable String trustStorePwd) throws Exception {

    char[] ksp = (keyStorePwd != null) ? keyStorePwd.toCharArray() : null;
    KeyStore keyStore = loadKeyStore(keyStorePath, ksp);

    char[] tsp = (trustStorePwd != null) ? trustStorePwd.toCharArray() : null;
    KeyStore trustStore = loadKeyStore(trustStorePath, tsp);

    SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(trustStore).loadKeyMaterial(keyStore, tsp)
            .build();//ww w .ja  va 2s.com

    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext);

    Registry<ConnectionSocketFactory> connectionSocketFactory = RegistryBuilder
            .<ConnectionSocketFactory>create().register("https", sslConnectionSocketFactory).build();

    connectionManager = new PoolingHttpClientConnectionManager(connectionSocketFactory);

    final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000)
            .setRedirectsEnabled(true).setStaleConnectionCheckEnabled(true).build();
    httpClient = HttpClients.custom().setConnectionManager(connectionManager)
            .setDefaultRequestConfig(requestConfig).setSSLSocketFactory(sslConnectionSocketFactory).build();

    etcdClient = new EtcdClientImpl(httpClient, new URI("https://127.0.0.1:" + port));

}

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

@Test
public void serverHeaderCanBeCustomizedWhenUsingSsl() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.setServerHeader("MyServer");
    factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks"));
    this.webServer = factory
            .getWebServer(new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello"));
    this.webServer.start();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    ClientHttpResponse response = getClientResponse(getLocalUrl("https", "/hello"), HttpMethod.GET,
            new HttpComponentsClientHttpRequestFactory(httpClient));
    assertThat(response.getHeaders().get("Server")).containsExactly("MyServer");
}