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