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

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

Introduction

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

Prototype

public SSLContextBuilder() 

Source Link

Usage

From source file:org.eclipse.php.composer.api.packages.AsyncDownloader.java

protected void init() {
    super.init();

    try {//from   ww  w.  ja  v a 2 s  .co m
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());

        SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(builder.build());
        Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", ssf) //$NON-NLS-1$//$NON-NLS-2$
                .build();
        connectionManager = new PoolingHttpClientConnectionManager(r);
    } catch (NoSuchAlgorithmException e) {
        log.error("Exception during init", e); //$NON-NLS-1$
    } catch (KeyManagementException e) {
        log.error("Exception during init", e); //$NON-NLS-1$
    } catch (KeyStoreException e) {
        log.error("Exception during init", e); //$NON-NLS-1$
    }

}

From source file:org.flowable.app.service.editor.AppDefinitionPublishService.java

protected void deployZipArtifact(String artifactName, byte[] zipArtifact, String deploymentKey,
        String deploymentName) {// ww  w .  ja v  a 2  s  . c  o  m
    String deployApiUrl = environment.getRequiredProperty("deployment.api.url");
    String basicAuthUser = environment.getRequiredProperty("idm.admin.user");
    String basicAuthPassword = environment.getRequiredProperty("idm.admin.password");

    if (deployApiUrl.endsWith("/") == false) {
        deployApiUrl = deployApiUrl.concat("/");
    }
    deployApiUrl = deployApiUrl
            .concat(String.format("repository/deployments?deploymentKey=%s&deploymentName=%s",
                    encode(deploymentKey), encode(deploymentName)));

    HttpPost httpPost = new HttpPost(deployApiUrl);
    httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + new String(
            Base64.encodeBase64((basicAuthUser + ":" + basicAuthPassword).getBytes(Charset.forName("UTF-8")))));

    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
    entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    entityBuilder.addBinaryBody("artifact", zipArtifact, ContentType.DEFAULT_BINARY, artifactName);

    HttpEntity entity = entityBuilder.build();
    httpPost.setEntity(entity);

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    SSLConnectionSocketFactory sslsf = null;
    try {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        sslsf = new SSLConnectionSocketFactory(builder.build(),
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        clientBuilder.setSSLSocketFactory(sslsf);
    } catch (Exception e) {
        logger.error("Could not configure SSL for http client", e);
        throw new InternalServerErrorException("Could not configure SSL for http client", e);
    }

    CloseableHttpClient client = clientBuilder.build();

    try {
        HttpResponse response = client.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
            return;
        } else {
            logger.error("Invalid deploy result code: {}", response.getStatusLine());
            throw new InternalServerErrorException("Invalid deploy result code: " + response.getStatusLine());
        }
    } catch (IOException ioe) {
        logger.error("Error calling deploy endpoint", ioe);
        throw new InternalServerErrorException("Error calling deploy endpoint: " + ioe.getMessage());
    } finally {
        if (client != null) {
            try {
                client.close();
            } catch (IOException e) {
                logger.warn("Exception while closing http client", e);
            }
        }
    }
}

From source file:org.teknux.jettybootstrap.test.jettybootstrap.AbstractJettyBootstrapTest.java

protected SimpleResponse get(String url) throws IllegalStateException, IOException, JettyBootstrapException,
        NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    SimpleResponse simpleResponse = new SimpleResponse();

    CloseableHttpClient httpClient;/*from w  w w. j  a va  2  s  .  c  o m*/
    HttpGet httpGet;
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIMEOUT).build();

    if (ssl) {
        SSLContextBuilder sSLContextBuilder = new SSLContextBuilder();
        sSLContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sSLConnectionSocketFactory = new SSLConnectionSocketFactory(
                sSLContextBuilder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        httpClient = HttpClients.custom().setSSLSocketFactory(sSLConnectionSocketFactory).build();

        httpGet = new HttpGet("https://" + HOST + ":" + getPort() + url);
    } else {
        httpClient = HttpClients.createDefault();

        httpGet = new HttpGet("http://" + HOST + ":" + getPort() + url);
    }

    httpGet.setConfig(requestConfig);
    CloseableHttpResponse response = null;

    try {
        response = httpClient.execute(httpGet);
        simpleResponse.setStatusCode(response.getStatusLine().getStatusCode());
        simpleResponse.setContent(IOUtils.toString(response.getEntity().getContent()));
    } finally {
        if (response != null) {
            response.close();
        }
        httpClient.close();
    }

    return simpleResponse;
}