use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class DecompressionTest method decompressesGet.
@Test
void decompressesGet() {
ByteBuf response = decodeHexDump(readResource("compressed_get_response.txt", DecompressionTest.class));
GetRequest request = new GetRequest("mydoc", Duration.ofSeconds(1), mock(CoreContext.class), CollectionIdentifier.fromDefault("bucket"), BestEffortRetryStrategy.INSTANCE, null);
GetResponse decoded = request.decode(response, null);
assertEquals(readResource("dummy.json", DecompressionTest.class), new String(decoded.content(), UTF_8));
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class UnsignedLEB128Test method readerIndexAdvancedOnSuccess.
@Test
void readerIndexAdvancedOnSuccess() {
ByteBuf buffer = Unpooled.buffer().writeByte(0x00);
assertEquals(0, UnsignedLEB128.read(buffer));
assertEquals(1, buffer.readerIndex());
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class SelectBucketHandler method channelRead.
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
if (msg instanceof ByteBuf) {
short status = status((ByteBuf) msg);
if (status == MemcacheProtocol.Status.SUCCESS.status()) {
endpointContext.environment().eventBus().publish(new SelectBucketCompletedEvent(latency.orElse(Duration.ZERO), ioContext, bucketName));
interceptedConnectPromise.trySuccess();
ctx.pipeline().remove(this);
ctx.fireChannelActive();
} else if (status == MemcacheProtocol.Status.ACCESS_ERROR.status()) {
// If the promise has been already completed with an error from a downstream handler, there is no
// need to WARN because it will just spam and reduce the visibility of the actual source error.
Event.Severity severity = interceptedConnectPromise.isDone() && !interceptedConnectPromise.isSuccess() ? Event.Severity.DEBUG : Event.Severity.WARN;
endpointContext.environment().eventBus().publish(new SelectBucketFailedEvent(severity, ioContext, status));
interceptedConnectPromise.tryFailure(new AuthenticationFailureException("Either the bucket with name \"" + redactMeta(bucketName) + "\" is not present or the user does not have " + "the right privileges to access it", new KeyValueIoErrorContext(MemcacheProtocol.decodeStatus(status), endpointContext, null), null));
} else if (status == MemcacheProtocol.Status.NOT_FOUND.status()) {
// NOT_FOUND severity is lowered to debug because it shows up when bootstrapping against
// a node in a MDS setup without the kv service / bucket enabled. If the bucket is legit not
// present, we'd get the access error from above (which is an error).
endpointContext.environment().eventBus().publish(new SelectBucketFailedEvent(Event.Severity.DEBUG, ioContext, status));
interceptedConnectPromise.tryFailure(BucketNotFoundException.forBucket(bucketName));
} else {
endpointContext.environment().eventBus().publish(new SelectBucketFailedEvent(Event.Severity.ERROR, ioContext, status));
interceptedConnectPromise.tryFailure(new CouchbaseException("Select bucket failed with unexpected status code 0x" + Integer.toHexString(status)));
}
} else {
interceptedConnectPromise.tryFailure(new CouchbaseException("Unexpected response type on channel read, this is a bug " + "- please report." + msg));
}
ReferenceCountUtil.release(msg);
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class SaslAuthenticationHandler method completeAuth.
/**
* Helper method to complete the SASL auth successfully.
*/
private void completeAuth(final ChannelHandlerContext ctx, ByteBuf msg) throws SaslException {
if (!saslClient.isComplete()) {
// validate final server response
ByteBuf responseBody = body(msg).orElse(Unpooled.EMPTY_BUFFER);
byte[] payload = ByteBufUtil.getBytes(responseBody);
saslClient.evaluateChallenge(payload);
if (!saslClient.isComplete()) {
throw new SaslException("Incomplete SASL exchange");
}
}
Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
endpointContext.environment().eventBus().publish(new SaslAuthenticationCompletedEvent(latency.orElse(Duration.ZERO), ioContext));
interceptedConnectPromise.trySuccess();
ctx.pipeline().remove(this);
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class SaslAuthenticationHandler method handleAuthResponse.
/**
* Handle a SASL AUTH response and start the next SASL step.
*
* @param ctx the channel context.
* @param response the response from the server from our AUTH request.
*/
private void handleAuthResponse(final ChannelHandlerContext ctx, final ByteBuf response) throws SaslException {
if (saslClient.isComplete()) {
completeAuth(ctx, response);
return;
}
ByteBuf responseBody = body(response).orElse(Unpooled.EMPTY_BUFFER);
byte[] payload = ByteBufUtil.getBytes(responseBody);
try {
byte[] evaluatedBytes = saslClient.evaluateChallenge(payload);
if (evaluatedBytes != null && evaluatedBytes.length > 0) {
ctx.writeAndFlush(buildStepRequest(ctx, evaluatedBytes));
maybePropagateChannelActive(ctx);
} else {
throw new SaslException("Evaluation returned empty payload, this is unexpected!");
}
} catch (SaslException e) {
failConnect(ctx, "Failure while evaluating SASL Auth Response.", response, e, MemcacheProtocol.status(response));
}
}
Aggregations