List of usage examples for io.netty.util CharsetUtil UTF_8
Charset UTF_8
To view the source code for io.netty.util CharsetUtil UTF_8.
Click Source Link
From source file:com.couchbase.client.core.endpoint.config.ConfigHandlerTest.java
License:Apache License
@Test public void shouldCloseStreamIfChannelDies() throws Exception { HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(200, "OK")); HttpContent responseChunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("conf", CharsetUtil.UTF_8)); HttpContent responseChunk2 = new DefaultHttpContent(Unpooled.copiedBuffer("ig\n\n\n\n", CharsetUtil.UTF_8)); BucketStreamingRequest requestMock = mock(BucketStreamingRequest.class); requestQueue.add(requestMock);//w ww . j av a 2s.com channel.writeInbound(responseHeader, responseChunk1, responseChunk2); assertEquals(1, eventSink.responseEvents().size()); BucketStreamingResponse event = (BucketStreamingResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(ResponseStatus.SUCCESS, event.status()); assertNotNull(event.configs()); assertNotNull(event.host()); Observable<String> configs = event.configs(); final CountDownLatch latch = new CountDownLatch(1); configs.subscribe(new Subscriber<String>() { @Override public void onCompleted() { latch.countDown(); } @Override public void onError(Throwable e) { } @Override public void onNext(String s) { } }); channel.pipeline().remove(handler); channel.disconnect().awaitUninterruptibly(); assertTrue(latch.await(1, TimeUnit.SECONDS)); }
From source file:com.couchbase.client.core.endpoint.dcp.DCPConnectionHandler.java
License:Apache License
/** * Creates a DCP Open Connection Request. * * @param ctx the channel handler context. * @return a converted {@link BinaryMemcacheRequest}. *//* w w w.j ava 2 s .co m*/ private BinaryMemcacheRequest handleOpenConnectionRequest(final ChannelHandlerContext ctx) { ByteBuf extras = ctx.alloc().buffer(8); extras.writeInt(0) // sequence number .writeInt(ConnectionType.CONSUMER.flags()); byte[] key = connectionName.getBytes(CharsetUtil.UTF_8); byte extrasLength = (byte) extras.readableBytes(); short keyLength = (short) key.length; BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras); request.setOpcode(DCPHandler.OP_OPEN_CONNECTION); request.setKeyLength(keyLength); request.setExtrasLength(extrasLength); request.setTotalBodyLength(keyLength + extrasLength); return request; }
From source file:com.couchbase.client.core.endpoint.dcp.DCPConnectionHandler.java
License:Apache License
private FullBinaryMemcacheRequest controlRequest(ChannelHandlerContext ctx, ControlParameter parameter, String value) {//from w ww . ja va 2 s . c o m byte[] key = parameter.value().getBytes(CharsetUtil.UTF_8); short keyLength = (short) key.length; byte[] val = value.getBytes(CharsetUtil.UTF_8); ByteBuf body = ctx.alloc().buffer(val.length); body.writeBytes(val); FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, Unpooled.EMPTY_BUFFER, body); request.setOpcode(DCPHandler.OP_CONTROL); request.setKeyLength(keyLength); request.setTotalBodyLength(keyLength + body.readableBytes()); return request; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueAuthHandler.java
License:Apache License
/** * Handles an incoming SASL list mechanisms response and dispatches the next SASL AUTH step. * * @param ctx the handler context./* w w w .ja va 2 s . com*/ * @param msg the incoming message to investigate. * @throws Exception if something goes wrong during negotiation. */ 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.getBytes(CharsetUtil.UTF_8), Unpooled.EMPTY_BUFFER, payload); initialRequest.setOpcode(SASL_AUTH_OPCODE).setKeyLength((short) mechanismLength) .setTotalBodyLength(mechanismLength + payload.readableBytes()); ChannelFuture future = ctx.writeAndFlush(initialRequest); 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); } } }); }
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./*from ww w. j a v a 2s . c om*/ * @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 www.ja v a 2 s . co 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.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldDecodeSuccessfulGet() { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER, content.copy());//from ww w . j a va 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); requestQueue.add(requestMock); channel.writeInbound(response); assertEquals(1, eventSink.responseEvents().size()); GetResponse event = (GetResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(123456789L, event.cas()); assertEquals(123, event.flags()); assertEquals("content", event.content().toString(CHARSET)); assertEquals(BUCKET, event.bucket()); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldDecodeNotFoundGet() { ByteBuf content = Unpooled.copiedBuffer("Not Found", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER, content.copy());//from w w w .j a va 2 s . c o m response.setStatus(BinaryMemcacheResponseStatus.KEY_ENOENT); GetRequest requestMock = mock(GetRequest.class); when(requestMock.bucket()).thenReturn(BUCKET); requestQueue.add(requestMock); channel.writeInbound(response); assertEquals(1, eventSink.responseEvents().size()); GetResponse event = (GetResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(0, event.cas()); assertEquals(0, event.flags()); assertEquals("Not Found", event.content().toString(CHARSET)); assertEquals(BUCKET, event.bucket()); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldDecodeReplicaGetResponse() { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER, content.copy());/* w w w . j av a2 s .c o m*/ response.setCAS(123456789L); response.setExtras(Unpooled.buffer().writeInt(123)); response.setExtrasLength((byte) 4); ReplicaGetRequest requestMock = mock(ReplicaGetRequest.class); when(requestMock.bucket()).thenReturn(BUCKET); requestQueue.add(requestMock); channel.writeInbound(response); assertEquals(1, eventSink.responseEvents().size()); GetResponse event = (GetResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(123456789L, event.cas()); assertEquals(123, event.flags()); assertEquals("content", event.content().toString(CHARSET)); assertEquals(BUCKET, event.bucket()); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache 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 ww w . ja v a 2 s. co m FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.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)); ReferenceCountUtil.releaseLater(outbound); request = new InsertRequest("key", content.copy(), 10, 0, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.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)); ReferenceCountUtil.releaseLater(outbound); request = new InsertRequest("key", content.copy(), 0, 5, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.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)); ReferenceCountUtil.releaseLater(outbound); request = new InsertRequest("key", content.copy(), 30, 99, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.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.releaseLater(outbound); ReferenceCountUtil.release(content); }