Search in sources :

Example 1 with HttpConnectionManager

use of org.apache.commons.httpclient.HttpConnectionManager in project camel by apache.

the class GeoCoderEndpoint method createGeocoder.

Geocoder createGeocoder() throws InvalidKeyException {
    HttpConnectionManager connectionManager = this.httpConnectionManager;
    if (connectionManager == null) {
        connectionManager = new MultiThreadedHttpConnectionManager();
    }
    HttpClient httpClient = new HttpClient(connectionManager);
    if (proxyHost != null && proxyPort != null) {
        httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
    }
    // validate that if proxy auth username is given then the proxy auth method is also provided
    if (proxyAuthUsername != null && proxyAuthMethod == null) {
        throw new IllegalArgumentException("Option proxyAuthMethod must be provided to use proxy authentication");
    }
    CompositeHttpConfigurer configurer = new CompositeHttpConfigurer();
    if (proxyAuthMethod != null) {
        configureProxyAuth(configurer, proxyAuthMethod, proxyAuthUsername, proxyAuthPassword, proxyAuthDomain, proxyAuthHost);
    }
    if (httpClientConfigurer != null) {
        configurer.addConfigurer(httpClientConfigurer);
    }
    configurer.configureHttpClient(httpClient);
    Geocoder geocoder;
    if (clientId != null) {
        geocoder = new AdvancedGeoCoder(httpClient, clientId, clientKey);
    } else {
        geocoder = new AdvancedGeoCoder(httpClient);
    }
    return geocoder;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) AdvancedGeoCoder(com.google.code.geocoder.AdvancedGeoCoder) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpConnectionManager(org.apache.commons.httpclient.HttpConnectionManager) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) CompositeHttpConfigurer(org.apache.camel.component.geocoder.http.CompositeHttpConfigurer) Geocoder(com.google.code.geocoder.Geocoder)

Example 2 with HttpConnectionManager

use of org.apache.commons.httpclient.HttpConnectionManager in project camel by apache.

the class HttpComponent method createEndpoint.

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    String addressUri = "http://" + remaining;
    if (uri.startsWith("https:")) {
        addressUri = "https://" + remaining;
    }
    Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
    // must extract well known parameters before we create the endpoint
    HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBinding", HttpBinding.class);
    HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
    UrlRewrite urlRewrite = resolveAndRemoveReferenceParameter(parameters, "urlRewrite", UrlRewrite.class);
    // http client can be configured from URI options
    HttpClientParams clientParams = new HttpClientParams();
    Map<String, Object> httpClientOptions = IntrospectionSupport.extractProperties(parameters, "httpClient.");
    IntrospectionSupport.setProperties(clientParams, httpClientOptions);
    // validate that we could resolve all httpClient. parameters as this component is lenient
    validateParameters(uri, httpClientOptions, null);
    // http client can be configured from URI options
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    // setup the httpConnectionManagerParams
    Map<String, Object> httpConnectionManagerOptions = IntrospectionSupport.extractProperties(parameters, "httpConnectionManager.");
    IntrospectionSupport.setProperties(connectionManagerParams, httpConnectionManagerOptions);
    // validate that we could resolve all httpConnectionManager. parameters as this component is lenient
    validateParameters(uri, httpConnectionManagerOptions, null);
    // make sure the component httpConnectionManager is take effect
    HttpConnectionManager thisHttpConnectionManager = httpConnectionManager;
    if (thisHttpConnectionManager == null) {
        // only set the params on the new created http connection manager
        thisHttpConnectionManager = new MultiThreadedHttpConnectionManager();
        thisHttpConnectionManager.setParams(connectionManagerParams);
    }
    // create the configurer to use for this endpoint (authMethods contains the used methods created by the configurer)
    final Set<AuthMethod> authMethods = new LinkedHashSet<AuthMethod>();
    HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, authMethods);
    addressUri = UnsafeUriCharactersEncoder.encodeHttpURI(addressUri);
    URI endpointUri = URISupport.createRemainingURI(new URI(addressUri), httpClientParameters);
    // create the endpoint and connectionManagerParams already be set
    HttpEndpoint endpoint = createHttpEndpoint(endpointUri.toString(), this, clientParams, thisHttpConnectionManager, configurer);
    // configure the endpoint with the common configuration from the component
    if (getHttpConfiguration() != null) {
        Map<String, Object> properties = new HashMap<>();
        IntrospectionSupport.getProperties(getHttpConfiguration(), properties, null);
        setProperties(endpoint, properties);
    }
    if (headerFilterStrategy != null) {
        endpoint.setHeaderFilterStrategy(headerFilterStrategy);
    } else {
        setEndpointHeaderFilterStrategy(endpoint);
    }
    if (urlRewrite != null) {
        // let CamelContext deal with the lifecycle of the url rewrite
        // this ensures its being shutdown when Camel shutdown etc.
        getCamelContext().addService(urlRewrite);
        endpoint.setUrlRewrite(urlRewrite);
    }
    // prefer to use endpoint configured over component configured
    if (binding == null) {
        // fallback to component configured
        binding = getHttpBinding();
    }
    if (binding != null) {
        endpoint.setBinding(binding);
    }
    setProperties(endpoint, parameters);
    // restructure uri to be based on the parameters left as we dont want to include the Camel internal options
    URI httpUri = URISupport.createRemainingURI(new URI(addressUri), parameters);
    // validate http uri that end-user did not duplicate the http part that can be a common error
    String part = httpUri.getSchemeSpecificPart();
    if (part != null) {
        part = part.toLowerCase();
        if (part.startsWith("//http//") || part.startsWith("//https//") || part.startsWith("//http://") || part.startsWith("//https://")) {
            throw new ResolveEndpointFailedException(uri, "The uri part is not configured correctly. You have duplicated the http(s) protocol.");
        }
    }
    endpoint.setHttpUri(httpUri);
    endpoint.setHttpClientOptions(httpClientOptions);
    return endpoint;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) UrlRewrite(org.apache.camel.http.common.UrlRewrite) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) HttpRestHeaderFilterStrategy(org.apache.camel.http.common.HttpRestHeaderFilterStrategy) URI(java.net.URI) ResolveEndpointFailedException(org.apache.camel.ResolveEndpointFailedException) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpBinding(org.apache.camel.http.common.HttpBinding) HttpConnectionManager(org.apache.commons.httpclient.HttpConnectionManager) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)

Example 3 with HttpConnectionManager

use of org.apache.commons.httpclient.HttpConnectionManager in project camel by apache.

the class WeatherComponent method createHttpClient.

private HttpClient createHttpClient(WeatherConfiguration configuration) {
    HttpConnectionManager connectionManager = configuration.getHttpConnectionManager();
    if (connectionManager == null) {
        connectionManager = new MultiThreadedHttpConnectionManager();
    }
    HttpClient httpClient = new HttpClient(connectionManager);
    if (configuration.getProxyHost() != null && configuration.getProxyPort() != null) {
        httpClient.getHostConfiguration().setProxy(configuration.getProxyHost(), configuration.getProxyPort());
    }
    if (configuration.getProxyAuthUsername() != null && configuration.getProxyAuthMethod() == null) {
        throw new IllegalArgumentException("Option proxyAuthMethod must be provided to use proxy authentication");
    }
    CompositeHttpConfigurer configurer = new CompositeHttpConfigurer();
    if (configuration.getProxyAuthMethod() != null) {
        configureProxyAuth(configurer, configuration.getProxyAuthMethod(), configuration.getProxyAuthUsername(), configuration.getProxyAuthPassword(), configuration.getProxyAuthDomain(), configuration.getProxyAuthHost());
    }
    configurer.configureHttpClient(httpClient);
    return httpClient;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpConnectionManager(org.apache.commons.httpclient.HttpConnectionManager) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) CompositeHttpConfigurer(org.apache.camel.component.weather.http.CompositeHttpConfigurer)

Example 4 with HttpConnectionManager

use of org.apache.commons.httpclient.HttpConnectionManager in project camel by apache.

the class MultiThreadedHttpGetTest method testHttpGetWithoutConversion.

@Test
public void testHttpGetWithoutConversion() throws Exception {
    // This is needed as by default there are 2 parallel
    // connections to some host and there is nothing that
    // closes the http connection here.
    // Need to set the httpConnectionManager 
    HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    httpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(5);
    context.getComponent("http", HttpComponent.class).setHttpConnectionManager(httpConnectionManager);
    String endpointName = "seda:withoutConversion?concurrentConsumers=5";
    sendMessagesTo(endpointName, 5);
}
Also used : MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpComponent(org.apache.camel.component.http.HttpComponent) HttpConnectionManager(org.apache.commons.httpclient.HttpConnectionManager) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) Test(org.junit.Test)

Example 5 with HttpConnectionManager

use of org.apache.commons.httpclient.HttpConnectionManager in project sling by apache.

the class JsonPipe method configureHttpClient.

/**
     * Configure http client
     */
private void configureHttpClient() {
    HttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    manager.setParams(params);
    client = new HttpClient(manager);
    client.getParams().setAuthenticationPreemptive(false);
}
Also used : HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpConnectionManager(org.apache.commons.httpclient.HttpConnectionManager) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)

Aggregations

HttpConnectionManager (org.apache.commons.httpclient.HttpConnectionManager)5 MultiThreadedHttpConnectionManager (org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)5 HttpClient (org.apache.commons.httpclient.HttpClient)3 HttpConnectionManagerParams (org.apache.commons.httpclient.params.HttpConnectionManagerParams)2 AdvancedGeoCoder (com.google.code.geocoder.AdvancedGeoCoder)1 Geocoder (com.google.code.geocoder.Geocoder)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)1 CompositeHttpConfigurer (org.apache.camel.component.geocoder.http.CompositeHttpConfigurer)1 HttpComponent (org.apache.camel.component.http.HttpComponent)1 CompositeHttpConfigurer (org.apache.camel.component.weather.http.CompositeHttpConfigurer)1 HttpBinding (org.apache.camel.http.common.HttpBinding)1 HttpRestHeaderFilterStrategy (org.apache.camel.http.common.HttpRestHeaderFilterStrategy)1 UrlRewrite (org.apache.camel.http.common.UrlRewrite)1 HeaderFilterStrategy (org.apache.camel.spi.HeaderFilterStrategy)1 HttpClientParams (org.apache.commons.httpclient.params.HttpClientParams)1 Test (org.junit.Test)1