Search in sources :

Example 1 with HTTPResponse

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

the class WebServiceClient method execute.

// used by generated code, must be public
public Object execute(HTTPMethod method, String serviceURL, Type requestType, Object requestBean, Type responseType) {
    HTTPRequest request = new HTTPRequest(method, serviceURL);
    request.accept(ContentType.APPLICATION_JSON);
    request.header(HTTPServerHandler.HEADER_CLIENT.toString(), logManager.appName);
    linkContext(request);
    if (requestType != null) {
        addRequestBean(request, method, requestType, requestBean);
    }
    if (interceptor != null) {
        logger.debug("intercept request, interceptor={}", interceptor.getClass().getCanonicalName());
        interceptor.intercept(request);
    }
    HTTPResponse response = httpClient.execute(request);
    validateResponse(response);
    if (void.class != responseType) {
        return JSONMapper.fromJSON(responseType, response.body());
    } else {
        return null;
    }
}
Also used : HTTPRequest(core.framework.http.HTTPRequest) HTTPResponse(core.framework.http.HTTPResponse)

Example 2 with HTTPResponse

use of core.framework.http.HTTPResponse 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 3 with HTTPResponse

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

the class WebServiceClientTest method validateResponseWithErrorResponse.

@Test
void validateResponseWithErrorResponse() {
    ErrorResponse response = new ErrorResponse();
    response.severity = "WARN";
    response.errorCode = "NOT_FOUND";
    response.message = "not found";
    RemoteServiceException exception = assertThrows(RemoteServiceException.class, () -> webServiceClient.validateResponse(new HTTPResponse(HTTPStatus.NOT_FOUND, Maps.newHashMap(), Strings.bytes(JSON.toJSON(response)))));
    assertEquals(Severity.WARN, exception.severity());
    assertEquals(response.errorCode, exception.errorCode());
    assertEquals(response.message, exception.getMessage());
}
Also used : RemoteServiceException(core.framework.web.service.RemoteServiceException) HTTPResponse(core.framework.http.HTTPResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HTTPResponse (core.framework.http.HTTPResponse)3 HTTPClientException (core.framework.http.HTTPClientException)1 HTTPRequest (core.framework.http.HTTPRequest)1 StopWatch (core.framework.util.StopWatch)1 RemoteServiceException (core.framework.web.service.RemoteServiceException)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 Test (org.junit.jupiter.api.Test)1