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