use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class MultiObserveViaCasRequest method decode.
@Override
public MultiObserveViaCasResponse decode(final ByteBuf response, final KeyValueChannelContext ctx) {
final ResponseStatus status = decodeStatus(response);
final Map<byte[], ObserveViaCasResponse.ObserveStatus> observed = new HashMap<>();
if (status.success()) {
Optional<ByteBuf> maybeContent = body(response);
if (maybeContent.isPresent()) {
ByteBuf content = maybeContent.get();
while (content.isReadable()) {
// skip the vbid
content.skipBytes(Short.BYTES);
short keyLength = content.readShort();
if (ctx.collectionsEnabled()) {
int skipped = UnsignedLEB128.skip(content);
keyLength = (short) (keyLength - skipped);
}
byte[] keyEncoded = new byte[keyLength];
content.readBytes(keyEncoded, 0, keyLength);
byte obs = content.readByte();
// skip reading the cas
content.skipBytes(Long.BYTES);
ObserveViaCasResponse.ObserveStatus decoded = ObserveViaCasResponse.ObserveStatus.valueOf(obs);
if (responsePredicate.test(decoded)) {
observed.put(keyEncoded, decoded);
}
}
}
}
return new MultiObserveViaCasResponse(this, status, observed);
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class GetResult method convertContentToString.
/**
* Converts the content to a string representation if possible (for toString).
*/
protected String convertContentToString() {
if (content.length == 0) {
return "";
}
boolean printable = CodecFlags.hasCommonFormat(flags, CodecFlags.JSON_COMMON_FLAGS) || CodecFlags.hasCommonFormat(flags, CodecFlags.STRING_COMMON_FLAGS) || (flags == 0 && content[0] == '{');
if (printable) {
return new String(content, StandardCharsets.UTF_8);
} else {
ByteBuf buf = Unpooled.wrappedBuffer(content);
String result = ByteBufUtil.prettyHexDump(buf);
buf.release();
return "\n" + result + "\n";
}
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class MutateInUtil method convertDocsToBytes.
/**
* Converts a list of objects (subdoc partials) into their low-level representation.
* <p>
* Per KV protocol each individual value is separated by a ",", which is not valid JSON on
* the wire but the server will split it apart and apply each one of them individually.
*
* @param docs the docs to serialize.
* @param serializer the serializer to use for JSON.
* @return the serialized and converted byte array.
*/
static byte[] convertDocsToBytes(final List<?> docs, final JsonSerializer serializer) {
if (docs.size() == 1) {
return serializer.serialize(docs.get(0));
} else {
final ByteBuf bytes = UnpooledByteBufAllocator.DEFAULT.heapBuffer();
try {
final Iterator<?> it = docs.iterator();
while (it.hasNext()) {
Object d = it.next();
bytes.writeBytes(serializer.serialize(d));
if (it.hasNext()) {
bytes.writeByte(',');
}
}
return ByteBufUtil.getBytes(bytes);
} finally {
bytes.release();
}
}
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class KeyValueMessageHandler method handleNotMyVbucket.
/**
* Helper method to handle a "not my vbucket" response.
*
* @param request the request to retry.
* @param response the response to extract the config from, potentially.
*/
private void handleNotMyVbucket(final KeyValueRequest<Response> request, final ByteBuf response) {
request.indicateRejectedWithNotMyVbucket();
eventBus.publish(new NotMyVbucketReceivedEvent(ioContext, request.partition()));
final String origin = request.context().lastDispatchedTo() != null ? request.context().lastDispatchedTo().hostname() : null;
RetryOrchestrator.maybeRetry(ioContext, request, RetryReason.KV_NOT_MY_VBUCKET);
body(response).map(b -> b.toString(UTF_8).trim()).filter(c -> c.startsWith("{")).ifPresent(c -> ioContext.core().configurationProvider().proposeBucketConfig(new ProposedBucketConfigContext(request.bucket(), c, origin)));
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class SaslAuthenticationHandler method failConnect.
/**
* Refactored method which is called from many places to fail the connection
* process because of an issue during SASL auth.
*
* <p>Usually errors during auth are very problematic and as a result we cannot continue
* with this channel connect attempt.</p>
*
* @param ctx the channel context.
*/
private void failConnect(final ChannelHandlerContext ctx, final String message, final ByteBuf lastPacket, final Throwable cause, final short status) {
Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
byte[] packetCopy = Bytes.EMPTY_BYTE_ARRAY;
Map<String, Object> serverContext = null;
if (lastPacket != null) {
if (MemcacheProtocol.verifyResponse(lastPacket)) {
// This is a proper response, try to extract server context
Optional<ByteBuf> body = MemcacheProtocol.body(lastPacket);
if (body.isPresent()) {
byte[] content = ByteBufUtil.getBytes(body.get());
try {
serverContext = Mapper.decodeInto(content, new TypeReference<Map<String, Object>>() {
});
} catch (Exception ex) {
// Ignore, no displayable content
}
}
} else {
// This is not a proper memcache response, store the raw packet for debugging purposes
int ridx = lastPacket.readerIndex();
lastPacket.readerIndex(lastPacket.writerIndex());
packetCopy = new byte[lastPacket.readableBytes()];
lastPacket.readBytes(packetCopy);
lastPacket.readerIndex(ridx);
}
}
KeyValueIoErrorContext errorContext = new KeyValueIoErrorContext(MemcacheProtocol.decodeStatus(status), endpointContext, serverContext);
endpointContext.environment().eventBus().publish(new SaslAuthenticationFailedEvent(latency.orElse(Duration.ZERO), errorContext, message, packetCopy));
interceptedConnectPromise.tryFailure(new AuthenticationFailureException(message, errorContext, cause));
ctx.pipeline().remove(this);
}
Aggregations