Example usage for io.netty.handler.codec.http HttpHeaderNames KEEP_ALIVE

List of usage examples for io.netty.handler.codec.http HttpHeaderNames KEEP_ALIVE

Introduction

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

Prototype

AsciiString KEEP_ALIVE

To view the source code for io.netty.handler.codec.http HttpHeaderNames KEEP_ALIVE.

Click Source Link

Usage

From source file:socketServer.dispatcherService.dispatcherServer.server.HttpServerHandler.java

License:Apache License

@Override
protected void messageReceived(ChannelHandlerContext ctx, Object msg) {
    HttpRequest request = null;/*ww w . java2s  .c om*/
    if (msg instanceof HttpRequest) {
        request = (HttpRequest) msg;
    }

    if (msg instanceof LastHttpContent) {
        // Decide whether to close the connection or not.
        boolean keepAlive = isKeepAlive(request);

        System.out.println("param : " + request.uri().substring(request.uri().lastIndexOf("/")));

        String address = chooseOne();

        // Build the response object.
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK,
                Unpooled.copiedBuffer(address, CharsetUtil.UTF_8));
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");

        if (keepAlive) {
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes() + "");
            response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderNames.KEEP_ALIVE);
        }

        // Write the response.
        ChannelFuture future = ctx.write(response);

        if (!keepAlive) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
}