Example usage for io.netty.buffer Unpooled EMPTY_BUFFER

List of usage examples for io.netty.buffer Unpooled EMPTY_BUFFER

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled EMPTY_BUFFER.

Prototype

ByteBuf EMPTY_BUFFER

To view the source code for io.netty.buffer Unpooled EMPTY_BUFFER.

Click Source Link

Document

A buffer whose capacity is 0 .

Usage

From source file:com.couchbase.client.core.message.kv.subdoc.simple.SubDeleteRequest.java

License:Apache License

/**
 * Creates a new {@link SubDeleteRequest}.
 *
 * @param key        the key of the document.
 * @param path       the subdocument path to delete from the document.
 * @param bucket     the bucket of the document.
 * @param expiration the TTL of the whole enclosing document.
 * @param cas        the cas value for the operation
 * @throws NullPointerException if the path is null (see {@link #EXCEPTION_NULL_PATH})
 * @throws IllegalArgumentException if the path is empty (see {@link #EXCEPTION_EMPTY_PATH})
 *///www . j  a  v a 2  s.c o  m
public SubDeleteRequest(String key, String path, String bucket, int expiration, long cas) {
    super(key, path, Unpooled.EMPTY_BUFFER, bucket, expiration, cas);
    if (path.isEmpty()) {
        //it is important to use cleanUpAndThrow in order to release the internal buffer
        cleanUpAndThrow(EXCEPTION_EMPTY_PATH);
    }
}

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

License:Apache License

@Test
public void shouldDispatchFirstNMVImmediately() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    ClusterFacade clusterMock = mock(ClusterFacade.class);
    when(clusterMock.send(any(CouchbaseRequest.class))).then(new Answer<Object>() {
        @Override//from w w w  . ja  v  a2  s . co m
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            latch.countDown();
            return null;
        }
    });

    ConfigurationProvider providerMock = mock(ConfigurationProvider.class);
    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(providerMock.config()).thenReturn(clusterConfig);
    when(clusterConfig.bucketConfig("bucket")).thenReturn(bucketConfig);
    when(bucketConfig.hasFastForwardMap()).thenReturn(true);

    ResponseHandler handler = new ResponseHandler(ENVIRONMENT, clusterMock, providerMock);

    GetRequest request = new GetRequest("key", "bucket");
    GetResponse response = new GetResponse(ResponseStatus.RETRY, (short) 0, 0L, 0, "bucket",
            Unpooled.EMPTY_BUFFER, request);

    ResponseEvent retryEvent = new ResponseEvent();
    retryEvent.setMessage(response);
    retryEvent.setObservable(request.observable());

    handler.onEvent(retryEvent, 1, true);

    long start = System.nanoTime();
    latch.await(5, TimeUnit.SECONDS);
    long end = System.nanoTime();

    // assert immediate dispatch
    assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) < 10);
}

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

License:Apache License

@Test
public void shouldDispatchFirstNMVBWithDelayIfNoFFMap() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    ClusterFacade clusterMock = mock(ClusterFacade.class);
    when(clusterMock.send(any(CouchbaseRequest.class))).then(new Answer<Object>() {
        @Override//w  w  w  .j av a2  s .c  o m
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            latch.countDown();
            return null;
        }
    });

    ConfigurationProvider providerMock = mock(ConfigurationProvider.class);
    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(providerMock.config()).thenReturn(clusterConfig);
    when(clusterConfig.bucketConfig("bucket")).thenReturn(bucketConfig);
    when(bucketConfig.hasFastForwardMap()).thenReturn(false);

    ResponseHandler handler = new ResponseHandler(ENVIRONMENT, clusterMock, providerMock);

    GetRequest request = new GetRequest("key", "bucket");
    GetResponse response = new GetResponse(ResponseStatus.RETRY, (short) 0, 0L, 0, "bucket",
            Unpooled.EMPTY_BUFFER, request);

    ResponseEvent retryEvent = new ResponseEvent();
    retryEvent.setMessage(response);
    retryEvent.setObservable(request.observable());

    handler.onEvent(retryEvent, 1, true);

    long start = System.nanoTime();
    latch.await(5, TimeUnit.SECONDS);
    long end = System.nanoTime();

    // assert delayed dispatch
    assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) >= 100);
}

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

License:Apache License

@Test
public void shouldDispatchSecondNMVWithDelay() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    ClusterFacade clusterMock = mock(ClusterFacade.class);
    when(clusterMock.send(any(CouchbaseRequest.class))).then(new Answer<Object>() {
        @Override//  w  ww  .j  a  va2s .c o m
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            latch.countDown();
            return null;
        }
    });

    ConfigurationProvider providerMock = mock(ConfigurationProvider.class);
    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(providerMock.config()).thenReturn(clusterConfig);
    when(clusterConfig.bucketConfig("bucket")).thenReturn(bucketConfig);
    when(bucketConfig.hasFastForwardMap()).thenReturn(true);

    ResponseHandler handler = new ResponseHandler(ENVIRONMENT, clusterMock, providerMock);

    GetRequest request = new GetRequest("key", "bucket");
    request.incrementRetryCount(); // pretend its at least once retried!
    GetResponse response = new GetResponse(ResponseStatus.RETRY, (short) 0, 0L, 0, "bucket",
            Unpooled.EMPTY_BUFFER, request);

    ResponseEvent retryEvent = new ResponseEvent();
    retryEvent.setMessage(response);
    retryEvent.setObservable(request.observable());

    handler.onEvent(retryEvent, 1, true);

    long start = System.nanoTime();
    latch.await(5, TimeUnit.SECONDS);
    long end = System.nanoTime();

    // assert delayed dispatch
    assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) >= 100);
}

From source file:com.creamsugardonut.CustomHttpRequestDecoder.java

License:Apache License

@Override
protected HttpMessage createInvalidMessage() {
    return new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/bad-request",
            Unpooled.EMPTY_BUFFER, validateHeaders);
}

From source file:com.dwarf.netty.guide.http.snoop.HttpSnoopServerHandler.java

License:Apache License

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

        if (HttpHeaderUtil.is100ContinueExpected(request)) {
            send100Continue(ctx);//  www .  j  av a  2  s  . c  o m
        }

        buf.setLength(0);
        buf.append("WELCOME TO THE WILD WILD WEB SERVER\r\n");
        buf.append("===================================\r\n");

        buf.append("VERSION: ").append(request.protocolVersion()).append("\r\n");
        buf.append("HOSTNAME: ").append(request.headers().get(HOST, "unknown")).append("\r\n");
        buf.append("REQUEST_URI: ").append(request.uri()).append("\r\n\r\n");

        HttpHeaders headers = request.headers();
        if (!headers.isEmpty()) {
            for (Map.Entry<CharSequence, CharSequence> h : headers) {
                CharSequence key = h.getKey();
                CharSequence value = h.getValue();
                buf.append("HEADER: ").append(key).append(" = ").append(value).append("\r\n");
            }
            buf.append("\r\n");
        }

        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri());
        Map<String, List<String>> params = queryStringDecoder.parameters();
        if (!params.isEmpty()) {
            for (Entry<String, List<String>> p : params.entrySet()) {
                String key = p.getKey();
                List<String> vals = p.getValue();
                for (String val : vals) {
                    buf.append("PARAM: ").append(key).append(" = ").append(val).append("\r\n");
                }
            }
            buf.append("\r\n");
        }

        appendDecoderResult(buf, request);
    }

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

        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            buf.append("CONTENT: ");
            buf.append(content.toString(CharsetUtil.UTF_8));
            buf.append("\r\n");
            appendDecoderResult(buf, request);
        }

        if (msg instanceof LastHttpContent) {
            buf.append("END OF CONTENT\r\n");

            LastHttpContent trailer = (LastHttpContent) msg;
            if (!trailer.trailingHeaders().isEmpty()) {
                buf.append("\r\n");
                for (CharSequence name : trailer.trailingHeaders().names()) {
                    for (CharSequence value : trailer.trailingHeaders().getAll(name)) {
                        buf.append("TRAILING HEADER: ");
                        buf.append(name).append(" = ").append(value).append("\r\n");
                    }
                }
                buf.append("\r\n");
            }

            if (!writeResponse(trailer, ctx)) {
                // If keep-alive is off, close the connection once the content is fully written.
                ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
            }
        }
    }
}

From source file:com.ebay.jetstream.http.netty.server.DefaultHttpServletResponse.java

License:MIT License

public DefaultHttpServletResponse(HttpVersion version, HttpResponseStatus status, int bufsz) {
    m_bufsz = bufsz;/*from  w  w  w.  ja v  a 2  s .c  o m*/
    if (m_bufsz == 0) {
        m_buffer = Unpooled.EMPTY_BUFFER;
    } else {
        m_buffer = Unpooled.buffer(m_bufsz);
    }
    m_nettyhttpresp = new DefaultFullHttpResponse(version, status, m_buffer);
}

From source file:com.fjn.helper.frameworkex.netty.v4.echotest.EchoServerHandler.java

License:Apache License

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}

From source file:com.flysoloing.learning.network.netty.http.snoop.HttpSnoopServerHandler.java

License:Apache License

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

        if (HttpUtil.is100ContinueExpected(request)) {
            send100Continue(ctx);//from  w  w w. j a v a  2s .co m
        }

        buf.setLength(0);
        buf.append("WELCOME TO THE WILD WILD WEB SERVER\r\n");
        buf.append("===================================\r\n");

        buf.append("VERSION: ").append(request.protocolVersion()).append("\r\n");
        buf.append("HOSTNAME: ").append(request.headers().get(HttpHeaderNames.HOST, "unknown")).append("\r\n");
        buf.append("REQUEST_URI: ").append(request.uri()).append("\r\n\r\n");

        HttpHeaders headers = request.headers();
        if (!headers.isEmpty()) {
            for (Entry<String, String> h : headers) {
                CharSequence key = h.getKey();
                CharSequence value = h.getValue();
                buf.append("HEADER: ").append(key).append(" = ").append(value).append("\r\n");
            }
            buf.append("\r\n");
        }

        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri());
        Map<String, List<String>> params = queryStringDecoder.parameters();
        if (!params.isEmpty()) {
            for (Entry<String, List<String>> p : params.entrySet()) {
                String key = p.getKey();
                List<String> vals = p.getValue();
                for (String val : vals) {
                    buf.append("PARAM: ").append(key).append(" = ").append(val).append("\r\n");
                }
            }
            buf.append("\r\n");
        }

        appendDecoderResult(buf, request);
    }

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

        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            buf.append("CONTENT: ");
            buf.append(content.toString(CharsetUtil.UTF_8));
            buf.append("\r\n");
            appendDecoderResult(buf, request);
        }

        if (msg instanceof LastHttpContent) {
            buf.append("END OF CONTENT\r\n");

            LastHttpContent trailer = (LastHttpContent) msg;
            if (!trailer.trailingHeaders().isEmpty()) {
                buf.append("\r\n");
                for (CharSequence name : trailer.trailingHeaders().names()) {
                    for (CharSequence value : trailer.trailingHeaders().getAll(name)) {
                        buf.append("TRAILING HEADER: ");
                        buf.append(name).append(" = ").append(value).append("\r\n");
                    }
                }
                buf.append("\r\n");
            }

            if (!writeResponse(trailer, ctx)) {
                // If keep-alive is off, close the connection once the content is fully written.
                ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
            }
        }
    }
}

From source file:com.github.sinsinpub.pero.backend.RelayTrafficHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
    watch.start();
}