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

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

Introduction

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

Prototype

X509HostnameVerifier BROWSER_COMPATIBLE_HOSTNAME_VERIFIER

To view the source code for org.apache.http.conn.ssl SSLSocketFactory BROWSER_COMPATIBLE_HOSTNAME_VERIFIER.

Click Source Link

Usage

From source file:org.switchyard.quickstarts.rules.multi.RulesMultiThreadBindingUtils.java

/**
 * Gets the client./*from   w  w  w  .  j ava2s.c o m*/
 *
 * @return the client
 */
public static DefaultHttpClient getClient() {
    DefaultHttpClient ret = null;

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

    // registers schemes for both http and https
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    registry.register(new Scheme("https", sslSocketFactory, 443));

    ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
    ret = new DefaultHttpClient(manager, params);
    return ret;
}

From source file:com.kenai.redminenb.repository.RedmineManagerFactoryHelper.java

public static HttpClient getTransportConfig() {
    /**/*from w  ww  .j  a v a 2 s  .  c o m*/
    * Implement a minimal hostname verifier. This is needed to be able to use
    * hosts with certificates, that don't match the used hostname (VServer).
     *
     * This is implemented by first trying the "Browser compatible" hostname
     * verifier and if that fails, fall back to the default java hostname
     * verifier.
     *
     * If the default case the hostname verifier in java always rejects, but
     * for netbeans the "SSL Certificate Exception" module is available that
     * catches this and turns a failure into a request to the GUI user.
     */
    X509HostnameVerifier hostnameverified = new X509HostnameVerifier() {
        @Override
        public void verify(String string, SSLSocket ssls) throws IOException {
            if (SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER.verify(string, ssls.getSession())) {
                return;
            }
            if (!HttpsURLConnection.getDefaultHostnameVerifier().verify(string, ssls.getSession())) {
                throw new SSLException("Hostname did not verify");
            }
        }

        @Override
        public void verify(String string, X509Certificate xc) throws SSLException {
            throw new SSLException("Check not implemented yet");
        }

        @Override
        public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            throw new SSLException("Check not implemented yet");
        }

        @Override
        public boolean verify(String string, SSLSession ssls) {
            if (SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER.verify(string, ssls)) {
                return true;
            }
            return HttpsURLConnection.getDefaultHostnameVerifier().verify(string, ssls);
        }
    };

    try {
        SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContext.getDefault(),
                hostnameverified);

        HttpClient hc = HttpClientBuilder.create()
                .setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
                .setSSLSocketFactory(scsf).build();

        return hc;
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.ale.scanner.zotero.web.HttpsClient.java

public static ThreadSafeClientConnManager setupSSLConnMan(HttpParams params) {
    SchemeRegistry registry = new SchemeRegistry();

    PlainSocketFactory pf = PlainSocketFactory.getSocketFactory();
    SSLSocketFactory sf = SSLSocketFactory.getSocketFactory();
    sf.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

    registry.register(new Scheme("http", pf, 80));
    registry.register(new Scheme("https", (SocketFactory) sf, 443));

    return new ThreadSafeClientConnManager(params, registry);
}

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

private SchemeRegistry createSchemeRegistry() {

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    try {/*  ww w. j  av  a  2 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);
    }
}

From source file:org.talend.core.utils.StudioSSLContextProvider.java

public static boolean setSSLSystemProperty(boolean isPreference) {
    try {/*from  ww  w.j a  v  a  2 s  . c  om*/
        buildContext();
        if (!isPreference && context == null) {
            return false;
        }
        changeProperty();
        Executor.unregisterScheme("https");
        SSLSocketFactory factory = new SSLSocketFactory(context,
                SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        Executor.registerScheme(new Scheme("https", 443, factory));
    } catch (Exception e) {
        if (isPreference) {
            changeProperty();
            Executor.unregisterScheme("https");
        }
        ExceptionHandler.process(new Exception("Please check the SSL settings in Preference>Talend>SSL", e));
        return false;
    }
    return true;
}

From source file:com.liferay.portal.search.solr.http.SSLSocketFactoryBuilderImpl.java

@Override
public SSLSocketFactory build() throws Exception {
    KeyStore keyStore = _keyStoreLoader.load(_keyStoreType, _keyStorePath, _keyStorePassword);

    if (keyStore == null) {
        if (_log.isDebugEnabled()) {
            _log.debug("Use system defaults because there is no custom key store");
        }/*from   ww  w.ja v a 2 s .  co m*/

        return SSLSocketFactory.getSystemSocketFactory();
    }

    KeyStore trustKeyStore = null;

    TrustStrategy trustStrategy = null;

    if (_verifyServerCertificate) {
        trustKeyStore = _keyStoreLoader.load(_trustStoreType, _trustStorePath, _trustStorePassword);

        if (trustKeyStore == null) {
            if (_log.isDebugEnabled()) {
                _log.debug("Use system defaults because there is no custom " + "trust store");
            }

            return SSLSocketFactory.getSystemSocketFactory();
        }
    } else {
        trustStrategy = new TrustSelfSignedStrategy();
    }

    X509HostnameVerifier x509HostnameVerifier = SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;

    if (!_verifyServerHostname) {
        x509HostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    }

    try {
        return new SSLSocketFactory(SSLSocketFactory.TLS, keyStore, String.valueOf(_keyStorePassword),
                trustKeyStore, null, trustStrategy, x509HostnameVerifier);
    } catch (Exception e) {
        if (_log.isWarnEnabled()) {
            _log.warn(
                    "Use system defaults because the custom SSL socket " + "factory was not able to initialize",
                    e);
        }

        return SSLSocketFactory.getSystemSocketFactory();
    }
}

From source file:com.amazonaws.http.ApacheHttpClient.java

public ApacheHttpClient(ClientConfiguration config) {
    HttpClientFactory httpClientFactory = new HttpClientFactory();
    httpClient = httpClientFactory.createHttpClient(config);
    // disable retry
    ((AbstractHttpClient) httpClient).setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

    SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
    Scheme https = schemeRegistry.getScheme("https");
    ((SSLSocketFactory) https.getSocketFactory())
            .setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
}

From source file:com.googlecode.androidannotations.test15.SSLConnectionTest.java

@Test
public void strictHostnameVerifier() {
    assertNotNull(activity.mHttpsClientTest2);
    ClientConnectionManager ccm = activity.mHttpsClientTest2.getConnectionManager();
    Scheme httpsScheme = ccm.getSchemeRegistry().getScheme("https");
    SSLSocketFactory socketFactHttps = (SSLSocketFactory) httpsScheme.getSocketFactory();

    assertEquals(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER,
            ((SSLSocketFactory) socketFactHttps).getHostnameVerifier());
}

From source file:org.androidannotations.test.SSLConnectionTest.java

@Test
public void strictHostnameVerifier() {
    assertNotNull(activity.mHttpsClientTest2);
    ClientConnectionManager ccm = activity.mHttpsClientTest2.getConnectionManager();
    Scheme httpsScheme = ccm.getSchemeRegistry().getScheme("https");
    SSLSocketFactory socketFactHttps = (SSLSocketFactory) httpsScheme.getSocketFactory();

    assertEquals(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER, socketFactHttps.getHostnameVerifier());
}

From source file:jsonbroker.library.client.http.HttpDispatcher.java

public HttpDispatcher(NetworkAddress networkAddress) {

    _networkAddress = networkAddress;/*  w w  w.  jav a  2  s .  c om*/

    /*
      with ... 
      _client = new DefaultHttpClient();
      ... we get the following error ... 
      "Invalid use of SingleClientConnManager: connection still allocated.\nMake sure to release the connection before allocating another one."
      ... using a thread safe connecion manager ... 
      * http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
      * http://thinkandroid.wordpress.com/2009/12/31/creating-an-http-client-example/ 
     */

    //sets up parameters
    HttpParams params = new BasicHttpParams();

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

    // timeouts ... 
    HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
    HttpConnectionParams.setSoTimeout(params, 5 * 1000);

    //registers schemes for both http and https
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    registry.register(new Scheme("https", sslSocketFactory, 443));

    ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
    _client = new DefaultHttpClient(manager, params);

}