Search in sources :

Example 1 with CLOSE

use of io.netty.handler.codec.http.HttpHeaderValues.CLOSE in project ignite-3 by apache.

the class RestApiHandler method channelRead0.

/**
 * {@inheritDoc}
 */
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
    CompletableFuture<DefaultFullHttpResponse> responseFuture = router.route(request).map(route -> {
        var response = new RestApiHttpResponse(new DefaultHttpResponse(HttpVersion.HTTP_1_1, OK));
        return route.handle(request, response).thenApply(resp -> {
            ByteBuf content = resp.content() != null ? Unpooled.wrappedBuffer(resp.content()) : new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
            return new DefaultFullHttpResponse(resp.protocolVersion(), resp.status(), content, resp.headers(), EmptyHttpHeaders.INSTANCE);
        });
    }).orElseGet(() -> CompletableFuture.completedFuture(new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.NOT_FOUND)));
    responseFuture.whenCompleteAsync((response, e) -> {
        if (e != null) {
            exceptionCaught(ctx, e);
            return;
        }
        response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
        boolean keepAlive = HttpUtil.isKeepAlive(request);
        if (keepAlive) {
            if (!request.protocolVersion().isKeepAliveDefault()) {
                response.headers().set(CONNECTION, KEEP_ALIVE);
            }
        } else {
            response.headers().set(CONNECTION, CLOSE);
        }
        ChannelFuture f = ctx.writeAndFlush(response);
        if (!keepAlive) {
            f.addListener(ChannelFutureListener.CLOSE);
        }
    }, ctx.executor());
}
Also used : UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) CONNECTION(io.netty.handler.codec.http.HttpHeaderNames.CONNECTION) CONTENT_LENGTH(io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH) HttpVersion(io.netty.handler.codec.http.HttpVersion) KEEP_ALIVE(io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) CompletableFuture(java.util.concurrent.CompletableFuture) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) IgniteLogger(org.apache.ignite.lang.IgniteLogger) Unpooled(io.netty.buffer.Unpooled) ChannelFuture(io.netty.channel.ChannelFuture) RestApiHttpResponse(org.apache.ignite.internal.rest.api.RestApiHttpResponse) EmptyHttpHeaders(io.netty.handler.codec.http.EmptyHttpHeaders) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) ChannelFutureListener(io.netty.channel.ChannelFutureListener) CLOSE(io.netty.handler.codec.http.HttpHeaderValues.CLOSE) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Router(org.apache.ignite.internal.rest.routes.Router) OK(io.netty.handler.codec.http.HttpResponseStatus.OK) HttpUtil(io.netty.handler.codec.http.HttpUtil) ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) RestApiHttpResponse(org.apache.ignite.internal.rest.api.RestApiHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 EmptyByteBuf (io.netty.buffer.EmptyByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 EmptyHttpHeaders (io.netty.handler.codec.http.EmptyHttpHeaders)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 CONNECTION (io.netty.handler.codec.http.HttpHeaderNames.CONNECTION)1 CONTENT_LENGTH (io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH)1 CLOSE (io.netty.handler.codec.http.HttpHeaderValues.CLOSE)1 KEEP_ALIVE (io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 OK (io.netty.handler.codec.http.HttpResponseStatus.OK)1 HttpUtil (io.netty.handler.codec.http.HttpUtil)1 HttpVersion (io.netty.handler.codec.http.HttpVersion)1