org.mortbay.http
Class HandlerContext

java.lang.Object
  |
  +--org.mortbay.http.HandlerContext
All Implemented Interfaces:
LifeCycle
Direct Known Subclasses:
ServletHandlerContext

public class HandlerContext
extends java.lang.Object
implements LifeCycle

Context for a collection of HttpHandlers. Handler Context provides an ordered container for HttpHandlers that share the same path prefix, filebase, resourcebase and/or classpath.

A HandlerContext is analageous to a ServletContext in the Servlet API, except that it may contain other types of handler other than servlets.

Convenience methods are provided for adding common handlers. See org.mortbay.jetty.JettyContext for conveniance methods for servlets. Note. that order is important when configuring a HandlerContext. For example, if resource serving is enabled before servlets, then resources take priority.

Version:
$Id: HandlerContext.java,v 1.8 2001/11/05 04:54:36 gregwilkins Exp $
Author:
Greg Wilkins (gregw)
See Also:
HttpServer, HttpHandler

Method Summary
 void addAuthConstraint(java.lang.String pathSpec, java.lang.String role)
          Add an auth security constraint.
 void addHandler(HttpHandler handler)
          Add a HttpHandler to the context.
 void addHandler(int i, HttpHandler handler)
          Add a handler.
 void addPermission(java.security.Permission permission)
          Add a permission to this context.
 void addSecurityConstraint(java.lang.String pathSpec, SecurityConstraint sc)
          Add a security constraint.
 void destroy()
          Stop all listeners then handlers.
 java.lang.Object getAttribute(java.lang.String name)
           
 java.util.Enumeration getAttributeNames()
           
 Resource getBaseResource()
          Get the base resource.
 java.lang.ClassLoader getClassLoader()
          Get the classloader.
 java.lang.String getClassPath()
           
 java.lang.String getContextPath()
           
 java.lang.String getEncodingByMimeType(java.lang.String type)
          Get char encoding by mime type.
 java.util.Map getEncodingMap()
          Get the map of mime type to char encoding.
 java.lang.String getErrorPage(java.lang.String error)
          get error page URI.
 HttpHandler getHandler(java.lang.Class handlerClass)
          Get a handler by class.
 HttpHandler getHandler(int i)
          Get handler by index.
 int getHandlerIndex(HttpHandler handler)
          Get handler index.
 java.util.List getHandlers()
          Get all handlers.
 int getHandlerSize()
          Gent the number of handlers.
 java.util.List getHosts()
           
 HttpServer getHttpServer()
           
 boolean getHttpServerAccess()
          Get HttpServer Access.
 java.lang.String getInitParameter(java.lang.String param)
          Get context init parameter.
 java.util.Enumeration getInitParameterNames()
          Get context init parameter.
 LogSink getLogSink()
          Deprecated. use HttpServer.getRequestLogSink()
 java.lang.String getMimeByExtension(java.lang.String filename)
           
 java.util.Map getMimeMap()
           
 java.security.PermissionCollection getPermissions()
          Get the permissions to be used for this context.
 java.lang.String getRealm()
           
 int getRequests()
           
 Resource getResource(java.lang.String path)
          Get a Resource from the context.
 java.lang.String getResourceAlias(java.lang.String alias)
           
 java.lang.String getResourceBase()
          Set the Resource Base.
 ResourceHandler getResourceHandler()
          Get the context ResourceHandler.
 int getResponses1xx()
           
 int getResponses2xx()
           
 int getResponses3xx()
           
 int getResponses4xx()
           
 int getResponses5xx()
           
 SecurityHandler getSecurityHandler()
          Get the context SecurityHandler.
 boolean getStatsOn()
           
 boolean handle(HttpRequest request, HttpResponse response)
          Handler request.
 boolean handle(java.lang.String pathInContext, java.lang.String pathParams, HttpRequest request, HttpResponse response)
          Handler request.
 boolean isDestroyed()
           
 boolean isRedirectNullPath()
           
 boolean isServingResources()
           
 boolean isStarted()
          Start all handlers then listeners.
 void registerHost(java.lang.String hostname)
          Register a host mapping for this context.
 void removeAttribute(java.lang.String name)
           
 java.lang.String removeErrorPage(java.lang.String error)
           
 void removeHandler(HttpHandler handler)
          Remove a handler.
 HttpHandler removeHandler(int i)
          Remove a handler.
 java.lang.String removeResourceAlias(java.lang.String alias)
           
 void setAttribute(java.lang.String name, java.lang.Object value)
           
 void setBaseResource(Resource base)
          Set the base resource.
 void setClassLoader(java.lang.ClassLoader loader)
          Set the class loader.
 void setClassPath(java.lang.String classPath)
          Sets the class path for the context.
 void setClassPaths(Resource lib, boolean append)
          Sets the class path for the context from the jar and zip files found in the specified resource.
 void setClassPaths(java.lang.String lib, boolean append)
          Sets the class path for the context from the jar and zip files found in the specified resource.
 void setEncodingMap(java.util.Map encodingMap)
          Set the map of mime type to char encoding.
 void setErrorPage(java.lang.String error, java.lang.String uriInContext)
          set error page URI.
 void setHttpServerAccess(boolean access)
          Set HttpServer Access.
 void setInitParameter(java.lang.String param, java.lang.String value)
          Set context init parameter.
 void setLogSink(LogSink logSink)
          Deprecated. use HttpServersetRequestLogSink()
 void setMimeMap(java.util.Map mimeMap)
          Also sets the org.mortbay.http.mimeMap context attribute
 void setMimeMapping(java.lang.String extension, java.lang.String type)
          Set a mime mapping
 void setPermissions(java.security.PermissionCollection permissions)
          Set the permissions to be used for this context.
 void setRealm(java.lang.String realmName)
          Set the SecurityHandler realm name.
 void setRealm(java.lang.String realmName, UserRealm realm)
          Set the SecurityHandler realm.
 void setRedirectNullPath(boolean b)
          Set null path redirection.
 void setResourceAlias(java.lang.String alias, java.lang.String uri)
          Set Resource Alias.
 void setResourceBase(java.lang.String resourceBase)
          Set the Resource Base.
 void setServingResources(boolean serve)
          Setup context for serving Resources as files.
 void setStatsOn(boolean on)
          True set statistics recording on for this context.
 void setTypeEncoding(java.lang.String mimeType, java.lang.String encoding)
          Set the encoding that should be used for a mimeType.
 void start()
          Start all handlers then listeners.
 void statsReset()
           
 void stop()
          Stop all listeners then handlers.
 java.lang.String toString()
           
 java.lang.String toString(boolean detail)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

setInitParameter

public void setInitParameter(java.lang.String param,
                             java.lang.String value)
Set context init parameter. Init Parameters differ from attributes as they can only have string values, servlets cannot set them and they do not have a package scoped name space.
Parameters:
param - param name
value - param value or null

getInitParameter

public java.lang.String getInitParameter(java.lang.String param)
Get context init parameter.
Parameters:
param - param name
Returns:
param value or null

getInitParameterNames

public java.util.Enumeration getInitParameterNames()
Get context init parameter.
Returns:
Enumeration of names

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object value)
Parameters:
name - attribute name
value - attribute value

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Parameters:
name - attribute name
Returns:
attribute value or null

getAttributeNames

public java.util.Enumeration getAttributeNames()
Returns:
enumaration of names.

removeAttribute

public void removeAttribute(java.lang.String name)
Parameters:
name - attribute name

registerHost

public void registerHost(java.lang.String hostname)
Register a host mapping for this context. This method is a shorthand for context.getHttpServer()..addContext(hostname,context). The call does not alter the original registration of the context, nor can it change the context path.
Parameters:
hostname -  

getHosts

public java.util.List getHosts()
Returns:
List of virtual hosts that this context is registered in.

getContextPath

public java.lang.String getContextPath()
Returns:
The context prefix

getHttpServer

public HttpServer getHttpServer()

getClassPath

public java.lang.String getClassPath()

setClassPath

public void setClassPath(java.lang.String classPath)
Sets the class path for the context. A class path is only required for a context if it uses classes that are not in the system class path.
Parameters:
fileBase -  

setClassPaths

public void setClassPaths(Resource lib,
                          boolean append)
Sets the class path for the context from the jar and zip files found in the specified resource.
Parameters:
lib - the resource that contains the jar and/or zip files.
append - true if the classpath entries are to be appended to any existing classpath, or false if they replace the existing classpath.
See Also:
setClassPath

setClassPaths

public void setClassPaths(java.lang.String lib,
                          boolean append)
                   throws java.io.IOException
Sets the class path for the context from the jar and zip files found in the specified resource.
Parameters:
lib - the resource that contains the jar and/or zip files.
append - true if the classpath entries are to be appended to any existing classpath, or false if they are to be prepended.
Throws:
java.io.IOException -  
See Also:
setClassPaths

getClassLoader

public java.lang.ClassLoader getClassLoader()
Get the classloader. If no classloader has been set and the context has been loaded normally, then null is returned. If no classloader has been set and the context was loaded from a classloader, that loader is returned. If a classloader has been set and no classpath has been set then the set classloader is returned. If a classloader and a classpath has been set, then a new URLClassloader initialized on the classpath with the set loader as a partent is return.
Returns:
Classloader or null.

setClassLoader

public void setClassLoader(java.lang.ClassLoader loader)
Set the class loader. If a classpath is also set, this classloader is treated as a parent loader to a URLClassLoader initialized with the classpath. Also sets the org.mortbay.http.HandlerContext.classLoader attribute.
Parameters:
loader -  

getResourceBase

public java.lang.String getResourceBase()
Set the Resource Base. The base resource is the Resource to use as a relative base for all context resources. The ResourceBase attribute is a string version of the baseResource. If a relative file is passed, it is converted to a file URL based on the current working directory.
Returns:
The file or URL to use as the base for all resources within the context.

setResourceBase

public void setResourceBase(java.lang.String resourceBase)
Set the Resource Base. The base resource is the Resource to use as a relative base for all context resources. The ResourceBase attribute is a string version of the baseResource. If a relative file is passed, it is converted to a file URL based on the current working directory.
Parameters:
resourceBase - A URL prefix or directory name.

getResource

public Resource getResource(java.lang.String path)
                     throws java.io.IOException
Get a Resource from the context. The resource base must be set
Parameters:
path - A path to be applied to the resource base
Returns:
A Resource or null if no resourceBasse is set.

getBaseResource

public Resource getBaseResource()
Get the base resource. The base resource is the Resource to use as a relative base for all context resources. The ResourceBase attribute is a string version of the baseResource.
Returns:
The resourceBase as a Resource instance

setBaseResource

public void setBaseResource(Resource base)
Set the base resource. The base resource is the Resource to use as a relative base for all context resources. The ResourceBase attribute is a string version of the baseResource.
Parameters:
base - The resourceBase as a Resource instance

setResourceAlias

public void setResourceAlias(java.lang.String alias,
                             java.lang.String uri)
Set Resource Alias. Resource aliases map resource uri's within a context. They may optionally be used by a handler when looking for a resource. The only known user is org.mortbay.http.handler.Servlet.Context.
Parameters:
alias -  
uri -  

getResourceAlias

public java.lang.String getResourceAlias(java.lang.String alias)

removeResourceAlias

public java.lang.String removeResourceAlias(java.lang.String alias)

setErrorPage

public void setErrorPage(java.lang.String error,
                         java.lang.String uriInContext)
set error page URI.
Parameters:
error - A string representing an error code or a exception classname
uriInContext -  

getErrorPage

public java.lang.String getErrorPage(java.lang.String error)
get error page URI.
Parameters:
error - A string representing an error code or a exception classname
Returns:
URI within context

removeErrorPage

public java.lang.String removeErrorPage(java.lang.String error)

setHttpServerAccess

public void setHttpServerAccess(boolean access)
Set HttpServer Access. If true then the HttpServer instance is available as a context attribute "org.mortbay.http.HttpServer". This should only been done for trusted contexts.
Parameters:
access -  

getHttpServerAccess

public boolean getHttpServerAccess()
Get HttpServer Access. If true then the HttpServer instance is available as a context attribute "org.mortbay.http.HttpServer". This should only been done for trusted contexts.
Returns:
 

getHandlers

public java.util.List getHandlers()
Get all handlers.
Returns:
 

getHandlerSize

public int getHandlerSize()
Gent the number of handlers.
Returns:
 

addHandler

public void addHandler(int i,
                       HttpHandler handler)
Add a handler.
Parameters:
i - The position in the handler list
handler - The handler.

addHandler

public void addHandler(HttpHandler handler)
Add a HttpHandler to the context.
Parameters:
handler -  

getHandler

public HttpHandler getHandler(int i)
Get handler by index.
Parameters:
i -  
Returns:
 

getHandlerIndex

public int getHandlerIndex(HttpHandler handler)
Get handler index.
Parameters:
Handler - instance
Returns:
Index of handler in context or -1 if not found.

getHandler

public HttpHandler getHandler(java.lang.Class handlerClass)
Get a handler by class.
Parameters:
handlerClass -  
Returns:
The first handler that is an instance of the handlerClass

removeHandler

public HttpHandler removeHandler(int i)
Remove a handler. The handler must be stopped before being removed.
Parameters:
i -  

removeHandler

public void removeHandler(HttpHandler handler)
Remove a handler. The handler must be stopped before being removed.

getResourceHandler

public ResourceHandler getResourceHandler()
Get the context ResourceHandler. Conveniance method. If no ResourceHandler exists, a new one is added to the context.
Returns:
ResourceHandler

getSecurityHandler

public SecurityHandler getSecurityHandler()
Get the context SecurityHandler. Conveniance method. If no SecurityHandler exists, a new one is added to the context at position 0.
Returns:
SecurityHandler

setServingResources

public void setServingResources(boolean serve)
Setup context for serving Resources as files. Conveniance method.
Parameters:
serve - If true and there is no ResourceHandler instance in the context, a ResourceHandler is added. If false, all ResourceHandler instances are removed from the context.

isServingResources

public boolean isServingResources()

setRealm

public void setRealm(java.lang.String realmName)
Set the SecurityHandler realm name. Conveniance method. If a SecurityHandler is not in the context, one is created as the 0th handler.
Parameters:
realmName - The name to use to retrieve the actual realm from the HttpServer

setRealm

public void setRealm(java.lang.String realmName,
                     UserRealm realm)
Set the SecurityHandler realm. Conveniance method. If a SecurityHandler is not in the context, one is created as the 0th handler.
Parameters:
realmName - The name of the realm
realm - The realm instance to use, instead of one retrieved from the HttpServer.

getRealm

public java.lang.String getRealm()

addSecurityConstraint

public void addSecurityConstraint(java.lang.String pathSpec,
                                  SecurityConstraint sc)
Add a security constraint. Conveniance method. If a SecurityHandler is not in the context, one is created as the 0th handler.
Parameters:
pathSpec -  
sc -  

addAuthConstraint

public void addAuthConstraint(java.lang.String pathSpec,
                              java.lang.String role)
Add an auth security constraint. Conveniance method.
Parameters:
pathSpec -  
Auth - role

getMimeMap

public java.util.Map getMimeMap()

setMimeMap

public void setMimeMap(java.util.Map mimeMap)
Also sets the org.mortbay.http.mimeMap context attribute
Parameters:
mimeMap -  

getMimeByExtension

public java.lang.String getMimeByExtension(java.lang.String filename)
Parameters:
filename -  
Returns:
 

setMimeMapping

public void setMimeMapping(java.lang.String extension,
                           java.lang.String type)
Set a mime mapping
Parameters:
extension -  
type -  

getEncodingMap

public java.util.Map getEncodingMap()
Get the map of mime type to char encoding.
Returns:
 

setEncodingMap

public void setEncodingMap(java.util.Map encodingMap)
Set the map of mime type to char encoding. Also sets the org.mortbay.http.encodingMap context attribute
Parameters:
encodingMap -  

getEncodingByMimeType

public java.lang.String getEncodingByMimeType(java.lang.String type)
Get char encoding by mime type.
Parameters:
type -  
Returns:
 

setTypeEncoding

public void setTypeEncoding(java.lang.String mimeType,
                            java.lang.String encoding)
Set the encoding that should be used for a mimeType.
Parameters:
mimeType -  
encoding -  

setRedirectNullPath

public void setRedirectNullPath(boolean b)
Set null path redirection.
Parameters:
b - if true a /context request will be redirected to /context/ if there is not path in the context.

isRedirectNullPath

public boolean isRedirectNullPath()
Returns:
True if a /context request is redirected to /context/ if there is not path in the context.

setPermissions

public void setPermissions(java.security.PermissionCollection permissions)
Set the permissions to be used for this context. The collection of permissions set here are used for all classes loaded by this context. This is simpler that creating a security policy file, as not all code sources may be statically known.
Parameters:
permissions -  

getPermissions

public java.security.PermissionCollection getPermissions()
Get the permissions to be used for this context.

addPermission

public void addPermission(java.security.Permission permission)
Add a permission to this context. The collection of permissions set here are used for all classes loaded by this context. This is simpler that creating a security policy file, as not all code sources may be statically known.
Parameters:
permission -  

handle

public boolean handle(HttpRequest request,
                      HttpResponse response)
               throws HttpException,
                      java.io.IOException
Handler request. Determine the path within the context and then call handle(pathInContext,request,response).
Parameters:
request -  
response -  
Returns:
True if the request has been handled.
Throws:
HttpException -  
java.io.IOException -  

handle

public boolean handle(java.lang.String pathInContext,
                      java.lang.String pathParams,
                      HttpRequest request,
                      HttpResponse response)
               throws HttpException,
                      java.io.IOException
Handler request. Call each HttpHandler until request is handled.
Parameters:
pathInContext - Path in context
pathParams - Path parameters such as encoded Session ID
request -  
response -  
Returns:
True if the request has been handled.
Throws:
HttpException -  
java.io.IOException -  

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(boolean detail)

start

public void start()
           throws java.lang.Exception
Start all handlers then listeners.
Specified by:
start in interface LifeCycle
Following copied from interface: org.mortbay.util.LifeCycle
Throws:
java.lang.Exception - An arbitrary exception may be thrown.

isStarted

public boolean isStarted()
Start all handlers then listeners.
Specified by:
isStarted in interface LifeCycle
Following copied from interface: org.mortbay.util.LifeCycle
Returns:
True if the LifeCycle has been started.

stop

public void stop()
          throws java.lang.InterruptedException
Stop all listeners then handlers.
Specified by:
stop in interface LifeCycle
Throws:
java.lang.InterruptedException - If interrupted, stop may not have been called on everything.

destroy

public void destroy()
Stop all listeners then handlers. All the handlers are unmapped and the listeners removed.
Specified by:
destroy in interface LifeCycle

isDestroyed

public boolean isDestroyed()
Specified by:
isDestroyed in interface LifeCycle
Following copied from interface: org.mortbay.util.LifeCycle
Returns:
True if the LifeCycle has been destroyed.

setStatsOn

public void setStatsOn(boolean on)
True set statistics recording on for this context.
Parameters:
on - If true, statistics will be recorded for this context.

getStatsOn

public boolean getStatsOn()

statsReset

public void statsReset()

getRequests

public int getRequests()
Returns:
Get the number of requests handled by this context since last call of statsReset(). If setStatsOn(false) then this is undefined.

getResponses1xx

public int getResponses1xx()
Returns:
Get the number of responses with a 2xx status returned by this context since last call of statsReset(). Undefined if if setStatsOn(false).

getResponses2xx

public int getResponses2xx()
Returns:
Get the number of responses with a 100 status returned by this context since last call of statsReset(). Undefined if if setStatsOn(false).

getResponses3xx

public int getResponses3xx()
Returns:
Get the number of responses with a 3xx status returned by this context since last call of statsReset(). Undefined if if setStatsOn(false).

getResponses4xx

public int getResponses4xx()
Returns:
Get the number of responses with a 4xx status returned by this context since last call of statsReset(). Undefined if if setStatsOn(false).

getResponses5xx

public int getResponses5xx()
Returns:
Get the number of responses with a 5xx status returned by this context since last call of statsReset(). Undefined if if setStatsOn(false).

getLogSink

public LogSink getLogSink()
Deprecated. use HttpServer.getRequestLogSink()


setLogSink

public void setLogSink(LogSink logSink)
Deprecated. use HttpServersetRequestLogSink()



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