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.config.loader.CarrierLoaderTest.java

License:Apache License

@Test
public void shouldDiscoverConfig() {
    ClusterFacade cluster = mock(ClusterFacade.class);
    ByteBuf content = Unpooled.copiedBuffer("myconfig", CharsetUtil.UTF_8);
    Observable<CouchbaseResponse> response = Observable
            .just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                    KeyValueStatus.SUCCESS.code(), "bucket", content, host));
    when(cluster.send(isA(GetBucketConfigRequest.class))).thenReturn(response);

    CarrierLoader loader = new CarrierLoader(cluster, environment);
    Observable<String> configObservable = loader.discoverConfig("bucket", "password", host);
    assertEquals("myconfig", configObservable.toBlocking().single());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresher.java

License:Apache License

/**
 * Helper method to fetch a config from a specific node of the cluster.
 *
 * @param bucketName the name of the bucket.
 * @param hostname the hostname of the node to fetch from.
 * @return a raw configuration or an error.
 *///from w  w w. j a va 2  s. c  om
private Observable<String> refreshAgainstNode(final String bucketName, final InetAddress hostname) {
    return Buffers.wrapColdWithAutoRelease(Observable.defer(new Func0<Observable<GetBucketConfigResponse>>() {
        @Override
        public Observable<GetBucketConfigResponse> call() {
            return cluster().send(new GetBucketConfigRequest(bucketName, hostname));
        }
    })).doOnNext(new Action1<GetBucketConfigResponse>() {
        @Override
        public void call(GetBucketConfigResponse response) {
            if (!response.status().isSuccess()) {
                if (response.content() != null && response.content().refCnt() > 0) {
                    response.content().release();
                }
                throw new ConfigurationException("Could not fetch config from node: " + response);
            }
        }
    }).map(new Func1<GetBucketConfigResponse, String>() {
        @Override
        public String call(GetBucketConfigResponse response) {
            String raw = response.content().toString(CharsetUtil.UTF_8).trim();
            if (response.content().refCnt() > 0) {
                response.content().release();
            }
            return raw.replace("$HOST", response.hostname().getHostName());
        }
    }).doOnError(new Action1<Throwable>() {
        @Override
        public void call(Throwable ex) {
            LOGGER.debug(
                    "Could not fetch config from bucket \"" + bucketName + "\" against \"" + hostname + "\".",
                    ex);
        }
    });
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldProposeConfigFromTaintedPoller() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    BucketConfig config = mock(BucketConfig.class);

    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.provider(provider);/*from  ww w.  j av  a  2  s  .co m*/

    when(config.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();
    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "localhost:8091", ports));
    when(config.nodes()).thenReturn(nodeInfos);

    final AtomicReference<ByteBuf> bufRef = new AtomicReference<ByteBuf>(null);
    when(cluster.send(any(GetBucketConfigRequest.class)))
            .thenAnswer(new Answer<Observable<GetBucketConfigResponse>>() {
                @Override
                public Observable<GetBucketConfigResponse> answer(InvocationOnMock invocation)
                        throws Throwable {
                    ByteBuf content = Unpooled.copiedBuffer("{\"config\": true}", CharsetUtil.UTF_8);
                    ByteBuf oldContent = bufRef.getAndSet(content);
                    if (oldContent != null) {
                        assertEquals(0, oldContent.refCnt());
                    }
                    GetBucketConfigResponse response = new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                            KeyValueStatus.SUCCESS.code(), "bucket", content,
                            InetAddress.getByName("localhost"));
                    return Observable.just(response);
                }
            });

    refresher.markTainted(config);

    Thread.sleep(1500);

    verify(provider, times(1)).proposeBucketConfig("bucket", "{\"config\": true}");
    assertEquals(0, bufRef.get().refCnt());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldNotProposeInvalidConfigFromTaintedPoller() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    BucketConfig config = mock(BucketConfig.class);

    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.provider(provider);//from  w w  w .j a v  a2 s.co  m

    when(config.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();
    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "localhost:8091", ports));
    when(config.nodes()).thenReturn(nodeInfos);

    ByteBuf content = Unpooled.copiedBuffer("", CharsetUtil.UTF_8);
    when(cluster.send(any(GetBucketConfigRequest.class)))
            .thenReturn(Observable.just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.FAILURE,
                    KeyValueStatus.ERR_NOT_FOUND.code(), "bucket", content,
                    InetAddress.getByName("localhost"))));

    refresher.markTainted(config);

    Thread.sleep(1500);

    verify(provider, never()).proposeBucketConfig("bucket", "");
    assertEquals(0, content.refCnt());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldRefreshWithValidClusterConfig() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.registerBucket("bucket", "");
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    refresher.provider(provider);//from  w ww  .ja  va  2 s. c o  m

    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(bucketConfig.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();
    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "localhost:8091", ports));
    when(bucketConfig.nodes()).thenReturn(nodeInfos);
    Map<String, BucketConfig> bucketConfigs = new HashMap<String, BucketConfig>();
    bucketConfigs.put("bucket", bucketConfig);

    when(clusterConfig.bucketConfigs()).thenReturn(bucketConfigs);

    ByteBuf content = Unpooled.copiedBuffer("{\"config\": true}", CharsetUtil.UTF_8);
    when(cluster.send(any(GetBucketConfigRequest.class)))
            .thenReturn(Observable.just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                    KeyValueStatus.SUCCESS.code(), "bucket", content, InetAddress.getByName("localhost"))));

    refresher.refresh(clusterConfig);

    Thread.sleep(200);

    verify(provider, times(1)).proposeBucketConfig("bucket", "{\"config\": true}");
    assertEquals(0, content.refCnt());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldNotRefreshWithInvalidClusterConfig() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.registerBucket("bucket", "");
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    refresher.provider(provider);//from w  w  w  .  j  a va2  s. com

    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(bucketConfig.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();

    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "localhost:8091", ports));
    when(bucketConfig.nodes()).thenReturn(nodeInfos);
    Map<String, BucketConfig> bucketConfigs = new HashMap<String, BucketConfig>();
    bucketConfigs.put("bucket", bucketConfig);

    when(clusterConfig.bucketConfigs()).thenReturn(bucketConfigs);

    ByteBuf content = Unpooled.copiedBuffer("", CharsetUtil.UTF_8);
    when(cluster.send(any(GetBucketConfigRequest.class)))
            .thenReturn(Observable.just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.FAILURE,
                    KeyValueStatus.ERR_NOT_FOUND.code(), "bucket", content,
                    InetAddress.getByName("localhost"))));

    refresher.refresh(clusterConfig);

    Thread.sleep(200);

    verify(provider, never()).proposeBucketConfig("bucket", "");
    assertEquals(0, content.refCnt());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldFallbackToNextOnRefreshWhenFirstFails() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.registerBucket("bucket", "");
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    refresher.provider(provider);//from www . ja  v a2  s  .  c o m

    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(bucketConfig.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();

    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "1.2.3.4:8091", ports));
    nodeInfos.add(new DefaultNodeInfo(null, "2.3.4.5:8091", ports));
    when(bucketConfig.nodes()).thenReturn(nodeInfos);
    Map<String, BucketConfig> bucketConfigs = new HashMap<String, BucketConfig>();
    bucketConfigs.put("bucket", bucketConfig);

    when(clusterConfig.bucketConfigs()).thenReturn(bucketConfigs);

    ByteBuf content = Unpooled.copiedBuffer("{\"config\": true}", CharsetUtil.UTF_8);
    Observable<CouchbaseResponse> goodResponse = Observable
            .just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                    KeyValueStatus.SUCCESS.code(), "bucket", content, InetAddress.getByName("1.2.3.4")));
    Observable<CouchbaseResponse> badResponse = Observable.error(new CouchbaseException("Woops.."));
    when(cluster.send(any(GetBucketConfigRequest.class))).thenReturn(badResponse, goodResponse);

    refresher.refresh(clusterConfig);

    Thread.sleep(1500);

    verify(provider, times(1)).proposeBucketConfig("bucket", "{\"config\": true}");
    assertEquals(0, content.refCnt());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldFallbackToNextOnPollWhenFirstFails() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    BucketConfig config = mock(BucketConfig.class);

    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.provider(provider);/*ww w  . j av  a2 s . co m*/

    when(config.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();

    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "1.2.3.4:8091", ports));
    nodeInfos.add(new DefaultNodeInfo(null, "2.3.4.5:8091", ports));
    when(config.nodes()).thenReturn(nodeInfos);

    ByteBuf content = Unpooled.copiedBuffer("{\"config\": true}", CharsetUtil.UTF_8);
    Observable<CouchbaseResponse> goodResponse = Observable
            .just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                    KeyValueStatus.SUCCESS.code(), "bucket", content, InetAddress.getByName("1.2.3.4")));
    Observable<CouchbaseResponse> badResponse = Observable.error(new CouchbaseException("Failure"));
    when(cluster.send(any(GetBucketConfigRequest.class))).thenReturn(badResponse, goodResponse);
    refresher.markTainted(config);

    Thread.sleep(1500);

    verify(provider, times(1)).proposeBucketConfig("bucket", "{\"config\": true}");
    assertEquals(0, content.refCnt());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldIgnoreNodeWithoutKVServiceEnabled() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    BucketConfig config = mock(BucketConfig.class);

    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.provider(provider);/*  w w  w. ja v  a2  s  . c om*/

    when(config.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();

    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "1.2.3.4:8091", ports));
    nodeInfos.add(new DefaultNodeInfo(null, "6.7.8.9:8091", new HashMap<String, Integer>()));
    nodeInfos.add(new DefaultNodeInfo(null, "2.3.4.5:8091", ports));
    when(config.nodes()).thenReturn(nodeInfos);

    ByteBuf content = Unpooled.copiedBuffer("{\"config\": true}", CharsetUtil.UTF_8);
    Observable<CouchbaseResponse> goodResponse = Observable
            .just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                    KeyValueStatus.SUCCESS.code(), "bucket", content, InetAddress.getByName("1.2.3.4")));
    Observable<CouchbaseResponse> badResponse = Observable.error(new CouchbaseException("Failure"));
    when(cluster.send(any(GetBucketConfigRequest.class))).thenReturn(badResponse, goodResponse);
    refresher.markTainted(config);

    Thread.sleep(1500);

    verify(provider, times(1)).proposeBucketConfig("bucket", "{\"config\": true}");
    assertEquals(0, content.refCnt());
}

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

License:Open Source License

/**
 * Handles an incoming SASL list mechanisms response and dispatches the next SASL AUTH step.
 *
 * @param ctx the handler context.//from w  w  w . ja va2s. com
 * @param msg the incoming message to investigate.
 * @throws Exception
 */
private void handleListMechsResponse(ChannelHandlerContext ctx, FullBinaryMemcacheResponse msg)
        throws Exception {
    String remote = ctx.channel().remoteAddress().toString();
    String[] supportedMechanisms = msg.content().toString(CharsetUtil.UTF_8).split(" ");
    if (supportedMechanisms.length == 0) {
        throw new AuthenticationException("Received empty SASL mechanisms list from server: " + remote);
    }

    saslClient = Sasl.createSaslClient(supportedMechanisms, null, "couchbase", remote, null, this);
    selectedMechanism = saslClient.getMechanismName();
    int mechanismLength = selectedMechanism.length();
    byte[] bytePayload = saslClient.hasInitialResponse() ? saslClient.evaluateChallenge(new byte[] {}) : null;
    ByteBuf payload = bytePayload != null ? ctx.alloc().buffer().writeBytes(bytePayload)
            : Unpooled.EMPTY_BUFFER;

    FullBinaryMemcacheRequest initialRequest = new DefaultFullBinaryMemcacheRequest(selectedMechanism,
            Unpooled.EMPTY_BUFFER, payload);
    initialRequest.setOpcode(SASL_AUTH_OPCODE).setKeyLength((short) mechanismLength)
            .setTotalBodyLength(mechanismLength + payload.readableBytes());

    ctx.writeAndFlush(initialRequest);
}