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 zeppelin by apache.
the class AbstractTestRestApi method httpGet.
protected static GetMethod httpGet(String path, String user, String pwd) throws IOException {
LOG.info("Connecting to {}", url + path);
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url + path);
getMethod.addRequestHeader("Origin", url);
if (userAndPasswordAreNotBlank(user, pwd)) {
getMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd));
}
httpClient.executeMethod(getMethod);
LOG.info("{} - {}", getMethod.getStatusCode(), getMethod.getStatusText());
return getMethod;
}
use of org.apache.commons.httpclient.HttpClient in project zeppelin by apache.
the class AbstractTestRestApi method httpDelete.
protected static DeleteMethod httpDelete(String path, String user, String pwd) throws IOException {
LOG.info("Connecting to {}", url + path);
HttpClient httpClient = new HttpClient();
DeleteMethod deleteMethod = new DeleteMethod(url + path);
deleteMethod.addRequestHeader("Origin", url);
if (userAndPasswordAreNotBlank(user, pwd)) {
deleteMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd));
}
httpClient.executeMethod(deleteMethod);
LOG.info("{} - {}", deleteMethod.getStatusCode(), deleteMethod.getStatusText());
return deleteMethod;
}
Aggregations