Search in sources :

Example 1 with HttpUriRequestBase

use of org.apache.hc.client5.http.classic.methods.HttpUriRequestBase in project chatty by chatty.

the class Request method apache.

private void apache() {
    if (listener == null) {
        return;
    }
    String responseText = null;
    int responseCode = -1;
    int ratelimitRemaining = -1;
    String responseEncoding = null;
    String requestError = null;
    LOGGER.info(String.format("%s*%s: %s", requestMethod, token != null ? " (auth) " : "", url));
    RequestConfig config = RequestConfig.custom().setConnectTimeout(Timeout.ofMilliseconds(CONNECT_TIMEOUT)).setResponseTimeout(30, TimeUnit.SECONDS).build();
    try (CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build()) {
        ClassicHttpRequest request = new HttpUriRequestBase(requestMethod, new URI(url));
        request.addHeader("Client-ID", CLIENT_ID);
        if (token != null) {
            request.addHeader("Authorization", "Bearer " + token);
        }
        if (data != null) {
            StringEntity stringEntity;
            if (contentType.equals("application/json")) {
                stringEntity = new StringEntity(data, ContentType.APPLICATION_JSON, "UTF-8", false);
            } else {
                stringEntity = new StringEntity(data, CHARSET);
            }
            request.setEntity(stringEntity);
            LOGGER.info("Sending data: " + data);
        }
        try (CloseableHttpResponse response = httpclient.execute(request)) {
            responseCode = response.getCode();
            responseEncoding = getStringHeader(response.getFirstHeader("Content-Encoding"), null);
            ratelimitRemaining = getIntHeader(response.getFirstHeader("Ratelimit-Remaining"), -1);
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                responseText = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
                EntityUtils.consume(responseEntity);
            }
        }
    } catch (IOException | URISyntaxException | ParseException ex) {
        requestError = ex.toString();
    }
    // -----------------------
    // Debug output / Output
    // -----------------------
    LOGGER.info(String.format("GOT (%d/%d, %d%s): %s%s", responseCode, ratelimitRemaining, responseText != null ? responseText.length() : -1, responseEncoding != null ? ", " + responseEncoding : "", url, requestError != null ? " [" + requestError + "]" : ""));
    listener.requestResult(responseText, responseCode, ratelimitRemaining);
}
Also used : RequestConfig(org.apache.hc.client5.http.config.RequestConfig) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpUriRequestBase(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase) HttpEntity(org.apache.hc.core5.http.HttpEntity) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) ParseException(org.apache.hc.core5.http.ParseException)

Example 2 with HttpUriRequestBase

use of org.apache.hc.client5.http.classic.methods.HttpUriRequestBase in project webdrivermanager by bonigarcia.

the class WdmServer method exchange.

public String exchange(String url, String method, String json, int timeoutSec) throws IOException {
    String responseContent = null;
    try (CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build()) {
        HttpUriRequestBase request = null;
        switch(method) {
            case GET:
                request = new HttpGet(url);
                break;
            case DELETE:
                request = new HttpDelete(url);
                break;
            default:
            case POST:
                request = new HttpPost(url);
                HttpEntity body = new StringEntity(json);
                request.setEntity(body);
                request.setHeader("Content-Type", "application/json");
                break;
        }
        RequestConfig requestConfig = custom().setConnectTimeout(timeoutSec, TimeUnit.SECONDS).build();
        request.setConfig(requestConfig);
        try (CloseableHttpResponse response = closeableHttpClient.execute(request)) {
            responseContent = IOUtils.toString(response.getEntity().getContent(), UTF_8);
        }
    }
    return responseContent;
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) HttpUriRequestBase(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase) HttpDelete(org.apache.hc.client5.http.classic.methods.HttpDelete) HttpEntity(org.apache.hc.core5.http.HttpEntity) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)

Example 3 with HttpUriRequestBase

use of org.apache.hc.client5.http.classic.methods.HttpUriRequestBase in project easeagent by megaease.

the class HttpClient5DoExecuteForwardedInterceptor method before.

@Override
public void before(MethodInfo methodInfo, Context context) {
    if (methodInfo.getArgs() == null) {
        return;
    }
    for (Object arg : methodInfo.getArgs()) {
        if (arg instanceof HttpUriRequestBase) {
            final HttpUriRequestBase httpRequestBase = (HttpUriRequestBase) arg;
            context.injectForwardedHeaders(httpRequestBase::setHeader);
            return;
        }
    }
}
Also used : HttpUriRequestBase(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase)

Example 4 with HttpUriRequestBase

use of org.apache.hc.client5.http.classic.methods.HttpUriRequestBase in project adyen-java-api-library by Adyen.

the class AdyenHttpClient method request.

@Override
public String request(String endpoint, String requestBody, Config config, boolean isApiKeyRequired, RequestOptions requestOptions, ApiConstants.HttpMethod httpMethod, Map<String, String> params) throws IOException, HTTPClientException {
    try (CloseableHttpClient httpclient = createCloseableHttpClient(config)) {
        HttpUriRequestBase httpRequest = createRequest(endpoint, requestBody, config, isApiKeyRequired, requestOptions, httpMethod, params);
        // Execute request with a custom response handler
        AdyenResponse response = httpclient.execute(httpRequest, new AdyenResponseHandler());
        if (response.getStatus() < 200 || response.getStatus() >= 300) {
            throw new HTTPClientException(response.getStatus(), "HTTP Exception", response.getHeaders(), response.getBody());
        }
        return response.getBody();
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpUriRequestBase(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase)

Example 5 with HttpUriRequestBase

use of org.apache.hc.client5.http.classic.methods.HttpUriRequestBase in project adyen-java-api-library by Adyen.

the class AdyenHttpClient method createHttpRequestBase.

private HttpUriRequestBase createHttpRequestBase(URI endpoint, String requestBody, ApiConstants.HttpMethod httpMethod) {
    StringEntity requestEntity = null;
    if (requestBody != null && !requestBody.isEmpty()) {
        requestEntity = new StringEntity(requestBody, Charset.forName(CHARSET));
    }
    switch(httpMethod) {
        case GET:
            return new HttpGet(endpoint);
        case PATCH:
            HttpPatch httpPatch = new HttpPatch(endpoint);
            httpPatch.setEntity(requestEntity);
            return httpPatch;
        case DELETE:
            return new HttpDelete(endpoint);
        default:
            // Default to POST if httpMethod is not provided
            HttpPost httpPost = new HttpPost(endpoint);
            httpPost.setEntity(requestEntity);
            return httpPost;
    }
}
Also used : StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) HttpDelete(org.apache.hc.client5.http.classic.methods.HttpDelete) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) HttpPatch(org.apache.hc.client5.http.classic.methods.HttpPatch)

Aggregations

HttpUriRequestBase (org.apache.hc.client5.http.classic.methods.HttpUriRequestBase)8 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)5 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)4 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)4 IOException (java.io.IOException)3 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)3 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)3 HttpEntity (org.apache.hc.core5.http.HttpEntity)3 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)3 URI (java.net.URI)2 HttpDelete (org.apache.hc.client5.http.classic.methods.HttpDelete)2 ConnectivityManager (android.net.ConnectivityManager)1 NetworkInfo (android.net.NetworkInfo)1 ApiException (ee.ajapaik.android.exception.ApiException)1 WebDriverManager (io.github.bonigarcia.wdm.WebDriverManager)1 MethodHandles.lookup (java.lang.invoke.MethodHandles.lookup)1 InetSocketAddress (java.net.InetSocketAddress)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1