Search in sources :

Example 1 with HttpClient

use of org.apache.commons.httpclient.HttpClient 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 HttpClient

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

the class HttpComponentVerifier method verifyHttpConnectivity.

private void verifyHttpConnectivity(ResultBuilder builder, Map<String, Object> parameters) throws Exception {
    Optional<String> uri = getOption(parameters, "httpUri", String.class);
    HttpClient httpclient = createHttpClient(builder, parameters);
    HttpMethod method = new GetMethod(uri.get());
    try {
        int code = httpclient.executeMethod(method);
        String okCodes = getOption(parameters, "okStatusCodeRange", String.class).orElse("200-299");
        if (!HttpHelper.isStatusCodeOk(code, okCodes)) {
            if (code == 401) {
                // Unauthorized, add authUsername and authPassword to the list
                // of parameters in error
                builder.error(ResultErrorBuilder.withHttpCode(code).description(method.getStatusText()).parameter("authUsername").parameter("authPassword").build());
            } else if (code >= 300 && code < 400) {
                // redirect
                builder.error(ResultErrorBuilder.withHttpCode(code).description(method.getStatusText()).parameter("httpUri").attribute(ComponentVerifier.HTTP_REDIRECT, true).attribute(ComponentVerifier.HTTP_REDIRECT_LOCATION, () -> HttpUtil.responseHeaderValue(method, "location")).build());
            } else if (code >= 400) {
                // generic http error
                builder.error(ResultErrorBuilder.withHttpCode(code).description(method.getStatusText()).build());
            }
        }
    } catch (UnknownHostException e) {
        builder.error(ResultErrorBuilder.withException(e).parameter("httpUri").build());
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 3 with HttpClient

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

the class HttpComponentVerifier method createHttpClient.

private HttpClient createHttpClient(ResultBuilder builder, Map<String, Object> parameters) throws Exception {
    HttpClientParams clientParams = setProperties(new HttpClientParams(), "httpClient.", parameters);
    HttpClient client = new HttpClient(clientParams);
    CompositeHttpConfigurer configurer = new CompositeHttpConfigurer();
    configureProxy(builder, parameters).ifPresent(configurer::addConfigurer);
    configureAuthentication(builder, parameters).ifPresent(configurer::addConfigurer);
    configurer.configureHttpClient(client);
    return client;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams)

Example 4 with HttpClient

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

the class HttpEndpoint method createHttpClient.

/**
     * Factory method used by producers and consumers to create a new {@link HttpClient} instance
     */
public HttpClient createHttpClient() {
    ObjectHelper.notNull(clientParams, "clientParams");
    ObjectHelper.notNull(httpConnectionManager, "httpConnectionManager");
    HttpClient answer = new HttpClient(getClientParams());
    // configure http proxy from camelContext
    if (ObjectHelper.isNotEmpty(getCamelContext().getProperty("http.proxyHost")) && ObjectHelper.isNotEmpty(getCamelContext().getProperty("http.proxyPort"))) {
        String host = getCamelContext().getProperty("http.proxyHost");
        int port = Integer.parseInt(getCamelContext().getProperty("http.proxyPort"));
        LOG.debug("CamelContext properties http.proxyHost and http.proxyPort detected. Using http proxy host: {} port: {}", host, port);
        answer.getHostConfiguration().setProxy(host, port);
    }
    if (getProxyHost() != null) {
        LOG.debug("Using proxy: {}:{}", getProxyHost(), getProxyPort());
        answer.getHostConfiguration().setProxy(getProxyHost(), getProxyPort());
    }
    if (getAuthMethodPriority() != null) {
        List<String> authPrefs = new ArrayList<String>();
        Iterator<?> it = getCamelContext().getTypeConverter().convertTo(Iterator.class, getAuthMethodPriority());
        int i = 1;
        while (it.hasNext()) {
            Object value = it.next();
            AuthMethod auth = getCamelContext().getTypeConverter().convertTo(AuthMethod.class, value);
            if (auth == null) {
                throw new IllegalArgumentException("Unknown authMethod: " + value + " in authMethodPriority: " + getAuthMethodPriority());
            }
            LOG.debug("Using authSchemePriority #{}: {}", i, auth);
            authPrefs.add(auth.name());
            i++;
        }
        if (!authPrefs.isEmpty()) {
            answer.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
        }
    }
    answer.setHttpConnectionManager(httpConnectionManager);
    HttpClientConfigurer configurer = getHttpClientConfigurer();
    if (configurer != null) {
        configurer.configureHttpClient(answer);
    }
    return answer;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) ArrayList(java.util.ArrayList) UriEndpoint(org.apache.camel.spi.UriEndpoint) HttpCommonEndpoint(org.apache.camel.http.common.HttpCommonEndpoint)

Example 5 with HttpClient

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

the class HttpProxyTest method testDifferentHttpProxyConfigured.

@Test
public void testDifferentHttpProxyConfigured() throws Exception {
    HttpEndpoint http1 = context.getEndpoint("http://www.google.com?proxyHost=myproxy&proxyPort=1234", HttpEndpoint.class);
    HttpEndpoint http2 = context.getEndpoint("http://www.google.com?test=parameter&proxyHost=myotherproxy&proxyPort=2345", HttpEndpoint.class);
    HttpClient client1 = http1.createHttpClient();
    assertEquals("myproxy", client1.getHostConfiguration().getProxyHost());
    assertEquals(1234, client1.getHostConfiguration().getProxyPort());
    HttpClient client2 = http2.createHttpClient();
    assertEquals("myotherproxy", client2.getHostConfiguration().getProxyHost());
    assertEquals(2345, client2.getHostConfiguration().getProxyPort());
    //As the endpointUri is recreated, so the parameter could be in different place, so we use the URISupport.normalizeUri
    assertEquals("Get a wrong endpoint uri of http1", "http://www.google.com?proxyHost=myproxy&proxyPort=1234", URISupport.normalizeUri(http1.getEndpointUri()));
    assertEquals("Get a wrong endpoint uri of http2", "http://www.google.com?proxyHost=myotherproxy&proxyPort=2345&test=parameter", URISupport.normalizeUri(http2.getEndpointUri()));
    assertEquals("Should get the same EndpointKey", http1.getEndpointKey(), http2.getEndpointKey());
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) Test(org.junit.Test)

Aggregations

HttpClient (org.apache.commons.httpclient.HttpClient)389 GetMethod (org.apache.commons.httpclient.methods.GetMethod)217 PostMethod (org.apache.commons.httpclient.methods.PostMethod)115 HttpMethod (org.apache.commons.httpclient.HttpMethod)98 Test (org.junit.Test)86 IOException (java.io.IOException)79 InputStream (java.io.InputStream)65 JSONParser (org.json.simple.parser.JSONParser)44 HttpException (org.apache.commons.httpclient.HttpException)43 JSONObject (org.json.simple.JSONObject)40 Map (java.util.Map)32 ArrayList (java.util.ArrayList)27 HttpState (org.apache.commons.httpclient.HttpState)25 ZMailbox (com.zimbra.client.ZMailbox)23 Account (com.zimbra.cs.account.Account)22 HashMap (java.util.HashMap)22 ServiceException (com.zimbra.common.service.ServiceException)21 JSONArray (org.json.simple.JSONArray)21 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)20 Element (org.w3c.dom.Element)20