org.mmbase.servlet
Class FileServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by org.mmbase.servlet.MMBaseServlet
              extended by org.mmbase.servlet.BridgeServlet
                  extended by org.mmbase.servlet.FileServlet
All Implemented Interfaces:
Serializable, Servlet, ServletConfig, MMBaseStarter

public class FileServlet
extends BridgeServlet

Straight-forward filter which can serve files from one directory (the directory 'files' in the mmbase 'datadir') outside the web application root.

Since:
MMBase-1.9
Version:
$Id: FileServlet.java 42794 2010-07-02 07:05:28Z michiel $
Author:
Michiel Meeuwissen
See Also:
AttachmentServlet, Serialized Form

Nested Class Summary
protected static class FileServlet.ChainedRange
          This implementation of Range parses and combines a number of FileServlet.FirstLastRanges.
static class FileServlet.DirectoryFunction
          The directory as a function.
protected static class FileServlet.FirstLastRange
          Implementation of Range simply stating the first and last chars which are available, perhaps with a maximum too.
protected static interface FileServlet.Range
           
 
Nested classes/interfaces inherited from class org.mmbase.servlet.BridgeServlet
BridgeServlet.QueryParts
 
Field Summary
(package private) static String PROTECTED_FILES
           
 
Fields inherited from class org.mmbase.servlet.BridgeServlet
FILE_PATTERN, MESSAGE_ATTRIBUTE
 
Fields inherited from class org.mmbase.servlet.MMBaseServlet
mmbase, retryAfter
 
Constructor Summary
FileServlet()
           
 
Method Summary
protected  boolean canRead(HttpServletRequest req, File f)
          Returns whether the given file can be served out for the given request.
protected  File checkFile(HttpServletRequest req, HttpServletResponse resp)
           
protected  void doGet(HttpServletRequest req, HttpServletResponse resp)
          Serves MMBase version information.
protected  void doHead(HttpServletRequest req, HttpServletResponse resp)
           
protected  void doPut(HttpServletRequest req, HttpServletResponse resp)
           
protected  Map<String,Integer> getAssociations()
          On default, servlets are not associated with any function.
static File getDirectory()
           
static File getFile(String pathInfo, ServletResponse res)
           
static FileServlet getInstance()
           
protected  long getLastModified(HttpServletRequest req)
          The idea is that a 'bridge servlet' on default serves 'nodes', and that there could be defined a 'last modified' time for nodes.
protected  byte[] getListingBytes(HttpServletRequest req, HttpServletResponse resp, File directory)
           
 File getMetaFile(File f)
          FileServlet supports 'meta' files like Cern HTTPD (and apaches mod_cern_meta).
 Map<String,String> getMetaHeaders(File f)
          Returns contents of getMetaFile(java.io.File) as a Map.
protected  FileServlet.ChainedRange getRange(HttpServletRequest req, File file)
          http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
 String getServletInfo()
          Returns information about this servlet.
protected static File getSessionFile(File f)
           
protected  boolean ignores(String pi)
           
 void init()
          Inits lastmodifiedField.
protected  void listing(HttpServletRequest req, HttpServletResponse resp, OutputStream o, File directory)
           
static void protectFile(HttpServletRequest req, File f)
           
protected  void setHeaders(HttpServletRequest req, HttpServletResponse resp, File file)
           
 void setMetaHeaders(File f, Map<String,String> meta)
          Sets contents of getMetaFile(java.io.File) as a Map.
 void setMMBase(MMBase mmb)
          Sets the mmbase member.
protected static void stream(FileServlet.ChainedRange range, InputStream in, OutputStream out)
           
protected  void stream(HttpServletRequest req, HttpServletResponse resp, OutputStream o, File file)
           
 
Methods inherited from class org.mmbase.servlet.BridgeServlet
desperatelyGetNode, findCloud, getAnonymousCloud, getClassCloud, getCloud, getCloudName, getNode, getServedNode, readQuery, readQuery, readQueryFromRequestURI, readServletPath
 
Methods inherited from class org.mmbase.servlet.MMBaseServlet
associateMapping, checkInited, decRefCount, destroy, getBasePath, getMMBase, getRequestURL, getServletByAssociation, getServletByMapping, getServletMappings, getServletMappingsByAssociation, incRefCount, service, setInitException
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doOptions, doPost, doTrace, service
 
Methods inherited from class javax.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROTECTED_FILES

static final String PROTECTED_FILES
Constructor Detail

FileServlet

public FileServlet()
Method Detail

init

public void init()
          throws ServletException
Description copied from class: BridgeServlet
Inits lastmodifiedField. The init of an MMBaseServlet checks if MMBase is running. It not then it is started.

Overrides:
init in class BridgeServlet
Throws:
ServletException

setMMBase

public void setMMBase(MMBase mmb)
Description copied from class: MMBaseServlet
Sets the mmbase member. Can be overriden to implement extra initalization for the servlet which needs a running MMBase.

Specified by:
setMMBase in interface MMBaseStarter
Overrides:
setMMBase in class MMBaseServlet

getServletInfo

public String getServletInfo()
Description copied from class: MMBaseServlet
Returns information about this servlet. Don't forget to override it.

Specified by:
getServletInfo in interface Servlet
Overrides:
getServletInfo in class MMBaseServlet

getAssociations

protected Map<String,Integer> getAssociations()
Description copied from class: MMBaseServlet
On default, servlets are not associated with any function. This function is called in the init method.

Overrides:
getAssociations in class MMBaseServlet
Returns:
A map of Strings (function) -> Integer (priority). Never null.

getInstance

public static FileServlet getInstance()

getDirectory

public static File getDirectory()

getFile

public static File getFile(String pathInfo,
                           ServletResponse res)

getSessionFile

protected static File getSessionFile(File f)

protectFile

public static void protectFile(HttpServletRequest req,
                               File f)
                        throws IOException
Throws:
IOException
Since:
MMBase-1.9.2

canRead

protected boolean canRead(HttpServletRequest req,
                          File f)
Returns whether the given file can be served out for the given request. You can use protectFile(javax.servlet.http.HttpServletRequest, java.io.File) to make the file only accessible to the current http session.


ignores

protected boolean ignores(String pi)

getMetaFile

public File getMetaFile(File f)
FileServlet supports 'meta' files like Cern HTTPD (and apaches mod_cern_meta).

Since:
MMBase-1.9.2

getMetaHeaders

public Map<String,String> getMetaHeaders(File f)
Returns contents of getMetaFile(java.io.File) as a Map.

Since:
MMBase-1.9.2

setMetaHeaders

public void setMetaHeaders(File f,
                           Map<String,String> meta)
Sets contents of getMetaFile(java.io.File) as a Map.

Since:
MMBase-1.9.2

checkFile

protected File checkFile(HttpServletRequest req,
                         HttpServletResponse resp)
                  throws ServletException,
                         IOException
Throws:
ServletException
IOException

setHeaders

protected void setHeaders(HttpServletRequest req,
                          HttpServletResponse resp,
                          File file)
                   throws ServletException,
                          IOException
Throws:
ServletException
IOException

doHead

protected void doHead(HttpServletRequest req,
                      HttpServletResponse resp)
               throws ServletException,
                      IOException
Overrides:
doHead in class HttpServlet
Throws:
ServletException
IOException

doGet

protected void doGet(HttpServletRequest req,
                     HttpServletResponse resp)
              throws ServletException,
                     IOException
Description copied from class: MMBaseServlet
Serves MMBase version information. This doesn't do much useful yet, but one could image lots of cool stuff here. Any other MMBase servlet will probably override this method.

Overrides:
doGet in class MMBaseServlet
Throws:
ServletException
IOException

getRange

protected FileServlet.ChainedRange getRange(HttpServletRequest req,
                                            File file)
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Returns:
A ChainedRange object if Range header was present and If-Range didn't provide useage. null otherwise.
Since:
MMBase-2.0

stream

protected static void stream(FileServlet.ChainedRange range,
                             InputStream in,
                             OutputStream out)
                      throws IOException
Throws:
IOException
To Do:
Generalize this stuff with Ranges to HandleServlet, so that it also could work for images and attachments.

stream

protected void stream(HttpServletRequest req,
                      HttpServletResponse resp,
                      OutputStream o,
                      File file)
               throws IOException
Throws:
IOException

getListingBytes

protected byte[] getListingBytes(HttpServletRequest req,
                                 HttpServletResponse resp,
                                 File directory)
                          throws IOException
Throws:
IOException

listing

protected void listing(HttpServletRequest req,
                       HttpServletResponse resp,
                       OutputStream o,
                       File directory)
                throws IOException
Throws:
IOException

doPut

protected void doPut(HttpServletRequest req,
                     HttpServletResponse resp)
              throws ServletException,
                     IOException
Overrides:
doPut in class HttpServlet
Throws:
ServletException
IOException

getLastModified

protected long getLastModified(HttpServletRequest req)
Description copied from class: BridgeServlet
The idea is that a 'bridge servlet' on default serves 'nodes', and that there could be defined a 'last modified' time for nodes. This can't be determined right now, so 'now' is returned. This function is defined in HttpServlet

Overrides:
getLastModified in class BridgeServlet


MMBase 2.0-SNAPSHOT - null