org.mmbase.servlet
Class FileServlet

Package class diagram package 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 46075 2011-11-23 13:26:45Z 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)
           
protected  void doHead(HttpServletRequest req, HttpServletResponse resp)
           
protected  void doPut(HttpServletRequest req, HttpServletResponse resp)
           
protected  Map<String,Integer> getAssociations()
           
static File getDirectory()
           
static File getFile(String pathInfo, ServletResponse res)
           
static FileServlet getInstance()
          FileServlet should normally be present only once.
protected  long getLastModified(HttpServletRequest req)
           
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.
static String getMetaValue(String field, String value)
          Constructs a key/value entry for use in the meta-files.
protected  FileServlet.ChainedRange getRange(HttpServletRequest req, File file)
          http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
 String getServletInfo()
           
protected static File getSessionFile(File f)
           
protected  boolean ignores(String pi)
           
 void init()
           
protected  void listing(HttpServletRequest req, HttpServletResponse resp, OutputStream o, File directory)
           
static String parseMetaValue(String fieldName, String cd)
          Parses back the result of getMetaValue(String, String).
static void protectFile(HttpServletRequest req, File f)
          FileServlet can be used to serve files out only to the owner of certain HTTP-session.
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)
           
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
Overrides:
init in class BridgeServlet
Throws:
ServletException

setMMBase

public void setMMBase(MMBase mmb)
Specified by:
setMMBase in interface MMBaseStarter
Overrides:
setMMBase in class MMBaseServlet

getServletInfo

public String getServletInfo()
Specified by:
getServletInfo in interface Servlet
Overrides:
getServletInfo in class MMBaseServlet

getAssociations

protected Map<String,Integer> getAssociations()
Overrides:
getAssociations in class MMBaseServlet

getInstance

public static FileServlet getInstance()
FileServlet should normally be present only once. This method returns that instance. It can be used to acquire some information from it, e.g. getDirectory().

Returns:
The first instance of FileServlet, or null if there isn't one.

getDirectory

public static File getDirectory()
Returns:
The directory which is served out.

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
FileServlet can be used to serve files out only to the owner of certain HTTP-session. This happens if you 'protected' the given file with this method.

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). Such a file contains the extra HTTP headers (e.g. the Content-Disposition) and can be found in .web/<filename>.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

parseMetaValue

public static String parseMetaValue(String fieldName,
                                    String cd)
Parses back the result of getMetaValue(String, String). E.g. to get the filename back from the content disposition in the meta header maps do: String inDisposition = FileServlet.parseMetaValue("filename", meta.get("Content-Disposition"));

Since:
MMBase-1.9.6

getMetaValue

public static String getMetaValue(String field,
                                  String value)
Constructs a key/value entry for use in the meta-files. This uses RFC 5987 to also encode other characters than those of ISO-8859-1 (we use UTF-8). Is is also added simply as field=value, which can serve as a fall back for browsers which don't yet understand the RFC. E.g. meta.put("Content-Disposition", "attachment; " + FileServlet.getMetaValue("filename", name));

Since:
MMBase-1.9.6

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
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 usage. 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)
Overrides:
getLastModified in class BridgeServlet


MMBase2 Resources 2.0-SNAPSHOT - 2013-03-30T06:37