Class DeferredServletContext

java.lang.Object
de.aristaflow.adept2.base.servletcontainer.demux.DeferredServletContext
All Implemented Interfaces:
DynamicServletContext, jakarta.servlet.ServletContext

public class DeferredServletContext extends Object implements DynamicServletContext
This servlet context provides the means for adding servlets after the parent servlet context has already been initialised. It is closely coupled to the corresponding servlet implementation which demultiplexes incoming requests to all servlets added to this context.

This servlet context has a parent servlet context to which most of the methods are forwarded. This excludes the handling of init parameters and servlets:
Init parameters added to this deferred servlet context will be merged with the init parameters of the parent servlet context. Therefore one cannot add an init parameter that has already been set for the parent servlet context.
Servlets can be added any time. The corresponding DeferredDynamic allows for setting URL patterns as well as changing init parameters. Note that as soon as a DeferredDynamic has been created, its init parameters will be a snapshot of the init parameters of this (and the parent) servlet context. That is, changing the init parameters in the servlet context will not affect the init parameters of the DeferredDynamic and therefore of the ServletConfig.

This servlet context can be used in two ways:

  • Instantiate it directly providing the parent servlet context: DeferredServletContext(ServletContext).
    This requires configuration in the init parameters of the parent servlet context. These are the mapping (e. g. servlet name and path) and whether the demultiplexing servlet should support asynchronism. The mapping has to end with /* to allow for the paths of sub servlets to be handled correctly. Async support will be required if any sub servlets should use async behaviour. The configuration is then used to instantiate and register the demultiplexing servlet.
  • Let the servlet container instantiate the demultiplexing servlet (e. g. via web.xml).
    In this case the demultiplexing servlet will instantiate the deferred servlet context (DeferredServletContext(ServletConfig, DemuxServlet)) providing its own servlet context as parent context. The URL mapping will be retrieved from the servlet context, i. e. the web.xml.
  • Field Details

    • INIT_PARAM_DEMUX_SERVLET_MAPPING

      public static final String INIT_PARAM_DEMUX_SERVLET_MAPPING
      Init parameter name for the URL mapping of the demultiplexing servlet.
      See Also:
    • INIT_PARAM_DEMUX_SERVLET_ASYNC_SUPPORTED

      public static final String INIT_PARAM_DEMUX_SERVLET_ASYNC_SUPPORTED
      Init parameter name for the boolean value whether the demultiplexing servlet should be marked as async supported. This is required in case dynamically addded servlets have/need async support.
      See Also:
    • parentContext

      protected final jakarta.servlet.ServletContext parentContext
      The parent servlet context to which to delegate to.
    • innerContextPath

      protected final String innerContextPath
      The path of this context, i. e. the context for the inner servlets. This is the path of the outer servlet context combined with the servlet path, i. e. the mapping of the demultiplexing servlet.
    • servletPath

      protected final String servletPath
      The servlet path, i. e. the last part of the innerContextPath. This is without leading and trailing slashes. It will be appended to the path before delegating getRealPath(String)
    • demuxServlet

      protected final DemuxServlet demuxServlet
      The demultiplexing servlet which forwards to the servlets added via this context.
    • ownServlets

      protected final Map<String,DeferredDynamic> ownServlets
      The servlets added dynamically via this context.
    • ownInitParams

      protected final Map<String,String> ownInitParams
      The parameters set at this context. This will be merged with the init parameters of the parent servlet context. The names of this and the parameters of the parent are disjoint.
  • Constructor Details

    • DeferredServletContext

      public DeferredServletContext(jakarta.servlet.ServletContext parentContext) throws ConfigurationException
      Creates a new servlet context with the designated servlet context as parent. This will also retrieve the configuration for the URL pattern and async support from the init parameters (of the parent servlet context) and use this to instantiate and register the corresponding multiplexing servlet. The default for async support is false, the mapping has to be provided.
      Parameters:
      parentContext - The servlet context used as parent of the created servlet context.
      Throws:
      ConfigurationException - If the mapping is not defined or does not end with /*, a ConfigurationException will be thrown.
    • DeferredServletContext

      public DeferredServletContext(jakarta.servlet.ServletConfig config, DemuxServlet servlet) throws jakarta.servlet.ServletException
      Creates a new servlet context for the designated demultiplexing servlet. This constructor is called by Servlet.init(ServletConfig) of the designated servlet. That is, the designated servlet is instantiated and handled by the servlet container.
      Parameters:
      config - The config of the designated servlet providing the outer servlet container as well as the servlet mapping.
      servlet - The demultiplexing servlet which forwards to the servlets added dynamically to this servlet context.
      Throws:
      jakarta.servlet.ServletException - If the mapping of the designated servlet does not exist (i. e. is not configured), a ServletException will be thrown.
  • Method Details

    • getContextPath

      public String getContextPath()
      Specified by:
      getContextPath in interface jakarta.servlet.ServletContext
    • getContext

      public jakarta.servlet.ServletContext getContext(String uripath)
      Specified by:
      getContext in interface jakarta.servlet.ServletContext
    • getMajorVersion

      public int getMajorVersion()
      Specified by:
      getMajorVersion in interface jakarta.servlet.ServletContext
    • getMinorVersion

      public int getMinorVersion()
      Specified by:
      getMinorVersion in interface jakarta.servlet.ServletContext
    • getEffectiveMajorVersion

      public int getEffectiveMajorVersion()
      Specified by:
      getEffectiveMajorVersion in interface jakarta.servlet.ServletContext
    • getEffectiveMinorVersion

      public int getEffectiveMinorVersion()
      Specified by:
      getEffectiveMinorVersion in interface jakarta.servlet.ServletContext
    • getMimeType

      public String getMimeType(String file)
      Specified by:
      getMimeType in interface jakarta.servlet.ServletContext
    • getResourcePaths

      public Set<String> getResourcePaths(String path)
      Specified by:
      getResourcePaths in interface jakarta.servlet.ServletContext
    • getResource

      public URL getResource(String path) throws MalformedURLException
      Specified by:
      getResource in interface jakarta.servlet.ServletContext
      Throws:
      MalformedURLException
    • getResourceAsStream

      public InputStream getResourceAsStream(String path)
      Specified by:
      getResourceAsStream in interface jakarta.servlet.ServletContext
    • getRequestDispatcher

      public jakarta.servlet.RequestDispatcher getRequestDispatcher(String path)
      Specified by:
      getRequestDispatcher in interface jakarta.servlet.ServletContext
    • getNamedDispatcher

      public jakarta.servlet.RequestDispatcher getNamedDispatcher(String name)
      Specified by:
      getNamedDispatcher in interface jakarta.servlet.ServletContext
    • log

      public void log(String msg)
      Specified by:
      log in interface jakarta.servlet.ServletContext
    • log

      public void log(String message, Throwable throwable)
      Specified by:
      log in interface jakarta.servlet.ServletContext
    • getRealPath

      public String getRealPath(String path)
      Specified by:
      getRealPath in interface jakarta.servlet.ServletContext
    • getServerInfo

      public String getServerInfo()
      Specified by:
      getServerInfo in interface jakarta.servlet.ServletContext
    • getInitParameter

      public String getInitParameter(String name)
      Specified by:
      getInitParameter in interface jakarta.servlet.ServletContext
    • getInitParameterNames

      public Enumeration<String> getInitParameterNames()
      This includes the init parameter names of the parent servlet context as well as the additional init parameter names set for this servlet context.
      Specified by:
      getInitParameterNames in interface jakarta.servlet.ServletContext
    • setInitParameter

      public boolean setInitParameter(String name, String value)
      This will only set the designated init parameter if there is no init parameter of the same name.
      Specified by:
      setInitParameter in interface jakarta.servlet.ServletContext
    • getAttribute

      public Object getAttribute(String name)
      Specified by:
      getAttribute in interface jakarta.servlet.ServletContext
    • getAttributeNames

      public Enumeration<String> getAttributeNames()
      Specified by:
      getAttributeNames in interface jakarta.servlet.ServletContext
    • setAttribute

      public void setAttribute(String name, Object object)
      Specified by:
      setAttribute in interface jakarta.servlet.ServletContext
    • removeAttribute

      public void removeAttribute(String name)
      Specified by:
      removeAttribute in interface jakarta.servlet.ServletContext
    • getServletContextName

      public String getServletContextName()
      Specified by:
      getServletContextName in interface jakarta.servlet.ServletContext
    • addServlet

      public DeferredDynamic addServlet(String servletName, String className)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • addServlet

      public DeferredDynamic addServlet(String servletName, jakarta.servlet.Servlet servlet)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • addJspFile

      public jakarta.servlet.ServletRegistration.Dynamic addJspFile(String servletName, String jspFile)
      Specified by:
      addJspFile in interface jakarta.servlet.ServletContext
    • addServlet

      public DeferredDynamic addServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • removeServlet

      public boolean removeServlet(String servletName)
      Description copied from interface: DynamicServletContext
      Removes the servlet with the designated name.
      Specified by:
      removeServlet in interface DynamicServletContext
      Parameters:
      servletName - The name of the servlet which to remove.
      Returns:
      Whether a servlet of the designated name was registered and has been removed by this call.
    • createServlet

      public <T extends jakarta.servlet.Servlet> T createServlet(Class<T> clazz) throws jakarta.servlet.ServletException
      Specified by:
      createServlet in interface jakarta.servlet.ServletContext
      Throws:
      jakarta.servlet.ServletException
    • getServletRegistration

      public jakarta.servlet.ServletRegistration getServletRegistration(String servletName)
      Specified by:
      getServletRegistration in interface jakarta.servlet.ServletContext
    • getServletRegistrations

      public Map<String,? extends jakarta.servlet.ServletRegistration> getServletRegistrations()
      Specified by:
      getServletRegistrations in interface jakarta.servlet.ServletContext
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, jakarta.servlet.Filter filter)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • createFilter

      public <T extends jakarta.servlet.Filter> T createFilter(Class<T> clazz) throws jakarta.servlet.ServletException
      Specified by:
      createFilter in interface jakarta.servlet.ServletContext
      Throws:
      jakarta.servlet.ServletException
    • getFilterRegistration

      public jakarta.servlet.FilterRegistration getFilterRegistration(String filterName)
      Specified by:
      getFilterRegistration in interface jakarta.servlet.ServletContext
    • getFilterRegistrations

      public Map<String,? extends jakarta.servlet.FilterRegistration> getFilterRegistrations()
      Specified by:
      getFilterRegistrations in interface jakarta.servlet.ServletContext
    • getSessionCookieConfig

      public jakarta.servlet.SessionCookieConfig getSessionCookieConfig()
      Specified by:
      getSessionCookieConfig in interface jakarta.servlet.ServletContext
    • setSessionTrackingModes

      public void setSessionTrackingModes(Set<jakarta.servlet.SessionTrackingMode> sessionTrackingModes)
      Specified by:
      setSessionTrackingModes in interface jakarta.servlet.ServletContext
    • getDefaultSessionTrackingModes

      public Set<jakarta.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
      Specified by:
      getDefaultSessionTrackingModes in interface jakarta.servlet.ServletContext
    • getEffectiveSessionTrackingModes

      public Set<jakarta.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
      Specified by:
      getEffectiveSessionTrackingModes in interface jakarta.servlet.ServletContext
    • addListener

      public void addListener(String className)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • addListener

      public <T extends EventListener> void addListener(T t)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • addListener

      public void addListener(Class<? extends EventListener> listenerClass)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • createListener

      public <T extends EventListener> T createListener(Class<T> clazz) throws jakarta.servlet.ServletException
      Specified by:
      createListener in interface jakarta.servlet.ServletContext
      Throws:
      jakarta.servlet.ServletException
    • getJspConfigDescriptor

      public jakarta.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
      Specified by:
      getJspConfigDescriptor in interface jakarta.servlet.ServletContext
    • getClassLoader

      public ClassLoader getClassLoader()
      Specified by:
      getClassLoader in interface jakarta.servlet.ServletContext
    • declareRoles

      public void declareRoles(String... roleNames)
      Specified by:
      declareRoles in interface jakarta.servlet.ServletContext
    • getVirtualServerName

      public String getVirtualServerName()
      Specified by:
      getVirtualServerName in interface jakarta.servlet.ServletContext
    • getSessionTimeout

      public int getSessionTimeout()
      Specified by:
      getSessionTimeout in interface jakarta.servlet.ServletContext
    • setSessionTimeout

      public void setSessionTimeout(int sessionTimeout)
      Specified by:
      setSessionTimeout in interface jakarta.servlet.ServletContext
    • getRequestCharacterEncoding

      public String getRequestCharacterEncoding()
      Specified by:
      getRequestCharacterEncoding in interface jakarta.servlet.ServletContext
    • setRequestCharacterEncoding

      public void setRequestCharacterEncoding(String encoding)
      Specified by:
      setRequestCharacterEncoding in interface jakarta.servlet.ServletContext
    • getResponseCharacterEncoding

      public String getResponseCharacterEncoding()
      Specified by:
      getResponseCharacterEncoding in interface jakarta.servlet.ServletContext
    • setResponseCharacterEncoding

      public void setResponseCharacterEncoding(String encoding)
      Specified by:
      setResponseCharacterEncoding in interface jakarta.servlet.ServletContext
    • getInitParams

      protected Map<String,String> getInitParams()
      Gets the current init parameters of this servlet context as well as the ones from the parent servlet context.
      Returns:
      The current init parameters of this servlet context as well as the ones from the parent servlet context.