Search in sources :

Example 1 with FullHttpResponse

use of com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse in project couchbase-jvm-clients by couchbase.

the class NonChunkedHttpMessageHandler method channelRead.

/**
 * Parses the full http response and sends it to decode into the request.
 *
 * @param ctx the channel handler context.
 * @param msg the FullHttpResponse from the server.
 */
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    try {
        if (msg instanceof FullHttpResponse) {
            try {
                currentRequest.context().dispatchLatency(System.nanoTime() - dispatchTimingStart);
                if (currentDispatchSpan != null) {
                    currentDispatchSpan.end();
                }
                FullHttpResponse httpResponse = (FullHttpResponse) msg;
                ResponseStatus responseStatus = HttpProtocol.decodeStatus(httpResponse.status());
                if (!currentRequest.completed()) {
                    if (responseStatus == ResponseStatus.SUCCESS) {
                        Response response = currentRequest.decode(httpResponse, channelContext);
                        currentRequest.succeed(response);
                    } else {
                        String body = httpResponse.content().toString(StandardCharsets.UTF_8);
                        Exception error = currentRequest.bypassExceptionTranslation() ? failRequestWithHttpStatusCodeException(httpResponse.status(), body, currentRequest) : failRequestWith(httpResponse.status(), body, currentRequest);
                        currentRequest.fail(error);
                    }
                } else {
                    ioContext.environment().orphanReporter().report(currentRequest);
                }
            } catch (Throwable ex) {
                currentRequest.fail(ex);
            } finally {
                currentRequest = null;
                currentDispatchSpan = null;
                endpoint.markRequestCompletion();
            }
        } else {
            ioContext.environment().eventBus().publish(new UnsupportedResponseTypeReceivedEvent(ioContext, msg));
            closeChannelWithReason(ioContext, ctx, ChannelClosedProactivelyEvent.Reason.INVALID_RESPONSE_FORMAT_DETECTED);
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}
Also used : FullHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse) Response(com.couchbase.client.core.msg.Response) HttpResponseStatus(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) UnsupportedResponseTypeReceivedEvent(com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent) FullHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse) HttpStatusCodeException(com.couchbase.client.core.error.HttpStatusCodeException)

Example 2 with FullHttpResponse

use of com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse in project couchbase-jvm-clients by couchbase.

the class NonChunkedHttpMessageHandlerTest method callsMarkRequestCompletedOnceFinished.

@Test
void callsMarkRequestCompletedOnceFinished() throws Exception {
    channel.pipeline().addFirst(NonChunkedHttpMessageHandler.IDENTIFIER, new TestNonChunkedHttpMessageHandler(endpoint));
    channel.pipeline().fireChannelActive();
    CoreHttpRequest request = CoreHttpRequest.builder(CoreCommonOptions.of(Duration.ofSeconds(1), BestEffortRetryStrategy.INSTANCE, null), endpoint.context(), HttpMethod.GET, CoreHttpPath.path("/"), RequestTarget.views("bucket")).build();
    channel.writeAndFlush(request);
    FullHttpRequest written = channel.readOutbound();
    assertEquals("/", written.uri());
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    channel.writeInbound(response);
    assertEquals(ResponseStatus.SUCCESS, request.response().get().status());
    verify(endpoint, times(1)).markRequestCompletion();
}
Also used : DefaultFullHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) CoreHttpRequest(com.couchbase.client.core.endpoint.http.CoreHttpRequest) FullHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

FullHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse)2 UnsupportedResponseTypeReceivedEvent (com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent)1 DefaultFullHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)1 HttpResponseStatus (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus)1 CoreHttpRequest (com.couchbase.client.core.endpoint.http.CoreHttpRequest)1 HttpStatusCodeException (com.couchbase.client.core.error.HttpStatusCodeException)1 Response (com.couchbase.client.core.msg.Response)1 ResponseStatus (com.couchbase.client.core.msg.ResponseStatus)1 Test (org.junit.jupiter.api.Test)1