Search in sources :

Example 6 with MultiThreadedHttpConnectionManager

use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project maven-plugins by apache.

the class DoapUtil method fetchURL.

/**
 * Fetch an URL
 *
 * @param settings the user settings used to fetch the url with an active proxy, if defined.
 * @param url the url to fetch
 * @throws IOException if any
 * @see #DEFAULT_TIMEOUT
 * @since 1.1
 */
public static void fetchURL(Settings settings, URL url) throws IOException {
    if (url == null) {
        throw new IllegalArgumentException("The url is null");
    }
    if ("file".equals(url.getProtocol())) {
        InputStream in = null;
        try {
            in = url.openStream();
            in.close();
            in = null;
        } finally {
            IOUtil.close(in);
        }
        return;
    }
    // http, https...
    HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_TIMEOUT);
    httpClient.getHttpConnectionManager().getParams().setSoTimeout(DEFAULT_TIMEOUT);
    httpClient.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
    // Some web servers don't allow the default user-agent sent by httpClient
    httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    if (settings != null && settings.getActiveProxy() != null) {
        Proxy activeProxy = settings.getActiveProxy();
        ProxyInfo proxyInfo = new ProxyInfo();
        proxyInfo.setNonProxyHosts(activeProxy.getNonProxyHosts());
        if (StringUtils.isNotEmpty(activeProxy.getHost()) && !ProxyUtils.validateNonProxyHosts(proxyInfo, url.getHost())) {
            httpClient.getHostConfiguration().setProxy(activeProxy.getHost(), activeProxy.getPort());
            if (StringUtils.isNotEmpty(activeProxy.getUsername()) && activeProxy.getPassword() != null) {
                Credentials credentials = new UsernamePasswordCredentials(activeProxy.getUsername(), activeProxy.getPassword());
                httpClient.getState().setProxyCredentials(AuthScope.ANY, credentials);
            }
        }
    }
    GetMethod getMethod = new GetMethod(url.toString());
    try {
        int status;
        try {
            status = httpClient.executeMethod(getMethod);
        } catch (SocketTimeoutException e) {
            // could be a sporadic failure, one more retry before we give up
            status = httpClient.executeMethod(getMethod);
        }
        if (status != HttpStatus.SC_OK) {
            throw new FileNotFoundException(url.toString());
        }
    } finally {
        getMethod.releaseConnection();
    }
}
Also used : ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Proxy(org.apache.maven.settings.Proxy) SocketTimeoutException(java.net.SocketTimeoutException) InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) GetMethod(org.apache.commons.httpclient.methods.GetMethod) FileNotFoundException(java.io.FileNotFoundException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 7 with MultiThreadedHttpConnectionManager

use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project cloudstack by apache.

the class ClusterServiceServletImpl method getHttpClient.

private HttpClient getHttpClient() {
    if (s_client == null) {
        final MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
        mgr.getParams().setDefaultMaxConnectionsPerHost(4);
        // TODO make it configurable
        mgr.getParams().setMaxTotalConnections(1000);
        s_client = new HttpClient(mgr);
        final HttpClientParams clientParams = new HttpClientParams();
        clientParams.setSoTimeout(ClusterServiceAdapter.ClusterMessageTimeOut.value() * 1000);
        s_client.setParams(clientParams);
    }
    return s_client;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams)

Example 8 with MultiThreadedHttpConnectionManager

use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project cloudstack by apache.

the class UriUtils method getInputStreamFromUrl.

public static InputStream getInputStreamFromUrl(String url, String user, String password) {
    try {
        Pair<String, Integer> hostAndPort = validateUrl(url);
        HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager());
        if ((user != null) && (password != null)) {
            httpclient.getParams().setAuthenticationPreemptive(true);
            Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
            httpclient.getState().setCredentials(new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds);
            s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second());
        }
        // Execute the method.
        GetMethod method = new GetMethod(url);
        int statusCode = httpclient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            s_logger.error("Failed to read from URL: " + url);
            return null;
        }
        return method.getResponseBodyAsStream();
    } catch (Exception ex) {
        s_logger.error("Failed to read from URL: " + url);
        return null;
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) AuthScope(org.apache.commons.httpclient.auth.AuthScope) GetMethod(org.apache.commons.httpclient.methods.GetMethod) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) URISyntaxException(java.net.URISyntaxException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 9 with MultiThreadedHttpConnectionManager

use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project hub-alert by blackducksoftware.

the class AuthenticationHandler method artifactBinding.

@Bean
public HTTPArtifactBinding artifactBinding(ParserPool parserPool, VelocityEngine velocityEngine) {
    ArtifactResolutionProfileImpl profileImpl = new ArtifactResolutionProfileImpl(new HttpClient(new MultiThreadedHttpConnectionManager()));
    profileImpl.setProcessor(new SAMLProcessorImpl(soapBinding()));
    return new HTTPArtifactBinding(parserPool, velocityEngine, profileImpl);
}
Also used : SAMLProcessorImpl(org.springframework.security.saml.processor.SAMLProcessorImpl) HTTPArtifactBinding(org.springframework.security.saml.processor.HTTPArtifactBinding) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) ArtifactResolutionProfileImpl(org.springframework.security.saml.websso.ArtifactResolutionProfileImpl) Bean(org.springframework.context.annotation.Bean)

Example 10 with MultiThreadedHttpConnectionManager

use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project coprhd-controller by CoprHD.

the class ECSApiFactory method init.

/**
 * Initialize
 */
public void init() {
    _log.info(" ECSApiFactory:init ECSApi factory initialization");
    _clientMap = new ConcurrentHashMap<String, ECSApi>();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(_maxConnPerHost);
    params.setMaxTotalConnections(_maxConn);
    params.setTcpNoDelay(true);
    params.setConnectionTimeout(_connTimeout);
    params.setSoTimeout(_socketConnTimeout);
    _connectionManager = new MultiThreadedHttpConnectionManager();
    _connectionManager.setParams(params);
    // close idle connections immediately
    _connectionManager.closeIdleConnections(0);
    HttpClient client = new HttpClient(_connectionManager);
    client.getParams().setConnectionManagerTimeout(connManagerTimeout);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        @Override
        public boolean retryMethod(HttpMethod httpMethod, IOException e, int i) {
            return false;
        }
    });
    _clientHandler = new ApacheHttpClientHandler(client);
    Protocol.registerProtocol("https", new Protocol("https", new NonValidatingSocketFactory(), 4443));
}
Also used : IOException(java.io.IOException) HttpMethodRetryHandler(org.apache.commons.httpclient.HttpMethodRetryHandler) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) ApacheHttpClient(com.sun.jersey.client.apache.ApacheHttpClient) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) ApacheHttpClientHandler(com.sun.jersey.client.apache.ApacheHttpClientHandler) Protocol(org.apache.commons.httpclient.protocol.Protocol) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

MultiThreadedHttpConnectionManager (org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)34 HttpClient (org.apache.commons.httpclient.HttpClient)26 HttpConnectionManagerParams (org.apache.commons.httpclient.params.HttpConnectionManagerParams)13 IOException (java.io.IOException)11 HttpConnectionManager (org.apache.commons.httpclient.HttpConnectionManager)7 HttpMethodRetryHandler (org.apache.commons.httpclient.HttpMethodRetryHandler)7 ApacheHttpClient (com.sun.jersey.client.apache.ApacheHttpClient)6 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 GetMethod (org.apache.commons.httpclient.methods.GetMethod)6 Protocol (org.apache.commons.httpclient.protocol.Protocol)6 ApacheHttpClientHandler (com.sun.jersey.client.apache.ApacheHttpClientHandler)5 HttpClientParams (org.apache.commons.httpclient.params.HttpClientParams)5 Credentials (org.apache.commons.httpclient.Credentials)3 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 AdvancedGeoCoder (com.google.code.geocoder.AdvancedGeoCoder)2 SocketTimeoutException (java.net.SocketTimeoutException)2 URISyntaxException (java.net.URISyntaxException)2 UnknownHostException (java.net.UnknownHostException)2 HttpException (org.apache.commons.httpclient.HttpException)2