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:nl.architolk.ldt.processors.HttpClientProperties.java

private static void initialize() throws Exception {
    notInitialized = false;/*from www  .  j  av a 2 s  .  c  o m*/

    //Fetch property-values
    PropertySet props = Properties.instance().getPropertySet();
    String proxyHost = props.getString("oxf.http.proxy.host");
    Integer proxyPort = props.getInteger("oxf.http.proxy.port");
    proxyExclude = props.getString("oxf.http.proxy.exclude");
    String sslKeystoreURI = props.getStringOrURIAsString("oxf.http.ssl.keystore.uri", false);
    String sslKeystorePassword = props.getString("oxf.http.ssl.keystore.password");

    //Create custom scheme if needed
    if (sslKeystoreURI != null && sslKeystorePassword != null) {
        SSLContext sslcontext = SSLContexts.custom()
                .loadTrustMaterial(new URL(sslKeystoreURI), sslKeystorePassword.toCharArray()).build();
        sslsf = new SSLConnectionSocketFactory(sslcontext);
    }

    //Create requestConfig proxy if needed
    if (proxyHost != null && proxyPort != null) {
        requestConfig = RequestConfig.custom().setProxy(new HttpHost(proxyHost, proxyPort, "http")).build();
    }
}

From source file:com.muhardin.endy.training.ws.aplikasi.absen.rest.client.AbsenRestClient.java

public AbsenRestClient() {
    try {//from ww w  .j  a  v a  2  s .  c  om
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());

        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("endy", "123");
        provider.setCredentials(AuthScope.ANY, credentials);
        HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider)
                .setSSLSocketFactory(sslsf).build();

        restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(client));
        restTemplate.setErrorHandler(new AbsenRestClientErrorHandler());
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
        Logger.getLogger(AbsenRestClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:comsat.sample.tomcat.SampleTomcatSslApplicationTests.java

@Test
public void testHome() throws Exception {
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());

    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();

    TestRestTemplate testRestTemplate = new TestRestTemplate();
    ((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory()).setHttpClient(httpClient);
    ResponseEntity<String> entity = testRestTemplate.getForEntity("https://localhost:" + this.port,
            String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertEquals("Hello, world", entity.getBody());
}

From source file:sample.tomcat.ssl.SampleTomcatSslApplicationTests.java

@Test
public void testHome() throws Exception {
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());

    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();

    TestRestTemplate testRestTemplate = new TestRestTemplate();
    ((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory()).setHttpClient(httpClient);
    ResponseEntity<String> entity = testRestTemplate.getForEntity("https://localhost:" + this.port,
            String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(entity.getBody()).isEqualTo("Hello, world");
}

From source file:com.vaushell.superpipes.tools.HTTPhelper.java

/**
 * Create a standard builder, firefox agent and ssl easy support.
 *
 * @return the builder./*  w ww.  ja v  a2s  .  com*/
 */
public static HttpClientBuilder createBuilder() {
    try {
        return HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore())
                .setUserAgent("Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0.1")
                .setSSLSocketFactory(new SSLConnectionSocketFactory(new SSLContextBuilder()
                        .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()));
    } catch (final KeyManagementException | KeyStoreException | NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.ibm.watson.app.common.util.http.HttpClientBuilder.java

public static CloseableHttpClient buildDefaultHttpClient(Credentials cred) {

    // Use custom cookie store if necessary.
    CookieStore cookieStore = new BasicCookieStore();
    // Use custom credentials provider if necessary.
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    RequestConfig defaultRequestConfig;/*from   www.ja  va2 s . co  m*/

    //private DefaultHttpClient client;
    connManager.setMaxTotal(200);
    connManager.setDefaultMaxPerRoute(50);
    try {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());

        // Create a registry of custom connection socket factories for supported
        // protocol schemes.
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                .<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE)
                .register("https", new SSLConnectionSocketFactory(builder.build())).build();
    } catch (Exception e) {
        logger.warn(MessageKey.AQWEGA02000W_unable_init_ssl_context.getMessage(), e);
    }
    // Create global request configuration
    defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH)
            .setExpectContinueEnabled(true)
            .setTargetPreferredAuthSchemes(
                    Arrays.asList(AuthSchemes.BASIC, AuthSchemes.NTLM, AuthSchemes.DIGEST))
            .setAuthenticationEnabled(true).build();

    if (cred != null)
        credentialsProvider.setCredentials(AuthScope.ANY, cred);

    return HttpClients.custom().setConnectionManager(connManager).setDefaultCookieStore(cookieStore)
            .setDefaultCredentialsProvider(credentialsProvider).setDefaultRequestConfig(defaultRequestConfig)
            .build();
}

From source file:comsat.sample.jetty.SampleJetty8SslApplicationTests.java

@Test
public void testHome() throws Exception {
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());

    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();

    TestRestTemplate testRestTemplate = new TestRestTemplate();
    ((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory()).setHttpClient(httpClient);
    ResponseEntity<String> entity = testRestTemplate.getForEntity("https://localhost:" + this.port,
            String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertEquals("Hello World", entity.getBody());
}

From source file:com.fanmei.pay4j.http.WeixinSSLRequestExecutor.java

public WeixinSSLRequestExecutor(WeixinConfig weixinConfig) throws WeixinException {
    InputStream inputStream = this.getClass().getClassLoader()
            .getResourceAsStream(weixinConfig.getCertificateFile());
    try {/*from  ww  w .  j a  v  a2 s . co m*/
        String password = weixinConfig.getAccount().getCertificateKey();
        KeyStore keyStore = KeyStore.getInstance(Constants.PKCS12);
        keyStore.load(inputStream, password.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(Constants.SunX509);
        kmf.init(keyStore, password.toCharArray());
        SSLContext sslContext = SSLContext.getInstance(Constants.TLS);
        sslContext.init(kmf.getKeyManagers(), null, new java.security.SecureRandom());

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    } catch (Exception e) {
        throw WeixinException.of("Key load error", e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {

            }
        }
    }
}

From source file:org.mobicents.servlet.restcomm.http.CustomHttpClientBuilder.java

private static HttpClient buildAllowallClient(RequestConfig requestConfig) {
    HttpConnectorList httpConnectorList = UriUtils.getHttpConnectorList();
    HttpClient httpClient = null;/*  w  w w. j a va2  s.co  m*/
    //Enable SSL only if we have HTTPS connector
    List<HttpConnector> connectors = httpConnectorList.getConnectors();
    Iterator<HttpConnector> iterator = connectors.iterator();
    while (iterator.hasNext()) {
        HttpConnector connector = iterator.next();
        if (connector.isSecure()) {
            SSLConnectionSocketFactory sslsf;
            try {
                SSLContextBuilder builder = new SSLContextBuilder();
                builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
                sslsf = new SSLConnectionSocketFactory(builder.build());
                httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
                        .setSSLSocketFactory(sslsf).build();
            } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
                throw new RuntimeException("Error creating HttpClient", e);
            }
            break;
        }
    }
    if (httpClient == null) {
        httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
    }

    return httpClient;
}

From source file:slash.navigation.rest.ssl.SSLConnectionManagerFactory.java

public HttpClientConnectionManager createConnectionManager() throws CertificateException,
        NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException {
    SSLContext sslContext = createSSLContext();
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
    return new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build());
}