Example usage for io.netty.handler.codec.http DefaultLastHttpContent content

List of usage examples for io.netty.handler.codec.http DefaultLastHttpContent content

Introduction

In this page you can find the example usage for io.netty.handler.codec.http DefaultLastHttpContent content.

Prototype

@Override
    public ByteBuf content() 

Source Link

Usage

From source file:com.qq.servlet.demo.netty.sample.http.helloworld.HttpHelloWorldServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println(msg);/* w w  w.ja v a  2  s .  c o  m*/
    System.out.println("\n\n\n\n============" + msg.getClass().getName() + "=================\n\n\n");
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;
        String uri = req.getUri();
        System.out.println("uri--->>>" + uri);
        if (is100ContinueExpected(req)) {
            ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }
        boolean keepAlive = isKeepAlive(req);
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK,
                Unpooled.wrappedBuffer("".getBytes()));
        response.headers().set(CONTENT_TYPE, "text/plain");
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());

        if (!keepAlive) {
            ctx.write(response).addListener(ChannelFutureListener.CLOSE);
        } else {
            response.headers().set(CONNECTION, Values.KEEP_ALIVE);
            ctx.write(response);
        }
    } else if (msg instanceof DefaultLastHttpContent) {
        DefaultLastHttpContent httpContent = (DefaultLastHttpContent) msg;
        String body = httpContent.content().toString(CharsetUtil.UTF_8);
        System.out.println("body===>>> " + body);
    }
}