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

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

Introduction

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

Prototype

public void setHostnameVerifier(final X509HostnameVerifier hostnameVerifier) 

Source Link

Usage

From source file:org.wso2.carbon.databridge.agent.internal.endpoint.thrift.client.ThriftSecureClientPoolFactory.java

@Override
public Object createClient(String protocol, String hostName, int port)
        throws DataEndpointAgentSecurityException {
    String trustStore, trustStorePw;
    if (protocol.equalsIgnoreCase(DataEndpointConfiguration.Protocol.TCP.toString())) {
        if (params == null) {
            if (getTrustStore() == null) {
                trustStore = System.getProperty("javax.net.ssl.trustStore");
                if (trustStore == null) {
                    throw new DataEndpointAgentSecurityException("No trustStore found");
                } else {
                    setTrustStore(trustStore);
                }//from   www  .  j  a v  a 2  s .c o m
            }

            if (getTrustStorePassword() == null) {
                trustStorePw = System.getProperty("javax.net.ssl.trustStorePassword");
                if (trustStorePw == null) {
                    throw new DataEndpointAgentSecurityException("No trustStore password found");
                } else {
                    setTrustStorePassword(trustStorePw);
                }
            }

            params = new TSSLTransportFactory.TSSLTransportParameters();
            params.setTrustStore(getTrustStore(), getTrustStorePassword());
        }

        TTransport receiverTransport = null;
        try {
            receiverTransport = TSSLTransportFactory.getClientSocket(hostName, port, 0, params);
            TProtocol tProtocol = new TBinaryProtocol(receiverTransport);
            return new ThriftSecureEventTransmissionService.Client(tProtocol);
        } catch (TTransportException e) {
            throw new DataEndpointAgentSecurityException(
                    "Error while trying to connect to " + protocol + "://" + hostName + ":" + port, e);
        }
    } else {
        //TODO:Error  thrown when connecting in http in tests...
        try {
            TrustManager easyTrustManager = new X509TrustManager() {
                public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                        throws java.security.cert.CertificateException {
                }

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

                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    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 httpsScheme = new Scheme("https", sf, port);

            DefaultHttpClient client = new DefaultHttpClient();
            client.getConnectionManager().getSchemeRegistry().register(httpsScheme);

            THttpClient tclient = new THttpClient("https://" + hostName + ":" + port + "/securedThriftReceiver",
                    client);
            TProtocol tProtocol = new TCompactProtocol(tclient);
            ThriftSecureEventTransmissionService.Client authClient = new ThriftSecureEventTransmissionService.Client(
                    tProtocol);
            tclient.open();
            return authClient;
        } catch (Exception e) {
            throw new DataEndpointAgentSecurityException("Cannot create Secure client for " + "https://"
                    + hostName + ":" + port + "/securedThriftReceiver", e);
        }
    }
}

From source file:com.decody.android.core.json.JSONClient.java

public JSONClient(GsonFactory factory, boolean https) {
    this.factory = factory;

    if (https) {/*from  w w w  .  j  a  va2s.com*/
        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
        DefaultHttpClient client = new DefaultHttpClient();

        SchemeRegistry registry = new SchemeRegistry();
        SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
        socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        registry.register(new Scheme("https", socketFactory, 443));
        SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);

        this.client = new DefaultHttpClient(mgr, client.getParams());
    } else {
        client = new DefaultHttpClient();
    }
}

From source file:com.strato.hidrive.api.connection.httpgateway.HTTPGateway.java

/**
 * wrap an httpclient with this stub for prevent ssl unverified exceptions (for testing purposes) 
 *//*from w  ww .  j  a  v a2s. c om*/
public DefaultHttpClient sslStubClient(HttpClient client) {
    try {
        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;
            }
        };
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new StubSSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = client.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, client.getParams());
    } catch (Exception ex) {
        return null;
    }
}

From source file:org.wso2.carbon.databridge.agent.thrift.internal.pool.client.secure.SecureClientPoolFactory.java

@Override
public ThriftSecureEventTransmissionService.Client makeObject(Object key)
        throws AgentSecurityException, TTransportException {
    String[] keyElements = key.toString().split(AgentConstants.SEPARATOR);
    if (keyElements[2].equals(ReceiverConfiguration.Protocol.TCP.toString())) {
        if (params == null) {
            if (trustStore == null) {
                trustStore = System.getProperty("javax.net.ssl.trustStore");
                if (trustStore == null) {
                    throw new AgentSecurityException("No trustStore found");
                }/*  w w w .j  a va2  s  .  co  m*/
                // trustStore = "/home/suho/projects/wso2/trunk/carbon/distribution/product/modules/distribution/target/wso2carbon-4.0.0-SNAPSHOT/repository/resources/security/client-truststore.jks";
            }

            if (trustStorePassword == null) {
                trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
                if (trustStorePassword == null) {
                    throw new AgentSecurityException("No trustStore password found");
                }
                //trustStorePassword = "wso2carbon";
            }

            params = new TSSLTransportFactory.TSSLTransportParameters();
            params.setTrustStore(trustStore, trustStorePassword);
        }

        String[] hostNameAndPort = keyElements[3].split(AgentConstants.HOSTNAME_AND_PORT_SEPARATOR);

        TTransport receiverTransport = null;
        try {
            receiverTransport = TSSLTransportFactory.getClientSocket(
                    HostAddressFinder.findAddress(hostNameAndPort[0]), Integer.parseInt(hostNameAndPort[1]), 0,
                    params);
        } catch (SocketException ignored) {
            //already checked
        }

        TProtocol protocol = new TBinaryProtocol(receiverTransport);
        return new ThriftSecureEventTransmissionService.Client(protocol);
    } else {
        try {
            TrustManager easyTrustManager = new X509TrustManager() {
                public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                        throws java.security.cert.CertificateException {
                }

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

                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            String[] hostNameAndPort = keyElements[3].split(AgentConstants.HOSTNAME_AND_PORT_SEPARATOR);

            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 httpsScheme = new Scheme("https", sf, Integer.parseInt(hostNameAndPort[1]));

            DefaultHttpClient client = new DefaultHttpClient();
            client.getConnectionManager().getSchemeRegistry().register(httpsScheme);

            THttpClient tclient = new THttpClient("https://" + keyElements[3] + "/securedThriftReceiver",
                    client);
            TProtocol protocol = new TCompactProtocol(tclient);
            ThriftSecureEventTransmissionService.Client authClient = new ThriftSecureEventTransmissionService.Client(
                    protocol);
            tclient.open();
            return authClient;
        } catch (Exception e) {
            throw new AgentSecurityException("Cannot create Secure client for " + keyElements[3], e);
        }
    }
}

From source file:com.blackboard.LearnServer.java

private AbstractHttpClient getTrustAllSSLHttpClient() {
    try {/*from  w  ww. j a va 2  s .  co m*/
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new TrustAllSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        System.out.println("WARNING: Could not create Trust All SSL client, using default" + e.getMessage());
        return new DefaultHttpClient();
    }
}

From source file:mobisocial.musubi.util.CertifiedHttpClient.java

private SSLSocketFactory newSslSocketFactory() {
    try {// ww w  . j  av a2 s  .  c  om
        KeyStore trusted = KeyStore.getInstance("BKS");
        InputStream in = mContext.getResources().openRawResource(R.raw.servercertificates);
        try {
            trusted.load(in, "ez24get".toCharArray());
        } finally {
            in.close();
        }
        SSLSocketFactory sf = new SSLSocketFactory(trusted);
        //don't check the host name because we are doing funny redirects.  the
        //actual cert is good enough because it is bundled.
        sf.setHostnameVerifier(new AllowAllHostnameVerifier());
        return sf;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

From source file:com.xyproto.archfriend.Web.java

private HttpClient getNewHttpClient() {
    try {//  w  ww  . j av  a  2s .  c  o  m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:com.cellobject.oikos.util.NetworkHelper.java

public HttpClient createHttpClient() {
    try {/*  www .  j a v a2  s .  co m*/
        final KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        final SSLSocketFactory sf = new IISSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        final HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        final SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));
        final ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (final Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:org.openiot.gsn.http.ac.GSNClient.java

public GSNClient(String host, int gsnhttpport, int gsnhttpsport) {
    this.host = host;
    this.gsnhttpport = gsnhttpport;
    this.gsnhttpsport = gsnhttpsport;
    httpclient = new DefaultHttpClient();
    FileInputStream instream = null;
    try {/*from   w  ww . java2 s  . c o m*/
        this.trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        instream = new FileInputStream(new File("conf/clienttestkeystore"));
        this.trustStore.load(instream, "changeit".toCharArray());
        SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
        socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme sch = new Scheme("https", socketFactory, gsnhttpsport);
        Scheme plainsch = new Scheme("http", PlainSocketFactory.getSocketFactory(), gsnhttpport);
        httpclient.getConnectionManager().getSchemeRegistry().register(sch);
        httpclient.getConnectionManager().getSchemeRegistry().register(plainsch);

    } catch (KeyStoreException e) {

        logger.error("ERROR IN GSNCLIENT : Exception while creating trustStore :");
        logger.error(e.getMessage(), e);
    } catch (FileNotFoundException e) {
        logger.error("ERROR IN GSNCLIENT : FileInputStream exception :");
        logger.error(e.getMessage(), e);
    } catch (Exception e) {
        logger.error("ERROR IN GSNCLIENT : Exception while loading truststore :");
        logger.error(e.getMessage(), e);
    } finally {
        try {
            if (instream != null) {
                instream.close();
            }
        } catch (Exception e) {
        }
    }
}

From source file:piecework.client.LoadTester.java

public LoadTester(KeyStore keystore, SecuritySettings securitySettings) {
    ClientConnectionManager cm;//from   w  w w. j a  v  a  2  s  .c o 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);
}