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.eclipse.lyo.testsuite.server.util.OSLCUtils.java

static public void setupLazySSLSupport(HttpClient httpClient) {
    ClientConnectionManager connManager = httpClient.getConnectionManager();
    SchemeRegistry schemeRegistry = connManager.getSchemeRegistry();
    schemeRegistry.unregister("https");
    /** Create a trust manager that does not validate certificate chains */
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            /** Ignore Method Call */
        }/*from  w w  w.ja v a2s  .c  o  m*/

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            /** Ignore Method Call */
        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };

    SSLContext sc = null;
    try {
        sc = SSLContext.getInstance("SSL"); //$NON-NLS-1$
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        /* Fail Silently */
    } catch (KeyManagementException e) {
        /* Fail Silently */
    }

    SSLSocketFactory sf = new SSLSocketFactory(sc);
    sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Scheme https = new Scheme("https", sf, 443);

    schemeRegistry.register(https);
}

From source file:eu.trentorise.smartcampus.ac.network.HttpsClientBuilder.java

private static HttpClient getWildcartHttpClient(HttpParams inParams) {
    HttpClient client = null;/*from  w w  w. j  av  a  2  s. c o m*/

    HttpParams params = inParams != null ? inParams : new BasicHttpParams();

    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

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

        SSLSocketFactory sslSocketFactory = new CustomSSLSocketFactory(trustStore);
        final X509HostnameVerifier delegate = sslSocketFactory.getHostnameVerifier();
        if (!(delegate instanceof WildcardVerifier)) {
            sslSocketFactory.setHostnameVerifier(new WildcardVerifier(delegate));
        }
        registry.register(new Scheme("https", sslSocketFactory, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

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

    return client;
}

From source file:com.evrythng.java.wrapper.core.api.ApiCommand.java

private static HttpClient wrapClient(final HttpClient base) {

    try {// w w w  . j  a v a 2 s.c om
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory ssf = new WrapperSSLSocketFactory(trustStore);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        return null;
    }
}

From source file:nz.net.catalyst.MaharaDroid.upload.http.RestClient.java

private static SSLSocketFactory getSocketFactory(Boolean d) {
    // Enable debug mode to ignore all certificates
    if (DEBUG) {//  ww  w. j a  va  2  s.c  om
        KeyStore trustStore;
        try {
            trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);
            SSLSocketFactory sf = new DebugSSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            return sf;

        } catch (KeyStoreException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } catch (NoSuchAlgorithmException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        } catch (CertificateException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        } catch (IOException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        } catch (KeyManagementException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } catch (UnrecoverableKeyException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
    }

    return SSLSocketFactory.getSocketFactory();
}

From source file:org.andstatus.app.net.HttpApacheUtils.java

static HttpClient getHttpClient() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    // This is done to get rid of the "javax.net.ssl.SSLException: hostname in certificate didn't match" error
    // See e.g. http://stackoverflow.com/questions/8839541/hostname-in-certificate-didnt-match
    socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    schemeRegistry.register(new Scheme("https", socketFactory, 443));

    HttpParams params = getHttpParams();
    ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
    return new DefaultHttpClient(clientConnectionManager, params);
}

From source file:eu.trentorise.smartcampus.ac.network.HttpsClientBuilder.java

private static HttpClient getAcceptAllHttpClient(HttpParams inParams) {
    HttpClient client = null;//from  ww w .  j a v a 2 s  .  co  m

    HttpParams params = inParams != null ? inParams : new BasicHttpParams();

    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

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

        // IMPORTANT: use CustolSSLSocketFactory for 2.2
        SSLSocketFactory sslSocketFactory = new SSLSocketFactory(trustStore);
        if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.FROYO) {
            sslSocketFactory = new CustomSSLSocketFactory(trustStore);
        }

        sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        registry.register(new Scheme("https", sslSocketFactory, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

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

    return client;
}

From source file:org.forgerock.openig.http.HttpClient.java

/**
 * Returns a new SSL socket factory that does not perform hostname verification.
 *
 * @param keyManagerFactory//from  w  w w. j  a  va  2 s. co  m
 *         Provides Keys/Certificates in case of SSL/TLS connections
 * @param trustManagerFactory
 *         Provides TrustManagers in case of SSL/TLS connections
 * @throws GeneralSecurityException
 *         if the SSL algorithm is unsupported or if an error occurs during SSL configuration
 */
private static SSLSocketFactory newSSLSocketFactory(final KeyManagerFactory keyManagerFactory,
        final TrustManagerFactory trustManagerFactory) throws GeneralSecurityException {
    SSLContext context = SSLContext.getInstance("TLS");
    context.init((keyManagerFactory == null) ? null : keyManagerFactory.getKeyManagers(),
            (trustManagerFactory == null) ? null : trustManagerFactory.getTrustManagers(), null);
    SSLSocketFactory factory = new SSLSocketFactory(context);
    factory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    return factory;
}

From source file:org.wso2.bam.integration.tests.reciever.RESTAPITestCase.java

@BeforeClass(groups = { "wso2.bam" })
private static void init() {

    DATA_RECEIVER = "/datareceiver/1.0.0";

    //        host = FrameworkSettings.HOST_NAME;
    //        httpsPort = Integer.parseInt(FrameworkSettings.HTTPS_PORT);

    restAppParentURL = "https://" + host + ":" + httpsPort + DATA_RECEIVER;
    streamsURL = restAppParentURL + "/streams";

    streamURL = restAppParentURL + "/stream";
    stockQuoteVersion1URL = streamURL + "/stockquote.stream/1.0.2/";

    stockQuoteVersion2URL = streamURL + "/stockquote.stream.2/2.0.0";

    stockQuoteVersion3URL = streamURL + "/labit.stream/0.0.3";
    stockQuoteVersion4URL = streamURL + "/eu.ima.event.stream/1.2.0";
    stockQuoteVersion5URL = streamURL + "/eu.ima.event.stream.2/1.3.0";

    try {//from   ww  w.  j  a  va2s .c o  m
        events1 = IOUtils
                .toString(RESTAPITestCase.class.getClassLoader().getResourceAsStream("rest/events1.json"));
        events2 = IOUtils
                .toString(RESTAPITestCase.class.getClassLoader().getResourceAsStream("rest/events2.json"));
        events3 = IOUtils
                .toString(RESTAPITestCase.class.getClassLoader().getResourceAsStream("rest/events3.json"));
        events4 = IOUtils
                .toString(RESTAPITestCase.class.getClassLoader().getResourceAsStream("rest/events4.json"));
        events5 = IOUtils
                .toString(RESTAPITestCase.class.getClassLoader().getResourceAsStream("rest/events5.json"));

        streamdefn1 = IOUtils
                .toString(RESTAPITestCase.class.getClassLoader().getResourceAsStream("rest/streamdefn1.json"));
        streamdefn2 = IOUtils
                .toString(RESTAPITestCase.class.getClassLoader().getResourceAsStream("rest/streamdefn2.json"));
        streamdefn3 = IOUtils
                .toString(RESTAPITestCase.class.getClassLoader().getResourceAsStream("rest/streamdefn3.json"));
        streamdefn4 = IOUtils
                .toString(RESTAPITestCase.class.getClassLoader().getResourceAsStream("rest/streamdefn4.json"));
        streamdefn5 = IOUtils
                .toString(RESTAPITestCase.class.getClassLoader().getResourceAsStream("rest/streamdefn5.json"));

        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, httpsPort);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "utf-8");
        params.setBooleanParameter("http.protocol.expect-continue", false);

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

    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}

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

@SuppressWarnings("deprecation")
private static 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 httpclient = new DefaultHttpClient(params);

    // Initialize/setup SSL
    TrustManager easyTrustManager = new X509TrustManager() {
        @Override/*from www . j  a v  a  2 s  .c  o m*/
        public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                throws java.security.cert.CertificateException {
        }

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

        @Override
        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 sch = new Scheme("https", sf, 443);
    httpclient.getConnectionManager().getSchemeRegistry().register(sch);

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

From source file:org.xdi.oxauth.BaseTest.java

public static DefaultHttpClient createHttpClient(HostnameVerifierType p_verifierType) {
    if (p_verifierType != null && p_verifierType != HostnameVerifierType.DEFAULT) {
        switch (p_verifierType) {
        case ALLOW_ALL:
            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);

            // Set verifier
            HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
            return new DefaultHttpClient(mgr, client.getParams());
        case DEFAULT:
            return new DefaultHttpClient();
        }//from w  w  w.j  a v  a  2s.co  m
    }
    return new DefaultHttpClient();
}