diff --git a/src/main/java/org/jboss/netty/example/http/file/HttpStaticFileServerHandler.java b/src/main/java/org/jboss/netty/example/http/file/HttpStaticFileServerHandler.java index 840d3c7ea6..abb17bb7ee 100644 --- a/src/main/java/org/jboss/netty/example/http/file/HttpStaticFileServerHandler.java +++ b/src/main/java/org/jboss/netty/example/http/file/HttpStaticFileServerHandler.java @@ -15,6 +15,12 @@ */ package org.jboss.netty.example.http.file; +import static org.jboss.netty.handler.codec.http.HttpHeaders.*; +import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.*; +import static org.jboss.netty.handler.codec.http.HttpMethod.*; +import static org.jboss.netty.handler.codec.http.HttpResponseStatus.*; +import static org.jboss.netty.handler.codec.http.HttpVersion.*; + import java.io.File; import java.io.FileNotFoundException; import java.io.RandomAccessFile; @@ -32,12 +38,9 @@ import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelUpstreamHandler; import org.jboss.netty.handler.codec.frame.TooLongFrameException; import org.jboss.netty.handler.codec.http.DefaultHttpResponse; -import org.jboss.netty.handler.codec.http.HttpHeaders; -import org.jboss.netty.handler.codec.http.HttpMethod; import org.jboss.netty.handler.codec.http.HttpRequest; import org.jboss.netty.handler.codec.http.HttpResponse; import org.jboss.netty.handler.codec.http.HttpResponseStatus; -import org.jboss.netty.handler.codec.http.HttpVersion; import org.jboss.netty.handler.stream.ChunkedFile; import org.jboss.netty.util.CharsetUtil; @@ -51,24 +54,24 @@ public class HttpStaticFileServerHandler extends SimpleChannelUpstreamHandler { @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { HttpRequest request = (HttpRequest) e.getMessage(); - if (request.getMethod() != HttpMethod.GET) { - sendError(ctx, HttpResponseStatus.METHOD_NOT_ALLOWED); + if (request.getMethod() != GET) { + sendError(ctx, METHOD_NOT_ALLOWED); return; } String path = sanitizeUri(request.getUri()); if (path == null) { - sendError(ctx, HttpResponseStatus.FORBIDDEN); + sendError(ctx, FORBIDDEN); return; } File file = new File(path); if (file.isHidden() || !file.exists()) { - sendError(ctx, HttpResponseStatus.NOT_FOUND); + sendError(ctx, NOT_FOUND); return; } if (!file.isFile()) { - sendError(ctx, HttpResponseStatus.FORBIDDEN); + sendError(ctx, FORBIDDEN); return; } @@ -76,15 +79,13 @@ public class HttpStaticFileServerHandler extends SimpleChannelUpstreamHandler { try { raf = new RandomAccessFile(file, "r"); } catch (FileNotFoundException fnfe) { - sendError(ctx, HttpResponseStatus.NOT_FOUND); + sendError(ctx, NOT_FOUND); return; } long fileLength = raf.length(); - HttpResponse response = new DefaultHttpResponse( - HttpVersion.HTTP_1_1, HttpResponseStatus.OK); - response.setHeader( - HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(fileLength)); + HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); + setContentLength(response, fileLength); Channel ch = e.getChannel(); @@ -95,8 +96,7 @@ public class HttpStaticFileServerHandler extends SimpleChannelUpstreamHandler { ChannelFuture writeFuture = ch.write(new ChunkedFile(raf, 0, fileLength, 8192)); // Decide whether to close the connection or not. - boolean close = !request.isKeepAlive(); - if (close) { + if (!request.isKeepAlive()) { // Close the connection when the whole content is written out. writeFuture.addListener(ChannelFutureListener.CLOSE); } @@ -108,13 +108,13 @@ public class HttpStaticFileServerHandler extends SimpleChannelUpstreamHandler { Channel ch = e.getChannel(); Throwable cause = e.getCause(); if (cause instanceof TooLongFrameException) { - sendError(ctx, HttpResponseStatus.BAD_REQUEST); + sendError(ctx, BAD_REQUEST); return; } cause.printStackTrace(); if (ch.isConnected()) { - sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR); + sendError(ctx, INTERNAL_SERVER_ERROR); } } @@ -146,10 +146,8 @@ public class HttpStaticFileServerHandler extends SimpleChannelUpstreamHandler { } private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { - HttpResponse response = new DefaultHttpResponse( - HttpVersion.HTTP_1_1, status); - response.setHeader( - HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); + HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); + response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer( "Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); diff --git a/src/main/java/org/jboss/netty/example/http/snoop/HttpRequestHandler.java b/src/main/java/org/jboss/netty/example/http/snoop/HttpRequestHandler.java index 63bc6b84b0..5da00cb754 100644 --- a/src/main/java/org/jboss/netty/example/http/snoop/HttpRequestHandler.java +++ b/src/main/java/org/jboss/netty/example/http/snoop/HttpRequestHandler.java @@ -17,6 +17,8 @@ package org.jboss.netty.example.http.snoop; import static org.jboss.netty.handler.codec.http.HttpHeaders.*; import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.*; +import static org.jboss.netty.handler.codec.http.HttpResponseStatus.*; +import static org.jboss.netty.handler.codec.http.HttpVersion.*; import java.util.List; import java.util.Map; @@ -38,11 +40,8 @@ import org.jboss.netty.handler.codec.http.CookieEncoder; import org.jboss.netty.handler.codec.http.DefaultHttpResponse; import org.jboss.netty.handler.codec.http.HttpChunk; import org.jboss.netty.handler.codec.http.HttpChunkTrailer; -import org.jboss.netty.handler.codec.http.HttpHeaders; import org.jboss.netty.handler.codec.http.HttpRequest; import org.jboss.netty.handler.codec.http.HttpResponse; -import org.jboss.netty.handler.codec.http.HttpResponseStatus; -import org.jboss.netty.handler.codec.http.HttpVersion; import org.jboss.netty.handler.codec.http.QueryStringDecoder; import org.jboss.netty.util.CharsetUtil; @@ -58,24 +57,25 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler { private volatile HttpRequest request; private volatile boolean readingChunks; - private final StringBuilder responseContent = new StringBuilder(); + /** Buffer that stores the response content */ + private final StringBuilder buf = new StringBuilder(); @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (!readingChunks) { HttpRequest request = this.request = (HttpRequest) e.getMessage(); - responseContent.setLength(0); - responseContent.append("WELCOME TO THE WILD WILD WEB SERVER\r\n"); - responseContent.append("===================================\r\n"); + buf.setLength(0); + buf.append("WELCOME TO THE WILD WILD WEB SERVER\r\n"); + buf.append("===================================\r\n"); - responseContent.append("VERSION: " + request.getProtocolVersion().getText() + "\r\n"); - responseContent.append("HOSTNAME: " + getHost(request, "unknown") + "\r\n"); - responseContent.append("REQUEST_URI: " + request.getUri() + "\r\n\r\n"); + buf.append("VERSION: " + request.getProtocolVersion() + "\r\n"); + buf.append("HOSTNAME: " + getHost(request, "unknown") + "\r\n"); + buf.append("REQUEST_URI: " + request.getUri() + "\r\n\r\n"); for (Map.Entry h: request.getHeaders()) { - responseContent.append("HEADER: " + h.getKey() + " = " + h.getValue() + "\r\n"); + buf.append("HEADER: " + h.getKey() + " = " + h.getValue() + "\r\n"); } - responseContent.append("\r\n"); + buf.append("\r\n"); QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri()); Map> params = queryStringDecoder.getParameters(); @@ -84,10 +84,10 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler { String key = p.getKey(); List vals = p.getValue(); for (String val : vals) { - responseContent.append("PARAM: " + key + " = " + val + "\r\n"); + buf.append("PARAM: " + key + " = " + val + "\r\n"); } } - responseContent.append("\r\n"); + buf.append("\r\n"); } if (request.isChunked()) { @@ -95,7 +95,7 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler { } else { ChannelBuffer content = request.getContent(); if (content.readable()) { - responseContent.append("CONTENT: " + content.toString(CharsetUtil.UTF_8) + "\r\n"); + buf.append("CONTENT: " + content.toString(CharsetUtil.UTF_8) + "\r\n"); } writeResponse(e); } @@ -103,22 +103,22 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler { HttpChunk chunk = (HttpChunk) e.getMessage(); if (chunk.isLast()) { readingChunks = false; - responseContent.append("END OF CONTENT\r\n"); + buf.append("END OF CONTENT\r\n"); HttpChunkTrailer trailer = (HttpChunkTrailer) chunk; if (!trailer.getHeaderNames().isEmpty()) { - responseContent.append("\r\n"); + buf.append("\r\n"); for (String name: trailer.getHeaderNames()) { for (String value: trailer.getHeaders(name)) { - responseContent.append("TRAILING HEADER: " + name + " = " + value + "\r\n"); + buf.append("TRAILING HEADER: " + name + " = " + value + "\r\n"); } } - responseContent.append("\r\n"); + buf.append("\r\n"); } writeResponse(e); } else { - responseContent.append("CHUNK: " + chunk.getContent().toString(CharsetUtil.UTF_8) + "\r\n"); + buf.append("CHUNK: " + chunk.getContent().toString(CharsetUtil.UTF_8) + "\r\n"); } } } @@ -128,8 +128,8 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler { boolean keepAlive = request.isKeepAlive(); // Build the response object. - HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); - response.setContent(ChannelBuffers.copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8)); + HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); + response.setContent(ChannelBuffers.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { @@ -138,7 +138,7 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler { } // Encode the cookie. - String cookieString = request.getHeader(HttpHeaders.Names.COOKIE); + String cookieString = request.getHeader(COOKIE); if (cookieString != null) { CookieDecoder cookieDecoder = new CookieDecoder(); Set cookies = cookieDecoder.decode(cookieString); diff --git a/src/main/java/org/jboss/netty/example/http/websocket/WebSocketServerHandler.java b/src/main/java/org/jboss/netty/example/http/websocket/WebSocketServerHandler.java index 5d9a26fc88..a9145e34fd 100644 --- a/src/main/java/org/jboss/netty/example/http/websocket/WebSocketServerHandler.java +++ b/src/main/java/org/jboss/netty/example/http/websocket/WebSocketServerHandler.java @@ -15,6 +15,13 @@ */ package org.jboss.netty.example.http.websocket; +import static org.jboss.netty.handler.codec.http.HttpHeaders.*; +import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.*; +import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.*; +import static org.jboss.netty.handler.codec.http.HttpMethod.*; +import static org.jboss.netty.handler.codec.http.HttpResponseStatus.*; +import static org.jboss.netty.handler.codec.http.HttpVersion.*; + import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.ChannelFuture; @@ -27,11 +34,11 @@ import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelUpstreamHandler; import org.jboss.netty.handler.codec.http.DefaultHttpResponse; import org.jboss.netty.handler.codec.http.HttpHeaders; -import org.jboss.netty.handler.codec.http.HttpMethod; import org.jboss.netty.handler.codec.http.HttpRequest; import org.jboss.netty.handler.codec.http.HttpResponse; import org.jboss.netty.handler.codec.http.HttpResponseStatus; -import org.jboss.netty.handler.codec.http.HttpVersion; +import org.jboss.netty.handler.codec.http.HttpHeaders.Names; +import org.jboss.netty.handler.codec.http.HttpHeaders.Values; import org.jboss.netty.handler.codec.http.websocket.DefaultWebSocketFrame; import org.jboss.netty.handler.codec.http.websocket.WebSocketFrame; import org.jboss.netty.handler.codec.http.websocket.WebSocketFrameDecoder; @@ -61,25 +68,21 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler { private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) { // Allow only GET methods. - if (req.getMethod() != HttpMethod.GET) { + if (req.getMethod() != GET) { sendHttpResponse( - ctx, req, new DefaultHttpResponse( - HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN)); + ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN)); return; } // Send the demo page. if (req.getUri().equals("/")) { - HttpResponse res = new DefaultHttpResponse( - HttpVersion.HTTP_1_1, HttpResponseStatus.OK); + HttpResponse res = new DefaultHttpResponse(HTTP_1_1, OK); ChannelBuffer content = WebSocketServerIndexPage.getContent(getWebSocketLocation(req)); - res.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/html"); - res.setHeader( - HttpHeaders.Names.CONTENT_LENGTH, - Integer.toString(content.readableBytes())); + res.setHeader(CONTENT_TYPE, "text/html; charset=UTF-8"); + setContentLength(res, content.readableBytes()); res.setContent(content); sendHttpResponse(ctx, req, res); @@ -88,20 +91,20 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler { // Serve the WebSocket handshake request. if (req.getUri().equals(WEBSOCKET_PATH) && - HttpHeaders.Values.UPGRADE.equalsIgnoreCase(req.getHeader(HttpHeaders.Names.CONNECTION)) && - HttpHeaders.Values.WEBSOCKET.equalsIgnoreCase(req.getHeader(HttpHeaders.Names.UPGRADE))) { + Values.UPGRADE.equalsIgnoreCase(req.getHeader(CONNECTION)) && + WEBSOCKET.equalsIgnoreCase(req.getHeader(Names.UPGRADE))) { // Create the WebSocket handshake response. HttpResponse res = new DefaultHttpResponse( - HttpVersion.HTTP_1_1, + HTTP_1_1, new HttpResponseStatus(101, "Web Socket Protocol Handshake")); - res.addHeader(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET); - res.addHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE); - res.addHeader(HttpHeaders.Names.WEBSOCKET_ORIGIN, req.getHeader(HttpHeaders.Names.ORIGIN)); - res.addHeader(HttpHeaders.Names.WEBSOCKET_LOCATION, getWebSocketLocation(req)); - String protocol = req.getHeader(HttpHeaders.Names.WEBSOCKET_PROTOCOL); + res.addHeader(Names.UPGRADE, WEBSOCKET); + res.addHeader(CONNECTION, Values.UPGRADE); + res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN)); + res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req)); + String protocol = req.getHeader(WEBSOCKET_PROTOCOL); if (protocol != null) { - res.addHeader(HttpHeaders.Names.WEBSOCKET_PROTOCOL, protocol); + res.addHeader(WEBSOCKET_PROTOCOL, protocol); } // Upgrade the connection and send the handshake response. @@ -117,8 +120,7 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler { // Send an error page otherwise. sendHttpResponse( - ctx, req, new DefaultHttpResponse( - HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN)); + ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN)); } private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { @@ -133,9 +135,7 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler { res.setContent( ChannelBuffers.copiedBuffer( res.getStatus().toString(), CharsetUtil.UTF_8)); - res.setHeader( - HttpHeaders.Names.CONTENT_LENGTH, - Integer.toString(res.getContent().readableBytes())); + setContentLength(res, res.getContent().readableBytes()); } // Send the response and close the connection if necessary.