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

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

Introduction

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

Prototype

public SSLSocketFactory(final javax.net.ssl.SSLSocketFactory socketfactory,
        final X509HostnameVerifier hostnameVerifier) 

Source Link

Usage

From source file:projekat.rest_client.Test.java

public static void main2(String[] args)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {

    String uri = "https://localhost:8443/secure/rest/places";
    RestTemplateFactory factory = new RestTemplateFactory();
    factory.afterPropertiesSet();/* w ww  .  j  a  v  a2s. c o  m*/
    RestTemplate restTemplate = factory.getObject();
    //        restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<T>(createHeaders("marko", "marko")), List.class);
    HttpClient httpClient = factory.getAuth().getHttpClient();
    TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
        @Override
        public boolean isTrusted(X509Certificate[] certificate, String authType) {
            return true;
        }
    };
    SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER);
    httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf));
    ResponseEntity<List> exchange = restTemplate.exchange(uri, HttpMethod.POST,
            new HttpEntity<>(createHeaders("marko", "marko")), List.class);

    List body = exchange.getBody();
    for (Object object : body) {
        System.out.println("Mesto: " + object);
    }

    System.out.println("******************");
    ResponseEntity<List> forEntity = restTemplate.getForEntity(uri, List.class);
    List body1 = forEntity.getBody();
    for (Object object : body1) {
        System.out.println("Mesto: " + object);
    }

}

From source file:com.senseidb.dataprovider.http.HttpsClientDecorator.java

public static DefaultHttpClient decorate(DefaultHttpClient base) {
    try {/*  ww w  .  ja v  a2  s .  c om*/
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
        return null;
    }
}

From source file:org.ow2.proactive_grid_cloud_portal.common.server.HttpUtils.java

public static DefaultHttpClient createDefaultExecutor() {
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    cm.setDefaultMaxPerRoute(50);//www. java 2  s. c o  m
    cm.setMaxTotal(50);
    DefaultHttpClient httpClient = new DefaultHttpClient(cm);

    try {
        SSLSocketFactory socketFactory = new SSLSocketFactory(new RelaxedTrustStrategy(),
                SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme https = new Scheme("https", 443, socketFactory);
        httpClient.getConnectionManager().getSchemeRegistry().register(https);
    } catch (Exception ignored) {
    }

    return httpClient;
}

From source file:org.ow2.proactive.scheduler.rest.utils.HttpUtility.java

public static void setInsecureAccess(HttpClient client)
        throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException {

    SSLSocketFactory socketFactory = new SSLSocketFactory(new RelaxedTrustStrategy(),
            SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Scheme https = new Scheme("https", 443, socketFactory);
    client.getConnectionManager().getSchemeRegistry().register(https);
}

From source file:brooklyn.networking.cloudstack.HttpUtil.java

public static HttpClient createHttpClient(URI uri, Optional<Credentials> credentials) {
    final DefaultHttpClient httpClient = new DefaultHttpClient();

    // TODO if supplier returns null, we may wish to defer initialization until url available?
    if (uri != null && "https".equalsIgnoreCase(uri.getScheme())) {
        try {/*from w  w  w . ja  va  2 s .c om*/
            int port = (uri.getPort() >= 0) ? uri.getPort() : 443;
            SSLSocketFactory socketFactory = new SSLSocketFactory(new TrustAllStrategy(),
                    SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            Scheme sch = new Scheme("https", port, socketFactory);
            httpClient.getConnectionManager().getSchemeRegistry().register(sch);
        } catch (Exception e) {
            LOG.warn("Error in HTTP Feed of {}, setting trust for uri {}", uri);
            throw Exceptions.propagate(e);
        }
    }

    // Set credentials
    if (uri != null && credentials.isPresent()) {
        String hostname = uri.getHost();
        int port = uri.getPort();
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(hostname, port), credentials.get());
    }

    return httpClient;
}

From source file:org.openntf.xpt.agents.master.ClientSSLResistanceExtender.java

public static HttpClient wrapClient(HttpClient base) {
    try {//ww w .jav a  2 s . c  o m
        SSLContext ctx = SSLContext.getInstance("sslv3");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            public void verify(String arg0, SSLSocket arg1) throws IOException {
            }

            public void verify(String arg0, X509Certificate arg1) throws SSLException {
            }

            public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException {
            }

            public boolean verify(String hostname, SSLSession session) {
                return true;
            }

        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, verifier);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:piecework.client.LoadTester.java

public LoadTester(KeyStore keystore, SecuritySettings securitySettings) {
    ClientConnectionManager cm;/*from www .  j  a v  a2s .co  m*/
    try {
        SSLSocketFactory sslSocketFactory = new SSLSocketFactory(keystore,
                new String(securitySettings.getKeystorePassword()));
        X509HostnameVerifier hostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
        sslSocketFactory.setHostnameVerifier(hostnameVerifier);

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("https", 8443, sslSocketFactory));

        cm = new PoolingClientConnectionManager(schemeRegistry);
    } catch (Exception e) {
        cm = new BasicClientConnectionManager();
    }
    this.client = new DefaultHttpClient(cm);
}

From source file:com.almende.reaal.apachehttp.ApacheHttpClient.java

/**
 * Instantiates a new apache http client.
 *
 *///  w w w  .  j a  v a  2  s  .c o  m
private ApacheHttpClient() {

    // Allow self-signed SSL certificates:
    final TrustStrategy trustStrategy = new TrustSelfSignedStrategy();
    final X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
    final SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();

    SSLSocketFactory sslSf;
    try {
        sslSf = new SSLSocketFactory(trustStrategy, hostnameVerifier);
        final Scheme https = new Scheme("https", 443, sslSf);
        schemeRegistry.register(https);
    } catch (Exception e) {
        LOG.warning("Couldn't init SSL socket, https not supported!");
    }

    // Work with PoolingClientConnectionManager
    final ClientConnectionManager connection = new PoolingClientConnectionManager(schemeRegistry);

    // Provide eviction thread to clear out stale threads.
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (true) {
                    synchronized (this) {
                        wait(5000);
                        connection.closeExpiredConnections();
                        connection.closeIdleConnections(30, TimeUnit.SECONDS);
                    }
                }
            } catch (final InterruptedException ex) {
            }
        }
    }).start();

    // generate httpclient
    httpClient = new DefaultHttpClient(connection);

    final HttpParams params = httpClient.getParams();

    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
    params.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
    params.setParameter(CoreConnectionPNames.TCP_NODELAY, true);
    httpClient.setParams(params);
}

From source file:com.seajas.search.codex.social.connection.TrustingClientHttpRequestFactory.java

public TrustingClientHttpRequestFactory()
        throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    super();//w w  w  .j a  v a2  s . com

    this.getHttpClient().getConnectionManager().getSchemeRegistry()
            .register(new Scheme("https", 443, new SSLSocketFactory(new TrustStrategy() {
                @Override
                public boolean isTrusted(final X509Certificate[] chain, final String authType)
                        throws CertificateException {
                    if (logger.isTraceEnabled())
                        logger.trace(format("Trusting certificate chain with %d certificates and auth type %s",
                                chain != null ? chain.length : 0, authType));

                    return true;
                }
            }, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)));
}

From source file:org.apache.activemq.transport.https.HttpsClientTransport.java

private SchemeRegistry createSchemeRegistry() {

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    try {/*from ww  w  .  j  a v a2 s  .  co m*/
        SSLSocketFactory sslSocketFactory = new SSLSocketFactory(createSocketFactory(),
                SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        schemeRegistry.register(new Scheme("https", getRemoteUrl().getPort(), sslSocketFactory));
        return schemeRegistry;
    } catch (Exception e) {
        throw new IllegalStateException("Failure trying to create scheme registry", e);
    }
}