public void channelRead0()

in impl/src/main/java/org/apache/peeco/impl/PeecoChannelHandler.java [61:128]


    public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception
    {
        if (msg instanceof HttpRequest)
        {
            try
            {
                requestContextController.activate();
                HttpRequest nettyRequest = (HttpRequest) msg;

                logger.log(Level.FINE, "Request received: HttpMethod: " + nettyRequest.method() + "; URI: " + nettyRequest.uri());

                HttpHandlerInfo info = PeecoUtils.getMatchingHandler(nettyRequest, httpHandlerInfos);

                logger.log(Level.FINE, "Calling matching HttpHandler: " + info.annotation);

                if (info == null)
                {
                    throw new Exception("No matching HttpHandler found for incoming URI from Netty Request: " + nettyRequest.uri());
                }

                Request request = new Request(PeecoUtils.mapHttpMethod(nettyRequest.method()), nettyRequest.uri(), null);

                parseHeaders(nettyRequest, request);
                parseQueryParams(nettyRequest, request);
                parseBodyParams(nettyRequest, request);

                Object handlerParentBean = info.bean;

                try
                {
                    Object returnValue = info.method.invoke(handlerParentBean, request);

                    if (returnValue instanceof Response)
                    {
                        Response response = (Response) returnValue;

                        ctx.write(createNettyResponse(ctx, response, nettyRequest), ctx.voidPromise());
                    }
                    else if (returnValue instanceof CompletionStage)
                    {
                        CompletionStage<Response> completionStageResponse = (CompletionStage<Response>) returnValue;

                        completionStageResponse.thenAccept(response ->
                        {
                            try
                            {
                                ctx.write(createNettyResponse(ctx, response, nettyRequest))
                                        .addListener((ChannelFutureListener) channelFuture ->
                                                logger.log(Level.FINE, "CompletionStage<Response> is finished"));
                            }
                            catch (IOException ex)
                            {
                                //redundant catch as it's caught in createNettyResponse() already, but IDE requires to catch it here again
                            }
                        });
                    }
                }
                catch (Exception ex)
                {
                    throw new RuntimeException("Failed to create Netty Response from given HttpHandler Response object, Netty ChannelHandlerContext and Netty Request.");
                }
            }
            finally
            {
                requestContextController.deactivate();
            }
        }
    }