Example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager getSchemeRegistry

List of usage examples for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager getSchemeRegistry

Introduction

In this page you can find the example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager getSchemeRegistry.

Prototype

public SchemeRegistry getSchemeRegistry() 

Source Link

Usage

From source file:com.redhat.rcm.version.util.InputUtils.java

private static void setupClient() throws VManException {
    if (client == null) {
        SSLSocketFactory sslSocketFactory;
        try {//from  www .j  a v a2s . c om
            sslSocketFactory = new SSLSocketFactory(SSLSocketFactory.TLS, null, null, trustKs, null,
                    new TrustSelfSignedStrategy(), SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            //                sslSocketFactory =
            //                    new SSLSocketFactory( SSLSocketFactory.TLS, null, null, trustKs, null, null,
            //                                          SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER );
        } catch (final KeyManagementException e) {
            logger.error("Failed to setup SSL socket factory: {}", e, e.getMessage());
            throw new VManException("Failed to setup SSL socket factory: %s", e, e.getMessage());
        } catch (final UnrecoverableKeyException e) {
            logger.error("Failed to setup SSL socket factory: {}", e, e.getMessage());
            throw new VManException("Failed to setup SSL socket factory: %s", e, e.getMessage());
        } catch (final NoSuchAlgorithmException e) {
            logger.error("Failed to setup SSL socket factory: {}", e, e.getMessage());
            throw new VManException("Failed to setup SSL socket factory: %s", e, e.getMessage());
        } catch (final KeyStoreException e) {
            logger.error("Failed to setup SSL socket factory: {}", e, e.getMessage());
            throw new VManException("Failed to setup SSL socket factory: %s", e, e.getMessage());
        }

        final ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager();
        ccm.getSchemeRegistry().register(new Scheme("https", 443, sslSocketFactory));

        final DefaultHttpClient hc = new DefaultHttpClient(ccm);
        hc.setRedirectStrategy(new DefaultRedirectStrategy());

        final String proxyHost = System.getProperty("http.proxyHost");
        final int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort", "-1"));

        if (proxyHost != null && proxyPort > 0) {
            final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            hc.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
        }

        client = hc;
    }
}

From source file:com.hp.mercury.ci.jenkins.plugins.OOBuildStep.java

private static void initializeHttpClient(DescriptorImpl descriptor) {

    final int maxConnectionsPerRoute = 100;
    final int maxConnectionsTotal = 100;

    ThreadSafeClientConnManager threadSafeClientConnManager = new ThreadSafeClientConnManager();
    threadSafeClientConnManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);
    threadSafeClientConnManager.setMaxTotal(maxConnectionsTotal);

    httpClient = new DefaultHttpClient(threadSafeClientConnManager);
    if (descriptor.isIgnoreSsl()) {
        threadSafeClientConnManager.getSchemeRegistry()
                .register(new Scheme("https", 443, new FakeSocketFactory()));
    } else if (descriptor.getKeystorePath() != null) {
        try {/* www  .  j  a v a  2  s .co m*/
            SSLSocketFactory sslSocketFactory = sslSocketFactoryFromCertificateFile(
                    descriptor.getKeystorePath(), decrypt(descriptor.getKeystorePassword()).toCharArray());
            sslSocketFactory.setHostnameVerifier(new BrowserCompatHostnameVerifier());
            // For less strict rules in dev mode you can try
            //sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
            threadSafeClientConnManager.getSchemeRegistry()
                    .register(new Scheme("https", 443, sslSocketFactory));
        } catch (NoSuchAlgorithmException e) {
            LOG.error("Could not register https scheme: ", e);
        } catch (KeyManagementException e) {
            LOG.error("Could not register https scheme: ", e);
        } catch (KeyStoreException e) {
            LOG.error("Could not register https scheme: ", e);
        } catch (UnrecoverableKeyException e) {
            LOG.error("Could not register https scheme: ", e);
        } catch (IOException e) {
            LOG.error("Could not load keystore file: ", e);
        } catch (CertificateException e) {
            LOG.error("Could not load keystore file: ", e);
        }
    }

    final HttpParams params = httpClient.getParams();
    final int timeoutInSeconds = descriptor.getTimeout() * 1000;
    HttpConnectionParams.setConnectionTimeout(params, timeoutInSeconds);
    HttpConnectionParams.setSoTimeout(params, timeoutInSeconds);
    HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false);

    for (OOServer s : descriptor.getOoServers(true).values()) {

        URL url = null;
        try {
            url = new URL(s.getUrl());
        } catch (MalformedURLException mue) {
            //can't happen, we pre-validate the URLS during configuration and set active to false if bad.
        }

        //check why it doesn't use the credentials provider
        httpClient.getCredentialsProvider().setCredentials(
                new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM, "basic"),
                new UsernamePasswordCredentials(s.getUsername(), decrypt(s.getPassword())));
    }

}

From source file:cn.ctyun.amazonaws.http.HttpClientFactory.java

/**
 * Creates a new HttpClient object using the specified AWS
 * ClientConfiguration to configure the client.
 *
 * @param config/*from   w  ww .j  a  v  a2 s.  c o  m*/
 *            Client configuration options (ex: proxy settings, connection
 *            limits, etc).
 *
 * @return The new, configured HttpClient.
 */
public HttpClient createHttpClient(ClientConfiguration config) {
    /* Set HTTP client parameters */
    HttpParams httpClientParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpClientParams, config.getConnectionTimeout());
    HttpConnectionParams.setSoTimeout(httpClientParams, config.getSocketTimeout());
    HttpConnectionParams.setStaleCheckingEnabled(httpClientParams, true);
    HttpConnectionParams.setTcpNoDelay(httpClientParams, true);

    int socketSendBufferSizeHint = config.getSocketBufferSizeHints()[0];
    int socketReceiveBufferSizeHint = config.getSocketBufferSizeHints()[1];
    if (socketSendBufferSizeHint > 0 || socketReceiveBufferSizeHint > 0) {
        HttpConnectionParams.setSocketBufferSize(httpClientParams,
                Math.max(socketSendBufferSizeHint, socketReceiveBufferSizeHint));
    }

    /* Set connection manager */
    ThreadSafeClientConnManager connectionManager = ConnectionManagerFactory
            .createThreadSafeClientConnManager(config, httpClientParams);
    DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, httpClientParams);
    httpClient.setRedirectStrategy(new LocationHeaderNotRequiredRedirectStrategy());

    try {
        Scheme http = new Scheme("http", 80, PlainSocketFactory.getSocketFactory());

        SSLSocketFactory sf = new SSLSocketFactory(SSLContext.getDefault(),
                SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        Scheme https = new Scheme("https", 443, sf);

        SchemeRegistry sr = connectionManager.getSchemeRegistry();
        sr.register(http);
        sr.register(https);
    } catch (NoSuchAlgorithmException e) {
        throw new AmazonClientException("Unable to access default SSL context", e);
    }

    /*
     * If SSL cert checking for endpoints has been explicitly disabled,
     * register a new scheme for HTTPS that won't cause self-signed certs to
     * error out.
     */
    if (System.getProperty("com.amazonaws.sdk.disableCertChecking") != null) {
        Scheme sch = new Scheme("https", 443, new TrustingSocketFactory());
        httpClient.getConnectionManager().getSchemeRegistry().register(sch);
    }

    /* Set proxy if configured */
    String proxyHost = config.getProxyHost();
    int proxyPort = config.getProxyPort();
    if (proxyHost != null && proxyPort > 0) {
        AmazonHttpClient.log
                .info("Configuring Proxy. Proxy Host: " + proxyHost + " " + "Proxy Port: " + proxyPort);
        HttpHost proxyHttpHost = new HttpHost(proxyHost, proxyPort);
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHttpHost);

        String proxyUsername = config.getProxyUsername();
        String proxyPassword = config.getProxyPassword();
        String proxyDomain = config.getProxyDomain();
        String proxyWorkstation = config.getProxyWorkstation();

        if (proxyUsername != null && proxyPassword != null) {
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort),
                    new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain));
        }
    }

    return httpClient;
}

From source file:pt.lunacloud.http.HttpClientFactory.java

/**
 * Creates a new HttpClient object using the specified AWS
 * ClientConfiguration to configure the client.
 *
 * @param config/*from  ww  w .jav  a  2 s .  com*/
 *            Client configuration options (ex: proxy settings, connection
 *            limits, etc).
 *
 * @return The new, configured HttpClient.
 */
public HttpClient createHttpClient(ClientConfiguration config) {
    /* Form User-Agent information */
    String userAgent = config.getUserAgent();
    if (!(userAgent.equals(ClientConfiguration.DEFAULT_USER_AGENT))) {
        userAgent += ", " + ClientConfiguration.DEFAULT_USER_AGENT;
    }

    /* Set HTTP client parameters */
    HttpParams httpClientParams = new BasicHttpParams();
    HttpProtocolParams.setUserAgent(httpClientParams, userAgent);
    HttpConnectionParams.setConnectionTimeout(httpClientParams, config.getConnectionTimeout());
    HttpConnectionParams.setSoTimeout(httpClientParams, config.getSocketTimeout());
    HttpConnectionParams.setStaleCheckingEnabled(httpClientParams, false);
    HttpConnectionParams.setTcpNoDelay(httpClientParams, true);

    int socketSendBufferSizeHint = config.getSocketBufferSizeHints()[0];
    int socketReceiveBufferSizeHint = config.getSocketBufferSizeHints()[1];
    if (socketSendBufferSizeHint > 0 || socketReceiveBufferSizeHint > 0) {
        HttpConnectionParams.setSocketBufferSize(httpClientParams,
                Math.max(socketSendBufferSizeHint, socketReceiveBufferSizeHint));
    }

    /* Set connection manager */
    ThreadSafeClientConnManager connectionManager = ConnectionManagerFactory
            .createThreadSafeClientConnManager(config, httpClientParams);
    DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, httpClientParams);
    httpClient.setRedirectStrategy(new LocationHeaderNotRequiredRedirectStrategy());

    try {
        Scheme http = new Scheme("http", 80, PlainSocketFactory.getSocketFactory());

        SSLSocketFactory sf = new SSLSocketFactory(SSLContext.getDefault(),
                SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        Scheme https = new Scheme("https", 443, sf);

        SchemeRegistry sr = connectionManager.getSchemeRegistry();
        sr.register(http);
        sr.register(https);
    } catch (NoSuchAlgorithmException e) {
        throw new LunacloudClientException("Unable to access default SSL context");
    }

    /*
     * If SSL cert checking for endpoints has been explicitly disabled,
     * register a new scheme for HTTPS that won't cause self-signed certs to
     * error out.
     */
    if (System.getProperty("com.amazonaws.sdk.disableCertChecking") != null) {
        Scheme sch = new Scheme("https", 443, new TrustingSocketFactory());
        httpClient.getConnectionManager().getSchemeRegistry().register(sch);
    }

    /* Set proxy if configured */
    String proxyHost = config.getProxyHost();
    int proxyPort = config.getProxyPort();
    if (proxyHost != null && proxyPort > 0) {
        AmazonHttpClient.log
                .info("Configuring Proxy. Proxy Host: " + proxyHost + " " + "Proxy Port: " + proxyPort);
        HttpHost proxyHttpHost = new HttpHost(proxyHost, proxyPort);
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHttpHost);

        String proxyUsername = config.getProxyUsername();
        String proxyPassword = config.getProxyPassword();
        String proxyDomain = config.getProxyDomain();
        String proxyWorkstation = config.getProxyWorkstation();

        if (proxyUsername != null && proxyPassword != null) {
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort),
                    new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain));
        }
    }

    return httpClient;
}