public class HttpContext extends Container implements LifeCycle, HttpHandler, EventProvider, Serializable
A HttpContext is analagous to a ServletContext in the Servlet API, except that it may contain other types of handler other than servlets.
A ClassLoader is created for the context and it uses Thread.currentThread().getContextClassLoader(); as it's parent loader. The class loader is initialized during start(), when a derived context calls initClassLoader() or on the first call to loadClass()
Note. that order is important when configuring a HttpContext. For example, if resource serving is enabled before servlets, then resources take priority.
HttpServer
,
HttpHandler
,
ServletHttpContext
,
Serialized FormModifier and Type | Field and Description |
---|---|
static String |
__ErrorHandler |
static String |
__fileClassPathAttr
File class path attribute.
|
Constructor and Description |
---|
HttpContext()
Constructor.
|
HttpContext(HttpServer httpServer,
String contextPathSpec)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addClassPath(String classPath)
Add the class path element to the context.
|
void |
addClassPaths(Resource lib)
Add elements to the class path for the context from the jar and zip files found
in the specified resource.
|
void |
addHandler(HttpHandler handler)
Add a HttpHandler to the context.
|
void |
addHandler(int i,
HttpHandler handler)
Add a handler.
|
void |
addPermission(Permission permission)
Add a permission to this context.
|
void |
addSecurityConstraint(String pathSpec,
SecurityConstraint sc) |
void |
addVirtualHost(String hostname)
Add a virtual host alias to this context.
|
void |
addWelcomeFile(String welcomeFile) |
static String |
canonicalContextPathSpec(String contextPathSpec) |
boolean |
checkSecurityConstraints(String pathInContext,
HttpRequest request,
HttpResponse response) |
void |
clearSecurityConstraints() |
void |
destroy()
Destroy a context.
|
protected void |
doStart()
Do start operations.
|
protected void |
doStop()
Stop the context.
|
Object |
enterContextScope(HttpRequest request,
HttpResponse response)
Enter the context scope.
|
void |
flushCache() |
Object |
getAttribute(String name) |
Enumeration |
getAttributeNames() |
Map |
getAttributes() |
Authenticator |
getAuthenticator() |
Resource |
getBaseResource() |
ClassLoader |
getClassLoader()
Get the classloader.
|
String |
getClassPath()
Get the context classpath.
|
String |
getContextPath() |
String |
getEncodingByMimeType(String type) |
Map |
getEncodingMap() |
String |
getFileClassPath()
Get the file classpath of the context.
|
HttpHandler |
getHandler(Class handlerClass)
Get a handler by class.
|
int |
getHandlerIndex(HttpHandler handler)
Get handler index.
|
HttpHandler[] |
getHandlers()
Get all handlers.
|
String[] |
getHosts()
Get the hosts for the context.
|
HttpConnection |
getHttpConnection()
Get the ThreadLocal HttpConnection.
|
HttpContext |
getHttpContext() |
String |
getHttpContextName() |
HttpServer |
getHttpServer() |
String |
getInitParameter(String param)
Get context init parameter.
|
Enumeration |
getInitParameterNames()
Get context init parameter.
|
int |
getMaxCachedFileSize() |
int |
getMaxCacheSize() |
String |
getMimeByExtension(String filename) |
Map |
getMimeMap() |
String |
getName()
Get the name of the handler.
|
ClassLoader |
getParentClassLoader() |
PermissionCollection |
getPermissions()
Get the permissions to be used for this context.
|
UserRealm |
getRealm() |
String |
getRealmName() |
RequestLog |
getRequestLog() |
int |
getRequests() |
int |
getRequestsActive() |
int |
getRequestsActiveMax() |
Resource |
getResource(String pathInContext) |
String |
getResourceBase() |
ResourceCache.ResourceMetaData |
getResourceMetaData(Resource resource) |
int |
getResponses1xx() |
int |
getResponses2xx() |
int |
getResponses3xx() |
int |
getResponses4xx() |
int |
getResponses5xx() |
String[] |
getServerClasses()
Get system classes.
|
boolean |
getStatsOn() |
long |
getStatsOnMs() |
boolean |
getStopGracefully() |
String[] |
getSystemClasses()
Get system classes.
|
File |
getTempDirectory()
Get Context temporary directory.
|
String[] |
getVirtualHosts()
Get the virtual hosts for the context.
|
String |
getWelcomeFile(Resource resource) |
String[] |
getWelcomeFiles() |
void |
handle(HttpRequest request,
HttpResponse response)
RestishHandler request.
|
void |
handle(String pathInContext,
String pathParams,
HttpRequest request,
HttpResponse response)
RestishHandler request.
|
protected void |
initClassLoader(boolean forceContextLoader)
Initialize the context classloader.
|
void |
initialize(HttpContext context) |
boolean |
isClassLoaderJava2Compliant()
Get Java2 compliant classloading.
|
boolean |
isRedirectNullPath() |
void |
leaveContextScope(HttpRequest request,
HttpResponse response,
Object oldScope)
Leave the context scope.
|
Class |
loadClass(String className) |
void |
log(HttpRequest request,
HttpResponse response,
int length)
Log a request and response.
|
void |
removeAttribute(String name) |
void |
removeHandler(HttpHandler handler)
Remove a handler.
|
HttpHandler |
removeHandler(int i)
Remove a handler.
|
void |
removeVirtualHost(String hostname)
remove a virtual host alias to this context.
|
void |
removeWelcomeFile(String welcomeFile) |
static void |
sendContextError(HttpResponse response,
int code,
String msg)
Send an error response.
|
void |
sendError(HttpResponse response,
int code,
String msg)
Send an error response.
|
void |
setAttribute(String name,
Object value)
Set a context attribute.
|
void |
setAttributes(Map attributes) |
void |
setAuthenticator(Authenticator authenticator) |
void |
setBaseResource(Resource base) |
void |
setClassLoader(ClassLoader loader)
Set ClassLoader.
|
void |
setClassLoaderJava2Compliant(boolean compliant)
Set Java2 compliant classloading.
|
void |
setClassPath(String classPath)
Sets the class path for the context.
|
void |
setContextPath(String contextPathSpec) |
void |
setEncodingMap(Map encodingMap) |
void |
setHandlers(HttpHandler[] handlers) |
void |
setHosts(String[] hosts)
Set the hosts for the context.
|
void |
setHttpContextName(String s) |
void |
setInitParameter(String param,
String value)
Set context init parameter.
|
void |
setMaxCachedFileSize(int maxCachedFileSize) |
void |
setMaxCacheSize(int maxCacheSize) |
void |
setMimeMap(Map mimeMap) |
void |
setMimeMapping(String extension,
String type) |
void |
setParentClassLoader(ClassLoader loader)
Set Parent ClassLoader.
|
void |
setPermissions(PermissionCollection permissions)
Set the permissions to be used for this context.
|
void |
setRealm(UserRealm realm)
Set the realm.
|
void |
setRealmName(String realmName)
Set the realm name.
|
void |
setRedirectNullPath(boolean b)
Set null path redirection.
|
void |
setRequestLog(RequestLog log)
Set the request log.
|
void |
setResourceBase(String resourceBase) |
void |
setServerClasses(String[] classes)
Set system classes.
|
void |
setStatsOn(boolean on)
True set statistics recording on for this context.
|
void |
setStopGracefully(boolean graceful) |
void |
setSystemClasses(String[] classes)
Set system classes.
|
void |
setTempDirectory(File dir)
Set temporary directory for context.
|
void |
setTypeEncoding(String mimeType,
String encoding) |
void |
setVirtualHosts(String[] hosts)
Set the virtual hosts for the context.
|
void |
setWelcomeFiles(String[] welcomes) |
protected void |
startHandlers()
Start the handlers.
|
void |
statsReset() |
void |
stop(boolean graceful)
Stop the context.
|
String |
toString() |
String |
toString(boolean detail) |
addComponent, addEventListener, getComponents, isStarted, isStarting, isStopping, removeComponent, removeEventListener, start, stop
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addEventListener, removeEventListener
public static final String __fileClassPathAttr
public static final String __ErrorHandler
public HttpContext()
public HttpContext(HttpServer httpServer, String contextPathSpec)
httpServer
- contextPathSpec
- public HttpConnection getHttpConnection()
public HttpServer getHttpServer()
public void setStopGracefully(boolean graceful)
public boolean getStopGracefully()
public void setContextPath(String contextPathSpec)
public String getContextPath()
public void addVirtualHost(String hostname)
hostname
- A hostname. A null host name means any hostname is
acceptable. Host names may String representation of IP addresses.setVirtualHosts(java.lang.String[])
public void removeVirtualHost(String hostname)
hostname
- A hostname. A null host name means any hostname is
acceptable. Host names may String representation of IP addresses.setVirtualHosts(java.lang.String[])
public void setVirtualHosts(String[] hosts)
hosts
- Array of virtual hosts that this context responds to. A
null host name or null/empty array means any hostname is acceptable.
Host names may String representation of IP addresses.public String[] getVirtualHosts()
public void setHosts(String[] hosts) throws UnknownHostException
UnknownHostException
public String[] getHosts()
public void setSystemClasses(String[] classes)
classes
- array of classname Strings. Names ending with '.' are treated as package names. Names starting with '-' are treated as
negative matches and must be listed before any enclosing packages.public String[] getSystemClasses()
public void setServerClasses(String[] classes)
classes
- array of classname Strings. Names ending with '.' are treated as package names. Names starting with '-' are treated as
negative matches and must be listed before any enclosing packages.public String[] getServerClasses()
public void setHandlers(HttpHandler[] handlers)
public HttpHandler[] getHandlers()
public void addHandler(int i, HttpHandler handler)
i
- The position in the handler listhandler
- The handler.public void addHandler(HttpHandler handler)
handler
- public int getHandlerIndex(HttpHandler handler)
handler
- instancepublic HttpHandler getHandler(Class handlerClass)
handlerClass
- public HttpHandler removeHandler(int i)
i
- index of handlerpublic void removeHandler(HttpHandler handler)
public void setInitParameter(String param, String value)
param
- param namevalue
- param value or nullpublic String getInitParameter(String param)
param
- param namepublic Enumeration getInitParameterNames()
public void setAttribute(String name, Object value)
name
- attribute namevalue
- attribute valuepublic Object getAttribute(String name)
name
- attribute namepublic Map getAttributes()
public void setAttributes(Map attributes)
public Enumeration getAttributeNames()
public void removeAttribute(String name)
name
- attribute namepublic void flushCache()
public String[] getWelcomeFiles()
public void setWelcomeFiles(String[] welcomes)
public void addWelcomeFile(String welcomeFile)
public void removeWelcomeFile(String welcomeFile)
public String getWelcomeFile(Resource resource) throws IOException
IOException
public String getClassPath()
getFileClassPath()
public String getFileClassPath() throws IllegalStateException
IllegalStateException
- HttpContext.initClassLoader
has not been called.public void setClassPath(String classPath)
classPath
- a comma or ';' separated list of class
resources. These may be jar files, directories or URLs to jars
or directories.public void addClassPath(String classPath)
classPath
- a comma or ';' separated list of class
resources. These may be jar files, directories or URLs to jars
or directories.public void addClassPaths(Resource lib)
lib
- the resource that contains the jar and/or zip files.setClassPath(String)
public boolean isClassLoaderJava2Compliant()
public void setClassLoaderJava2Compliant(boolean compliant)
compliant
- If true, the class loader will conform to the java 2
specification and delegate all loads to the parent classloader. If
false, the context classloader only delegate loads for system classes
or classes that it can't find itself.public void setTempDirectory(File dir)
dir
- Writable temporary directory.public File getTempDirectory()
public void setClassLoader(ClassLoader loader)
loader
- The loader to be used by this context.public ClassLoader getClassLoader()
public void setParentClassLoader(ClassLoader loader)
loader
- The class loader to use for the parent loader of
the context classloader.public ClassLoader getParentClassLoader()
protected void initClassLoader(boolean forceContextLoader) throws MalformedURLException, IOException
forceContextLoader
- If true, a ContextLoader is always if
no loader has been set.MalformedURLException
IOException
public Class loadClass(String className) throws ClassNotFoundException
ClassNotFoundException
public void setRealmName(String realmName)
realmName
- The name to use to retrieve the actual realm
from the HttpServerpublic String getRealmName()
public void setRealm(UserRealm realm)
public UserRealm getRealm()
public Authenticator getAuthenticator()
public void setAuthenticator(Authenticator authenticator)
public void addSecurityConstraint(String pathSpec, SecurityConstraint sc)
public void clearSecurityConstraints()
public boolean checkSecurityConstraints(String pathInContext, HttpRequest request, HttpResponse response) throws HttpException, IOException
HttpException
IOException
public void setRedirectNullPath(boolean b)
b
- if true a /context request will be redirected to
/context/ if there is not path in the context.public boolean isRedirectNullPath()
public void setPermissions(PermissionCollection permissions)
permissions
- public PermissionCollection getPermissions()
public void addPermission(Permission permission)
permission
- public void handle(HttpRequest request, HttpResponse response) throws HttpException, IOException
request
- response
- HttpException
IOException
public void handle(String pathInContext, String pathParams, HttpRequest request, HttpResponse response) throws HttpException, IOException
handle
in interface HttpHandler
pathInContext
- Path in contextpathParams
- Path parameters such as encoded Session IDrequest
- response
- HttpException
IOException
public Object enterContextScope(HttpRequest request, HttpResponse response)
public void leaveContextScope(HttpRequest request, HttpResponse response, Object oldScope)
public String getHttpContextName()
public void setHttpContextName(String s)
public String toString(boolean detail)
protected void doStart() throws Exception
Container
protected void startHandlers() throws Exception
Exception
public void stop(boolean graceful) throws InterruptedException
graceful
- If true and statistics are on, then this method will wait
for requestsActive to go to zero before calling stop()InterruptedException
protected void doStop() throws Exception
public void destroy()
public void setRequestLog(RequestLog log)
log
- RequestLog to use.public RequestLog getRequestLog()
public void sendError(HttpResponse response, int code, String msg) throws IOException
response
- the response to sendcode
- The error codemsg
- The message for the error or null for the defaultIOException
- Problem sending response.public static void sendContextError(HttpResponse response, int code, String msg) throws IOException
response
- the response to sendcode
- The error codemsg
- The message for the error or null for the defaultIOException
- Problem sending response.public void setStatsOn(boolean on)
on
- If true, statistics will be recorded for this context.public boolean getStatsOn()
public long getStatsOnMs()
public void statsReset()
public int getRequests()
public int getRequestsActive()
public int getRequestsActiveMax()
public int getResponses1xx()
public int getResponses2xx()
public int getResponses3xx()
public int getResponses4xx()
public int getResponses5xx()
public void log(HttpRequest request, HttpResponse response, int length)
request
- response
- public String getName()
HttpHandler
getName
in interface HttpHandler
public HttpContext getHttpContext()
getHttpContext
in interface HttpHandler
public void initialize(HttpContext context)
initialize
in interface HttpHandler
public Resource getBaseResource()
public String getEncodingByMimeType(String type)
type
- public Map getEncodingMap()
public int getMaxCachedFileSize()
public int getMaxCacheSize()
public String getMimeByExtension(String filename)
filename
- public Map getMimeMap()
public Resource getResource(String pathInContext) throws IOException
pathInContext
- IOException
public String getResourceBase()
public ResourceCache.ResourceMetaData getResourceMetaData(Resource resource)
resource
- public void setBaseResource(Resource base)
base
- public void setEncodingMap(Map encodingMap)
encodingMap
- public void setMaxCachedFileSize(int maxCachedFileSize)
maxCachedFileSize
- public void setMaxCacheSize(int maxCacheSize)
maxCacheSize
- public void setMimeMap(Map mimeMap)
mimeMap
- public void setMimeMapping(String extension, String type)
extension
- type
- public void setResourceBase(String resourceBase)
resourceBase
- Copyright © 2013. All Rights Reserved.