Search in sources :

Example 31 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 32 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 33 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 34 with HttpClient

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;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 35 with HttpClient

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;
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) HttpClient(org.apache.commons.httpclient.HttpClient)

Aggregations

HttpClient (org.apache.commons.httpclient.HttpClient)399 GetMethod (org.apache.commons.httpclient.methods.GetMethod)221 PostMethod (org.apache.commons.httpclient.methods.PostMethod)120 HttpMethod (org.apache.commons.httpclient.HttpMethod)98 Test (org.junit.Test)87 IOException (java.io.IOException)82 InputStream (java.io.InputStream)70 HttpException (org.apache.commons.httpclient.HttpException)44 JSONParser (org.json.simple.parser.JSONParser)44 JSONObject (org.json.simple.JSONObject)40 Map (java.util.Map)35 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 HttpState (org.apache.commons.httpclient.HttpState)25 ZMailbox (com.zimbra.client.ZMailbox)23 Account (com.zimbra.cs.account.Account)22 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)22 ServiceException (com.zimbra.common.service.ServiceException)21 JSONArray (org.json.simple.JSONArray)21 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)20