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

protected final void testBasicSslWithKeyStore(String keyStore) throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);/*from   w  w w. ja v  a  2  s.c  o m*/
    factory.setSsl(getSsl(null, "password", keyStore));
    this.webServer = factory.getWebServer();
    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);
    assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory)).isEqualTo("test");
}

From source file:com.buffalokiwi.api.APIHttpClient.java

/**
 * Create a HTTP client that uses a self-signed and always trusted
 * SSL strategy.//from w  w  w . j a va  2 s . c o  m
 *
 * @param custom The client builder
 * @return builder with unsafe SSL strategy
 * @throws APIException If there is a problem creating the client or strategy
 */
private HttpClientBuilder setClientToSelfSigned(final HttpClientBuilder custom) throws APIException {
    final SSLContextBuilder builder = new SSLContextBuilder();
    try {
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(builder.build());
        return custom.setSSLSocketFactory(sf);
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
        throw new APIException("Failed to create self-signed trust strategy and/or SSL-enabled HTTP Client", e);
    }
}

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

@Test
public void pkcs12KeyStoreAndTrustStore() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);//from  w  w w. java 2 s  .c  o  m
    factory.setSsl(getSsl(ClientAuth.NEED, null, "classpath:test.p12", "classpath:test.p12", null, null));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    KeyStore keyStore = KeyStore.getInstance("pkcs12");
    keyStore.load(new FileInputStream(new File("src/test/resources/test.p12")), "secret".toCharArray());
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
                    .loadKeyMaterial(keyStore, "secret".toCharArray()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);
    assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory)).isEqualTo("test");
}

From source file:com.hpe.elderberry.TaxiiConnection.java

public RestTemplate getRestTemplate() {
    if (restTemplate == null) {
        HttpClientBuilder builder = custom();

        if (useProxy) {
            if ("".equals(proxyHost)) {
                proxyHost = System.getProperty(discoveryUrl.getScheme() + ".proxyHost");
            }/*w ww  .jav a2s.  co m*/

            if (proxyPort == 0) {
                proxyPort = Integer.parseInt(System.getProperty(discoveryUrl.getScheme() + ".proxyPort", "0"));
            }

            if ("".equals(proxyHost) || proxyHost == null || proxyPort == 0) {
                log.warn("proxy requested, but not setup, not using a proxy");
            } else {
                log.info("using " + discoveryUrl.getScheme() + " proxy: " + proxyHost + ":" + proxyPort);
                HttpHost proxy = new HttpHost(proxyHost, proxyPort);
                DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
                builder.setRoutePlanner(routePlanner);
            }
        }

        if (getTrustStore() != null || getKeyStore() != null) {
            SSLContext sslContext;
            try {
                sslContext = SSLContexts.custom()
                        .loadTrustMaterial(getTrustStore(), new TrustSelfSignedStrategy())
                        .loadKeyMaterial(getKeyStore(), keyPassword).build();
            } catch (Exception e) {
                log.error("unable to create SSL context, " + e.getMessage(), e);
                throw new RuntimeException(e);
            }
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
            builder.setSSLSocketFactory(sslsf);
        }

        if (!"".equals(username)) {
            restTemplate = new RestTemplate(
                    new PreemptiveAuthHttpRequestFactor(username, password, builder.build()));
        } else {
            restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(builder.build()));
        }

        if (marshaller == null) {
            marshaller = new Jaxb2Marshaller();
            marshaller.setPackagesToScan("org.mitre");
            try {
                marshaller.afterPropertiesSet();
            } catch (Exception e) {
                log.error("unable to create Jaxb2 Marshaller: " + e.getMessage(), e);
                throw new RuntimeException(e);
            }
        }

        MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller);
        converter.setSupportedMediaTypes(singletonList(APPLICATION_XML));
        //noinspection unchecked
        restTemplate.setMessageConverters(Collections.<HttpMessageConverter<?>>singletonList(converter));
    }

    return restTemplate;
}

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

@Test
public void sslNeedsClientAuthenticationSucceedsWithClientCertificate() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);/*ww w  .  j a v  a  2  s. com*/
    factory.setSsl(getSsl(ClientAuth.NEED, "password", "classpath:test.jks", "classpath:test.jks", null, null));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray());
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
                    .loadKeyMaterial(keyStore, "password".toCharArray()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);
    assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory)).isEqualTo("test");
}

From source file:eu.vital.TrustManager.connectors.dms.DMSManager.java

private String queryDMS(String dms_endpoint, String body) throws UnsupportedEncodingException, IOException,
        KeyManagementException, NoSuchAlgorithmException, KeyStoreException {

    //        SSLContextBuilder builder = new SSLContextBuilder();
    //        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    //        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
    //                builder.build(),SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    //        CloseableHttpClient httpclient = HttpClients.custom()
    //                //.setSSLSocketFactory(sslsf)
    //                .setHostnameVerifier(new AllowAllHostnameVerifier())
    //                .setRedirectStrategy(new LaxRedirectStrategy()).build();
    //        //from   w w  w  .  j  a  va2  s .c o  m
    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf)
            .setRedirectStrategy(new LaxRedirectStrategy()).build();

    String url = this.dms_URL + "/" + dms_endpoint;

    HttpPost post = new HttpPost(url);

    post.addHeader("Content-Type", "application/json");

    post.addHeader("Content-Type", cookie.substring(17));
    RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000)
            .setConnectTimeout(5000).setSocketTimeout(5000).build();
    post.setConfig(requestConfig);
    //            

    HttpEntity entity = new StringEntity(body, StandardCharsets.UTF_8);
    post.setEntity(entity);
    CloseableHttpResponse clientresponse = httpclient.execute(post);

    if (clientresponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK
            && clientresponse.getStatusLine().getStatusCode() != HttpStatus.SC_ACCEPTED) {
        return null;
    }
    String sdata;
    sdata = EntityUtils.toString(clientresponse.getEntity(), StandardCharsets.UTF_8);

    return sdata;
    //       
}

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

@Test(expected = IOException.class)
public void sslNeedsClientAuthenticationFailsWithoutClientCertificate() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);//from w  w  w .  j a va 2s . com
    factory.setSsl(getSsl(ClientAuth.NEED, "password", "classpath:test.jks"));
    this.webServer = factory.getWebServer();
    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);
    getResponse(getLocalUrl("https", "/test.txt"), requestFactory);
}

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

@Test
public void sslWantsClientAuthenticationSucceedsWithClientCertificate() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);//w  w w . ja va  2 s.  co m
    factory.setSsl(getSsl(ClientAuth.WANT, "password", "classpath:test.jks"));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray());
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
                    .loadKeyMaterial(keyStore, "password".toCharArray()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);
    assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory)).isEqualTo("test");
}