Class FileHandler
- java.lang.Object
-
- sunlabs.brazil.server.FileHandler
-
- All Implemented Interfaces:
Handler
public class FileHandler extends java.lang.Object implements Handler
Standard handler for fetching static files. This handler does URL to file conversion, file suffix to mime type lookup, delivery of index files where providing directory references, and redirection for missing slashes (/) at the end of directory requests.The following configuration parameters are used:
- root
- property for document root (.) Since the document root is common to many handlers, if no root property is found with the supplied prefix, then the root property with the empty prefix ("") is used instead. This allows many handlers to share the common property.
- default
- The document to deliver if the URL ends in "/". (defaults to index.html.)
- prefix
- Only url's that start with this are allowed.
defaults to "". The prefix is removed from the
url before looking it up in the file system.
So, if prefix is
/foo
then the the file[root]/foo/bar.html
will be delivered in response to the url/bar.html
. - mime
- property for mime type For each file suffix .XX, the property mime.XX is used to determine the mime type. If no property exists (or its value is "unknown", the document will not be delivered.
- mimePatterns
- List of glob patterns that match file name suffixes
for matching mime types. For example:
mimePatterns=.x* .a? mime.x*=text/xml mime.a?=application/octet-stream
The types corrosponding to mime patterns are searched for in mimePattern order, first looking forprefix.mime.pattern
thenmime.pattern
. If neither property exists, then the type is invalid. - getOnly
- If defined, only "GET" requests will be processed. By default, all request types are handled. (Note: this is the inverse of the previous policy, defined by the undocumented "allow" parameter).
The FileHandler sets the following entries in the request properties as a side-effect:
- fileName
- The absolute path of the file that couldn't be found.
- DirectoryName
- If the URL specified is a directory name, its absolute path is placed here.
- lastModified
- The Time stamp of the last modified time
This handler supports a subset of the http
range
header of the formrange bytes=[start]-[end]
, where start and end are byte positions in the file, starting at zero. A large or missing end value is treated as the end of the file. If a validrange
header is found, the appropriatecontent-range
header is returned, along with the partial contents of the file.- Version:
- 2.8
- Author:
- Stephen Uhler
-
-
Constructor Summary
Constructors Constructor Description FileHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.lang.String
getMimeType(java.lang.String name, java.util.Properties props, java.lang.String prefix)
Get the mime type based on the suffix of a String.boolean
init(Server server, java.lang.String prefix)
Initialize the file handler.boolean
respond(Request request)
Find, read, and deliver via http the requested file.static void
sendFile(Request request, java.io.File file, int code, java.lang.String type)
Send a file as a response.static void
setModified(java.util.Properties props, long mod)
Set the "lastModified" request property.static java.lang.String
urlToPath(java.lang.String url)
Helper function to convert an url into a pathname.
-
-
-
Field Detail
-
MIME
public static final java.lang.String MIME
- See Also:
- Constant Field Values
-
UNKNOWN
public static final java.lang.String UNKNOWN
- See Also:
- Constant Field Values
-
ROOT
public static final java.lang.String ROOT
- See Also:
- Constant Field Values
-
urlPrefix
public java.lang.String urlPrefix
-
-
Method Detail
-
init
public boolean init(Server server, java.lang.String prefix)
Initialize the file handler.- Specified by:
init
in interfaceHandler
- Parameters:
server
- The HTTP server that created thisHandler
. TypicalHandler
s will useServer.props
to obtain run-time configuration information.prefix
- The handlers name. The string thisHandler
may prepend to all of the keys that it uses to extract configuration information fromServer.props
. This is set (by theServer
andChainHandler
) to help avoid configuration parameter namespace collisions.- Returns:
- The file handler always returns true.
-
respond
public boolean respond(Request request) throws java.io.IOException
Find, read, and deliver via http the requested file. The server propertyroot
is used as the document root. The document root is recalculated for each request, so an upstream handler may change it for that request. For URL's ending with "/", the server propertydefault
(normally index.html) is automatically appended. If the file suffix is not found as a server propertymime.suffix
, the file is not delivered.- Specified by:
respond
in interfaceHandler
- Parameters:
request
- TheRequest
object that represents the HTTP request.- Returns:
true
if the request was handled. A request was handled if a response was supplied to the client, typically by callingRequest.sendResponse()
orRequest.sendError
.- Throws:
java.io.IOException
- if there was an I/O error while sending the response to the client. Typically, in that case, theServer
will (try to) send an error message to the client and then close the client's connection.The
IOException
should not be used to silently ignore problems such as being unable to access some server-side resource (for example getting aFileNotFoundException
due to not being able to open a file). In that case, theHandler
's duty is to turn thatIOException
into a HTTP response indicating, in this case, that a file could not be found.
-
getMimeType
public static java.lang.String getMimeType(java.lang.String name, java.util.Properties props, java.lang.String prefix)
Get the mime type based on the suffix of a String. The suffix (e.g. text after the last ".") is used to lookup the entry "prefix.mime.[suffix]" then "mime.[suffix]" inprops
. If neither entry is found, then mime glob pattern are used, if available. If there is no suffix, then the empty string is used.If the mime type is set to the special string "unknown", then the type is unknown. This allows specific types to be undefined when glob patterns are used.
If the property "prefix.mimePatterns" (or "mimePatterns") exists, then it specifies a white-space delimited set of glob style patterns for matching file suffixes to types. If a match for a specific file suffix fails, then the property "mime.[pattern]" is used for type comparisons.
The entries:
mimePatterns=*ml mime.*ml=text/xml
would associate the type "text/xml" with the file foo.html, foo.xml and foo.dhtml. The entries:mimePatterns=* mime*=application/octet-stream mime.config=unknown
Would set the types for all file types not otherwise defined to be "application/octet-stream", except that files ending in ".config" would have no type (e.g. they would generate a file not found error).- Parameters:
name
- The string to compute the mime type forprops
- The properties to look up the mime types in.- Returns:
- The type (or null if not found).
-
urlToPath
public static java.lang.String urlToPath(java.lang.String url)
Helper function to convert an url into a pathname.- Collapse all %XX sequences.
- Ignore missing initial "/".
- Collapse all "/..", "/.", and "//" sequences.
URL(String)
collapses all "/.." (and "/.") sequences, except for a trailing "/.." (or "/."), which would lead to the possibility of escaping from the document root.File.getPath
in jdk-1.1 leaves all the "//" constructs in, but it collapses them in jdk-1.2, so we have to always take it out ourselves, just to be sure.- Parameters:
url
- The file path from the URL (that is, minus the "http://host" part). May benull
.- Returns:
- The path that corresponds to the URL. The returned value begins with "/". The caller can concatenate this path onto the end of some document root.
-
sendFile
public static void sendFile(Request request, java.io.File file, int code, java.lang.String type) throws java.io.IOException
Send a file as a response.- Parameters:
request
- The request objectfileHandle
- The file to outputtype
- The mime type of the file- Throws:
java.io.IOException
-
setModified
public static void setModified(java.util.Properties props, long mod)
Set the "lastModified" request property. If already set, use the most recent value.- Parameters:
props
- Where to find the "lastModified" propertymod
- The modidied time, in ms since the epoch
-
-