Example usage for io.netty.buffer Unpooled EMPTY_BUFFER

List of usage examples for io.netty.buffer Unpooled EMPTY_BUFFER

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled EMPTY_BUFFER.

Prototype

ByteBuf EMPTY_BUFFER

To view the source code for io.netty.buffer Unpooled EMPTY_BUFFER.

Click Source Link

Document

A buffer whose capacity is 0 .

Usage

From source file:com.couchbase.client.core.endpoint.kv.KeyValueAuthHandler.java

License:Apache License

/**
 * Handles an incoming SASL AUTH response and - if needed - dispatches the SASL STEPs.
 *
 * @param ctx the handler context./*  w ww.j  a  v a  2  s . co  m*/
 * @param msg the incoming message to investigate.
 * @throws Exception if something goes wrong during negotiation.
 */
private void handleAuthResponse(ChannelHandlerContext ctx, FullBinaryMemcacheResponse msg) throws Exception {
    if (saslClient.isComplete()) {
        checkIsAuthed(msg);
        return;
    }

    byte[] response = new byte[msg.content().readableBytes()];
    msg.content().readBytes(response);
    byte[] evaluatedBytes = saslClient.evaluateChallenge(response);

    if (evaluatedBytes != null) {
        ByteBuf content;

        // This is needed against older server versions where the protocol does not
        // align on cram and plain, the else block is used for all the newer cram-sha*
        // mechanisms.
        //
        // Note that most likely this is only executed in the CRAM-MD5 case only, but
        // just to play it safe keep it for both mechanisms.
        if (selectedMechanism.equals("CRAM-MD5") || selectedMechanism.equals("PLAIN")) {
            String[] evaluated = new String(evaluatedBytes).split(" ");
            content = Unpooled.copiedBuffer(username + "\0" + evaluated[1], CharsetUtil.UTF_8);
        } else {
            content = Unpooled.wrappedBuffer(evaluatedBytes);
        }

        FullBinaryMemcacheRequest stepRequest = new DefaultFullBinaryMemcacheRequest(
                selectedMechanism.getBytes(CharsetUtil.UTF_8), Unpooled.EMPTY_BUFFER, content);
        stepRequest.setOpcode(SASL_STEP_OPCODE).setKeyLength((short) selectedMechanism.length())
                .setTotalBodyLength(content.readableBytes() + selectedMechanism.length());

        ChannelFuture future = ctx.writeAndFlush(stepRequest);
        future.addListener(new GenericFutureListener<Future<Void>>() {
            @Override
            public void operationComplete(Future<Void> future) throws Exception {
                if (!future.isSuccess()) {
                    LOGGER.warn("Error during SASL Auth negotiation phase.", future);
                }
            }
        });
    } else {
        throw new AuthenticationException("SASL Challenge evaluation returned null.");
    }
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueFeatureHandler.java

License:Apache License

/**
 * Creates the HELLO request to ask for certain supported features.
 *
 * @return the request to send over the wire
 *//*from w w  w  . ja v  a2  s .c  o m*/
private FullBinaryMemcacheRequest helloRequest() {
    byte[] key = userAgent.getBytes(CharsetUtil.UTF_8);
    short keyLength = (short) key.length;

    ByteBuf wanted = Unpooled.buffer(features.size() * 2);
    for (ServerFeatures feature : features) {
        wanted.writeShort(feature.value());
    }

    LOGGER.debug("Requesting supported features: {}", features);
    FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, Unpooled.EMPTY_BUFFER,
            wanted);
    request.setOpcode(HELLO_CMD);
    request.setKeyLength(keyLength);
    request.setTotalBodyLength(keyLength + wanted.readableBytes());
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

/**
 * Encodes a {@link GetRequest} into its lower level representation.
 *
 * Depending on the flags set on the {@link GetRequest}, the appropriate opcode gets chosen. Currently, a regular
 * get, as well as "get and touch" and "get and lock" are supported. Latter variants have server-side side-effects
 * but do not differ in response behavior.
 *
 * @param ctx the {@link ChannelHandlerContext} to use for allocation and others.
 * @param msg the incoming message./*  w  ww .j  a  v a  2  s .  co  m*/
 * @return a ready {@link BinaryMemcacheRequest}.
 */
private static BinaryMemcacheRequest handleGetRequest(final ChannelHandlerContext ctx, final GetRequest msg) {
    byte opcode;
    ByteBuf extras;
    if (msg.lock()) {
        opcode = OP_GET_AND_LOCK;
        extras = ctx.alloc().buffer().writeInt(msg.expiry());
    } else if (msg.touch()) {
        opcode = OP_GET_AND_TOUCH;
        extras = ctx.alloc().buffer().writeInt(msg.expiry());
    } else {
        opcode = OP_GET;
        extras = Unpooled.EMPTY_BUFFER;
    }

    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;
    byte extrasLength = (byte) extras.readableBytes();
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key);
    request.setOpcode(opcode).setKeyLength(keyLength).setExtras(extras).setExtrasLength(extrasLength)
            .setTotalBodyLength(keyLength + extrasLength);
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

/**
 * Encodes a {@link ObserveRequest} into its lower level representation.
 *
 * @return a ready {@link BinaryMemcacheRequest}.
 *//*from  ww  w  . j  a v a2s.  co m*/
private static BinaryMemcacheRequest handleObserveRequest(final ChannelHandlerContext ctx,
        final ObserveRequest msg) {
    String key = msg.key();
    short keyLength = (short) msg.keyBytes().length;
    ByteBuf content = ctx.alloc().buffer();
    content.writeShort(msg.partition());
    content.writeShort(keyLength);
    content.writeBytes(key.getBytes(CHARSET));

    BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(EMPTY_BYTES, Unpooled.EMPTY_BUFFER,
            content);
    request.setOpcode(OP_OBSERVE);
    request.setTotalBodyLength(content.readableBytes());
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

private static BinaryMemcacheRequest handleObserveSeqnoRequest(final ChannelHandlerContext ctx,
        final ObserveSeqnoRequest msg) {
    ByteBuf content = ctx.alloc().buffer();
    content.writeLong(msg.vbucketUUID());

    BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(EMPTY_BYTES, Unpooled.EMPTY_BUFFER,
            content);//from  w w w .  j ava 2  s .c om
    request.setOpcode(OP_OBSERVE_SEQ);
    request.setTotalBodyLength(content.readableBytes());
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

private static BinaryMemcacheRequest handleAppendRequest(final AppendRequest msg) {
    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;
    BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, Unpooled.EMPTY_BUFFER,
            msg.content());//from w  w w  .j  ava  2 s  . co m

    request.setOpcode(OP_APPEND);
    request.setKeyLength(keyLength);
    request.setCAS(msg.cas());
    request.setTotalBodyLength(keyLength + msg.content().readableBytes());
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

private static BinaryMemcacheRequest handlePrependRequest(final PrependRequest msg) {
    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;
    BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, Unpooled.EMPTY_BUFFER,
            msg.content());/*w  w  w  . jav  a  2s.co m*/

    request.setOpcode(OP_PREPEND);
    request.setKeyLength(keyLength);
    request.setCAS(msg.cas());
    request.setTotalBodyLength(keyLength + msg.content().readableBytes());
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

/**
 * Encodes a {@link KeepAliveRequest} request into a NOOP operation.
 *
 * @param msg the {@link KeepAliveRequest} triggering the NOOP.
 * @return a ready {@link BinaryMemcacheRequest}.
 *///from  w  ww.  j a  va2  s.c  o m
private static BinaryMemcacheRequest handleKeepAliveRequest(KeepAliveRequest msg) {
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest();
    request.setOpcode(OP_NOOP).setKeyLength((short) 0).setExtras(Unpooled.EMPTY_BUFFER)
            .setExtrasLength((byte) 0).setTotalBodyLength(0);
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

private static BinaryMemcacheRequest handleGetAllMutationTokensRequest(ChannelHandlerContext ctx,
        GetAllMutationTokensRequest msg) {
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(EMPTY_BYTES);

    ByteBuf extras;//from w w w .ja  va 2 s.  c om
    switch (msg.partitionState()) {
    case ANY:
        extras = Unpooled.EMPTY_BUFFER;
        break;
    case ACTIVE:
    case REPLICA:
    case PENDING:
    case DEAD:
    default:
        extras = ctx.alloc().buffer().writeInt(msg.partitionState().value());
    }
    byte extrasLength = (byte) extras.readableBytes();

    request.setOpcode(OP_GET_ALL_MUTATION_TOKENS).setExtras(extras).setExtrasLength(extrasLength)
            .setTotalBodyLength(extrasLength);
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

private static BinaryMemcacheRequest handleSubdocumentMultiLookupRequest(ChannelHandlerContext ctx,
        BinarySubdocMultiLookupRequest msg) {
    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;

    FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, Unpooled.EMPTY_BUFFER,
            msg.content());//from w  w  w.  j  av  a2 s. c  om
    request.setOpcode(OP_SUB_MULTI_LOOKUP).setKeyLength(keyLength).setExtrasLength((byte) 0)
            .setTotalBodyLength(keyLength + msg.content().readableBytes());

    return request;
}