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:org.surveydroid.android.coms.SDHttpClient.java

private SSLSocketFactory newSslSocketFactory() {
    try {/*w  w w  .ja v a  2  s  .co m*/
        KeyStore trusted = KeyStore.getInstance("BKS");
        InputStream in = ctxt.getResources().openRawResource(R.raw.sd_keystore);
        try {
            trusted.load(in, PASSWORD.toCharArray());
        } catch (CertificateException e) {
            Util.e(null, TAG, "Cert Exception: " + Util.fmt(e));
            throw new AssertionError(e);
        } finally {
            in.close();
        }
        SSLSocketFactory sf = new SSLSocketFactory(trusted);
        //TODO look into this
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        return sf;
    } catch (Exception e) {
        Util.e(ctxt, TAG, Util.fmt(e));
        throw new AssertionError(e);
    }
}

From source file:HttpsRequestDemo.java

private void registerSSLSocketFactory(HttpClient httpclient) throws Exception {
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    // E:\\cer\\cpic_jttp.keystore
    FileInputStream instream = new FileInputStream(
            new File("E:\\Program Files\\Java\\jdk1.7.0_21\\bin\\cpic_jttp.keystore"));
    try {//w ww. j  a v a 2s  .  co  m
        trustStore.load(instream, "cpicJttp".toCharArray());
    } finally {
        instream.close();
    }

    SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
    Scheme sch = new Scheme("https", socketFactory, 443);
    httpclient.getConnectionManager().getSchemeRegistry().register(sch);
}

From source file:org.qi4j.library.shiro.StrictX509Test.java

@Test
public void test() throws IOException {
    HttpGet get = new HttpGet(SECURED_SERVLET_PATH);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();

    DefaultHttpClient client = new DefaultHttpClient();
    SSLSocketFactory sslsf = new SSLSocketFactory(X509FixturesData.clientSSLContext());
    sslsf.setHostnameVerifier(new AllowAllHostnameVerifier()); // For unit testing convenience only, do not use in production
    Scheme https = new Scheme("https", sslsf, httpHost.getPort());
    client.getConnectionManager().getSchemeRegistry().register(https);

    String response = client.execute(httpHost, get, responseHandler);
    assertEquals(ServletUsingSecuredService.OK, response);
}

From source file:com.ntsync.android.sync.client.MyHttpClient.java

private SocketFactory getSSLSocketFactory() {
    InputStream in = null;//w  w w. ja  v  a  2  s .co  m
    SocketFactory socketFack = null;
    try {
        KeyStore trusted = KeyStore.getInstance("BKS");
        in = context.getResources().openRawResource(R.raw.mykeystore);
        trusted.load(in, "pwd23key".toCharArray());
        SSLSocketFactory sslSocketFack = new SSLSocketFactory(trusted);
        socketFack = sslSocketFack;
        if (Constants.USE_RELEASE_CONFIG) {
            sslSocketFack.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        } else {
            Log.w(TAG, "Disable SSL Hostname verification");
            sslSocketFack.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        }
    } catch (GeneralSecurityException e) {
        Log.e(TAG, "Loading truststore failed.", e);
    } catch (IOException e) {
        Log.e(TAG, "Loading truststore failed.", e);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            Log.e(TAG, "closing filescocket failed.", e);
        }
    }
    if (socketFack == null) {
        Log.w(TAG, "Fallback to custom ssl socket factory.");
        socketFack = new MySSLSocketFactory();
    }
    return socketFack;
}

From source file:com.sun.identity.proxy.client.ClientHandler.java

/**
 * Returns a new SSL socket factory that does not perform hostname
 * verification.//w w w.j a  v a2s  . c  o  m
 *
 * @return the new SSL socket factory.
 */
private static SSLSocketFactory newSSLSocketFactory() {
    SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("TLS");
    } catch (NoSuchAlgorithmException nsae) {
        throw new IllegalStateException(nsae); // TODO: handle this better?
    }
    try {
        sslContext.init(null, null, null);
    } catch (KeyManagementException kme) {
        throw new IllegalStateException(kme); // TODO: handle this better?
    }
    SSLSocketFactory sslSocketFactory = new SSLSocketFactory(sslContext);
    sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    return sslSocketFactory;
}

From source file:co.cask.cdap.security.server.ExternalAuthenticationServerSSLTest.java

@Override
protected HttpClient getHTTPClient() throws Exception {
    SSLContext sslContext = SSLContext.getInstance("SSL");

    // set up a TrustManager that trusts everything
    sslContext.init(null, new TrustManager[] { new X509TrustManager() {
        @Override/* ww  w  . j av a 2s  . com*/
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
            //
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
            //
        }

    } }, new SecureRandom());

    SSLSocketFactory sf = new SSLSocketFactory(sslContext);
    Scheme httpsScheme = new Scheme("https", getAuthServerPort(), sf);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(httpsScheme);

    // apache HttpClient version >4.2 should use BasicClientConnectionManager
    ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry);
    return new DefaultHttpClient(cm);
}

From source file:com.vmware.vchs.publicapi.samples.HttpUtils.java

/**
 * This method returns an HttpClient instance wrapped to trust all HTTPS certificates.
 * /*ww  w. j a va2 s .  co m*/
 * @return HttpClient a new instance of HttpClient
 */
static HttpClient createHttpClient() {
    HttpClient base = new DefaultHttpClient();

    try {
        SSLContext ctx = SSLContext.getInstance("TLS");

        // WARNING: This creates a TrustManager that trusts all certificates and should not be used in production code.
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

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

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } };

        ctx.init(null, trustAllCerts, 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", 443, ssf));

        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.globo.aclapi.client.ClientAclAPI.java

private static ApacheHttpTransport getTransport(int timeout, boolean verifySSL) throws RuntimeException {
    if (verifySSL) {
        return new ApacheHttpTransport(newDefaultHttpClient(SSLSocketFactory.getSocketFactory(),
                getHttpParams(timeout), ProxySelector.getDefault()));

    } else {//www .j  a v a 2 s  . com
        try {
            SSLContext ctx = SSLContext.getInstance("SSL");
            X509TrustManager tm = new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
            };

            ctx.init(null, new TrustManager[] { tm }, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx);
            return new ApacheHttpTransport(
                    newDefaultHttpClient(ssf, getHttpParams(timeout), ProxySelector.getDefault()));

        } catch (Exception e) {
            throw new RuntimeException("ERRO ssl schema", e);
        }
    }
}

From source file:org.openiot.gsn.http.rest.RestRemoteWrapper.java

public boolean initialize() {
    try {//ww  w  .  java 2 s . co  m
        initParams = new RemoteWrapperParamParser(getActiveAddressBean(), false);
        httpclient = new DefaultHttpClient(getHttpClientParams(initParams.getTimeout()));
        // Init the http client
        if (initParams.isSSLRequired()) {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(new FileInputStream(new File("conf/servertestkeystore")),
                    Main.getContainerConfig().getSSLKeyStorePassword().toCharArray());
            SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
            socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            int sslPort = Main.getContainerConfig().getSSLPort() > 0 ? Main.getContainerConfig().getSSLPort()
                    : ContainerConfig.DEFAULT_SSL_PORT;
            Scheme sch = new Scheme("https", socketFactory, sslPort);
            httpclient.getConnectionManager().getSchemeRegistry().register(sch);
        }
        Scheme plainsch = new Scheme("http", PlainSocketFactory.getSocketFactory(),
                Main.getContainerConfig().getContainerPort());
        httpclient.getConnectionManager().getSchemeRegistry().register(plainsch);
        //
        lastReceivedTimestamp = initParams.getStartTime();
        structure = connectToRemote();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    }
    return true;
}