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 errorcode; import java.io.*; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.QueryStringDecoder; import static io.netty.handler.codec.http.HttpHeaders.Names.*; import static io.netty.handler.codec.http.HttpHeaders.*; import static io.netty.handler.codec.http.HttpResponseStatus.*; import static io.netty.handler.codec.http.HttpVersion.*; public class DaumStatusServerHandler extends ChannelInboundHandlerAdapter { private static byte[] E404; private static byte[] E503; static { ByteArrayOutputStream ous = null; InputStream ios = null; try { byte[] buffer = new byte[4096]; ous = new ByteArrayOutputStream(); ios = new FileInputStream(new File("/Users/ihongjun/Factory/Test-Netty/bin/404.html")); int read = 0; while ((read = ios.read(buffer)) != -1) { ous.write(buffer, 0, read); } ios.close(); E404 = ous.toByteArray(); ous.reset(); ios = new FileInputStream(new File("/Users/ihongjun/Factory/Test-Netty/bin/503.html")); read = 0; while ((read = ios.read(buffer)) != -1) { ous.write(buffer, 0, read); } E503 = ous.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (ous != null) ous.close(); } catch (IOException e) { } try { if (ios != null) ios.close(); } catch (IOException e) { } } } @Override public void channelReadComplete(ChannelHandlerContext ctx) { ctx.flush(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { FullHttpResponse response = null; if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri()); if (is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); } boolean keepAlive = isKeepAlive(req); if (queryDecoder.path().startsWith("/503.html")) { response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(E503)); response.setStatus(SERVICE_UNAVAILABLE); } else { response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(E404)); response.setStatus(NOT_FOUND); } response.headers().set(CONTENT_TYPE, "text/html"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, Values.KEEP_ALIVE); ctx.write(response); } } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } }