From 87224b8ea6eb7f74968393bae65401e47a4cd55a Mon Sep 17 00:00:00 2001 From: lburgazzoli Date: Fri, 1 Mar 2019 13:52:41 +0100 Subject: [PATCH] chore(netty): improve health service --- .../apache/camel/k/health/HealthEndpoint.java | 45 ++++++++++--------- .../src/test/resources/log4j2-test.xml | 5 ++- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/camel-k-runtime-health/src/main/java/org/apache/camel/k/health/HealthEndpoint.java b/camel-k-runtime-health/src/main/java/org/apache/camel/k/health/HealthEndpoint.java index 05990631a..4f0516ee8 100644 --- a/camel-k-runtime-health/src/main/java/org/apache/camel/k/health/HealthEndpoint.java +++ b/camel-k-runtime-health/src/main/java/org/apache/camel/k/health/HealthEndpoint.java @@ -32,10 +32,10 @@ import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpObject; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpServerCodec; -import io.netty.handler.codec.http.HttpServerExpectContinueHandler; import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; @@ -80,7 +80,6 @@ protected void doStart() throws Exception { protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline() .addLast(new HttpServerCodec()) - .addLast(new HttpServerExpectContinueHandler()) .addLast(new Handler()); } }); @@ -98,33 +97,37 @@ protected void doStop() throws Exception { } } - private class Handler extends SimpleChannelInboundHandler { + private class Handler extends SimpleChannelInboundHandler { @Override public void channelReadComplete(ChannelHandlerContext ctx) { ctx.flush(); } @Override - protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception { - HttpResponseStatus status; - ByteBuf content; - - if (!Objects.equals(path, msg.uri())) { - status = HttpResponseStatus.NOT_FOUND; - content = Unpooled.wrappedBuffer(KO); - } else if (context.getStatus() == ServiceStatus.Started) { - status = HttpResponseStatus.OK; - content = Unpooled.wrappedBuffer(OK); - } else { - status = HttpResponseStatus.SERVICE_UNAVAILABLE; - content = Unpooled.wrappedBuffer(KO); - } + protected void channelRead0(ChannelHandlerContext ctx, HttpObject object) throws Exception { + if (object instanceof HttpRequest) { + final HttpRequest msg = (HttpRequest)object; + + HttpResponseStatus status; + ByteBuf content; - FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content); - response.headers().set(CONTENT_TYPE, "text/plain"); - response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); + if (!Objects.equals(path, msg.uri())) { + status = HttpResponseStatus.NOT_FOUND; + content = Unpooled.wrappedBuffer(KO); + } else if (context.getStatus() == ServiceStatus.Started) { + status = HttpResponseStatus.OK; + content = Unpooled.wrappedBuffer(OK); + } else { + status = HttpResponseStatus.SERVICE_UNAVAILABLE; + content = Unpooled.wrappedBuffer(KO); + } - ctx.write(response).addListener(ChannelFutureListener.CLOSE); + FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content); + response.headers().set(CONTENT_TYPE, "text/plain"); + response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); + + ctx.write(response).addListener(ChannelFutureListener.CLOSE); + } } } } diff --git a/camel-k-runtime-health/src/test/resources/log4j2-test.xml b/camel-k-runtime-health/src/test/resources/log4j2-test.xml index fb3238e7e..7c6c8d38d 100644 --- a/camel-k-runtime-health/src/test/resources/log4j2-test.xml +++ b/camel-k-runtime-health/src/test/resources/log4j2-test.xml @@ -2,13 +2,14 @@ - + - + +