org.mortbay.http.handler

Class ResourceHandler

Implemented Interfaces:
HttpHandler, LifeCycle, Serializable

public class ResourceHandler
extends AbstractHttpHandler

Handler to serve files and resources. Serves files from a given resource URL base and implements the GET, HEAD, DELETE, OPTIONS, PUT, MOVE methods and the IfModifiedSince and IfUnmodifiedSince header fields. A simple memory cache is also provided to reduce file I/O. HTTP/1.1 ranges are supported.
Version:
$Id: ResourceHandler.java,v 1.66 2005/08/24 08:18:17 gregwilkins Exp $
Authors:
Nuno Preguiça
Greg Wilkins

Constructor Summary

ResourceHandler()
Construct a ResourceHandler.

Method Summary

String[]
getAllowedMethods()
String
getAllowedString()
int
getMinGzipLength()
Get minimum content length for GZIP encoding.
boolean
getRedirectWelcome()
protected Resource
getResource(String pathInContext)
get Resource to serve.
void
handle(String pathInContext, String pathParams, HttpRequest request, HttpResponse response)
Handle a request.
void
handleGet(HttpRequest request, HttpResponse response, String pathInContext, String pathParams, Resource resource)
boolean
isAcceptRanges()
boolean
isDirAllowed()
boolean
isMethodAllowed(String method)
void
sendData(HttpRequest request, HttpResponse response, String pathInContext, Resource resource, boolean writeHeaders)
void
setAcceptRanges(boolean ar)
Set if the handler accepts range requests.
void
setAllowedMethods(String[] methods)
void
setDirAllowed(boolean dirAllowed)
void
setMinGzipLength(int minGzipLength)
Set minimum content length for GZIP encoding.
void
setRedirectWelcome(boolean redirectWelcome)
void
start()
void
stop()
void
writeHeaders(HttpResponse response, Resource resource, long count)

Methods inherited from class org.mortbay.http.handler.AbstractHttpHandler

getHttpContext, getName, handleTrace, initialize, isStarted, setName, start, stop, toString

Constructor Details

ResourceHandler

public ResourceHandler()
Construct a ResourceHandler.

Method Details

getAllowedMethods

public String[] getAllowedMethods()

getAllowedString

public String getAllowedString()

getMinGzipLength

public int getMinGzipLength()
Get minimum content length for GZIP encoding.
Returns:
Minimum length of content for gzip encoding or -1 if disabled.

getRedirectWelcome

public boolean getRedirectWelcome()
Returns:
True if welcome files are redirected to. False if forward is used.

getResource

protected Resource getResource(String pathInContext)
            throws IOException
get Resource to serve. Map a path to a resource. The default implementation calls HttpContext.getResource but derived handers may provide their own mapping.
Parameters:
pathInContext - The path to find a resource for.
Returns:
The resource to serve.

handle

public void handle(String pathInContext,
                   String pathParams,
                   HttpRequest request,
                   HttpResponse response)
            throws HttpException,
                   IOException
Handle a request. Note that Handlers are tried in order until one has handled the request. i.e. until request.isHandled() returns true. In broad terms this means, either a response has been commited or request.setHandled(true) has been called.
Specified by:
handle in interface HttpHandler
Parameters:
pathInContext - The context path
pathParams - Path parameters such as encoded Session ID
request - The HttpRequest request
response - The HttpResponse response

handleGet

public void handleGet(HttpRequest request,
                      HttpResponse response,
                      String pathInContext,
                      String pathParams,
                      Resource resource)
            throws IOException

isAcceptRanges

public boolean isAcceptRanges()

isDirAllowed

public boolean isDirAllowed()

isMethodAllowed

public boolean isMethodAllowed(String method)

sendData

public void sendData(HttpRequest request,
                     HttpResponse response,
                     String pathInContext,
                     Resource resource,
                     boolean writeHeaders)
            throws IOException

setAcceptRanges

public void setAcceptRanges(boolean ar)
Set if the handler accepts range requests. Default is false;
Parameters:
ar - True if the handler should accept ranges

setAllowedMethods

public void setAllowedMethods(String[] methods)

setDirAllowed

public void setDirAllowed(boolean dirAllowed)

setMinGzipLength

public void setMinGzipLength(int minGzipLength)
Set minimum content length for GZIP encoding.
Parameters:
minGzipLength - If set to a positive integer, then static content larger than this will be served as gzip content encoded if a matching resource is found ending with ".gz"

setRedirectWelcome

public void setRedirectWelcome(boolean redirectWelcome)
Parameters:
redirectWelcome - True if welcome files are redirected to. False if forward is used.

start

public void start()
            throws Exception
Specified by:
start in interface LifeCycle
Overrides:
start in interface AbstractHttpHandler

stop

public void stop()
            throws InterruptedException
Specified by:
stop in interface LifeCycle
Overrides:
stop in interface AbstractHttpHandler

writeHeaders

public void writeHeaders(HttpResponse response,
                         Resource resource,
                         long count)
            throws IOException

Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.