Search in sources :

Example 1 with ResponseStatus

use of com.couchbase.client.core.msg.ResponseStatus in project couchbase-jvm-clients by couchbase.

the class GetAndLockRequest method decode.

@Override
public GetAndLockResponse decode(final ByteBuf response, KeyValueChannelContext ctx) {
    ResponseStatus status = decodeStatus(response);
    long cas = cas(response);
    if (status.success()) {
        byte[] bytes = bodyAsBytes(response);
        byte[] content = bytes != null ? tryDecompression(bytes, datatype(response)) : Bytes.EMPTY_BYTE_ARRAY;
        int flags = extrasAsInt(response, 0, 0);
        return new GetAndLockResponse(status, content, cas, flags);
    } else {
        return new GetAndLockResponse(status, null, cas, 0);
    }
}
Also used : ResponseStatus(com.couchbase.client.core.msg.ResponseStatus)

Example 2 with ResponseStatus

use of com.couchbase.client.core.msg.ResponseStatus 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 ResponseStatus

use of com.couchbase.client.core.msg.ResponseStatus in project couchbase-jvm-clients by couchbase.

the class GetAndTouchRequest method decode.

@Override
public GetAndTouchResponse decode(final ByteBuf response, KeyValueChannelContext ctx) {
    ResponseStatus status = decodeStatus(response);
    long cas = cas(response);
    if (status.success()) {
        byte[] bytes = bodyAsBytes(response);
        byte[] content = bytes != null ? tryDecompression(bytes, datatype(response)) : Bytes.EMPTY_BYTE_ARRAY;
        int flags = extrasAsInt(response, 0, 0);
        return new GetAndTouchResponse(status, content, cas, flags);
    } else {
        return new GetAndTouchResponse(status, null, cas, 0);
    }
}
Also used : ResponseStatus(com.couchbase.client.core.msg.ResponseStatus)

Example 4 with ResponseStatus

use of com.couchbase.client.core.msg.ResponseStatus in project couchbase-jvm-clients by couchbase.

the class GetCollectionManifestRequest method decode.

@Override
public GetCollectionManifestResponse decode(final ByteBuf response, final KeyValueChannelContext ctx) {
    ResponseStatus status = MemcacheProtocol.decodeStatus(response);
    Optional<String> manifest = Optional.empty();
    if (status.success()) {
        manifest = Optional.of(body(response).map(ByteBufUtil::getBytes).orElse(Bytes.EMPTY_BYTE_ARRAY)).map(b -> new String(b, StandardCharsets.UTF_8));
    }
    return new GetCollectionManifestResponse(status, manifest);
}
Also used : ByteBufUtil(com.couchbase.client.core.deps.io.netty.buffer.ByteBufUtil) MemcacheProtocol.noKey(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noKey) ByteBufAllocator(com.couchbase.client.core.deps.io.netty.buffer.ByteBufAllocator) MemcacheProtocol.noDatatype(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noDatatype) Bytes(com.couchbase.client.core.util.Bytes) MemcacheProtocol.noExtras(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noExtras) StandardCharsets(java.nio.charset.StandardCharsets) MemcacheProtocol.noCas(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noCas) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) KeyValueChannelContext(com.couchbase.client.core.io.netty.kv.KeyValueChannelContext) ByteBufUtil(com.couchbase.client.core.deps.io.netty.buffer.ByteBufUtil) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) MemcacheProtocol.noBody(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noBody) Optional(java.util.Optional) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) MemcacheProtocol(com.couchbase.client.core.io.netty.kv.MemcacheProtocol) MemcacheProtocol.noPartition(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noPartition) MemcacheProtocol.body(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.body) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus)

Example 5 with ResponseStatus

use of com.couchbase.client.core.msg.ResponseStatus in project couchbase-jvm-clients by couchbase.

the class SubdocGetRequest method decode.

@Override
public SubdocGetResponse decode(final ByteBuf response, KeyValueChannelContext ctx) {
    Optional<ByteBuf> maybeBody = body(response);
    SubDocumentField[] values;
    List<CouchbaseException> errors = null;
    if (maybeBody.isPresent()) {
        ByteBuf body = maybeBody.get();
        values = new SubDocumentField[commands.size()];
        for (Command command : commands) {
            short statusRaw = body.readShort();
            SubDocumentOpResponseStatus status = decodeSubDocumentStatus(statusRaw);
            Optional<CouchbaseException> error = Optional.empty();
            if (status != SubDocumentOpResponseStatus.SUCCESS) {
                if (errors == null)
                    errors = new ArrayList<>();
                CouchbaseException err = mapSubDocumentError(this, status, command.path, command.originalIndex());
                errors.add(err);
                error = Optional.of(err);
            }
            int valueLength = body.readInt();
            byte[] value = new byte[valueLength];
            body.readBytes(value, 0, valueLength);
            SubDocumentField op = new SubDocumentField(status, error, value, command.path, command.type);
            values[command.originalIndex] = op;
        }
    } else {
        values = new SubDocumentField[0];
    }
    short rawStatus = status(response);
    ResponseStatus status = decodeStatus(response);
    boolean isDeleted = rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE_DELETED.status() || rawStatus == Status.SUBDOC_SUCCESS_DELETED_DOCUMENT.status();
    Optional<CouchbaseException> error = Optional.empty();
    // Note that we send all subdoc requests as multi currently so always get this back on error
    if (rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE.status() || rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE_DELETED.status()) {
        // Special case logic for CMD_EXISTS
        if (commands.size() == 1 && commands.get(0).type == SubdocCommandType.EXISTS) {
            status = ResponseStatus.SUCCESS;
        } else // If a single subdoc op was tried and failed, return that directly
        if (commands.size() == 1 && errors != null && errors.size() == 1) {
            error = Optional.of(errors.get(0));
        } else {
            // Otherwise return success, as some of the operations have succeeded
            status = ResponseStatus.SUCCESS;
        }
    }
    // Handle any document-level failures here
    if (rawStatus == Status.SUBDOC_DOC_NOT_JSON.status()) {
        SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.DOC_NOT_JSON);
        error = Optional.of(new DocumentNotJsonException(e));
    } else if (rawStatus == Status.SUBDOC_DOC_TOO_DEEP.status()) {
        SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.DOC_TOO_DEEP);
        error = Optional.of(new DocumentTooDeepException(e));
    } else if (rawStatus == Status.SUBDOC_XATTR_INVALID_KEY_COMBO.status()) {
        SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.XATTR_INVALID_KEY_COMBO);
        error = Optional.of(new XattrInvalidKeyComboException(e));
    }
    // Do not handle SUBDOC_INVALID_COMBO here, it indicates a client-side bug
    return new SubdocGetResponse(status, error, values, cas(response), isDeleted);
}
Also used : DocumentNotJsonException(com.couchbase.client.core.error.subdoc.DocumentNotJsonException) ArrayList(java.util.ArrayList) CompositeByteBuf(com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) SubDocumentErrorContext(com.couchbase.client.core.error.context.SubDocumentErrorContext) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) DocumentTooDeepException(com.couchbase.client.core.error.subdoc.DocumentTooDeepException) XattrInvalidKeyComboException(com.couchbase.client.core.error.subdoc.XattrInvalidKeyComboException)

Aggregations

ResponseStatus (com.couchbase.client.core.msg.ResponseStatus)14 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)6 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)3 CompositeByteBuf (com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf)2 SubDocumentErrorContext (com.couchbase.client.core.error.context.SubDocumentErrorContext)2 DocumentNotJsonException (com.couchbase.client.core.error.subdoc.DocumentNotJsonException)2 DocumentTooDeepException (com.couchbase.client.core.error.subdoc.DocumentTooDeepException)2 XattrInvalidKeyComboException (com.couchbase.client.core.error.subdoc.XattrInvalidKeyComboException)2 Response (com.couchbase.client.core.msg.Response)2 CoreContext (com.couchbase.client.core.CoreContext)1 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 KeyValueErrorMapCodeHandledEvent (com.couchbase.client.core.cnc.events.io.KeyValueErrorMapCodeHandledEvent)1 UnknownResponseStatusReceivedEvent (com.couchbase.client.core.cnc.events.io.UnknownResponseStatusReceivedEvent)1 UnsupportedResponseTypeReceivedEvent (com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent)1 ByteBufAllocator (com.couchbase.client.core.deps.io.netty.buffer.ByteBufAllocator)1 ByteBufUtil (com.couchbase.client.core.deps.io.netty.buffer.ByteBufUtil)1 FullHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse)1 HttpResponseStatus (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus)1 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)1 HttpStatusCodeException (com.couchbase.client.core.error.HttpStatusCodeException)1