Example usage for org.apache.commons.httpclient MultiThreadedHttpConnectionManager DEFAULT_MAX_TOTAL_CONNECTIONS

List of usage examples for org.apache.commons.httpclient MultiThreadedHttpConnectionManager DEFAULT_MAX_TOTAL_CONNECTIONS

Introduction

In this page you can find the example usage for org.apache.commons.httpclient MultiThreadedHttpConnectionManager DEFAULT_MAX_TOTAL_CONNECTIONS.

Prototype

int DEFAULT_MAX_TOTAL_CONNECTIONS

To view the source code for org.apache.commons.httpclient MultiThreadedHttpConnectionManager DEFAULT_MAX_TOTAL_CONNECTIONS.

Click Source Link

Usage

From source file:flex.messaging.services.http.HTTPProxyAdapter.java

/**
 * Initializes the <code>HTTPProxyAdapter</code> with the properties.
 *
 * <pre>/*from   w  w w  .j a  v a2 s . c  o m*/
 * &lt;connection-manager&gt;
 *     &lt;cookie-policy&gt;rfc2109&lt;/cookie-policy&gt;
 *     &lt;max-total-connections&gt;100&lt;/max-total-connections&gt;
 *     &lt;default-max-connections-per-host&gt;2&lt;/default-max-connections-per-host&gt;
 *     &lt;connection-timeout&gt;0&lt;/connection-timeout&gt;
 *     &lt;socket-timeout&gt;&lt;/socket-timeout&gt;
 *     &lt;stale-checking-enabled&gt;&lt;/stale-checking-enabled&gt;
 *     &lt;send-buffer-size&gt;&lt;/send-buffer-size&gt;
 *     &lt;receive-buffer-size&gt;&lt;/receive-buffer-size&gt;
 *     &lt;tcp-no-delay&gt;true&lt;/tcp-no-delay&gt;
 *     &lt;linger&gt;-1&lt;/linger&gt;
 *     &lt;max-per-host&gt;
 *           &lt;host&gt;...&lt;/host&gt;
 *           &lt;port&gt;80&lt;/port&gt;
 *           &lt;protocol&gt;http&lt;/protocol&gt;
 *           &lt;protocol-factory class="flex.messaging.services.http.ProtocolFactory"&gt;
 *               &lt;properties&gt;...&lt;/properties&gt;
 *           &lt;/protocol-factory&gt;
 *           &lt;max-connections&gt;2&lt;/max-connections&gt;
 *           &lt;proxy&gt;
 *               &lt;host&gt;...&lt;/host&gt;
 *               &lt;port&gt;80&lt;/port&gt;
 *           &lt;/proxy&gt;
 *           &lt;local-address&gt;...&lt;/local-address&gt;
 *           &lt;virtual-host&gt;...&lt;/virtual-host&gt;
 *     &lt;/max-per-host&gt;
 *  &lt;/connection-manager&gt;
 *  &lt;cookie-limit&gt;200&lt;/cookie-limit&gt;
 *  &lt;allow-lax-ssl&gt;false&lt;/allow-lax-ssl&gt;
 *  &lt;content-chunked&gt;false&lt;/content-chunked&gt;
 *  &lt;external-proxy&gt;
 *      &lt;server&gt;...&lt;/server&gt;
 *      &lt;port&gt;80&lt;/port&gt;
 *      &lt;nt-domain&gt;...&lt;/nt-domain&gt;
 *      &lt;username&gt;...&lt;/username&gt;
 *      &lt;password&gt;...&lt;/password&gt;
 *  &lt;/external-proxy&gt;
 *  </pre>
 *
 * @param id         The id of the destination.
 * @param properties Properties for the <code>Destination</code>.
 */
public void initialize(String id, ConfigMap properties) {
    super.initialize(id, properties);

    if (properties == null || properties.size() == 0)
        return;

    // Connection Manager
    ConfigMap conn = properties.getPropertyAsMap(HTTPConnectionManagerSettings.CONNECTION_MANAGER, null);
    if (conn != null) {
        // Cookie policy.
        if (conn.getProperty(HTTPConnectionManagerSettings.COOKIE_POLICY) != null) {
            connectionManagerSettings.setCookiePolicy(conn
                    .getPropertyAsString(HTTPConnectionManagerSettings.COOKIE_POLICY, CookiePolicy.DEFAULT));
        }

        // Max Connections Total
        if (conn.getProperty(HTTPConnectionManagerSettings.MAX_TOTAL_CONNECTIONS) != null) {
            int maxTotal = conn.getPropertyAsInt(HTTPConnectionManagerSettings.MAX_TOTAL_CONNECTIONS,
                    MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS);
            connectionManagerSettings.setMaxTotalConnections(maxTotal);
        }

        // Default Max Connections Per Host
        int defaultMaxConnsPerHost = MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS;
        if (conn.getProperty(HTTPConnectionManagerSettings.DEFAULT_MAX_CONNECTIONS_PER_HOST) != null) {
            defaultMaxConnsPerHost = conn.getPropertyAsInt(
                    HTTPConnectionManagerSettings.DEFAULT_MAX_CONNECTIONS_PER_HOST,
                    MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS);
            connectionManagerSettings.setDefaultMaxConnectionsPerHost(defaultMaxConnsPerHost);
        }

        // Connection Timeout
        if (conn.getProperty(HTTPConnectionManagerSettings.CONNECTION_TIMEOUT) != null) {
            int timeout = conn.getPropertyAsInt(HTTPConnectionManagerSettings.CONNECTION_TIMEOUT, 0);
            if (timeout >= 0)
                connectionManagerSettings.setConnectionTimeout(timeout);
        }

        // Socket Timeout
        if (conn.getProperty(HTTPConnectionManagerSettings.SOCKET_TIMEOUT) != null) {
            int timeout = conn.getPropertyAsInt(HTTPConnectionManagerSettings.SOCKET_TIMEOUT, 0);
            if (timeout >= 0)
                connectionManagerSettings.setSocketTimeout(timeout);
        }

        // Stale Checking
        if (conn.getProperty(HTTPConnectionManagerSettings.STALE_CHECKING_ENABLED) != null) {
            boolean staleCheck = conn.getPropertyAsBoolean(HTTPConnectionManagerSettings.STALE_CHECKING_ENABLED,
                    true);
            connectionManagerSettings.setStaleCheckingEnabled(staleCheck);
        }

        // Send Buffer Size
        if (conn.getProperty(HTTPConnectionManagerSettings.SEND_BUFFER_SIZE) != null) {
            int bufferSize = conn.getPropertyAsInt(HTTPConnectionManagerSettings.SEND_BUFFER_SIZE, 0);
            if (bufferSize > 0)
                connectionManagerSettings.setSendBufferSize(bufferSize);
        }

        // Send Receive Size
        if (conn.getProperty(HTTPConnectionManagerSettings.RECEIVE_BUFFER_SIZE) != null) {
            int bufferSize = conn.getPropertyAsInt(HTTPConnectionManagerSettings.RECEIVE_BUFFER_SIZE, 0);
            if (bufferSize > 0)
                connectionManagerSettings.setReceiveBufferSize(bufferSize);
        }

        // TCP No Delay (Nagel's Algorithm)
        if (conn.getProperty(HTTPConnectionManagerSettings.TCP_NO_DELAY) != null) {
            boolean noNagel = conn.getPropertyAsBoolean(HTTPConnectionManagerSettings.TCP_NO_DELAY, true);
            connectionManagerSettings.setTcpNoDelay(noNagel);
        }

        // Linger
        if (conn.getProperty(HTTPConnectionManagerSettings.LINGER) != null) {
            int linger = conn.getPropertyAsInt(HTTPConnectionManagerSettings.LINGER, -1);
            connectionManagerSettings.setLinger(linger);
        }

        // Max Connections Per Host
        List hosts = conn.getPropertyAsList(HTTPConnectionManagerSettings.MAX_PER_HOST, null);
        if (hosts != null) {
            List hostSettings = new ArrayList();
            Iterator it = hosts.iterator();
            while (it.hasNext()) {
                ConfigMap maxPerHost = (ConfigMap) it.next();
                HostConfigurationSettings hostConfig = new HostConfigurationSettings();

                // max-connections
                if (maxPerHost.getProperty(HostConfigurationSettings.MAX_CONNECTIONS) != null) {
                    int maxConn = maxPerHost.getPropertyAsInt(HostConfigurationSettings.MAX_CONNECTIONS,
                            defaultMaxConnsPerHost);
                    hostConfig.setMaximumConnections(maxConn);
                }

                // host
                if (maxPerHost.getProperty(HostConfigurationSettings.HOST) != null) {
                    String host = maxPerHost.getPropertyAsString(HostConfigurationSettings.HOST, null);
                    hostConfig.setHost(host);
                    if (host != null) {
                        // port
                        int port = maxPerHost.getPropertyAsInt(HostConfigurationSettings.PORT, 0);
                        hostConfig.setPort(port);

                        // protocol-factory
                        ConfigMap factoryMap = maxPerHost
                                .getPropertyAsMap(HostConfigurationSettings.PROTOCOL_FACFORY, null);
                        if (factoryMap != null) {
                            String className = factoryMap.getPropertyAsString(CLASS, null);
                            if (className != null) {
                                Class factoryClass = ClassUtil.createClass(className);
                                ProtocolFactory protocolFactory = (ProtocolFactory) ClassUtil
                                        .createDefaultInstance(factoryClass, ProtocolFactory.class);
                                String factoryId = factoryMap.getPropertyAsString(ID,
                                        host + "_protocol_factory");
                                ConfigMap protocolProperties = factoryMap.getPropertyAsMap(PROPERTIES, null);
                                protocolFactory.initialize(factoryId, protocolProperties);
                            }
                        }
                        // protocol
                        else {
                            String protocol = maxPerHost.getPropertyAsString(HostConfigurationSettings.PROTOCOL,
                                    null);
                            hostConfig.setProtocol(protocol);
                        }
                    }
                }

                // proxy
                ConfigMap proxy = maxPerHost.getPropertyAsMap(HostConfigurationSettings.PROXY, null);
                if (proxy != null) {
                    // host
                    String proxyHost = proxy.getPropertyAsString(HostConfigurationSettings.HOST, null);
                    hostConfig.setProxyHost(proxyHost);
                    if (proxyHost != null) {
                        // port
                        int port = proxy.getPropertyAsInt(HostConfigurationSettings.PORT, 0);
                        hostConfig.setProxyPort(port);
                    }
                }

                // local-address
                if (maxPerHost.getProperty(HostConfigurationSettings.LOCAL_ADDRESS) != null) {
                    String localAddress = maxPerHost
                            .getPropertyAsString(HostConfigurationSettings.LOCAL_ADDRESS, null);
                    hostConfig.setLocalAddress(localAddress);
                }

                // virtual-host
                if (maxPerHost.getProperty(HostConfigurationSettings.VIRTUAL_HOST) != null) {
                    String virtualHost = maxPerHost.getPropertyAsString(HostConfigurationSettings.VIRTUAL_HOST,
                            null);
                    hostConfig.setVirtualHost(virtualHost);
                }
                hostSettings.add(hostConfig);
            }

            if (hostSettings.size() > 0)
                connectionManagerSettings.setMaxConnectionsPerHost(hostSettings);
        }
        setConnectionManagerSettings(connectionManagerSettings);
    }

    // Cookie Limit
    if (properties.getProperty(COOKIE_LIMIT) != null) {
        int cl = properties.getPropertyAsInt(COOKIE_LIMIT, DEFAULT_COOKIE_LIMIT);
        setCookieLimit(cl);
    }

    // Allow Lax SSL
    if (properties.getProperty(ALLOW_LAX_SSL) != null) {
        boolean lax = properties.getPropertyAsBoolean(ALLOW_LAX_SSL, false);
        setAllowLaxSSL(lax);
    }

    // Content Chunked
    if (properties.getProperty(CONTENT_CHUNKED) != null) {
        boolean ch = properties.getPropertyAsBoolean(CONTENT_CHUNKED, false);
        setContentChunked(ch);
    }

    // External Proxy
    ConfigMap extern = properties.getPropertyAsMap(ExternalProxySettings.EXTERNAL_PROXY, null);
    if (extern != null) {
        ExternalProxySettings proxy = new ExternalProxySettings();

        String proxyServer = extern.getPropertyAsString(ExternalProxySettings.SERVER, null);
        proxy.setProxyServer(proxyServer);
        int proxyPort = extern.getPropertyAsInt(ExternalProxySettings.PORT,
                ExternalProxySettings.DEFAULT_PROXY_PORT);
        proxy.setProxyPort(proxyPort);
        String ntdomain = extern.getPropertyAsString(ExternalProxySettings.NT_DOMAIN, null);
        proxy.setNTDomain(ntdomain);
        String username = extern.getPropertyAsString(ExternalProxySettings.USERNAME, null);
        proxy.setUsername(username);
        String password = extern.getPropertyAsString(ExternalProxySettings.PASSWORD, null);
        proxy.setPassword(password);

        setExternalProxySettings(proxy);
    }
}

From source file:org.apache.abdera.protocol.client.AbderaClient.java

/**
 * Return the maximum number of connections allowed for the client
 *///from   w w  w  .j av a 2s .c  o  m
public int getMaxConnectionsTotal() {
    return client.getHttpConnectionManager().getParams().getIntParameter(
            HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
            MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS);
}

From source file:org.sonatype.nexus.proxy.storage.remote.commonshttpclient.HttpClientProxyUtil.java

public static void applyProxyToHttpClient(HttpClient httpClient, RemoteStorageContext ctx, Logger logger) {
    httpClient.setHttpConnectionManager(new CustomMultiThreadedHttpConnectionManager());

    // getting the timeout from RemoteStorageContext. The value we get depends on per-repo and global settings.
    // The value will "cascade" from repo level to global level, see imple of it.
    int timeout = ctx.getRemoteConnectionSettings().getConnectionTimeout();

    // getting the connection pool size, using a little trick to allow us "backdoor" to tune it using system
    // properties, but defaulting it to the same we had before (httpClient defaults)
    int connectionPoolSize = SystemPropertiesHelper.getInteger(CONNECTION_POOL_SIZE_KEY,
            MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS);

    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
    httpClient.getHttpConnectionManager().getParams().setSoTimeout(timeout);
    // httpClient.getHttpConnectionManager().getParams().setTcpNoDelay( true );
    httpClient.getHttpConnectionManager().getParams().setMaxTotalConnections(connectionPoolSize);
    // NOTE: connPool is _per_ repo, hence all of those will connect to same host (unless mirrors are used)
    // so, we are violating intentionally the RFC and we let the whole pool size to chase same host
    httpClient.getHttpConnectionManager().getParams()
            .setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, connectionPoolSize);

    // Setting auth if needed
    HostConfiguration httpConfiguration = httpClient.getHostConfiguration();

    // BASIC and DIGEST auth only
    RemoteAuthenticationSettings ras = ctx.getRemoteAuthenticationSettings();

    boolean isSimpleAuthUsed = false;
    boolean isNtlmUsed = false;

    if (ras != null) {
        List<String> authPrefs = new ArrayList<String>(2);
        authPrefs.add(AuthPolicy.DIGEST);
        authPrefs.add(AuthPolicy.BASIC);

        if (ras instanceof ClientSSLRemoteAuthenticationSettings) {
            // ClientSSLRemoteAuthenticationSettings cras = (ClientSSLRemoteAuthenticationSettings) ras;

            // TODO - implement this
        } else if (ras instanceof NtlmRemoteAuthenticationSettings) {
            NtlmRemoteAuthenticationSettings nras = (NtlmRemoteAuthenticationSettings) ras;

            // Using NTLM auth, adding it as first in policies
            authPrefs.add(0, AuthPolicy.NTLM);

            logger(logger).info("... authentication setup for NTLM domain '{}'", nras.getNtlmDomain());

            httpConfiguration.setHost(nras.getNtlmHost());

            httpClient.getState().setCredentials(AuthScope.ANY, new NTCredentials(nras.getUsername(),
                    nras.getPassword(), nras.getNtlmHost(), nras.getNtlmDomain()));

            isNtlmUsed = true;/*from  www.j  a  v  a 2  s  .  c  o  m*/
        } else if (ras instanceof UsernamePasswordRemoteAuthenticationSettings) {
            UsernamePasswordRemoteAuthenticationSettings uras = (UsernamePasswordRemoteAuthenticationSettings) ras;

            // Using Username/Pwd auth, will not add NTLM
            logger(logger).info("... authentication setup for remote storage with username '{}'",
                    uras.getUsername());

            httpClient.getState().setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(uras.getUsername(), uras.getPassword()));

            isSimpleAuthUsed = true;
        }

        httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    }

    RemoteProxySettings rps = ctx.getRemoteProxySettings();

    boolean isProxyUsed = false;

    if (rps.isEnabled()) {
        isProxyUsed = true;

        logger(logger).info("... proxy setup with host '{}'", rps.getHostname());

        httpConfiguration.setProxy(rps.getHostname(), rps.getPort());

        // check if we have non-proxy hosts
        if (rps.getNonProxyHosts() != null && !rps.getNonProxyHosts().isEmpty()) {
            Set<Pattern> nonProxyHostPatterns = new HashSet<Pattern>(rps.getNonProxyHosts().size());
            for (String nonProxyHostRegex : rps.getNonProxyHosts()) {
                try {
                    nonProxyHostPatterns.add(Pattern.compile(nonProxyHostRegex, Pattern.CASE_INSENSITIVE));
                } catch (PatternSyntaxException e) {
                    logger(logger).warn("Invalid non proxy host regex: {}", nonProxyHostRegex, e);
                }
            }
            httpConfiguration.getParams().setParameter(
                    CustomMultiThreadedHttpConnectionManager.NON_PROXY_HOSTS_PATTERNS_KEY,
                    nonProxyHostPatterns);
        }

        if (rps.getProxyAuthentication() != null) {
            ras = rps.getProxyAuthentication();

            List<String> authPrefs = new ArrayList<String>(2);
            authPrefs.add(AuthPolicy.DIGEST);
            authPrefs.add(AuthPolicy.BASIC);

            if (ras instanceof ClientSSLRemoteAuthenticationSettings) {
                // ClientSSLRemoteAuthenticationSettings cras = (ClientSSLRemoteAuthenticationSettings) ras;

                // TODO - implement this
            } else if (ras instanceof NtlmRemoteAuthenticationSettings) {
                NtlmRemoteAuthenticationSettings nras = (NtlmRemoteAuthenticationSettings) ras;

                // Using NTLM auth, adding it as first in policies
                authPrefs.add(0, AuthPolicy.NTLM);

                if (ctx.getRemoteAuthenticationSettings() != null && (ctx
                        .getRemoteAuthenticationSettings() instanceof NtlmRemoteAuthenticationSettings)) {
                    logger(logger).warn("... Apache Commons HttpClient 3.x is unable to use NTLM auth scheme\n"
                            + " for BOTH server side and proxy side authentication!\n"
                            + " You MUST reconfigure server side auth and use BASIC/DIGEST scheme\n"
                            + " if you have to use NTLM proxy, otherwise it will not work!\n"
                            + " *** SERVER SIDE AUTH OVERRIDDEN");
                }

                logger(logger).info("... proxy authentication setup for NTLM domain '{}'",
                        nras.getNtlmDomain());

                httpConfiguration.setHost(nras.getNtlmHost());

                httpClient.getState().setProxyCredentials(AuthScope.ANY, new NTCredentials(nras.getUsername(),
                        nras.getPassword(), nras.getNtlmHost(), nras.getNtlmDomain()));

                isNtlmUsed = true;
            } else if (ras instanceof UsernamePasswordRemoteAuthenticationSettings) {
                UsernamePasswordRemoteAuthenticationSettings uras = (UsernamePasswordRemoteAuthenticationSettings) ras;

                // Using Username/Pwd auth, will not add NTLM
                logger(logger).info("... proxy authentication setup for remote storage with username '{}'",
                        uras.getUsername());

                httpClient.getState().setProxyCredentials(AuthScope.ANY,
                        new UsernamePasswordCredentials(uras.getUsername(), uras.getPassword()));
            }

            httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
        }
    }

    // set preemptive only for simplest scenario:
    // no proxy and BASIC auth is used
    if (isSimpleAuthUsed && !isProxyUsed) {
        logger(logger)
                .info("... simple scenario: simple authentication used with no proxy in between target and us,"
                        + " will use preemptive authentication");

        // we have authentication, let's do it preemptive
        httpClient.getParams().setAuthenticationPreemptive(true);
    }

    // mark the fact that NTLM is in use
    // but ONLY IF IT CHANGED!
    // Otherwise, doing it always, actually marks the ctx itself as "changed", causing an avalanche of other
    // consequences, like resetting all the HTTP clients of all remote storages (coz they think there is a change
    // in proxy or remote connection settings, etc).
    final Boolean isNtlmUsedOldValue = (Boolean) ctx.getContextObject(NTLM_IS_IN_USE_KEY);
    if (isNtlmUsedOldValue == null || isNtlmUsedOldValue.booleanValue() != isNtlmUsed) {
        if (isNtlmUsed) {
            ctx.putContextObject(NTLM_IS_IN_USE_KEY, Boolean.TRUE);
        } else {
            ctx.putContextObject(NTLM_IS_IN_USE_KEY, Boolean.FALSE);
        }
    }
}