List of usage examples for io.netty.buffer Unpooled copiedBuffer
private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset)
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 va 2s . 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 shouldDecodeGetSuccessResponse() throws Exception { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse("key", Unpooled.EMPTY_BUFFER, content.copy());/*from ww w. j ava 2 s . co 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());/*from ww w.ja v a 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());//from w ww. j a v a2s. 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"); 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 shouldDecodeGetBucketConfigResponse() throws Exception { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse("key", Unpooled.EMPTY_BUFFER, content.copy());//from ww w. j av a 2 s. c o m GetBucketConfigRequest requestMock = mock(GetBucketConfigRequest.class); when(requestMock.bucket()).thenReturn("bucket"); when(requestMock.hostname()).thenReturn(InetAddress.getLocalHost()); queue.add(requestMock); channel.writeInbound(response); channel.readInbound(); latch.await(1, TimeUnit.SECONDS); assertEquals(1, firedEvents.size()); GetBucketConfigResponse inbound = (GetBucketConfigResponse) firedEvents.get(0); assertEquals(ResponseStatus.SUCCESS, inbound.status()); assertEquals("bucket", inbound.bucket()); assertEquals(InetAddress.getLocalHost(), inbound.hostname()); assertEquals("content", inbound.content().toString(CharsetUtil.UTF_8)); assertTrue(queue.isEmpty()); ReferenceCountUtil.release(content); }
From source file:com.couchbase.client.core.endpoint.binary.BinarySaslClient.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. j a v a2 s. co m*/ * @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 IllegalStateException("SASL Challenge evaluation returned null."); } }
From source file:com.couchbase.client.core.endpoint.config.ConfigHandler.java
License:Apache License
@Override protected HttpRequest encodeRequest(final ChannelHandlerContext ctx, final ConfigRequest msg) throws Exception { HttpMethod httpMethod = HttpMethod.GET; if (msg instanceof FlushRequest || msg instanceof InsertBucketRequest || msg instanceof UpdateBucketRequest) { httpMethod = HttpMethod.POST;// ww w . j a v a 2s. co m } else if (msg instanceof RemoveBucketRequest) { httpMethod = HttpMethod.DELETE; } ByteBuf content; if (msg instanceof InsertBucketRequest) { content = Unpooled.copiedBuffer(((InsertBucketRequest) msg).payload(), CharsetUtil.UTF_8); } else if (msg instanceof UpdateBucketRequest) { content = Unpooled.copiedBuffer(((UpdateBucketRequest) msg).payload(), CharsetUtil.UTF_8); } else { content = Unpooled.EMPTY_BUFFER; } FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, msg.path(), content); request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent()); if (msg instanceof InsertBucketRequest || msg instanceof UpdateBucketRequest) { request.headers().set(HttpHeaders.Names.ACCEPT, "*/*"); request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded"); } request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes()); request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx)); addHttpBasicAuth(ctx, request, msg.bucket(), msg.password()); return request; }
From source file:com.couchbase.client.core.endpoint.config.ConfigHandlerTest.java
License:Apache License
@Test public void shouldDecodeSuccessBucketConfigResponse() throws Exception { HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(200, "OK")); HttpContent responseChunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("foo", CHARSET)); HttpContent responseChunk2 = new DefaultLastHttpContent(Unpooled.copiedBuffer("bar", CHARSET)); BucketConfigRequest requestMock = mock(BucketConfigRequest.class); requestQueue.add(requestMock);/*from w ww . j a v a 2 s.com*/ channel.writeInbound(responseHeader, responseChunk1, responseChunk2); channel.readInbound(); assertEquals(1, eventSink.responseEvents().size()); BucketConfigResponse event = (BucketConfigResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(ResponseStatus.SUCCESS, event.status()); assertEquals("foobar", event.config()); assertTrue(requestQueue.isEmpty()); }
From source file:com.couchbase.client.core.endpoint.config.ConfigHandlerTest.java
License:Apache License
@Test public void shouldDecodeNotFoundBucketConfigResponse() throws Exception { HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(404, "Object Not Found")); HttpContent responseChunk = new DefaultLastHttpContent( Unpooled.copiedBuffer("Not found.", CharsetUtil.UTF_8)); BucketConfigRequest requestMock = mock(BucketConfigRequest.class); requestQueue.add(requestMock);/* www . ja va2 s . c om*/ channel.writeInbound(responseHeader, responseChunk); assertEquals(1, eventSink.responseEvents().size()); BucketConfigResponse event = (BucketConfigResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(ResponseStatus.NOT_EXISTS, event.status()); assertEquals("Not found.", event.config()); assertTrue(requestQueue.isEmpty()); }
From source file:com.couchbase.client.core.endpoint.config.ConfigHandlerTest.java
License:Apache License
@Test public void shouldDecodeFlushNotEnabledResponse() throws Exception { String content = "{\"_\":\"Flush is disabled for the bucket\"}"; HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(400, "Bad Request")); HttpContent responseChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(content, CharsetUtil.UTF_8)); FlushRequest requestMock = mock(FlushRequest.class); requestQueue.add(requestMock);/*from w ww . j ava 2 s . c o m*/ channel.writeInbound(responseHeader, responseChunk); assertEquals(1, eventSink.responseEvents().size()); FlushResponse event = (FlushResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(ResponseStatus.INVALID_ARGUMENTS, event.status()); assertEquals("{\"_\":\"Flush is disabled for the bucket\"}", event.content()); assertTrue(requestQueue.isEmpty()); }