Example usage for io.netty.util CharsetUtil UTF_8

List of usage examples for io.netty.util CharsetUtil UTF_8

Introduction

In this page you can find the example usage for io.netty.util CharsetUtil UTF_8.

Prototype

Charset UTF_8

To view the source code for io.netty.util CharsetUtil UTF_8.

Click Source Link

Document

8-bit UTF (UCS Transformation Format)

Usage

From source file:com.couchbase.client.core.endpoint.binary.BinaryAuthHandler.java

License:Open Source License

/**
 * Handles an incoming SASL AUTH response and - if needed - dispatches the SASL STEPs.
 *
 * @param ctx the handler context./*  w w w  .  jav  a 2 s .com*/
 * @param msg the incoming message to investigate.
 * @throws Exception
 */
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) {
        String[] evaluated = new String(evaluatedBytes).split(" ");
        ByteBuf content = Unpooled.copiedBuffer(username + "\0" + evaluated[1], CharsetUtil.UTF_8);

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

        ctx.writeAndFlush(stepRequest);
    } else {
        throw new AuthenticationException("SASL Challenge evaluation returned null.");
    }
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryCodec.java

License:Open Source License

@Override
protected void decode(final ChannelHandlerContext ctx, final FullBinaryMemcacheResponse msg,
        final List<Object> in) throws Exception {
    Class<?> clazz = queue.poll();

    ResponseStatus status = convertStatus(msg.getStatus());
    long cas = msg.getCAS();
    if (clazz.equals(GetBucketConfigRequest.class)) {
        InetSocketAddress addr = (InetSocketAddress) ctx.channel().remoteAddress();
        in.add(new GetBucketConfigResponse(convertStatus(msg.getStatus()),
                msg.content().toString(CharsetUtil.UTF_8), addr.getHostName()));
    } else if (clazz.equals(GetRequest.class)) {
        in.add(new GetResponse(status, cas, msg.content().copy()));
    } else if (clazz.equals(InsertRequest.class)) {
        in.add(new InsertResponse(status, cas));
    } else if (clazz.equals(UpsertRequest.class)) {
        in.add(new UpsertResponse(status, cas));
    } else if (clazz.equals(ReplaceRequest.class)) {
        in.add(new ReplaceResponse(status, cas));
    } else if (clazz.equals(RemoveRequest.class)) {
        in.add(new RemoveResponse(convertStatus(msg.getStatus())));
    } else {//from  w  w w . j a va2 s .  c  o  m
        throw new IllegalStateException("Got a response message for a request that was not sent." + msg);
    }
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldEncodeInsertRequest() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);

    InsertRequest request = new InsertRequest("key", content.copy(), "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);//from   www.j a v a 2s . c  o  m
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals(0, outbound.getCAS());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new InsertRequest("key", content.copy(), 10, 0, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(10, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new InsertRequest("key", content.copy(), 0, 5, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(5, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new InsertRequest("key", content.copy(), 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(99, outbound.getExtras().readInt());
    assertEquals(30, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldEncodeUpsertRequest() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);

    UpsertRequest request = new UpsertRequest("key", content.copy(), "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);//from   w  w  w  .  j  a  v a  2 s .co m
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals(0, outbound.getCAS());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new UpsertRequest("key", content.copy(), 10, 0, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(10, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new UpsertRequest("key", content.copy(), 0, 5, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(5, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new UpsertRequest("key", content.copy(), 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(99, outbound.getExtras().readInt());
    assertEquals(30, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldEncodeReplaceRequest() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);

    ReplaceRequest request = new ReplaceRequest("key", content.copy(), "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);/*w w  w  .ja  v a 2s .com*/
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals(0, outbound.getCAS());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new ReplaceRequest("key", content.copy(), 0, 10, 0, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(10, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new ReplaceRequest("key", content.copy(), 0, 0, 5, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(5, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new ReplaceRequest("key", content.copy(), 0, 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(99, outbound.getExtras().readInt());
    assertEquals(30, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldEncodeReplaceWithCASRequest() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    ReplaceRequest request = new ReplaceRequest("key", content.copy(), 4234234234L, 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);/* w ww . j a  v a  2 s . c o  m*/
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertEquals(4234234234L, outbound.getCAS());
    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldEncodeObserveRequest() {
    ObserveRequest request = new ObserveRequest("key", 234234234L, true, (short) 0, "bucket");
    request.partition((short) 1);

    channel.writeOutbound(request);/*  w w  w . ja  v a  2s . com*/
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals(0, outbound.getKeyLength());
    assertEquals(0, outbound.getExtrasLength());
    assertEquals(7, outbound.getTotalBodyLength());
    assertEquals(BinaryHandler.OP_OBSERVE, outbound.getOpcode());
    assertEquals(1, outbound.content().readShort());
    assertEquals("key".length(), outbound.content().readShort());
    assertEquals("key",
            outbound.content().readBytes(outbound.content().readableBytes()).toString(CharsetUtil.UTF_8));
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldDecodeGetSuccessResponse() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse("key", Unpooled.EMPTY_BUFFER,
            content.copy());//w  ww. jav  a 2s.c  o m
    response.setCAS(123456789L);
    response.setExtras(Unpooled.buffer().writeInt(123));
    response.setExtrasLength((byte) 4);

    GetRequest requestMock = mock(GetRequest.class);
    when(requestMock.bucket()).thenReturn("bucket");
    queue.add(requestMock);
    channel.writeInbound(response);
    channel.readInbound();

    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GetResponse inbound = (GetResponse) firedEvents.get(0);
    assertEquals(ResponseStatus.SUCCESS, inbound.status());
    assertEquals("bucket", inbound.bucket());
    assertEquals(123456789L, inbound.cas());
    assertEquals(123, inbound.flags());
    assertEquals("content", inbound.content().toString(CharsetUtil.UTF_8));
    assertTrue(queue.isEmpty());

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldDecodeGetNotFoundResponse() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("Not Found", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse("key", Unpooled.EMPTY_BUFFER,
            content.copy());// ww w.  j a va 2 s. co m
    response.setStatus(BinaryMemcacheResponseStatus.KEY_ENOENT);

    GetRequest requestMock = mock(GetRequest.class);
    when(requestMock.bucket()).thenReturn("bucket");
    queue.add(requestMock);
    channel.writeInbound(response);
    channel.readInbound();

    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GetResponse inbound = (GetResponse) firedEvents.get(0);
    assertEquals(ResponseStatus.NOT_EXISTS, inbound.status());
    assertEquals("bucket", inbound.bucket());
    assertEquals(0, inbound.cas());
    assertEquals(0, inbound.flags());
    assertEquals("Not Found", inbound.content().toString(CharsetUtil.UTF_8));
    assertTrue(queue.isEmpty());

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldDecodeReplicaGetResponse() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse("key", Unpooled.EMPTY_BUFFER,
            content.copy());//  ww w  . j a v a 2s  .  co m
    response.setCAS(123456789L);
    response.setExtras(Unpooled.buffer().writeInt(123));
    response.setExtrasLength((byte) 4);

    ReplicaGetRequest requestMock = mock(ReplicaGetRequest.class);
    when(requestMock.bucket()).thenReturn("bucket");
    queue.add(requestMock);
    channel.writeInbound(response);
    channel.readInbound();

    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GetResponse inbound = (GetResponse) firedEvents.get(0);
    assertEquals(ResponseStatus.SUCCESS, inbound.status());
    assertEquals("bucket", inbound.bucket());
    assertEquals(123456789L, inbound.cas());
    assertEquals(123, inbound.flags());
    assertEquals("content", inbound.content().toString(CharsetUtil.UTF_8));
    assertTrue(queue.isEmpty());

    ReferenceCountUtil.release(content);
}