Search in sources :

Example 56 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class SaslAuthenticationHandler method buildAuthRequest.

/**
 * Helper method to build the SASL auth request.
 *
 * @param ctx the channel context.
 * @return the created auth request.
 * @throws SaslException if something went wrong during challenge evaluation.
 */
private ByteBuf buildAuthRequest(final ChannelHandlerContext ctx) throws SaslException {
    byte[] payload = saslClient.hasInitialResponse() ? saslClient.evaluateChallenge(Bytes.EMPTY_BYTE_ARRAY) : null;
    ByteBuf body = payload != null ? ctx.alloc().buffer().writeBytes(payload) : Unpooled.EMPTY_BUFFER;
    ByteBuf key = Unpooled.copiedBuffer(saslClient.getMechanismName(), UTF_8);
    ByteBuf request = request(ctx.alloc(), MemcacheProtocol.Opcode.SASL_AUTH, noDatatype(), noPartition(), BaseKeyValueRequest.nextOpaque(), noCas(), noExtras(), key, body);
    key.release();
    body.release();
    return request;
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 57 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class SaslAuthenticationHandler method buildStepRequest.

/**
 * Helper method to build the SASL step request based on the evaluated challenge.
 *
 * @param ctx the channel context
 * @param evaluatedBytes the evaluated challenge.
 * @return the full SASL step request.
 */
private ByteBuf buildStepRequest(final ChannelHandlerContext ctx, final byte[] evaluatedBytes) {
    final String mech = saslClient.getMechanismName();
    ByteBuf body;
    if (mech.equalsIgnoreCase(SaslMechanism.PLAIN.mech())) {
        String[] evaluated = new String(evaluatedBytes, UTF_8).split(" ");
        body = Unpooled.copiedBuffer(username + "\0" + evaluated[1], UTF_8);
    } else {
        body = Unpooled.wrappedBuffer(evaluatedBytes);
    }
    ByteBuf key = Unpooled.copiedBuffer(mech, UTF_8);
    ByteBuf request = request(ctx.alloc(), MemcacheProtocol.Opcode.SASL_STEP, noDatatype(), noPartition(), BaseKeyValueRequest.nextOpaque(), noCas(), noExtras(), key, body);
    key.release();
    body.release();
    return request;
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 58 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class SaslListMechanismsHandler method channelRead.

/**
 * As soon as we get a response, turn it into a list of SASL mechanisms the server supports.
 * <p>
 * If the server responds with an empty list this is an issue and as a result we need to fail the connection
 * immediately.
 *
 * @param ctx the {@link ChannelHandlerContext} for which the channel read operation is made.
 * @param msg the incoming msg that needs to be parsed.
 */
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    if (msg instanceof ByteBuf) {
        Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
        if (successful((ByteBuf) msg)) {
            String[] rawMechansisms = body((ByteBuf) msg).orElse(Unpooled.EMPTY_BUFFER).toString(UTF_8).split(" ");
            boolean isEmpty = rawMechansisms.length == 1 && rawMechansisms[0].isEmpty();
            if (rawMechansisms.length > 0 && !isEmpty) {
                final Set<SaslMechanism> serverMechanisms = decodeMechanisms(rawMechansisms);
                ioContext.environment().eventBus().publish(new SaslMechanismsListedEvent(ioContext, serverMechanisms, latency.orElse(Duration.ZERO)));
                ctx.channel().attr(ChannelAttributes.SASL_MECHS_KEY).set(serverMechanisms);
                interceptedConnectPromise.trySuccess();
                ctx.pipeline().remove(this);
            } else {
                failConnection("Received empty mechanism list from server", status((ByteBuf) msg), latency);
            }
        } else {
            failConnection("Received non-success status from server", status((ByteBuf) msg), latency);
        }
    } else {
        interceptedConnectPromise.tryFailure(new CouchbaseException("Unexpected response " + "type on channel read, this is a bug - please report. " + msg));
    }
    ReferenceCountUtil.release(msg);
}
Also used : SaslMechanismsListedEvent(com.couchbase.client.core.cnc.events.io.SaslMechanismsListedEvent) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) SaslMechanism(com.couchbase.client.core.env.SaslMechanism) Duration(java.time.Duration) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 59 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class AnalyticsRequest method encode.

@Override
public FullHttpRequest encode() {
    ByteBuf content = Unpooled.wrappedBuffer(query);
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, URI, content);
    request.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
    request.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
    request.headers().set(HttpHeaderNames.USER_AGENT, context().environment().userAgent().formattedLong());
    if (priority != NO_PRIORITY) {
        request.headers().set("Analytics-Priority", priority);
    }
    authenticator.authHttpRequest(serviceType(), request);
    return request;
}
Also used : FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 60 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class AppendRequest method encode.

@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
    ByteBuf key = null;
    ByteBuf content = null;
    ByteBuf flexibleExtras = mutationFlexibleExtras(this, ctx, alloc, syncReplicationType);
    try {
        key = encodedKeyWithCollection(alloc, ctx);
        byte datatype = 0;
        CompressionConfig config = ctx.compressionConfig();
        if (config != null && config.enabled() && this.content.length >= config.minSize()) {
            ByteBuf maybeCompressed = MemcacheProtocol.tryCompression(this.content, config.minRatio());
            if (maybeCompressed != null) {
                datatype |= MemcacheProtocol.Datatype.SNAPPY.datatype();
                content = maybeCompressed;
            } else {
                content = Unpooled.wrappedBuffer(this.content);
            }
        } else {
            content = Unpooled.wrappedBuffer(this.content);
        }
        return MemcacheProtocol.flexibleRequest(alloc, MemcacheProtocol.Opcode.APPEND, datatype, partition(), opaque, cas, flexibleExtras, noExtras(), key, content);
    } finally {
        ReferenceCountUtil.release(key);
        ReferenceCountUtil.release(content);
        ReferenceCountUtil.release(flexibleExtras);
    }
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) CompressionConfig(com.couchbase.client.core.env.CompressionConfig)

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