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;
}
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());
}
}
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;
}
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;
}
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());
}
Aggregations