Example usage for org.apache.http.ssl SSLContextBuilder loadTrustMaterial

List of usage examples for org.apache.http.ssl SSLContextBuilder loadTrustMaterial

Introduction

In this page you can find the example usage for org.apache.http.ssl SSLContextBuilder loadTrustMaterial.

Prototype

public SSLContextBuilder loadTrustMaterial(final File file)
            throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException 

Source Link

Usage

From source file:com.liferay.sync.engine.session.Session.java

private static SSLConnectionSocketFactory _getTrustingSSLSocketFactory() throws Exception {

    if (_trustingSSLSocketFactory == null) {
        SSLContextBuilder sslContextBuilder = SSLContexts.custom();

        sslContextBuilder.loadTrustMaterial(new TrustStrategy() {

            @Override/*  ww w .j ava  2s  .c  om*/
            public boolean isTrusted(X509Certificate[] x509Certificates, String authType) {

                return true;
            }

        });

        _trustingSSLSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(),
                new NoopHostnameVerifier());
    }

    return _trustingSSLSocketFactory;
}

From source file:com.adobe.ags.curly.ConnectionManager.java

private void createNewConnectionManager() {
    try {//  ww  w.  ja  va2 s  .c o  m
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(new TrustSelfSignedStrategy());

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(),
                NoopHostnameVerifier.INSTANCE);
        Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", new PlainConnectionSocketFactory()).register("https", sslsf).build();
        connectionManager = new PoolingHttpClientConnectionManager(r);
        connectionManager.setValidateAfterInactivity(500);
        sharedContext = ThreadLocal.withInitial(HttpClientContext::new);
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
        Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.springframework.http.server.reactive.ServerHttpsRequestIntegrationTests.java

@Before
public void setup() throws Exception {
    this.server.setHandler(new CheckRequestHandler());
    this.server.afterPropertiesSet();
    this.server.start();

    // Set dynamically chosen port
    this.port = this.server.getPort();

    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(builder.build(),
            NoopHostnameVerifier.INSTANCE);
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpclient);// w  ww . j a va  2  s.c  o m
    this.restTemplate = new RestTemplate(requestFactory);
}

From source file:org.drugis.addis.config.MainConfig.java

@Bean
public HttpClient httpClient(RequestConfig requestConfig) throws KeyStoreException, IOException,
        CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyManagementException {
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(new FileInputStream(KEYSTORE_PATH), KEYSTORE_PASSWORD.toCharArray());
    String ADDIS_LOCAL = System.getenv("ADDIS_LOCAL");

    SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadKeyMaterial(keyStore,
            KEYSTORE_PASSWORD.toCharArray());
    if (ADDIS_LOCAL != null) {
        String TRUSTSTORE_PATH = WebConstants.loadSystemEnv("TRUSTSTORE_PATH");
        sslContextBuilder.loadTrustMaterial(new File(TRUSTSTORE_PATH));
    }/*from  ww  w  . ja va  2  s  .com*/
    sslContextBuilder.build();
    SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(
            sslContextBuilder.build());

    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", connectionSocketFactory).register("http", new PlainConnectionSocketFactory())
            .build();
    HttpClientConnectionManager clientConnectionManager = new PoolingHttpClientConnectionManager(registry);

    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    return httpClientBuilder.setConnectionManager(clientConnectionManager).setMaxConnTotal(20)
            .setMaxConnPerRoute(2).setDefaultRequestConfig(requestConfig).build();
}

From source file:de.zazaz.iot.bosch.indego.ifttt.IftttIndegoAdapter.java

/**
 * This creates a HTTP client instance for connecting the IFTTT server.
 * //  w ww . j  av a 2 s . com
 * @return the HTTP client instance
 */
private CloseableHttpClient buildHttpClient() {
    if (configuration.isIftttIgnoreServerCertificate()) {
        try {
            SSLContextBuilder builder = new SSLContextBuilder();
            builder.loadTrustMaterial(new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] chain_, String authType_)
                        throws CertificateException {
                    return true;
                }
            });
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (Exception ex) {
            LOG.error(ex);
            // This should never happen, but we have to handle it
            throw new RuntimeException(ex);
        }
    } else {
        return HttpClients.createDefault();
    }
}

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}.
 *///  w w  w  .ja v a2s  .  c o 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.apache.camel.component.etcd.EtcdEndpoint.java

@Override
protected void doStart() throws Exception {
    if ((configuration.getTrustSelfsigned() == true) || (configuration.getCaFile() != null)
            || (configuration.getKeyFile() != null)) {
        // Need to create a custom httpclient since we need to change the SSL information.
        SSLContextBuilder builder = new SSLContextBuilder();
        if (configuration.getTrustSelfsigned() == true) {
            // Don't need to look at the CA file since we are going to trust anyhow.
            final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
                @Override/*from  ww  w .ja  va  2  s  .c  om*/
                public boolean isTrusted(X509Certificate[] certificate, String authType) {
                    return true;
                }
            };
            builder.loadTrustMaterial(acceptingTrustStrategy);
        } else {
            if (configuration.getCaFile() != null) {
                builder.loadTrustMaterial(new File(configuration.getCaFile()));
            }
        }
        // Now check if there are any private keys.
        if (configuration.getKeyFile() != null) {
            builder.loadKeyMaterial(new File(configuration.getKeyFile()), null, null);
        }
        //SSLSocketFactory socketfactory = SSLSocketFactory(builder.build());
        final CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom().setSSLContext(builder.build())
                .build();
        etcdClient = new EtcdClient(configuration.makeURI());
    } else {
        etcdClient = new EtcdClient(configuration.makeURI());
    }
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestHopsworksRMAppSecurityActions.java

private Pair<String, String[]> loginAndGetJWT() throws Exception {
    CloseableHttpClient client = null;/*w  w  w .ja  va 2 s. c  o m*/
    try {
        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
        sslContextBuilder.loadTrustMaterial(new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                return true;
            }
        });
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(),
                NoopHostnameVerifier.INSTANCE);

        client = HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
        URL loginURL = new URL(new URL(HOPSWORKS_ENDPOINT), HOPSWORKS_LOGIN_PATH);
        HttpUriRequest login = RequestBuilder.post().setUri(loginURL.toURI())
                .addParameter("email", HOPSWORKS_USER).addParameter("password", HOPSWORKS_PASSWORD).build();
        CloseableHttpResponse response = client.execute(login);
        Assert.assertNotNull(response);
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
        Header[] authHeaders = response.getHeaders(HttpHeaders.AUTHORIZATION);

        String masterJWT = null;
        for (Header h : authHeaders) {
            Matcher matcher = HopsworksRMAppSecurityActions.JWT_PATTERN.matcher(h.getValue());
            if (matcher.matches()) {
                masterJWT = matcher.group(1);
            }
        }
        JsonParser jsonParser = new JsonParser();
        JsonObject json = jsonParser.parse(EntityUtils.toString(response.getEntity())).getAsJsonObject();
        JsonArray array = json.getAsJsonArray("renewTokens");
        String[] renewTokens = new String[array.size()];
        boolean renewalTokensFound = false;
        for (int i = 0; i < renewTokens.length; i++) {
            renewTokens[i] = array.get(i).getAsString();
            renewalTokensFound = true;
        }
        if (masterJWT != null && renewalTokensFound) {
            return new Pair<>(masterJWT, renewTokens);
        }

        throw new IOException("Could not get JWT from Hopsworks");
    } finally {
        if (client != null) {
            client.close();
        }
    }
}