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.node.locate.KeyValueLocatorTest.java

License:Apache License

@Test
public void shouldLocateGetRequestForCouchbaseBucket() throws Exception {
    Locator locator = new KeyValueLocator();

    NodeInfo nodeInfo1 = new DefaultNodeInfo("foo", "192.168.56.101:11210", Collections.EMPTY_MAP);
    NodeInfo nodeInfo2 = new DefaultNodeInfo("foo", "192.168.56.102:11210", Collections.EMPTY_MAP);

    GetRequest getRequestMock = mock(GetRequest.class);
    ClusterConfig configMock = mock(ClusterConfig.class);
    List<Node> nodes = new ArrayList<Node>();
    Node node1Mock = mock(Node.class);
    when(node1Mock.hostname()).thenReturn(InetAddress.getByName("192.168.56.101"));
    Node node2Mock = mock(Node.class);
    when(node2Mock.hostname()).thenReturn(InetAddress.getByName("192.168.56.102"));
    nodes.addAll(Arrays.asList(node1Mock, node2Mock));
    CouchbaseBucketConfig bucketMock = mock(CouchbaseBucketConfig.class);
    when(getRequestMock.bucket()).thenReturn("bucket");
    when(getRequestMock.key()).thenReturn("key");
    when(getRequestMock.keyBytes()).thenReturn("key".getBytes(CharsetUtil.UTF_8));
    when(configMock.bucketConfig("bucket")).thenReturn(bucketMock);
    when(bucketMock.nodes()).thenReturn(Arrays.asList(nodeInfo1, nodeInfo2));
    when(bucketMock.numberOfPartitions()).thenReturn(1024);
    when(bucketMock.nodeIndexForMaster(656, false)).thenReturn((short) 0);
    when(bucketMock.nodeAtIndex(0)).thenReturn(nodeInfo1);

    locator.locateAndDispatch(getRequestMock, nodes, configMock, null, null);
    verify(node1Mock, times(1)).send(getRequestMock);
    verify(node2Mock, never()).send(getRequestMock);
}

From source file:com.couchbase.client.core.node.locate.KeyValueLocatorTest.java

License:Apache License

@Test
public void shouldPickFastForwardIfAvailableAndRetry() throws Exception {
    Locator locator = new KeyValueLocator();

    // Setup 2 nodes
    NodeInfo nodeInfo1 = new DefaultNodeInfo("foo", "192.168.56.101:11210", Collections.EMPTY_MAP);
    NodeInfo nodeInfo2 = new DefaultNodeInfo("foo", "192.168.56.102:11210", Collections.EMPTY_MAP);
    List<Node> nodes = new ArrayList<Node>();
    Node node1Mock = mock(Node.class);
    when(node1Mock.hostname()).thenReturn(InetAddress.getByName("192.168.56.101"));
    Node node2Mock = mock(Node.class);
    when(node2Mock.hostname()).thenReturn(InetAddress.getByName("192.168.56.102"));
    nodes.addAll(Arrays.asList(node1Mock, node2Mock));

    // Configure Cluster and Bucket config
    ClusterConfig configMock = mock(ClusterConfig.class);
    CouchbaseBucketConfig bucketMock = mock(CouchbaseBucketConfig.class);
    when(configMock.bucketConfig("bucket")).thenReturn(bucketMock);
    when(bucketMock.nodes()).thenReturn(Arrays.asList(nodeInfo1, nodeInfo2));
    when(bucketMock.numberOfPartitions()).thenReturn(1024);
    when(bucketMock.nodeAtIndex(0)).thenReturn(nodeInfo1);
    when(bucketMock.nodeAtIndex(1)).thenReturn(nodeInfo2);
    when(bucketMock.hasFastForwardMap()).thenReturn(true);

    // Fake a vbucket move in ffwd map from node 0 to node 1
    when(bucketMock.nodeIndexForMaster(656, false)).thenReturn((short) 0);
    when(bucketMock.nodeIndexForMaster(656, true)).thenReturn((short) 1);

    // Create Request
    GetRequest getRequestMock = mock(GetRequest.class);
    when(getRequestMock.bucket()).thenReturn("bucket");
    when(getRequestMock.key()).thenReturn("key");
    when(getRequestMock.keyBytes()).thenReturn("key".getBytes(CharsetUtil.UTF_8));

    // Dispatch with retry 0
    when(getRequestMock.retryCount()).thenReturn(0);
    locator.locateAndDispatch(getRequestMock, nodes, configMock, null, null);
    verify(node1Mock, times(1)).send(getRequestMock);
    verify(node2Mock, never()).send(getRequestMock);

    // Dispatch with retry 1
    when(getRequestMock.retryCount()).thenReturn(1);
    locator.locateAndDispatch(getRequestMock, nodes, configMock, null, null);
    verify(node1Mock, times(1)).send(getRequestMock);
    verify(node2Mock, times(1)).send(getRequestMock);

    // Dispatch with retry 5
    when(getRequestMock.retryCount()).thenReturn(5);
    locator.locateAndDispatch(getRequestMock, nodes, configMock, null, null);
    verify(node1Mock, times(1)).send(getRequestMock);
    verify(node2Mock, times(2)).send(getRequestMock);
}

From source file:com.couchbase.client.core.node.locate.KeyValueLocatorTest.java

License:Apache License

@Test
public void shouldPickCurrentIfNoFFMapAndRetry() throws Exception {
    Locator locator = new KeyValueLocator();

    // Setup 2 nodes
    NodeInfo nodeInfo1 = new DefaultNodeInfo("foo", "192.168.56.101:11210", Collections.EMPTY_MAP);
    NodeInfo nodeInfo2 = new DefaultNodeInfo("foo", "192.168.56.102:11210", Collections.EMPTY_MAP);
    List<Node> nodes = new ArrayList<Node>();
    Node node1Mock = mock(Node.class);
    when(node1Mock.hostname()).thenReturn(InetAddress.getByName("192.168.56.101"));
    Node node2Mock = mock(Node.class);
    when(node2Mock.hostname()).thenReturn(InetAddress.getByName("192.168.56.102"));
    nodes.addAll(Arrays.asList(node1Mock, node2Mock));

    // Configure Cluster and Bucket config
    ClusterConfig configMock = mock(ClusterConfig.class);
    CouchbaseBucketConfig bucketMock = mock(CouchbaseBucketConfig.class);
    when(configMock.bucketConfig("bucket")).thenReturn(bucketMock);
    when(bucketMock.nodes()).thenReturn(Arrays.asList(nodeInfo1, nodeInfo2));
    when(bucketMock.numberOfPartitions()).thenReturn(1024);
    when(bucketMock.nodeAtIndex(0)).thenReturn(nodeInfo1);
    when(bucketMock.nodeAtIndex(1)).thenReturn(nodeInfo2);
    when(bucketMock.hasFastForwardMap()).thenReturn(false);

    // Fake a vbucket move in ffwd map from node 0 to node 1
    when(bucketMock.nodeIndexForMaster(656, false)).thenReturn((short) 0);
    when(bucketMock.nodeIndexForMaster(656, true)).thenReturn((short) 1);

    // Create Request
    GetRequest getRequestMock = mock(GetRequest.class);
    when(getRequestMock.bucket()).thenReturn("bucket");
    when(getRequestMock.key()).thenReturn("key");
    when(getRequestMock.keyBytes()).thenReturn("key".getBytes(CharsetUtil.UTF_8));

    // Dispatch with retry 0
    when(getRequestMock.retryCount()).thenReturn(0);
    locator.locateAndDispatch(getRequestMock, nodes, configMock, null, null);
    verify(node1Mock, times(1)).send(getRequestMock);
    verify(node2Mock, never()).send(getRequestMock);

    // Dispatch with retry 1
    when(getRequestMock.retryCount()).thenReturn(1);
    locator.locateAndDispatch(getRequestMock, nodes, configMock, null, null);
    verify(node1Mock, times(2)).send(getRequestMock);
    verify(node2Mock, never()).send(getRequestMock);

    // Dispatch with retry 5
    when(getRequestMock.retryCount()).thenReturn(5);
    locator.locateAndDispatch(getRequestMock, nodes, configMock, null, null);
    verify(node1Mock, times(3)).send(getRequestMock);
    verify(node2Mock, never()).send(getRequestMock);
}

From source file:com.couchbase.client.core.ResponseHandler.java

License:Apache License

private void retry(final ResponseEvent event, final boolean isNotMyVbucket) {
    final CouchbaseMessage message = event.getMessage();
    if (message instanceof CouchbaseRequest) {
        scheduleForRetry((CouchbaseRequest) message, isNotMyVbucket);
    } else {//from w w w .  ja v  a 2s.  com

        CouchbaseRequest request = ((CouchbaseResponse) message).request();
        if (request != null) {
            scheduleForRetry(request, isNotMyVbucket);
        } else {
            event.getObservable().onError(
                    new CouchbaseException("Operation failed because it does not " + "support cloning."));
        }
        if (message instanceof BinaryResponse) {
            BinaryResponse response = (BinaryResponse) message;
            if (response.content() != null && response.content().readableBytes() > 0) {
                try {
                    String config = response.content().toString(CharsetUtil.UTF_8).trim();
                    if (config.startsWith("{")) {
                        configurationProvider.proposeBucketConfig(response.bucket(), config);
                    }
                } finally {
                    response.content().release();
                }
            }
        }
    }
}

From source file:com.couchbase.client.core.ResponseHandlerTest.java

License:Apache License

@Test
public void shouldSendProposedConfigToProvider() throws Exception {
    ClusterFacade clusterMock = mock(ClusterFacade.class);
    ConfigurationProvider providerMock = mock(ConfigurationProvider.class);
    ResponseHandler handler = new ResponseHandler(ENVIRONMENT, clusterMock, providerMock);
    ByteBuf config = Unpooled.copiedBuffer("{\"json\": true}", CharsetUtil.UTF_8);

    ResponseEvent retryEvent = new ResponseEvent();
    retryEvent.setMessage(new InsertResponse(ResponseStatus.RETRY, KeyValueStatus.ERR_TEMP_FAIL.code(), 0,
            "bucket", config, null, mock(InsertRequest.class)));
    retryEvent.setObservable(mock(Subject.class));
    handler.onEvent(retryEvent, 1, true);

    verify(providerMock, times(1)).proposeBucketConfig("bucket", "{\"json\": true}");
    assertEquals(0, config.refCnt());/*from   ww w  .j ava 2  s . co  m*/
    assertNull(retryEvent.getMessage());
    assertNull(retryEvent.getObservable());
}

From source file:com.couchbase.client.core.ResponseHandlerTest.java

License:Apache License

@Test
public void shouldIgnoreInvalidConfig() throws Exception {
    ClusterFacade clusterMock = mock(ClusterFacade.class);
    ConfigurationProvider providerMock = mock(ConfigurationProvider.class);
    ResponseHandler handler = new ResponseHandler(ENVIRONMENT, clusterMock, providerMock);
    ByteBuf config = Unpooled.copiedBuffer("Not my Vbucket", CharsetUtil.UTF_8);

    ResponseEvent retryEvent = new ResponseEvent();
    retryEvent.setMessage(new InsertResponse(ResponseStatus.RETRY, KeyValueStatus.ERR_TEMP_FAIL.code(), 0,
            "bucket", config, null, mock(InsertRequest.class)));
    retryEvent.setObservable(mock(Subject.class));
    handler.onEvent(retryEvent, 1, true);

    verify(providerMock, never()).proposeBucketConfig("bucket", "Not my Vbucket");
    assertEquals(0, config.refCnt());/*w  ww  .j av  a2s .  co  m*/
    assertNull(retryEvent.getMessage());
    assertNull(retryEvent.getObservable());
}

From source file:com.couchbase.client.core.util.HttpUtils.java

License:Open Source License

/**
 * Add the HTTP basic auth headers to a netty request.
 *
 * @param request the request to modify.
 * @param user the user of the request./*  w  ww.ja  v  a2  s.  c  o  m*/
 * @param password the password of the request.
 */
public static void addAuth(final HttpRequest request, final String user, final String password) {
    ByteBuf rawBuf = Unpooled.copiedBuffer(user + ":" + password, CharsetUtil.UTF_8);
    try {
        ByteBuf encoded = Base64.encode(rawBuf);
        request.headers().add(HttpHeaders.Names.AUTHORIZATION, encoded.toString(CharsetUtil.UTF_8));
    } finally {
        rawBuf.release();
    }
}

From source file:com.couchbase.client.io.QueryDecoder.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof HttpResponse) {
        currentEvent = queue.poll();/*from ww  w . j  a  v  a2  s .com*/
        HttpResponse response = (HttpResponse) msg;
        if (response.getStatus().code() != 200) {
            setCurrentEventStatus(false, response.getStatus().toString());
        }
    }

    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;
        contentBuffer.append(content.content().toString(CharsetUtil.UTF_8));

        if (msg instanceof LastHttpContent) {
            QueryResult result = decodeContentBuffer();
            setCurrentEventStatus(true, "Success");
            currentEvent.getFuture().set(result, currentStatus);
            currentEvent.getLatch().countDown();
            clearForNextEvent();
        }
    }
}

From source file:com.couchbase.client.io.QueryEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, QueryEvent ev, List<Object> out) throws Exception {
    ByteBuf queryBuf = Unpooled.copiedBuffer(ev.getQuery(), CharsetUtil.UTF_8);
    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/query", queryBuf);

    request.headers().set("Content-Length", queryBuf.readableBytes());
    request.headers().set("Content-Type", "text/plain");

    out.add(request);/*from www . ja  v a2  s.c o  m*/
    queue.add(ev);
}

From source file:com.crm.provisioning.thread.osa.CallbackServerHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        HttpRequest request = this.request = (HttpRequest) msg;

        if (is100ContinueExpected(request)) {
            send100Continue(ctx);//w ww . j a v  a  2s . c o m
        }

        buffer.setLength(0);
        requestDate = new Date();
        appendDecoderResult(buffer, request);
    }

    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;

        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            buffer.append(content.toString(CharsetUtil.UTF_8));

            appendDecoderResult(buffer, request);
        }

        if (msg instanceof LastHttpContent) {
            String actionType = parserContent();

            writeResponse((LastHttpContent) msg, ctx, actionType);
        }
    }
}