Java tutorial
/* * Copyright 2013 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package esens.wp6.ibmJmsBackend.log4http; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpHeaderUtil; import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpRequest; import org.apache.log4j.Logger; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import static io.netty.handler.codec.http.HttpHeaderNames.*; import static io.netty.handler.codec.http.HttpResponseStatus.*; import static io.netty.handler.codec.http.HttpVersion.*; public class HttpLogServerHandler extends ChannelHandlerAdapter { private static final Logger LOGGER = Logger.getLogger(HttpLogServerHandler.class); @Override public void channelReadComplete(ChannelHandlerContext ctx) { ctx.flush(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; if (HttpHeaderUtil.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); } boolean keepAlive = HttpHeaderUtil.isKeepAlive(req); System.out.println(req.uri()); //get number of lines to show from the request uri. String lines = "-n 500"; //pull last 500 lines as default if (req.uri().equals("/")) { //all lines } else { try { lines = "-n " + Integer.parseInt(req.uri().substring(1)); } catch (NumberFormatException ex) { } } //LOGGER.debug("# of lines to tail \"" + lines + "\""); /** * *********** DOES NOT WORK ON WINDOWS because uses tail command */ StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); try { final Process exec = Runtime.getRuntime().exec("tail " + lines + " logs/ibm-jms-backend.log"); BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream())); String line = null; while ((line = br.readLine()) != null) { pw.println(line); } } catch (Exception ex) { LOGGER.error("Couldn't run tail, are you using Windows (smirk)"); pw.println("Couldn't run tail, are you using Windows (smirk)"); LOGGER.error(ex.getMessage(), ex); ex.printStackTrace(pw); } FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(sw.toString().getBytes())); response.headers().set(CONTENT_TYPE, "text/plain"); response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.write(response); } } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } }