Dekoh platform API

dekoh.portal.util
Class FileServer

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by dekoh.portal.util.FileServer
All Implemented Interfaces:
Serializable, Servlet, ServletConfig
Direct Known Subclasses:
AbstractResizeImageServlet, AppIconServlet

public abstract class FileServer
extends HttpServlet

Provides an abstract class to be subclassed to create a HTTP servlet suitable for serving files which are not modified frequently.

A sub class of FileServer must implement method : FileServer.getRequestedFile(javax.servlet.http.HttpServletRequest).

The last modified time of the file returned by this method will be returned by getLastModified method. When this file is not modified since it was last served the web server will return a response with HTTP standard status code NOT_MODIFIED. Browser or a proxy cache upon noticing this status code in the response will not fetch actual bytes of the file being requested, and will serve the cached copy.

Hence subclassing this class makes browser and proxy caches work more effectively in serving this content, reducing the load on server and network resources.

Few examples in which extending FileServer makes sense:

There's almost no reason to override the doGet, and doPost methods. doPost calls the doGet method which writes the bytes of the file returned by FileServer.getRequestedFile(javax.servlet.http.HttpServletRequest) method to the output stream of the HttpServletResponse.

In situations where lastModified time on the FileServer.getRequestedFile(javax.servlet.http.HttpServletRequest) does not represent availability of new information, sub classes can override the FileServer.getLastModified(javax.servlet.http.HttpServletRequest) method.

In situations where authorization should preceed file to be served sub classes can override FileServer.authorizedToAccessCachedCopy(javax.servlet.http.HttpServletRequest) method.

Thread Safety: This implementation is not thread safe. In particular no file lock is obtained while requested file is being served. Implementations have to make sure that when the method getRequestedFile is accessed concurrently, all the concurrent requests are served without errors.

Since:
Apr 30, 2007
See Also:
Serialized Form

Field Summary
static String FILE_EXT_HEADER
           
protected static String FILE_TO_SERVE
          Request attribute used with-in this servlet to store path to the file to be served.
protected static int UNKNOWN
           
 
Constructor Summary
FileServer()
           
 
Method Summary
protected  boolean authorizedToAccessCachedCopy(HttpServletRequest request)
           
protected  void copyStream(InputStream in, OutputStream out)
           
protected  void doGet(HttpServletRequest request, HttpServletResponse response)
           
protected  void doPost(HttpServletRequest request, HttpServletResponse response)
           
protected  long getLastModified(HttpServletRequest request)
           
protected abstract  File getRequestedFile(HttpServletRequest request)
          Serves the resource requested in FileServer.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) or FileServer.doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) method of this servlet.
protected  long handleExceptionInLastModified(Exception e)
           
protected  void serveRequestedFile(File file, HttpServletRequest request, HttpServletResponse response)
           
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE_TO_SERVE

protected static final String FILE_TO_SERVE
Request attribute used with-in this servlet to store path to the file to be served.

See Also:
Constant Field Values

UNKNOWN

protected static final int UNKNOWN
See Also:
Constant Field Values

FILE_EXT_HEADER

public static final String FILE_EXT_HEADER
See Also:
Constant Field Values
Constructor Detail

FileServer

public FileServer()
Method Detail

getLastModified

protected long getLastModified(HttpServletRequest request)
Overrides:
getLastModified in class HttpServlet

authorizedToAccessCachedCopy

protected boolean authorizedToAccessCachedCopy(HttpServletRequest request)
Parameters:
request - HttpServletRequest
Returns:
true always

handleExceptionInLastModified

protected long handleExceptionInLastModified(Exception e)

doGet

protected void doGet(HttpServletRequest request,
                     HttpServletResponse response)
              throws ServletException,
                     IOException
Overrides:
doGet in class HttpServlet
Throws:
ServletException
IOException

doPost

protected void doPost(HttpServletRequest request,
                      HttpServletResponse response)
               throws ServletException,
                      IOException
Overrides:
doPost in class HttpServlet
Throws:
ServletException
IOException

getRequestedFile

protected abstract File getRequestedFile(HttpServletRequest request)
                                  throws ServletException,
                                         IOException
Serves the resource requested in FileServer.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) or FileServer.doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) method of this servlet. Bytes of this file will be written to output stream of the HttpServletResponse.

Parameters:
request - - HttpServletRequest
Returns:
File if the return value is null or a File that does not exist on disc, a 404/Page not found response will be sent.
Throws:
ServletException - Servlet Exception
IOException - throws when could not create the requried file

serveRequestedFile

protected void serveRequestedFile(File file,
                                  HttpServletRequest request,
                                  HttpServletResponse response)
                           throws IOException
Throws:
IOException

copyStream

protected void copyStream(InputStream in,
                          OutputStream out)
                   throws IOException
Throws:
IOException

Dekoh platform API

© 2009 Pramati Technologies - Dekoh - Portal for Developers