Search in sources :

Example 1 with HTTPClientException

use of core.framework.http.HTTPClientException in project core-ng-project by neowu.

the class HTTPClientImpl method execute.

@Override
public HTTPResponse execute(HTTPRequest request) {
    StopWatch watch = new StopWatch();
    HttpUriRequest httpRequest = httpRequest(request);
    try (CloseableHttpResponse httpResponse = client.execute(httpRequest)) {
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        logger.debug("[response] status={}", statusCode);
        Map<String, String> headers = Maps.newHashMap();
        for (Header header : httpResponse.getAllHeaders()) {
            logger.debug("[response:header] {}={}", header.getName(), header.getValue());
            headers.putIfAbsent(header.getName(), header.getValue());
        }
        HttpEntity entity = httpResponse.getEntity();
        byte[] body = responseBody(entity);
        HTTPResponse response = new HTTPResponse(parseHTTPStatus(statusCode), headers, body);
        logResponseText(response);
        return response;
    } catch (IOException | UncheckedIOException e) {
        throw new HTTPClientException(e.getMessage(), "HTTP_COMMUNICATION_FAILED", e);
    } finally {
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("http", elapsedTime);
        logger.debug("execute, elapsedTime={}", elapsedTime);
        if (elapsedTime > slowOperationThresholdInNanos) {
            logger.warn(Markers.errorCode("SLOW_HTTP"), "slow http operation, elapsedTime={}", elapsedTime);
        }
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) HTTPResponse(core.framework.http.HTTPResponse) HTTPClientException(core.framework.http.HTTPClientException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch) Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 2 with HTTPClientException

use of core.framework.http.HTTPClientException in project core-ng-project by neowu.

the class HTTPClientImpl method httpRequest.

HttpUriRequest httpRequest(HTTPRequest request) {
    HTTPMethod method = request.method();
    String uri = request.uri();
    logger.debug("[request] method={}, uri={}", method, uri);
    RequestBuilder builder = RequestBuilder.create(method.name());
    try {
        builder.setUri(uri);
    } catch (IllegalArgumentException e) {
        throw new HTTPClientException("uri is invalid, uri=" + uri, "INVALID_URL", e);
    }
    request.headers().forEach((name, value) -> {
        logger.debug("[request:header] {}={}", name, new FieldParam(name, value));
        builder.setHeader(name, value);
    });
    request.params().forEach((name, value) -> {
        logger.debug("[request:param] {}={}", name, value);
        builder.addParameter(name, value);
    });
    byte[] body = request.body();
    if (body != null) {
        ContentType contentType = request.contentType();
        logRequestBody(request, contentType);
        org.apache.http.entity.ContentType type = org.apache.http.entity.ContentType.create(contentType.mediaType(), contentType.charset().orElse(null));
        builder.setEntity(new ByteArrayEntity(request.body(), type));
    }
    return builder.build();
}
Also used : RequestBuilder(org.apache.http.client.methods.RequestBuilder) FieldParam(core.framework.impl.log.filter.FieldParam) ContentType(core.framework.http.ContentType) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HTTPMethod(core.framework.http.HTTPMethod) HTTPClientException(core.framework.http.HTTPClientException)

Aggregations

HTTPClientException (core.framework.http.HTTPClientException)2 ContentType (core.framework.http.ContentType)1 HTTPMethod (core.framework.http.HTTPMethod)1 HTTPResponse (core.framework.http.HTTPResponse)1 FieldParam (core.framework.impl.log.filter.FieldParam)1 StopWatch (core.framework.util.StopWatch)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 RequestBuilder (org.apache.http.client.methods.RequestBuilder)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1