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;
}
}
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);
}
}
}
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());
}
Aggregations