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 SSLContext sslContext) 

Source Link

Usage

From source file:com.nesscomputing.httpclient.factory.httpclient4.ApacheHttpClient4Factory.java

public ApacheHttpClient4Factory(final HttpClientDefaults clientDefaults,
        @Nullable final Set<? extends HttpClientObserver> httpClientObservers) {
    Preconditions.checkArgument(clientDefaults != null, "clientDefaults can not be null!");

    this.httpClientObservers = httpClientObservers;

    initParams();//  w w  w. ja  va 2  s. c o  m

    registry.register(HTTP_SCHEME);

    try {
        final TrustManager[] trustManagers = new TrustManager[] { getTrustManager(clientDefaults) };
        final KeyManager[] keyManagers = getKeyManagers(clientDefaults);

        final SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagers, trustManagers, null);
        final SSLSocketFactory sslSocketFactory = new SSLSocketFactory(sslContext);

        registry.register(new Scheme("https", HTTPS_PORT, sslSocketFactory));
    } catch (GeneralSecurityException ce) {
        throw new IllegalStateException(ce);
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }

    connectionManager = new ThreadSafeClientConnManager(registry);

    defaultAcceptEncoding = StringUtils.trimToNull(clientDefaults.getDefaultAcceptEncoding());
}

From source file:iristk.speech.nuancecloud.NuanceCloudSynthesizer.java

private HttpClient getHttpClient() throws NoSuchAlgorithmException, KeyManagementException {
    // Standard HTTP parameters
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUseExpectContinue(params, false);

    // Initialize the HTTP client
    httpclient = new DefaultHttpClient(params);

    // Initialize/setup SSL
    TrustManager easyTrustManager = new X509TrustManager() {
        @Override// w w w. j  a v  a 2s .c om
        public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                throws java.security.cert.CertificateException {
            // TODO Auto-generated method stub
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                throws java.security.cert.CertificateException {
            // TODO Auto-generated method stub
        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            // TODO Auto-generated method stub
            return null;
        }
    };

    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);
    SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
    sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Scheme sch = new Scheme("https", sf, PORT); // PORT = 443
    httpclient.getConnectionManager().getSchemeRegistry().register(sch);

    // Return the initialized instance of our httpclient
    return httpclient;
}

From source file:com.foundationdb.http.HttpMonitorVerifySSLIT.java

/**
 * This code sets up the httpclient to accept any SSL certificate. The 
 * SSL certificate generated by the instructions above is not correctly
 * signed, so we need ignore the problem. 
 * This code should not, under any circumstances, be allowed anywhere 
 * the production code. //from  w ww.j  a  va 2 s  .co m
 * @param base
 * @return
 */
private HttpClient wrapClient(HttpClient base) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");

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

From source file:org.gw2InfoViewer.factories.HttpsConnectionFactory.java

public static HttpClient getHttpsClientWithProxy(Certificate[] sslCertificate, String proxyAddress,
        int proxyPort) {
    DefaultHttpClient httpClient;/*from   w  w  w.j av  a2 s  .  co m*/
    HttpHost proxy;

    httpClient = new DefaultHttpClient();
    try {
        TrustManagerFactory tf = TrustManagerFactory.getInstance("X509");
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null);
        for (int i = 0; i < sslCertificate.length; i++) {
            ks.setCertificateEntry("StartCom" + i, sslCertificate[i]);
        }

        tf.init(ks);
        TrustManager[] tm = tf.getTrustManagers();

        SSLContext sslCon = SSLContext.getInstance("SSL");
        sslCon.init(null, tm, new SecureRandom());
        SSLSocketFactory socketFactory = new SSLSocketFactory(ks);
        Scheme sch = new Scheme("https", 443, socketFactory);

        proxy = new HttpHost(proxyAddress, proxyPort, "https");
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        httpClient.getConnectionManager().getSchemeRegistry().register(sch);
    } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException
            | KeyManagementException | UnrecoverableKeyException ex) {
        Logger.getLogger(HttpsConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
    }

    return httpClient;
}

From source file:com.googlesource.gerrit.plugins.hooks.rtc.network.RTCClient.java

private void setSSLTrustStrategy(boolean sslVerify) throws IOException {
    try {/*  w  w w. j  a va  2s .  c o m*/
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        SSLContext sc;

        if (sslVerify) {
            sc = SSLContext.getDefault();
        } else {
            sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
        }

        SSLSocketFactory sf = new SSLSocketFactory(sc);
        sf.setHostnameVerifier(new AllowAllHostnameVerifier());
        SchemeRegistry schemeRegistry = httpclient.getConnectionManager().getSchemeRegistry();
        schemeRegistry.register(new Scheme("https", sf, 443));
    } catch (Exception any) {
        throw new IOException(any);
    }
}

From source file:com.googlesource.gerrit.plugins.its.rtc.network.RTCClient.java

private void setSSLTrustStrategy(boolean sslVerify) throws IOException {
    try {//from   w  w w . ja va  2  s  .  c  o  m
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        SSLContext sc;

        if (sslVerify) {
            sc = SSLContext.getDefault();
        } else {
            sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
        }

        SSLSocketFactory sf = new SSLSocketFactory(sc);
        sf.setHostnameVerifier(new AllowAllHostnameVerifier());
        SchemeRegistry schemeRegistry = httpclient.getConnectionManager().getSchemeRegistry();
        schemeRegistry.register(new Scheme("https", sf, 443));
    } catch (Exception any) {
        throw new IOException(any);
    }
}

From source file:org.forgerock.openig.handler.ClientHandler.java

/**
 * Returns a new SSL socket factory that does not perform hostname verification.
 *//*from   ww w . j av a  2s  .c o m*/
private static SSLSocketFactory newSSLSocketFactory() {
    SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("TLS");
    } catch (NoSuchAlgorithmException nsae) {
        throw new IllegalStateException(nsae);
    }
    try {
        sslContext.init(null, null, null);
    } catch (KeyManagementException kme) {
        throw new IllegalStateException(kme);
    }
    SSLSocketFactory sslSocketFactory = new SSLSocketFactory(sslContext);
    sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    return sslSocketFactory;
}

From source file:edu.isi.misd.tagfiler.client.JakartaClient.java

/**
 * Initialize the HTTP client//from  w w  w . j  a  v  a 2s. com
 * 
 * @param connections
 *            the maximum number of HTTP connections
 * @param socketBufferSize
 *            the socket buffer size
 * @param socketTimeout
 *            the socket buffer timeout
 */
private void init(int maxConnections, int socketBufferSize, int socketTimeout) throws Throwable {
    TrustManager easyTrustManager = new X509TrustManager() {

        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // Oh, I am easy!
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            if (chain != null) {
                for (int i = 0; i < chain.length; i++) {
                    chain[i].checkValidity();
                }
            }
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

    };

    SSLContext sslcontext = SSLContext.getInstance("SSL");
    sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);
    SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
    sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    BasicHttpParams params = new BasicHttpParams();
    params.setParameter("http.protocol.handle-redirects", false);
    params.setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, socketBufferSize);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout);

    // enable parallelism
    ConnPerRouteBean connPerRoute = new ConnPerRouteBean(maxConnections);
    ConnManagerParams.setMaxTotalConnections(params, maxConnections >= 2 ? maxConnections : 2);
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    Scheme sch = new Scheme("https", sf, 443);
    schemeRegistry.register(sch);
    //schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

    httpclient = new DefaultHttpClient(cm, params);
    BasicCookieStore cookieStore = new BasicCookieStore();
    httpclient.setCookieStore(cookieStore);
}

From source file:com.wso2.mobile.mdm.utils.ServerUtilities.java

public static HttpClient getCertifiedHttpClient(Context context) {
    try {//from   ww w  .  jav  a  2s.  c  o  m
        KeyStore localTrustStore = KeyStore.getInstance("BKS");
        InputStream in = context.getResources().openRawResource(R.raw.emm_truststore);
        localTrustStore.load(in, CommonUtilities.TRUSTSTORE_PASSWORD.toCharArray());

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        SSLSocketFactory sslSocketFactory = new SSLSocketFactory(localTrustStore);
        schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
        HttpParams params = new BasicHttpParams();
        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

        HttpClient client = new DefaultHttpClient(cm, params);
        return client;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}