org.glassfish.grizzly.servlet
Class WebappContext

java.lang.Object
  extended by org.glassfish.grizzly.servlet.WebappContext
All Implemented Interfaces:
javax.servlet.ServletContext

public class WebappContext
extends Object
implements javax.servlet.ServletContext

This class acts as the foundation for registering listeners, servlets, and filters in an embedded environment.

Additionally, this class implements the the requirements set forth by the Servlet 2.5 specification of ServletContext, however, it also exposes the dynamic registration API of Servlet 3.0.

TODO: Provide code examples once the api firms up a bit.

Since:
2.2

Field Summary
protected  boolean deployed
           
 
Constructor Summary
protected WebappContext()
           
  WebappContext(String displayName)
           Creates a simple WebappContext with the root being "/".
  WebappContext(String displayName, String contextPath)
           
  WebappContext(String displayName, String contextPath, String basePath)
           
 
Method Summary
 void addContextInitParameter(String name, String value)
           
 FilterRegistration addFilter(String filterName, Class<? extends javax.servlet.Filter> filterClass)
          Adds the filter with the given name and class type to this servlet context.
 FilterRegistration addFilter(String filterName, javax.servlet.Filter filter)
          Registers the given filter instance with this WebappContext under the given filterName.
 FilterRegistration addFilter(String filterName, String className)
          Adds the filter with the given name and class name to this servlet context.
 void addListener(Class<? extends EventListener> listener)
          Adds the given listener class to this WebappContext.
 void addListener(String className)
          Adds the listener with the given class name to this WebappContext.
 ServletRegistration addServlet(String servletName, Class<? extends javax.servlet.Servlet> servletClass)
          Adds the servlet with the given name and class type to this servlet context.
 ServletRegistration addServlet(String servletName, javax.servlet.Servlet servlet)
          Registers the given servlet instance with this WebappContext under the given servletName.
 ServletRegistration addServlet(String servletName, String className)
          Adds the servlet with the given name and class name to this servlet context.
 void clearContextInitParameters()
           
 void deploy(HttpServer targetServer)
           
 Object getAttribute(String name)
          
 Enumeration getAttributeNames()
          
protected  String getBasePath()
           
 javax.servlet.ServletContext getContext(String uri)
          
 String getContextPath()
          
protected  EventListener[] getEventListeners()
           
 FilterRegistration getFilterRegistration(String name)
          Gets the FilterRegistration corresponding to the filter with the given filterName.
 Map<String,? extends FilterRegistration> getFilterRegistrations()
          Gets a (possibly empty) Map of the FilterRegistration objects (keyed by filter name) corresponding to all filters registered with this WebappContext.
 String getInitParameter(String name)
          
 Enumeration getInitParameterNames()
          
 int getMajorVersion()
          
 String getMimeType(String file)
          
 int getMinorVersion()
          
 javax.servlet.RequestDispatcher getNamedDispatcher(String name)
          
 String getRealPath(String path)
          
 javax.servlet.RequestDispatcher getRequestDispatcher(String path)
          
 URL getResource(String path)
          
 InputStream getResourceAsStream(String path)
          
 Set getResourcePaths(String path)
          
 String getServerInfo()
          
 javax.servlet.Servlet getServlet(String name)
          Deprecated.  
 String getServletContextName()
          
 Enumeration getServletNames()
          Deprecated.  
 ServletRegistration getServletRegistration(String name)
          Gets the ServletRegistration corresponding to the servlet with the given servletName.
 Map<String,? extends ServletRegistration> getServletRegistrations()
          Gets a (possibly empty) Map of the ServletRegistration objects (keyed by servlet name) corresponding to all servlets registered with this WebappContext.
 Enumeration getServlets()
          Deprecated.  
 void log(Exception e, String message)
          Deprecated.  
 void log(String message)
          
 void log(String message, Throwable throwable)
          
protected  String normalize(String path)
          Return a context-relative path, beginning with a "/", that represents the canonical version of the specified path after ".." and "." elements are resolved out.
 void removeAttribute(String name)
          
 void removeContextInitParameter(String name)
           
 void setAttribute(String name, Object value)
          
protected  void setDispatcherHelper(DispatcherHelper dispatcherHelper)
           
 void undeploy()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

deployed

protected boolean deployed
Constructor Detail

WebappContext

protected WebappContext()

WebappContext

public WebappContext(String displayName)

Creates a simple WebappContext with the root being "/".

Parameters:
displayName -

WebappContext

public WebappContext(String displayName,
                     String contextPath)

WebappContext

public WebappContext(String displayName,
                     String contextPath,
                     String basePath)
Method Detail

deploy

public void deploy(HttpServer targetServer)
Parameters:
targetServer -

undeploy

public void undeploy()

addContextInitParameter

public void addContextInitParameter(String name,
                                    String value)
Parameters:
name -
value -

removeContextInitParameter

public void removeContextInitParameter(String name)
Parameters:
name -

clearContextInitParameters

public void clearContextInitParameters()

addFilter

public FilterRegistration addFilter(String filterName,
                                    Class<? extends javax.servlet.Filter> filterClass)
Adds the filter with the given name and class type to this servlet context.

The registered filter may be further configured via the returned FilterRegistration object.

If this WebappContext already contains a preliminary FilterRegistration for a filter with the given filterName, it will be completed (by assigning the name of the given filterClass to it) and returned.

Parameters:
filterName - the name of the filter
filterClass - the class object from which the filter will be instantiated
Returns:
a FilterRegistration object that may be used to further configure the registered filter, or null if this WebappContext already contains a complete FilterRegistration for a filter with the given filterName
Throws:
IllegalStateException - if this WebappContext has already been initialized

addFilter

public FilterRegistration addFilter(String filterName,
                                    javax.servlet.Filter filter)
Registers the given filter instance with this WebappContext under the given filterName.

The registered filter may be further configured via the returned FilterRegistration object.

If this WebappContext already contains a preliminary FilterRegistration for a filter with the given filterName, it will be completed (by assigning the class name of the given filter instance to it) and returned.

Parameters:
filterName - the name of the filter
filter - the filter instance to register
Returns:
a FilterRegistration object that may be used to further configure the given filter, or null if this WebappContext already contains a complete FilterRegistration for a filter with the given filterName or if the same filter instance has already been registered with this or another WebappContext in the same container
Throws:
IllegalStateException - if this WebappContext has already been initialized
Since:
Servlet 3.0

addFilter

public FilterRegistration addFilter(String filterName,
                                    String className)
Adds the filter with the given name and class name to this servlet context.

The registered filter may be further configured via the returned FilterRegistration object.

The specified className will be loaded using the classloader associated with the application represented by this WebappContext.

If this WebappContext already contains a preliminary FilterRegistration for a filter with the given filterName, it will be completed (by assigning the given className to it) and returned.

Parameters:
filterName - the name of the filter
className - the fully qualified class name of the filter
Returns:
a FilterRegistration object that may be used to further configure the registered filter, or null if this WebappContext already contains a complete FilterRegistration for a filter with the given filterName
Throws:
IllegalStateException - if this WebappContext has already been initialized

addServlet

public ServletRegistration addServlet(String servletName,
                                      Class<? extends javax.servlet.Servlet> servletClass)
Adds the servlet with the given name and class type to this servlet context.

The registered servlet may be further configured via the returned ServletRegistration object.

If this WebappContext already contains a preliminary ServletRegistration for a servlet with the given servletName, it will be completed (by assigning the name of the given servletClass to it) and returned.

Parameters:
servletName - the name of the servlet
servletClass - the class object from which the servlet will be instantiated
Returns:
a ServletRegistration object that may be used to further configure the registered servlet, or null if this WebappContext already contains a complete ServletRegistration for the given servletName
Throws:
IllegalStateException - if this WebappContext has already been initialized

addServlet

public ServletRegistration addServlet(String servletName,
                                      javax.servlet.Servlet servlet)
Registers the given servlet instance with this WebappContext under the given servletName.

The registered servlet may be further configured via the returned ServletRegistration object.

If this WebappContext already contains a preliminary ServletRegistration for a servlet with the given servletName, it will be completed (by assigning the class name of the given servlet instance to it) and returned.

Parameters:
servletName - the name of the servlet
servlet - the servlet instance to register
Returns:
a ServletRegistration object that may be used to further configure the given servlet, or null if this WebappContext already contains a complete ServletRegistration for a servlet with the given servletName or if the same servlet instance has already been registered with this or another WebappContext in the same container
Throws:
IllegalStateException - if this WebappContext has already been initialized
IllegalArgumentException - if the given servlet instance implements SingleThreadModel

addServlet

public ServletRegistration addServlet(String servletName,
                                      String className)
Adds the servlet with the given name and class name to this servlet context.

The registered servlet may be further configured via the returned ServletRegistration object.

The specified className will be loaded using the classloader associated with the application represented by this WebappContext.

If this WebappContext already contains a preliminary ServletRegistration for a servlet with the given servletName, it will be completed (by assigning the given className to it) and returned.

Parameters:
servletName - the name of the servlet
className - the fully qualified class name of the servlet
Returns:
a ServletRegistration object that may be used to further configure the registered servlet, or null if this WebappContext already contains a complete ServletRegistration for a servlet with the given servletName
Throws:
IllegalStateException - if this WebappContext has already been initialized

getFilterRegistration

public FilterRegistration getFilterRegistration(String name)
Gets the FilterRegistration corresponding to the filter with the given filterName.

Returns:
the (complete or preliminary) FilterRegistration for the filter with the given filterName, or null if no FilterRegistration exists under that name

getFilterRegistrations

public Map<String,? extends FilterRegistration> getFilterRegistrations()
Gets a (possibly empty) Map of the FilterRegistration objects (keyed by filter name) corresponding to all filters registered with this WebappContext.

The returned Map includes the FilterRegistration objects corresponding to all declared and annotated filters, as well as the FilterRegistration objects corresponding to all filters that have been added via one of the addFilter methods.

Any changes to the returned Map must not affect this WebappContext.

Returns:
Map of the (complete and preliminary) FilterRegistration objects corresponding to all filters currently registered with this WebappContext

getServletRegistration

public ServletRegistration getServletRegistration(String name)
Gets the ServletRegistration corresponding to the servlet with the given servletName.

Returns:
the (complete or preliminary) ServletRegistration for the servlet with the given servletName, or null if no ServletRegistration exists under that name

getServletRegistrations

public Map<String,? extends ServletRegistration> getServletRegistrations()
Gets a (possibly empty) Map of the ServletRegistration objects (keyed by servlet name) corresponding to all servlets registered with this WebappContext.

The returned Map includes the ServletRegistration objects corresponding to all declared and annotated servlets, as well as the ServletRegistration objects corresponding to all servlets that have been added via one of the addServlet methods.

If permitted, any changes to the returned Map must not affect this WebappContext.

Returns:
Map of the (complete and preliminary) ServletRegistration objects corresponding to all servlets currently registered with this WebappContext
Since:
Servlet 3.0

addListener

public void addListener(Class<? extends EventListener> listener)
Adds the given listener class to this WebappContext.

The given listener must be an instance of one or more of the following interfaces:

If the given listener is an instance of a listener interface whose invocation order corresponds to the declaration order (i.e., if it is an instance of ServletRequestListener, ServletContextListener, or HttpSessionListener), then the listener will be added to the end of the ordered list of listeners of that interface.

Throws:
IllegalArgumentException - if the given listener is not an instance of any of the above interfaces
IllegalStateException - if this WebappContext has already been initialized

addListener

public void addListener(String className)
Adds the listener with the given class name to this WebappContext.

The class with the given name will be loaded using the classloader associated with the application represented by this WebappContext, and must implement one or more of the following interfaces:

As part of this method call, the container must load the class with the specified class name to ensure that it implements one of the required interfaces.

If the class with the given name implements a listener interface whose invocation order corresponds to the declaration order (i.e., if it implements ServletRequestListener, ServletContextListener, or HttpSessionListener), then the new listener will be added to the end of the ordered list of listeners of that interface.

Parameters:
className - the fully qualified class name of the listener
Throws:
IllegalArgumentException - if the class with the given name does not implement any of the above interfaces
IllegalStateException - if this WebappContext has already been initialized

getContextPath

public String getContextPath()

Specified by:
getContextPath in interface javax.servlet.ServletContext

getContext

public javax.servlet.ServletContext getContext(String uri)

Specified by:
getContext in interface javax.servlet.ServletContext

getMajorVersion

public int getMajorVersion()

Specified by:
getMajorVersion in interface javax.servlet.ServletContext

getMinorVersion

public int getMinorVersion()

Specified by:
getMinorVersion in interface javax.servlet.ServletContext

getMimeType

public String getMimeType(String file)

Specified by:
getMimeType in interface javax.servlet.ServletContext

getResourcePaths

public Set getResourcePaths(String path)

Specified by:
getResourcePaths in interface javax.servlet.ServletContext

getResource

public URL getResource(String path)
                throws MalformedURLException

Specified by:
getResource in interface javax.servlet.ServletContext
Throws:
MalformedURLException

getResourceAsStream

public InputStream getResourceAsStream(String path)

Specified by:
getResourceAsStream in interface javax.servlet.ServletContext

getRequestDispatcher

public javax.servlet.RequestDispatcher getRequestDispatcher(String path)

Specified by:
getRequestDispatcher in interface javax.servlet.ServletContext

getNamedDispatcher

public javax.servlet.RequestDispatcher getNamedDispatcher(String name)

Specified by:
getNamedDispatcher in interface javax.servlet.ServletContext

getServlet

@Deprecated
public javax.servlet.Servlet getServlet(String name)
                                 throws javax.servlet.ServletException
Deprecated. 

Specified by:
getServlet in interface javax.servlet.ServletContext
Throws:
javax.servlet.ServletException

getServlets

@Deprecated
public Enumeration getServlets()
Deprecated. 

Specified by:
getServlets in interface javax.servlet.ServletContext

getServletNames

@Deprecated
public Enumeration getServletNames()
Deprecated. 

Specified by:
getServletNames in interface javax.servlet.ServletContext

log

public void log(String message)

Specified by:
log in interface javax.servlet.ServletContext

log

@Deprecated
public void log(Exception e,
                           String message)
Deprecated. 

Specified by:
log in interface javax.servlet.ServletContext

log

public void log(String message,
                Throwable throwable)

Specified by:
log in interface javax.servlet.ServletContext

getRealPath

public String getRealPath(String path)

Specified by:
getRealPath in interface javax.servlet.ServletContext

getServerInfo

public String getServerInfo()

Specified by:
getServerInfo in interface javax.servlet.ServletContext

getInitParameter

public String getInitParameter(String name)

Specified by:
getInitParameter in interface javax.servlet.ServletContext

getInitParameterNames

public Enumeration getInitParameterNames()

Specified by:
getInitParameterNames in interface javax.servlet.ServletContext

getAttribute

public Object getAttribute(String name)

Specified by:
getAttribute in interface javax.servlet.ServletContext

getAttributeNames

public Enumeration getAttributeNames()

Specified by:
getAttributeNames in interface javax.servlet.ServletContext

setAttribute

public void setAttribute(String name,
                         Object value)

Specified by:
setAttribute in interface javax.servlet.ServletContext

removeAttribute

public void removeAttribute(String name)

Specified by:
removeAttribute in interface javax.servlet.ServletContext

getServletContextName

public String getServletContextName()

Specified by:
getServletContextName in interface javax.servlet.ServletContext

normalize

protected String normalize(String path)
Return a context-relative path, beginning with a "/", that represents the canonical version of the specified path after ".." and "." elements are resolved out. If the specified path attempts to go outside the boundaries of the current context (i.e. too many ".." path elements are present), return null instead.

Parameters:
path - Path to be normalized

getBasePath

protected String getBasePath()
Returns:

setDispatcherHelper

protected void setDispatcherHelper(DispatcherHelper dispatcherHelper)
Parameters:
dispatcherHelper -

getEventListeners

protected EventListener[] getEventListeners()
Returns:


Copyright © 2012 Oracle Corporation. All Rights Reserved.