Class HttpStaticFileServerHandler
- java.lang.Object
-
- org.jboss.netty.channel.SimpleChannelUpstreamHandler
-
- org.jboss.netty.example.http.file.HttpStaticFileServerHandler
-
- All Implemented Interfaces:
ChannelHandler
,ChannelUpstreamHandler
public class HttpStaticFileServerHandler extends SimpleChannelUpstreamHandler
A simple handler that serves incoming HTTP requests to send their respective HTTP responses. It also implements'If-Modified-Since'
header to take advantage of browser cache, as described in RFC 2616.How Browser Caching Works
Web browser caching works with HTTP headers as illustrated by the following sample:- Request #1 returns the content of
/file1.txt
. - Contents of
/file1.txt
is cached by the browser. - Request #2 for
/file1.txt
does return the contents of the file again. Rather, a 304 Not Modified is returned. This tells the browser to use the contents stored in its cache. - The server knows the file has not been modified because the
If-Modified-Since
date is the same as the file's last modified date.
Request #1 Headers =================== GET /file1.txt HTTP/1.1 Response #1 Headers =================== HTTP/1.1 200 OK Date: Tue, 01 Mar 2011 22:44:26 GMT Last-Modified: Wed, 30 Jun 2010 21:36:48 GMT Expires: Tue, 01 Mar 2012 22:44:26 GMT Cache-Control: private, max-age=31536000 Request #2 Headers =================== GET /file1.txt HTTP/1.1 If-Modified-Since: Wed, 30 Jun 2010 21:36:48 GMT Response #2 Headers =================== HTTP/1.1 304 Not Modified Date: Tue, 01 Mar 2011 22:44:28 GMT
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static int
HTTP_CACHE_SECONDS
(package private) static java.lang.String
HTTP_DATE_FORMAT
(package private) static java.lang.String
HTTP_DATE_GMT_TIMEZONE
-
Constructor Summary
Constructors Constructor Description HttpStaticFileServerHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
Invoked when an exception was raised by an I/O thread or aChannelHandler
.void
messageReceived(ChannelHandlerContext ctx, MessageEvent e)
Invoked when a message object (e.g:ChannelBuffer
) was received from a remote peer.private static java.lang.String
sanitizeUri(java.lang.String uri)
private static void
sendError(ChannelHandlerContext ctx, HttpResponseStatus status)
private static void
sendNotModified(ChannelHandlerContext ctx)
When file timestamp is the same as what the browser is sending up, send a "304 Not Modified"private static void
setContentTypeHeader(HttpResponse response, java.io.File file)
Sets the content type header for the HTTP Responseprivate static void
setDateAndCacheHeaders(HttpResponse response, java.io.File fileToCache)
Sets the Date and Cache headers for the HTTP Responseprivate static void
setDateHeader(HttpResponse response)
Sets the Date header for the HTTP response-
Methods inherited from class org.jboss.netty.channel.SimpleChannelUpstreamHandler
channelBound, channelClosed, channelConnected, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, handleUpstream, writeComplete
-
-
-
-
Field Detail
-
HTTP_DATE_FORMAT
static final java.lang.String HTTP_DATE_FORMAT
- See Also:
- Constant Field Values
-
HTTP_DATE_GMT_TIMEZONE
static final java.lang.String HTTP_DATE_GMT_TIMEZONE
- See Also:
- Constant Field Values
-
HTTP_CACHE_SECONDS
static final int HTTP_CACHE_SECONDS
- See Also:
- Constant Field Values
-
-
Method Detail
-
messageReceived
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelUpstreamHandler
Invoked when a message object (e.g:ChannelBuffer
) was received from a remote peer.- Overrides:
messageReceived
in classSimpleChannelUpstreamHandler
- Throws:
java.lang.Exception
-
exceptionCaught
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
Description copied from class:SimpleChannelUpstreamHandler
Invoked when an exception was raised by an I/O thread or aChannelHandler
.- Overrides:
exceptionCaught
in classSimpleChannelUpstreamHandler
-
sanitizeUri
private static java.lang.String sanitizeUri(java.lang.String uri)
-
sendError
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status)
-
sendNotModified
private static void sendNotModified(ChannelHandlerContext ctx)
When file timestamp is the same as what the browser is sending up, send a "304 Not Modified"
-
setDateHeader
private static void setDateHeader(HttpResponse response)
Sets the Date header for the HTTP response
-
setDateAndCacheHeaders
private static void setDateAndCacheHeaders(HttpResponse response, java.io.File fileToCache)
Sets the Date and Cache headers for the HTTP Response- Parameters:
fileToCache
- the file to extract content type
-
setContentTypeHeader
private static void setContentTypeHeader(HttpResponse response, java.io.File file)
Sets the content type header for the HTTP Response- Parameters:
file
- the file to extract content type
-
-