|
![]() |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectsunlabs.brazil.server.FileHandler (view source)
public class FileHandler
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:
/foo
then the the file
[root]/foo/bar.html
will be delivered
in response to the url /bar.html
.
mimePatterns=.x* .a? mime.x*=text/xml mime.a?=application/octet-streamThe types corrosponding to mime patterns are searched for in mimePattern order, first looking for
prefix.mime.pattern
then
mime.pattern
. If neither property exists,
then the type is invalid.
The FileHandler sets the following entries in the request properties as a side-effect:
This handler supports a subset of the http range
header of the form
range 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 valid
range
header is found, the appropriate
content-range
header is returned, along with the partial
contents of the file.
Field Summary | |
---|---|
static String |
MIME
|
static String |
ROOT
|
static String |
UNKNOWN
|
String |
urlPrefix
|
Constructor Summary | |
---|---|
FileHandler()
|
Method Summary | |
---|---|
static String |
getMimeType(String name,
Properties props,
String prefix)
Get the mime type based on the suffix of a String. |
static String |
getMimeType(String name,
Properties props,
String prefix,
boolean strict)
If "strict" is set, only return types that specifically match properties with this handlers prefix. |
boolean |
init(Server server,
String prefix)
Initialize the file handler. |
boolean |
respond(Request request)
Find, read, and deliver via http the requested file. |
static void |
sendFile(Request request,
File file,
int code,
String type)
Send a file as a response. |
static void |
setModified(Properties props,
long mod)
Set the "lastModified" request property. |
static String |
urlToPath(String url)
Helper function to convert an url into a pathname. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String MIME
public static final String UNKNOWN
public static final String ROOT
public String urlPrefix
Constructor Detail |
---|
public FileHandler()
Method Detail |
---|
public boolean init(Server server, String prefix)
init
in interface Handler
server
- The HTTP server that created this Handler
.
Typical Handler
s will use Server.props
to obtain run-time configuration information.prefix
- The handlers name.
The string this Handler
may prepend to all
of the keys that it uses to extract configuration information
from Server.props
. This is set (by the Server
and ChainHandler
) to help avoid configuration parameter
namespace collisions.
public boolean respond(Request request) throws IOException
root
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 property default
(normally index.html) is automatically appended.
If the file suffix is not found as a server property
mime.suffix
, the file is not delivered.
respond
in interface Handler
request
- The Request
object that represents the HTTP
request.
true
if the request was handled. A request was
handled if a response was supplied to the client, typically
by calling Request.sendResponse()
or
Request.sendError
.
IOException
- if there was an I/O error while sending the response to
the client. Typically, in that case, the Server
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 a
FileNotFoundException
due to not being able
to open a file). In that case, the Handler
's
duty is to turn that IOException
into a
HTTP response indicating, in this case, that a file could
not be found.
public static String getMimeType(String name, Properties props, String prefix)
props
. 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/xmlwould associate the type "text/xml" with the file foo.html, foo.xml and foo.dhtml. The entries:
mimePatterns=* mime*=application/octet-stream mime.config=unknownWould 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).
name
- The string to compute the mime type forprops
- The properties to look up the mime types in.prefix
- The properties prefix for the name lookup
public static String getMimeType(String name, Properties props, String prefix, boolean strict)
public static String urlToPath(String url)
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.
url
- The file path from the URL (that is, minus the "http://host"
part). May be null
.
public static void sendFile(Request request, File file, int code, String type) throws IOException
request
- The request objectfile
- The file to outputcode
- The HTTP status code.type
- The mime type of the file
IOException
public static void setModified(Properties props, long mod)
props
- Where to find the "lastModified" propertymod
- The modidied time, in ms since the epoch
|
Version Kenai-svn-r24, Generated 08/18/09 Copyright (c) 2001-2009, Sun Microsystems. |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |