Sun and Oracle Community Voices How to Buy Log In United States [Change] English

»  Spotlight Articles
»  Projects
»  Publications
»  People
»  Awards
»  Events
»  Downloads
»  Internships
»  Contrarian Minds
»  About Sun Labs
Brazil project: Class FileHandler

sunlabs.brazil.server Class FileHandler

java.lang.Object
  |
  +--sunlabs.brazil.server.FileHandler
All Implemented Interfaces:
Handler

public class FileHandler
extends 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. This handler only responds to GET requests.

The following coniguration parameters are used:

root
property for document root (.) Since the document root is common to many handers, if non 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
property for default document, given directory (index.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, the document will not be delivered.

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.


Field Summary
static String ALLOW            
static String DEFAULT            
static String MIME            
static String ROOT            
 
Constructor Summary
FileHandler()            
 
Method Summary
 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 String urlToPath(String url)           Helper function to convert an url into a pathname.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ROOT

public static final String ROOT

DEFAULT

public static final String DEFAULT

MIME

public static final String MIME

ALLOW

public static final String ALLOW
Constructor Detail

FileHandler

public FileHandler()
Method Detail

init

public boolean init(Server server,
                    String prefix)
Initialize the file handler.
Specified by:
init in interface Handler
Returns:
The file handler always returns true.

respond

public boolean respond(Request request)
                throws IOException
Find, read, and deliver via http the requested file. The server property 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.
Specified by:
respond in interface Handler
Tags copied from interface: Handler
Parameters:
request - The Request 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 calling Request.sendResponse() or Request.sendError.
Throws:
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.


urlToPath

public static String urlToPath(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 be null.

sendFile

public static void sendFile(Request request,
                            File file,
                            int code,
                            String type)
                     throws IOException
Send a file as a response.
Parameters:
request - The request object
fileHandle - The file to output
type - The mime type of the file

Version 2.2, Generated 07/25/00
Copyright (c) 2000, Sun Microsystems.