Search in sources :

Example 1 with UnsupportedResponseTypeReceivedEvent

use of com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent in project couchbase-jvm-clients by couchbase.

the class KeyValueMessageHandler method channelRead.

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    try {
        if (msg instanceof ByteBuf) {
            decode(ctx, (ByteBuf) msg);
        } else {
            ioContext.environment().eventBus().publish(new UnsupportedResponseTypeReceivedEvent(ioContext, msg));
            closeChannelWithReason(ioContext, ctx, ChannelClosedProactivelyEvent.Reason.INVALID_RESPONSE_FORMAT_DETECTED);
        }
    } finally {
        if (endpoint != null) {
            endpoint.markRequestCompletion();
        }
        ReferenceCountUtil.release(msg);
    }
}
Also used : UnsupportedResponseTypeReceivedEvent(com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 2 with UnsupportedResponseTypeReceivedEvent

use of com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent 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 3 with UnsupportedResponseTypeReceivedEvent

use of com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent in project couchbase-jvm-clients by couchbase.

the class ManagerMessageHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpResponse) {
        currentResponse = ((HttpResponse) msg);
        if (isStreamingConfigRequest()) {
            streamingResponse = (BucketConfigStreamingResponse) currentRequest.decode(currentResponse, null);
            currentRequest.succeed(streamingResponse);
            ctx.pipeline().addFirst(new IdleStateHandler(coreContext.environment().ioConfig().configIdleRedialTimeout().toMillis(), 0, 0, TimeUnit.MILLISECONDS));
        }
    } else if (msg instanceof HttpContent) {
        currentContent.writeBytes(((HttpContent) msg).content());
        if (isStreamingConfigRequest()) {
            // there might be more than one config in the full batch, so keep iterating until all are pushed
            while (true) {
                String encodedConfig = currentContent.toString(StandardCharsets.UTF_8);
                int separatorIndex = encodedConfig.indexOf("\n\n\n\n");
                // if -1 is returned it means that no full config has been located yet, need to wait for more chunks
                if (separatorIndex >= 0) {
                    String content = encodedConfig.substring(0, separatorIndex);
                    streamingResponse.pushConfig(content.trim());
                    currentContent.clear();
                    currentContent.writeBytes(encodedConfig.substring(separatorIndex + 4).getBytes(StandardCharsets.UTF_8));
                } else {
                    break;
                }
            }
        }
        if (msg instanceof LastHttpContent) {
            if (isStreamingConfigRequest()) {
                streamingResponse.completeStream();
                streamingResponse = null;
                ctx.pipeline().remove(IdleStateHandler.class);
            } else {
                byte[] copy = new byte[currentContent.readableBytes()];
                currentContent.readBytes(copy);
                Response response = currentRequest.decode(currentResponse, copy);
                currentRequest.succeed(response);
            }
            currentRequest = null;
            if (endpoint != null) {
                endpoint.markRequestCompletion();
            }
        }
    } else {
        ioContext.environment().eventBus().publish(new UnsupportedResponseTypeReceivedEvent(ioContext, msg));
    }
    ReferenceCountUtil.release(msg);
}
Also used : BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) Response(com.couchbase.client.core.msg.Response) IdleStateHandler(com.couchbase.client.core.deps.io.netty.handler.timeout.IdleStateHandler) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) UnsupportedResponseTypeReceivedEvent(com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent) LastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent) LastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent)

Aggregations

UnsupportedResponseTypeReceivedEvent (com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent)3 Response (com.couchbase.client.core.msg.Response)2 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)1 FullHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse)1 HttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent)1 HttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse)1 HttpResponseStatus (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus)1 LastHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent)1 IdleStateHandler (com.couchbase.client.core.deps.io.netty.handler.timeout.IdleStateHandler)1 HttpStatusCodeException (com.couchbase.client.core.error.HttpStatusCodeException)1 ResponseStatus (com.couchbase.client.core.msg.ResponseStatus)1 BucketConfigStreamingResponse (com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse)1