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:io.aos.netty5.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);/*from w  ww .j a  va2s . 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<String, String> h : headers) {
                String key = h.getKey();
                String 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 (String name : trailer.trailingHeaders().names()) {
                    for (String 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:io.apigee.trireme.container.netty.NettyHttpChunk.java

License:Open Source License

@Override
public boolean hasData() {
    return (chunk.content() != null) && (chunk.content() != Unpooled.EMPTY_BUFFER);
}

From source file:io.crate.protocols.http.HttpAuthUpstreamHandlerTest.java

@Test
public void testSendUnauthorizedWithoutBody() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel();
    HttpAuthUpstreamHandler.sendUnauthorized(ch, null);

    DefaultFullHttpResponse resp = ch.readOutbound();
    assertThat(resp.content(), is(Unpooled.EMPTY_BUFFER));
}

From source file:io.gatling.http.client.body.multipart.impl.MessageEndPartImpl.java

License:Apache License

@Override
protected ByteBuf computePreContentBytes(int preContentLength) {
    return Unpooled.EMPTY_BUFFER;
}

From source file:io.gatling.http.client.body.multipart.impl.MessageEndPartImpl.java

License:Apache License

@Override
protected ByteBuf computePostContentBytes(int postContentLength) {
    return Unpooled.EMPTY_BUFFER;
}

From source file:io.grpc.alts.internal.AltsProtocolNegotiatorTest.java

License:Apache License

@Test
public void doNotFlushEmptyBuffer() throws Exception {
    doHandshake();/*w w w .j a  v a2s. c  o m*/
    assertEquals(1, protectors.size());
    InterceptingProtector protector = protectors.poll();

    String message = "hello";
    ByteBuf in = Unpooled.copiedBuffer(message, UTF_8);

    assertEquals(0, protector.flushes.get());
    Future<?> done = channel.write(in);
    channel.flush();
    done.get(5, TimeUnit.SECONDS);
    assertEquals(1, protector.flushes.get());

    done = channel.write(Unpooled.EMPTY_BUFFER);
    channel.flush();
    done.get(5, TimeUnit.SECONDS);
    assertEquals(1, protector.flushes.get());
}

From source file:io.grpc.netty.NettyClientHandler.java

License:Apache License

/**
 * Handler for commands sent from the stream.
 *///from  ww  w  .j a v a  2  s  .  c om
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (msg instanceof CreateStreamCommand) {
        createStream((CreateStreamCommand) msg, promise);
    } else if (msg instanceof SendGrpcFrameCommand) {
        sendGrpcFrame(ctx, (SendGrpcFrameCommand) msg, promise);
    } else if (msg instanceof CancelClientStreamCommand) {
        cancelStream(ctx, (CancelClientStreamCommand) msg, promise);
    } else if (msg instanceof SendPingCommand) {
        sendPingFrame(ctx, (SendPingCommand) msg, promise);
    } else if (msg instanceof GracefulCloseCommand) {
        gracefulClose(ctx, (GracefulCloseCommand) msg, promise);
    } else if (msg instanceof ForcefulCloseCommand) {
        forcefulClose(ctx, (ForcefulCloseCommand) msg, promise);
    } else if (msg == NOOP_MESSAGE) {
        ctx.write(Unpooled.EMPTY_BUFFER, promise);
    } else {
        throw new AssertionError("Write called for unexpected type: " + msg.getClass().getName());
    }
}

From source file:io.grpc.netty.NettyHandlerTestBase.java

License:Apache License

protected final ByteBuf goAwayFrame(int lastStreamId) {
    return goAwayFrame(lastStreamId, 0, Unpooled.EMPTY_BUFFER);
}

From source file:io.hekate.network.netty.NettyMessageReadWritTest.java

License:Apache License

@Test
public void testMarkSupported() throws IOException {
    NettyMessage reader = new NettyMessage(Unpooled.EMPTY_BUFFER, fakeCodec);

    assertFalse(reader.markSupported());
}

From source file:io.hekate.network.netty.NettyMessageReadWritTest.java

License:Apache License

@Test(expected = UnsupportedOperationException.class)
public void testMark() throws IOException {
    NettyMessage reader = new NettyMessage(Unpooled.EMPTY_BUFFER, fakeCodec);

    reader.mark(1);/*from w w w .ja  va2s . c o  m*/
}