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

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

Introduction

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

Prototype

public static SSLSocketFactory getSocketFactory() throws SSLInitializationException 

Source Link

Document

Obtains default SSL socket factory with an SSL context based on the standard JSSE trust material (cacerts file in the security properties directory).

Usage

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  a2 s  . c  om
    }
    return new DefaultHttpClient();
}

From source file:org.opensaml.util.http.HttpClientBuilder.java

/**
 * Creates the default scheme registry for connection. The constructed registry supports http with a default port of
 * 80 and https with a default port of 443. If {@link #connectionDisregardSslCertificate} is true, than the https
 * port will accept any certificate presented by the responder.
 * // w  ww. j  a va 2s.  c om
 * @return the default scheme registry.
 */
private SchemeRegistry buildSchemeRegistry() {
    final SchemeRegistry registry = new SchemeRegistry();

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

    final SSLSocketFactory sslSF;
    if (!connectionDisregardSslCertificate) {
        sslSF = SSLSocketFactory.getSocketFactory();
    } else {
        X509TrustManager noTrustManager = new X509TrustManager() {

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

            public void checkServerTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
                // accept everything
            }

            public void checkClientTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
                // accept everything
            }
        };

        try {
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(null, new TrustManager[] { noTrustManager }, null);
            sslSF = new SSLSocketFactory(sslcontext);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("TLS SSLContext type is required to be supported by the JVM but is not",
                    e);
        } catch (KeyManagementException e) {
            throw new RuntimeException("Some how the trust everything trust manager didn't trust everything",
                    e);
        }
    }
    registry.register(new Scheme("https", 443, sslSF));

    return registry;
}

From source file:org.string_db.psicquic.ws.StringdbSolrBasedPsicquicRestService.java

protected HttpClient createHttpClient() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    cm.setMaxTotal(maxTotalConnections);
    cm.setDefaultMaxPerRoute(defaultMaxConnectionsPerHost);

    HttpClient httpClient = new DefaultHttpClient(cm);

    String proxyHost = config.getProxyHost();
    String proxyPort = config.getProxyPort();

    if (isValueSet(proxyHost) && proxyHost.trim().length() > 0 && isValueSet(proxyPort)
            && proxyPort.trim().length() > 0) {
        try {//from w w  w  .  j  a v a2  s . co  m
            HttpHost proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort));
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        } catch (Exception e) {
            logger.error("Impossible to create proxy host:" + proxyHost + ", port:" + proxyPort, e);
        }
    }

    return httpClient;
}

From source file:com.fhc25.percepcion.osiris.mapviewer.common.restutils.RestClient.java

public static HttpClient getHttpClient() {
    try {/*from  ww w.j  a v a 2  s. co  m*/

        HttpParams params = new BasicHttpParams();

        // Turn off stale checking.  Our connections break all the time anyway,
        // and it's not worth it to pay the penalty of checking every time.
        HttpConnectionParams.setStaleCheckingEnabled(params, false);

        // Default connection and socket timeout of 20 seconds.  Tweak to taste.
        HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, SOCKETCONNECTION_TIMEOUT);

        HttpConnectionParams.setSocketBufferSize(params, 8192);

        // Don't handle redirects -- return them to the caller.  Our code
        // often wants to re-POST after a redirect, which we must do ourselves.
        HttpClientParams.setRedirecting(params, false);

        SSLSocketFactory mySSLSocketFactory = SSLSocketFactory.getSocketFactory();

        // disable ssl check on debug
        /*
        if (DisableSSLcertificateCheck ) {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        mySSLSocketFactory = new MySSLSocketFactory(trustStore);
        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
        mySSLSocketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        }
        */

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https", mySSLSocketFactory, 443));
        ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);

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

From source file:com.pc.dailymile.DailyMileClient.java

private void initHttpClient() {
    HttpParams parameters = new BasicHttpParams();
    HttpProtocolParams.setVersion(parameters, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
    // set the User-Agent to a common User-Agent because currently
    // the default httpclient User-Agent doesn't work with dailymile
    HttpProtocolParams.setUserAgent(parameters, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
    HttpProtocolParams.setUseExpectContinue(parameters, false);
    HttpConnectionParams.setTcpNoDelay(parameters, true);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager tsccm = new ThreadSafeClientConnManager(parameters, schReg);

    DefaultHttpClient defaultHttpClient = new DefaultHttpClient(tsccm, parameters);
    defaultHttpClient.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }//from w w w .  j  a va2s  . c om
        }
    });

    defaultHttpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header ceheader = entity.getContentEncoding();
            if (ceheader != null) {
                HeaderElement[] codecs = ceheader.getElements();
                for (int i = 0; i < codecs.length; i++) {
                    if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });
    httpClient = defaultHttpClient;
}

From source file:com.microsoft.live.LiveConnectClient.java

private static HttpClient getHttpClient() {
    // The LiveConnectClients can share one HttpClient with a ThreadSafeConnManager.
    if (HTTP_CLIENT == null) {
        synchronized (HTTP_CLIENT_LOCK) {
            if (HTTP_CLIENT == null) {
                HttpParams params = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(params, CONNECT_TIMEOUT_IN_MS);
                HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT_IN_MS);

                ConnManagerParams.setMaxTotalConnections(params, 100);
                HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

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

                // Create an HttpClient with the ThreadSafeClientConnManager.
                // This connection manager must be used if more than one thread will
                // be using the HttpClient, which is a common scenario.
                ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
                HTTP_CLIENT = new DefaultHttpClient(cm, params);
            }/*from   ww  w  . j  a v a2s.  co  m*/
        }
    }

    return HTTP_CLIENT;
}

From source file:com.mediatek.systemupdate.HttpManager.java

private void initHttpClientMgr() {

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

    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    mHttpConnMgr = new ThreadSafeClientConnManager(mHttpParam, schemeRegistry);
}