use of org.apache.hc.core5.http.nio.AsyncResponseConsumer in project httpcomponents-core by apache.
the class BenchmarkWorker method createResponseConsumer.
private AsyncResponseConsumer<Void> createResponseConsumer() {
return new AsyncResponseConsumer<Void>() {
volatile int status;
volatile Charset charset;
final AtomicLong contentLength = new AtomicLong();
final AtomicReference<FutureCallback<Void>> resultCallbackRef = new AtomicReference<>();
@Override
public void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext context, final FutureCallback<Void> resultCallback) throws HttpException, IOException {
status = response.getCode();
resultCallbackRef.set(resultCallback);
stats.setVersion(response.getVersion());
final Header serverHeader = response.getFirstHeader(HttpHeaders.SERVER);
if (serverHeader != null) {
stats.setServerName(serverHeader.getValue());
}
if (config.getVerbosity() >= 2) {
System.out.println(response.getCode());
}
if (entityDetails != null) {
if (config.getVerbosity() >= 6) {
if (entityDetails.getContentType() != null) {
final ContentType contentType = ContentType.parseLenient(entityDetails.getContentType());
charset = ContentType.getCharset(contentType, null);
}
}
} else {
streamEnd(null);
}
}
@Override
public void informationResponse(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
}
@Override
public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
capacityChannel.update(Integer.MAX_VALUE);
}
@Override
public void consume(final ByteBuffer src) throws IOException {
final int n = src.remaining();
contentLength.addAndGet(n);
stats.incTotalContentLength(n);
if (config.getVerbosity() >= 6) {
final CharsetDecoder decoder = (charset != null ? charset : StandardCharsets.US_ASCII).newDecoder();
System.out.print(decoder.decode(src));
}
}
@Override
public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
if (status == HttpStatus.SC_OK) {
stats.incSuccessCount();
} else {
stats.incFailureCount();
}
stats.setContentLength(contentLength.get());
final FutureCallback<Void> resultCallback = resultCallbackRef.getAndSet(null);
if (resultCallback != null) {
resultCallback.completed(null);
}
if (config.getVerbosity() >= 6) {
System.out.println();
System.out.println();
}
}
@Override
public void failed(final Exception cause) {
stats.incFailureCount();
final FutureCallback<Void> resultCallback = resultCallbackRef.getAndSet(null);
if (resultCallback != null) {
resultCallback.failed(cause);
}
if (config.getVerbosity() >= 1) {
System.out.println("HTTP response error: " + cause.getMessage());
}
}
@Override
public void releaseResources() {
}
};
}
use of org.apache.hc.core5.http.nio.AsyncResponseConsumer in project httpcomponents-core by apache.
the class H2MultiplexingRequester method execute.
public final <T> Future<T> execute(final AsyncRequestProducer requestProducer, final AsyncResponseConsumer<T> responseConsumer, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final Timeout timeout, final HttpContext context, final FutureCallback<T> callback) {
Args.notNull(requestProducer, "Request producer");
Args.notNull(responseConsumer, "Response consumer");
Args.notNull(timeout, "Timeout");
final ComplexFuture<T> future = new ComplexFuture<>(callback);
final AsyncClientExchangeHandler exchangeHandler = new BasicClientExchangeHandler<>(requestProducer, responseConsumer, new FutureContribution<T>(future) {
@Override
public void completed(final T result) {
future.completed(result);
}
});
execute(exchangeHandler, pushHandlerFactory, future, timeout, context != null ? context : HttpCoreContext.create());
return future;
}
use of org.apache.hc.core5.http.nio.AsyncResponseConsumer in project httpcomponents-core by apache.
the class HttpAsyncRequester method execute.
public final <T> Future<T> execute(final AsyncRequestProducer requestProducer, final AsyncResponseConsumer<T> responseConsumer, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final Timeout timeout, final HttpContext context, final FutureCallback<T> callback) {
Args.notNull(requestProducer, "Request producer");
Args.notNull(responseConsumer, "Response consumer");
Args.notNull(timeout, "Timeout");
final BasicFuture<T> future = new BasicFuture<>(callback);
final AsyncClientExchangeHandler exchangeHandler = new BasicClientExchangeHandler<>(requestProducer, responseConsumer, new FutureContribution<T>(future) {
@Override
public void completed(final T result) {
future.completed(result);
}
});
execute(exchangeHandler, pushHandlerFactory, timeout, context != null ? context : HttpCoreContext.create());
return future;
}
use of org.apache.hc.core5.http.nio.AsyncResponseConsumer in project skywalking-java by apache.
the class HttpAsyncClientDoExecuteInterceptor method beforeMethod.
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
AsyncResponseConsumer consumer = (AsyncResponseConsumer) allArguments[2];
HttpContext context = (HttpContext) allArguments[4];
FutureCallback callback = (FutureCallback) allArguments[5];
allArguments[2] = new AsyncResponseConsumerWrapper(consumer);
allArguments[5] = new FutureCallbackWrapper(callback);
if (ContextManager.isActive()) {
context.setAttribute(Constants.SKYWALKING_CONTEXT_SNAPSHOT, ContextManager.capture());
}
}
use of org.apache.hc.core5.http.nio.AsyncResponseConsumer in project httpcomponents-jackson by ok2c.
the class JsonResponseConsumerTest method testJsonErrorResponseBodyNotMatchingResponseClass.
@Test
void testJsonErrorResponseBodyNotMatchingResponseClass() throws Exception {
String errorBody = "{\"code\": 500, \"message\": \"Unexpected internal failure\"}";
AsyncResponseConsumer<Message<HttpResponse, RequestData>> consumer = JsonResponseConsumers.create(objectMapper, RequestData.class);
CompletableFuture<RequestData> resultFuture = new CompletableFuture<>();
consumer.consumeResponse(new BasicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR), new BasicEntityDetails(errorBody.length(), ContentType.APPLICATION_JSON), HttpClientContext.create(), new FutureCallback<Message<HttpResponse, RequestData>>() {
@Override
public void completed(Message<HttpResponse, RequestData> result) {
// NOTE: The error body is lost, we would need a custom consumer to retrieve that. Actually
// it might also be mapped accidentally to the response class.
handleResponseResult(result, resultFuture);
}
@Override
public void failed(Exception ex) {
resultFuture.completeExceptionally(ex);
}
@Override
public void cancelled() {
resultFuture.cancel(false);
}
});
consumer.consume(ByteBuffer.wrap(errorBody.getBytes(StandardCharsets.UTF_8)));
consumer.streamEnd(null);
// FAILS: consumer attempts to parse the error body as it was a successful response and throws a parsing error
assertThat(resultFuture).hasFailedWithThrowableThat().isInstanceOf(HttpResponseException.class);
}
Aggregations