Search in sources :

Example 71 with SchemeRegistry

use of org.apache.http.conn.scheme.SchemeRegistry in project liferay-ide by liferay.

the class RemoteConnection method _getHttpClient.

private HttpClient _getHttpClient() {
    if (_httpClient != null) {
        return _httpClient;
    }
    DefaultHttpClient newDefaultHttpClient = null;
    if ((getUsername() != null) || (getPassword() != null)) {
        try {
            IProxyService proxyService = LiferayCore.getProxyService();
            URI uri = new URI("http://" + getHost() + ":" + getHttpPort());
            IProxyData[] proxyDataForHost = proxyService.select(uri);
            for (IProxyData data : proxyDataForHost) {
                if ((data.getHost() == null) || (data.getPort() == 0)) {
                    continue;
                }
                SchemeRegistry schemeRegistry = new SchemeRegistry();
                schemeRegistry.register(new Scheme("http", data.getPort(), PlainSocketFactory.getSocketFactory()));
                PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
                cm.setMaxTotal(200);
                cm.setDefaultMaxPerRoute(20);
                DefaultHttpClient newHttpClient = new DefaultHttpClient(cm);
                HttpHost proxy = new HttpHost(data.getHost(), data.getPort());
                newHttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
                newDefaultHttpClient = newHttpClient;
                break;
            }
            if (newDefaultHttpClient == null) {
                uri = new URI("SOCKS://" + getHost() + ":" + getHttpPort());
                proxyDataForHost = proxyService.select(uri);
                for (IProxyData data : proxyDataForHost) {
                    if (data.getHost() == null) {
                        continue;
                    }
                    DefaultHttpClient newHttpClient = new DefaultHttpClient();
                    newHttpClient.getParams().setParameter("socks.host", data.getHost());
                    newHttpClient.getParams().setParameter("socks.port", data.getPort());
                    SchemeRegistry registry = newHttpClient.getConnectionManager().getSchemeRegistry();
                    Scheme scheme = new Scheme("socks", data.getPort(), PlainSocketFactory.getSocketFactory());
                    registry.register(scheme);
                    newDefaultHttpClient = newHttpClient;
                    break;
                }
            }
        } catch (URISyntaxException urise) {
            LiferayCore.logError("Unable to read proxy data", urise);
        }
        if (newDefaultHttpClient == null) {
            newDefaultHttpClient = new DefaultHttpClient();
        }
        _httpClient = newDefaultHttpClient;
    } else {
        _httpClient = new DefaultHttpClient();
    }
    return _httpClient;
}
Also used : PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) IProxyData(org.eclipse.core.net.proxy.IProxyData) Scheme(org.apache.http.conn.scheme.Scheme) HttpHost(org.apache.http.HttpHost) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IProxyService(org.eclipse.core.net.proxy.IProxyService)

Example 72 with SchemeRegistry

use of org.apache.http.conn.scheme.SchemeRegistry in project ovirt-engine-sdk-java by oVirt.

the class ConnectionBuilder42 method createConnectionSocketFactoryRegistry.

private SchemeRegistry createConnectionSocketFactoryRegistry() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    SSLSocketFactory sf;
    // Create SSL/TLS or plain connection:
    if (HTTP_PROTOCOL.equals(getProtocol())) {
        schemeRegistry.register(new Scheme(HTTP_PROTOCOL, getPort(), PlainSocketFactory.getSocketFactory()));
    } else if (HTTPS_PROTOCOL.equals(getProtocol())) {
        try {
            if (this.insecure) {
                SSLContext sslcontext = SSLContext.getInstance("TLS");
                sslcontext.init(null, new TrustManager[] { noCaTrustManager }, null);
                sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            } else {
                KeyStore truststore = null;
                InputStream in = null;
                if (this.trustStoreFile != null) {
                    truststore = KeyStore.getInstance(KeyStore.getDefaultType());
                    try {
                        in = new FileInputStream(this.trustStoreFile);
                        truststore.load(in, this.trustStorePassword != null ? this.trustStorePassword.toCharArray() : null);
                    } finally {
                        if (in != null) {
                            in.close();
                        }
                    }
                }
                sf = new SSLSocketFactory(SSLSocketFactory.TLS, null, null, truststore, null, null, SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
            }
            schemeRegistry.register(new Scheme(HTTPS_PROTOCOL, getPort(), sf));
        } catch (NoSuchAlgorithmException e) {
            throw new Error(NO_TLS_ERROR, e);
        } catch (KeyManagementException e) {
            throw new Error(BAD_KEY_ERROR, e);
        } catch (KeyStoreException e) {
            throw new Error(KEY_STORE_ERROR, e);
        } catch (FileNotFoundException e) {
            throw new Error(KEY_STORE_FILE_NOT_FOUND_ERROR, e);
        } catch (CertificateException e) {
            throw new Error(CERTIFICATE_ERROR, e);
        } catch (IOException e) {
            throw new Error(IO_ERROR, e);
        } catch (UnrecoverableKeyException e) {
            throw new Error(UNRECOVERABLE_KEY_ERROR, e);
        }
    }
    return schemeRegistry;
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Error(org.ovirt.engine.sdk4.Error) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) UnrecoverableKeyException(java.security.UnrecoverableKeyException) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) AuthSchemeRegistry(org.apache.http.auth.AuthSchemeRegistry) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

Example 73 with SchemeRegistry

use of org.apache.http.conn.scheme.SchemeRegistry in project androidquery by androidquery.

the class AbstractAjaxCallback method getClient.

private static DefaultHttpClient getClient() {
    if (client == null || !REUSE_CLIENT) {
        AQUtility.debug("creating http client");
        HttpParams httpParams = new BasicHttpParams();
        // httpParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpConnectionParams.setConnectionTimeout(httpParams, NET_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, NET_TIMEOUT);
        // ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(NETWORK_POOL));
        ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(25));
        // Added this line to avoid issue at: http://stackoverflow.com/questions/5358014/android-httpclient-oom-on-4g-lte-htc-thunderbolt
        HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", ssf == null ? SSLSocketFactory.getSocketFactory() : ssf, 443));
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, registry);
        client = new DefaultHttpClient(cm, httpParams);
    }
    return client;
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams) ConnPerRouteBean(org.apache.http.conn.params.ConnPerRouteBean) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 74 with SchemeRegistry

use of org.apache.http.conn.scheme.SchemeRegistry in project spring-boot-quick by vector4wang.

the class HttpUtil method sslClient.

private static void sslClient(HttpClient httpClient) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

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

            public void checkClientTrusted(X509Certificate[] xcs, String str) {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String str) {
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = httpClient.getConnectionManager();
        SchemeRegistry registry = ccm.getSchemeRegistry();
        registry.register(new Scheme("https", 443, ssf));
    } catch (KeyManagementException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) X509TrustManager(javax.net.ssl.X509TrustManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException)

Example 75 with SchemeRegistry

use of org.apache.http.conn.scheme.SchemeRegistry in project spring-boot-quick by vector4wang.

the class HttpUtils method sslClient.

private static void sslClient(HttpClient httpClient) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

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

            public void checkClientTrusted(X509Certificate[] xcs, String str) {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String str) {
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = httpClient.getConnectionManager();
        SchemeRegistry registry = ccm.getSchemeRegistry();
        registry.register(new Scheme("https", 443, ssf));
    } catch (KeyManagementException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) X509TrustManager(javax.net.ssl.X509TrustManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException)

Aggregations

SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)91 Scheme (org.apache.http.conn.scheme.Scheme)88 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)58 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)50 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)48 BasicHttpParams (org.apache.http.params.BasicHttpParams)35 HttpParams (org.apache.http.params.HttpParams)33 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)30 SSLContext (javax.net.ssl.SSLContext)24 IOException (java.io.IOException)22 KeyManagementException (java.security.KeyManagementException)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)18 CertificateException (java.security.cert.CertificateException)15 HttpClient (org.apache.http.client.HttpClient)12 BasicClientConnectionManager (org.apache.http.impl.conn.BasicClientConnectionManager)12 PoolingClientConnectionManager (org.apache.http.impl.conn.PoolingClientConnectionManager)12 KeyStoreException (java.security.KeyStoreException)11 UnrecoverableKeyException (java.security.UnrecoverableKeyException)10 X509Certificate (java.security.cert.X509Certificate)10 X509TrustManager (javax.net.ssl.X509TrustManager)9