Example usage for org.apache.http.impl.conn SchemeRegistryFactory createSystemDefault

List of usage examples for org.apache.http.impl.conn SchemeRegistryFactory createSystemDefault

Introduction

In this page you can find the example usage for org.apache.http.impl.conn SchemeRegistryFactory createSystemDefault.

Prototype

public static SchemeRegistry createSystemDefault() 

Source Link

Document

Initializes default scheme registry using system properties as described in <a href="http://download.oracle.com/javase/1,5.0/docs/guide/security/jsse/JSSERefGuide.html"> "JavaTM Secure Socket Extension (JSSE) Reference Guide for the JavaTM 2 Platform Standard Edition 5</a> <p> The following system properties are taken into account by this method: <ul> <li>ssl.TrustManagerFactory.algorithm</li> <li>javax.net.ssl.trustStoreType</li> <li>javax.net.ssl.trustStore</li> <li>javax.net.ssl.trustStoreProvider</li> <li>javax.net.ssl.trustStorePassword</li> <li>java.home</li> <li>ssl.KeyManagerFactory.algorithm</li> <li>javax.net.ssl.keyStoreType</li> <li>javax.net.ssl.keyStore</li> <li>javax.net.ssl.keyStoreProvider</li> <li>javax.net.ssl.keyStorePassword</li> </ul> <p>

Usage

From source file:org.sonatype.nexus.client.rest.jersey.NexusClientFactoryImpl.java

protected ApacheHttpClient4 doCreateHttpClientFor(final ConnectionInfo connectionInfo, final XStream xstream) {
    final ApacheHttpClient4Config config = new DefaultApacheHttpClient4Config();
    config.getSingletons().add(new XStreamXmlProvider(xstream, APPLICATION_XML_UTF8_TYPE));
    // set _real_ URL for baseUrl, and not a redirection (typically http instead of https)
    config.getProperties().put(PROPERTY_FOLLOW_REDIRECTS, Boolean.FALSE);

    applyAuthenticationIfAny(connectionInfo, config);
    applyProxyIfAny(connectionInfo, config);

    // obey JSSE defined system properties
    config.getProperties().put(ApacheHttpClient4Config.PROPERTY_CONNECTION_MANAGER,
            new PoolingClientConnectionManager(SchemeRegistryFactory.createSystemDefault()));

    final ApacheHttpClient4 client = ApacheHttpClient4.create(config);

    // set UA//from ww  w . ja va2  s  .  c  o m
    client.getClientHandler().getHttpClient().getParams().setParameter(CoreProtocolPNames.USER_AGENT,
            "Nexus-Client/" + discoverClientVersion());

    // "tweak" HTTPS scheme as requested
    final TrustStrategy trustStrategy;
    switch (connectionInfo.getSslCertificateValidation()) {
    case NONE:
        trustStrategy = new TrustStrategy() {
            @Override
            public boolean isTrusted(final X509Certificate[] chain, final String authType)
                    throws CertificateException {
                return true;
            }
        };
        break;
    case LAX:
        trustStrategy = new TrustSelfSignedStrategy();
        break;
    default:
        trustStrategy = null;
    }

    final X509HostnameVerifier hostnameVerifier;
    switch (connectionInfo.getSslCertificateHostnameValidation()) {
    case NONE:
        hostnameVerifier = new AllowAllHostnameVerifier();
        break;
    case STRICT:
        hostnameVerifier = new StrictHostnameVerifier();
        break;
    default:
        hostnameVerifier = new BrowserCompatHostnameVerifier();
    }

    try {
        final SSLSocketFactory ssf = new SSLSocketFactory(trustStrategy, hostnameVerifier);
        final Scheme tweakedHttpsScheme = new Scheme("https", 443, ssf);
        client.getClientHandler().getHttpClient().getConnectionManager().getSchemeRegistry()
                .register(tweakedHttpsScheme);
    } catch (Exception e) {
        Throwables.propagate(e);
    }

    // NXCM-4547 JERSEY-1293 Enforce proxy setting on httpclient
    enforceProxyUri(config, client);

    if (LOG.isDebugEnabled()) {
        client.addFilter(new LoggingFilter());
    }

    client.addFilter(new RequestFilters());

    return client;
}

From source file:org.graphity.core.util.jena.HttpOp.java

/** Create an HttpClient that performs connection pooling.  This can be used
 * with {@link #setDefaultHttpClient} or provided in the HttpOp calls.
 *///from www . j a  v  a 2 s  .  com
public static HttpClient createCachingHttpClient() {
    return new SystemDefaultHttpClient() {
        /** See SystemDefaultHttpClient (4.2).  This version always sets the connection cache */
        @Override
        protected ClientConnectionManager createClientConnectionManager() {
            PoolingClientConnectionManager connmgr = new PoolingClientConnectionManager(
                    SchemeRegistryFactory.createSystemDefault());
            String s = System.getProperty("http.maxConnections", "5");
            int max = Integer.parseInt(s);
            connmgr.setDefaultMaxPerRoute(max);
            connmgr.setMaxTotal(2 * max);
            return connmgr;
        }
    };
}