Search in sources :

Example 51 with ByteBuf

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));
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 52 with ByteBuf

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());
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 53 with ByteBuf

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);
}
Also used : SelectBucketFailedEvent(com.couchbase.client.core.cnc.events.io.SelectBucketFailedEvent) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) KeyValueIoErrorContext(com.couchbase.client.core.error.context.KeyValueIoErrorContext) SelectBucketCompletedEvent(com.couchbase.client.core.cnc.events.io.SelectBucketCompletedEvent) Duration(java.time.Duration) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 54 with ByteBuf

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);
}
Also used : SaslAuthenticationCompletedEvent(com.couchbase.client.core.cnc.events.io.SaslAuthenticationCompletedEvent) Duration(java.time.Duration) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) SaslException(javax.security.sasl.SaslException)

Example 55 with ByteBuf

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));
    }
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) SaslException(javax.security.sasl.SaslException)

Aggregations

ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)107 Test (org.junit.jupiter.api.Test)54 InetSocketAddress (java.net.InetSocketAddress)17 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)12 ChannelFuture (com.couchbase.client.core.deps.io.netty.channel.ChannelFuture)11 Duration (java.time.Duration)10 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)9 CoreContext (com.couchbase.client.core.CoreContext)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 CompressionConfig (com.couchbase.client.core.env.CompressionConfig)6 ResponseStatus (com.couchbase.client.core.msg.ResponseStatus)6 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)6 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)5 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)5 ArrayList (java.util.ArrayList)5 DurabilityTimeoutCoercedEvent (com.couchbase.client.core.cnc.events.io.DurabilityTimeoutCoercedEvent)4 FeaturesNegotiatedEvent (com.couchbase.client.core.cnc.events.io.FeaturesNegotiatedEvent)4 CompositeByteBuf (com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf)4 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)4 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)4