Class ServletContextHandler

All Implemented Interfaces:
AliasCheck, Handler, Handler.Container, Handler.Singleton, Request.Handler, Attributes, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle, Invocable
Direct Known Subclasses:
WebAppContext

@ManagedObject("Servlet Context Handler") public class ServletContextHandler extends ContextHandler
Servlet Context.

This extension to the ContextHandler allows for simple construction of a context with ServletHandler and optionally session and security handlers, et.

   new ServletContext("/context",Context.SESSIONS|Context.NO_SECURITY);
 

This class should have been called ServletContext, but this would have cause confusion with ServletContext.

  • Field Details

  • Constructor Details

    • ServletContextHandler

      public ServletContextHandler()
    • ServletContextHandler

      public ServletContextHandler(String contextPath)
    • ServletContextHandler

      public ServletContextHandler(int options)
    • ServletContextHandler

      public ServletContextHandler(String contextPath, int options)
    • ServletContextHandler

      public ServletContextHandler(String contextPath, boolean sessions, boolean security)
    • ServletContextHandler

      public ServletContextHandler(SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
    • ServletContextHandler

      public ServletContextHandler(String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
    • ServletContextHandler

      public ServletContextHandler(String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler, int options)
  • Method Details

    • getServletContextHandler

      public static ServletContextHandler getServletContextHandler(jakarta.servlet.ServletContext servletContext, String purpose)
    • getServletContextHandler

      public static ServletContextHandler getServletContextHandler(jakarta.servlet.ServletContext servletContext)
    • getCurrentServletContext

      public static jakarta.servlet.ServletContext getCurrentServletContext()
    • getServletContext

      public static jakarta.servlet.ServletContext getServletContext(Context context)
    • getCurrentServletContextHandler

      public static ServletContextHandler getCurrentServletContextHandler()
    • setTempDirectory

      public void setTempDirectory(File tempDirectory)
      Description copied from class: ContextHandler

      Set the temporary directory returned by ContextHandler.ScopedContext.getTempDirectory(). If not set here, then the Server.getTempDirectory() is returned by ContextHandler.ScopedContext.getTempDirectory().

      If ContextHandler.isTempDirectoryPersistent() is true, the directory set here is used directly but may be created if it does not exist. If ContextHandler.isTempDirectoryPersistent() is false, then any File set here will be deleted and recreated as a directory during AbstractLifeCycle.start() and will be deleted during AbstractLifeCycle.stop().

      Overrides:
      setTempDirectory in class ContextHandler
      Parameters:
      tempDirectory - A directory. If it does not exist, it must be able to be created during start.
      See Also:
    • newServletContextApi

      public ServletContextHandler.ServletContextApi newServletContextApi()
    • getInvocationType

      public Invocable.InvocationType getInvocationType()
      Specified by:
      getInvocationType in interface Invocable
      Specified by:
      getInvocationType in interface Request.Handler
      Overrides:
      getInvocationType in class Handler.Wrapper
      Returns:
      The InvocationType of this object
    • dump

      public void dump(Appendable out, String indent) throws IOException
      Description copied from interface: Dumpable
      Dump this object (and children) into an Appendable using the provided indent after any new lines. The indent should not be applied to the first object dumped.
      Specified by:
      dump in interface Dumpable
      Overrides:
      dump in class ContextHandler
      Parameters:
      out - The appendable to dump to
      indent - The indent to apply after any new lines.
      Throws:
      IOException - if unable to write to Appendable
    • isUsingSecurityManager

      public boolean isUsingSecurityManager()
    • setUsingSecurityManager

      public void setUsingSecurityManager(boolean usingSecurityManager)
    • getContextPathEncoded

      public String getContextPathEncoded()
      Returns:
      Returns the encoded contextPath.
    • getRequestContextPath

      public String getRequestContextPath()
      Get the context path in a form suitable to be returned from HttpServletRequest.getContextPath() or ServletContext.getContextPath().
      Returns:
      Returns the encoded contextPath, or empty string for root context
    • getInitParameter

      public String getInitParameter(String name)
    • setInitParameter

      public String setInitParameter(String name, String value)
    • getInitParameterNames

      public Enumeration<String> getInitParameterNames()
    • getInitParams

      @ManagedAttribute("Initial Parameter map for the context") public Map<String,String> getInitParams()
      Returns:
      Returns the initParams.
    • removeEventListener

      public boolean removeEventListener(EventListener listener)
      Description copied from interface: Container
      Remove an event listener.
      Specified by:
      removeEventListener in interface Container
      Specified by:
      removeEventListener in interface LifeCycle
      Overrides:
      removeEventListener in class ContextHandler
      Parameters:
      listener - the listener to remove
      Returns:
      true if the listener was removed
      See Also:
    • addProgrammaticListener

      protected void addProgrammaticListener(EventListener listener)
      Apply any necessary restrictions on a programmatic added listener.
      Parameters:
      listener - the programmatic listener to add
    • isProgrammaticListener

      public boolean isProgrammaticListener(EventListener listener)
    • isDurableListener

      public boolean isDurableListener(EventListener listener)
    • getLogger

      public org.slf4j.Logger getLogger()
    • setLogger

      public void setLogger(org.slf4j.Logger logger)
    • contextInitialized

      public void contextInitialized() throws Exception
      Call the ServletContextListeners contextInitialized methods. This can be called from a ServletHandler during the proper sequence of initializing filters, servlets and listeners. However, if there is no ServletHandler, the ContextHandler will call this method during doStart().
      Throws:
      Exception
    • contextDestroyed

      public void contextDestroyed() throws Exception
      Call the ServletContextListeners with contextDestroyed. This method can be called from a ServletHandler in the proper sequence of destroying filters, servlets and listeners. If there is no ServletHandler, the ContextHandler must ensure these listeners are called instead.
      Throws:
      Exception
    • requestInitialized

      protected void requestInitialized(Request baseRequest, jakarta.servlet.http.HttpServletRequest request)
    • requestDestroyed

      protected void requestDestroyed(Request baseRequest, jakarta.servlet.http.HttpServletRequest request)
    • setDefaultRequestCharacterEncoding

      public void setDefaultRequestCharacterEncoding(String encoding)
    • getDefaultRequestCharacterEncoding

      public String getDefaultRequestCharacterEncoding()
    • setDefaultResponseCharacterEncoding

      public void setDefaultResponseCharacterEncoding(String encoding)
    • getDefaultResponseCharacterEncoding

      public String getDefaultResponseCharacterEncoding()
    • setContextPath

      public void setContextPath(String contextPath)
      Overrides:
      setContextPath in class ContextHandler
      Parameters:
      contextPath - The _contextPath to set.
    • setWelcomeFiles

      public void setWelcomeFiles(String[] files)
    • getWelcomeFiles

      @ManagedAttribute(value="Partial URIs of directory welcome files", readonly=true) public String[] getWelcomeFiles()
      Returns:
      The names of the files which the server should consider to be welcome files in this context.
      See Also:
    • getMaxFormContentSize

      @ManagedAttribute("The maximum content size") public int getMaxFormContentSize()
    • setMaxFormContentSize

      public void setMaxFormContentSize(int maxSize)
      Set the maximum size of a form post, to protect against DOS attacks from large forms.
      Parameters:
      maxSize - the maximum size of the form content (in bytes)
    • getMaxFormKeys

      public int getMaxFormKeys()
    • setMaxFormKeys

      public void setMaxFormKeys(int max)
      Set the maximum number of form Keys to protect against DOS attack from crafted hash keys.
      Parameters:
      max - the maximum number of form keys
    • loadClass

      public Class<?> loadClass(String className) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • addLocaleEncoding

      public void addLocaleEncoding(String locale, String encoding)
    • getLocaleEncoding

      public String getLocaleEncoding(String locale)
    • getLocaleEncoding

      public String getLocaleEncoding(Locale locale)
      Get the character encoding for a locale. The full locale name is first looked up in the map of encodings. If no encoding is found, then the locale language is looked up.
      Parameters:
      locale - a Locale value
      Returns:
      a String representing the character encoding for the locale or null if none found.
    • getLocaleEncodings

      public Map<String,String> getLocaleEncodings()
      Get all of the locale encodings
      Returns:
      a map of all the locale encodings: key is name of the locale and value is the char encoding
    • getResource

      public Resource getResource(String pathInContext) throws MalformedURLException
      Attempt to get a Resource from the Context.
      Parameters:
      pathInContext - the path within the base resource to attempt to get
      Returns:
      the resource, or null if not available.
      Throws:
      MalformedURLException - if unable to form a Resource from the provided path
    • newResource

      public Resource newResource(URL url) throws IOException
      Convert URL to Resource wrapper for ResourceFactory.newResource(URL) enables extensions to provide alternate resource implementations.
      Parameters:
      url - the url to convert to a Resource
      Returns:
      the Resource for that url
      Throws:
      IOException - if unable to create a Resource from the URL
    • newResource

      public Resource newResource(URI uri)
      Convert URL to Resource wrapper for ResourceFactory.newResource(URL) enables extensions to provide alternate resource implementations.
      Parameters:
      uri - the URI to convert to a Resource
      Returns:
      the Resource for that URI
    • newResource

      public Resource newResource(String urlOrPath)
      Convert a URL or path to a Resource. The default implementation is a wrapper for ResourceFactory.newResource(String).
      Parameters:
      urlOrPath - The URL or path to convert
      Returns:
      The Resource for the URL/path
    • getResourcePaths

      public Set<String> getResourcePaths(String path)
    • getServletContext

      public jakarta.servlet.ServletContext getServletContext()
    • newContext

      protected ContextHandler.ScopedContext newContext()
      Overrides:
      newContext in class ContextHandler
    • getContext

      Overrides:
      getContext in class ContextHandler
    • addEventListener

      public boolean addEventListener(EventListener listener)
      Add a context event listeners.
      Specified by:
      addEventListener in interface Container
      Specified by:
      addEventListener in interface LifeCycle
      Overrides:
      addEventListener in class ContextHandler
      Parameters:
      listener - the event listener to add
      Returns:
      true if the listener was added
      See Also:
    • setHandler

      public void setHandler(Handler handler)
      Specified by:
      setHandler in interface Handler.Singleton
      Overrides:
      setHandler in class Handler.Wrapper
      Parameters:
      handler - The Handler to set as a child
    • doStart

      protected void doStart() throws Exception
      Description copied from class: ContainerLifeCycle
      Starts the managed lifecycle beans in the order they were added.
      Overrides:
      doStart in class ContextHandler
      Throws:
      AbstractLifeCycle.StopException - If thrown, the lifecycle will immediately be stopped.
      Exception - If there was a problem starting. Will cause a transition to FAILED state
    • doStop

      protected void doStop() throws Exception
      Description copied from class: ContainerLifeCycle
      Stops the managed lifecycle beans in the reverse order they were added.
      Overrides:
      doStop in class ContextHandler
      Throws:
      Exception - If there was a problem stopping. Will cause a transition to FAILED state
    • newServletContextRequest

      protected ServletContextRequest newServletContextRequest(ServletChannel servletChannel, Request request, Response response, String decodedPathInContext, MatchedResource<ServletHandler.MappedServlet> matchedResource)
    • wrapRequest

      protected ContextRequest wrapRequest(Request request, Response response)
      Overrides:
      wrapRequest in class ContextHandler
    • wrapResponse

      protected ContextResponse wrapResponse(ContextRequest request, Response response)
      Overrides:
      wrapResponse in class ContextHandler
    • handleByContextHandler

      protected boolean handleByContextHandler(String pathInContext, ContextRequest request, Response response, Callback callback)
      Overrides:
      handleByContextHandler in class ContextHandler
    • notifyEnterScope

      protected void notifyEnterScope(Request request)
      Overrides:
      notifyEnterScope in class ContextHandler
      Parameters:
      request - A request that is applicable to the scope, or null
    • notifyExitScope

      protected void notifyExitScope(Request request)
      Overrides:
      notifyExitScope in class ContextHandler
      Parameters:
      request - A request that is applicable to the scope, or null
    • getDefaultSecurityHandlerClass

      public Class<? extends SecurityHandler> getDefaultSecurityHandlerClass()
      Get the defaultSecurityHandlerClass.
      Returns:
      the defaultSecurityHandlerClass
    • setDefaultSecurityHandlerClass

      public void setDefaultSecurityHandlerClass(Class<? extends SecurityHandler> defaultSecurityHandlerClass)
      Set the defaultSecurityHandlerClass.
      Parameters:
      defaultSecurityHandlerClass - the defaultSecurityHandlerClass to set
    • newSessionHandler

      protected SessionHandler newSessionHandler()
    • newSecurityHandler

      protected SecurityHandler newSecurityHandler()
    • newServletHandler

      protected ServletHandler newServletHandler()
    • startContext

      protected void startContext() throws Exception
      Finish constructing handlers and link them together.
      Throws:
      Exception
    • stopContext

      protected void stopContext() throws Exception
      Throws:
      Exception
    • getSecurityHandler

      @ManagedAttribute(value="context security handler", readonly=true) public SecurityHandler getSecurityHandler()
      Returns:
      Returns the securityHandler.
    • getServletHandler

      @ManagedAttribute(value="context servlet handler", readonly=true) public ServletHandler getServletHandler()
      Returns:
      Returns the servletHandler.
    • getSessionHandler

      @ManagedAttribute(value="context session handler", readonly=true) public SessionHandler getSessionHandler()
      Returns:
      Returns the sessionHandler.
    • addServlet

      public ServletHolder addServlet(String className, String pathSpec)
      Convenience method to add a servlet.
      Parameters:
      className - the servlet class name
      pathSpec - the path spec to map servlet to
      Returns:
      the ServletHolder for the added servlet
    • addServlet

      public ServletHolder addServlet(Class<? extends jakarta.servlet.Servlet> servlet, String pathSpec)
      Convenience method to add a servlet.
      Parameters:
      servlet - the servlet class
      pathSpec - the path spec to map servlet to
      Returns:
      the ServletHolder for the added servlet
    • addServlet

      public void addServlet(ServletHolder servlet, String pathSpec)
      Convenience method to add a servlet.
      Parameters:
      servlet - the servlet holder
      pathSpec - the path spec
    • addServlet

      public ServletHolder addServlet(jakarta.servlet.http.HttpServlet servlet, String pathSpec)
      Convenience method to add a servlet.
      Parameters:
      servlet - the servlet instance
      pathSpec - the path spec
      Returns:
      the ServletHolder for the added servlet
    • addFilter

      public void addFilter(FilterHolder holder, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
      Convenience method to add a filter
      Parameters:
      holder - the filter holder
      pathSpec - the path spec
      dispatches - the dispatcher types for this filter
    • addFilter

      public FilterHolder addFilter(Class<? extends jakarta.servlet.Filter> filterClass, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
      Convenience method to add a filter
      Parameters:
      filterClass - the filter class
      pathSpec - the path spec
      dispatches - the dispatcher types for this filter
      Returns:
      the FilterHolder that was created
    • addFilter

      public FilterHolder addFilter(String filterClass, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
      Convenience method to add a filter
      Parameters:
      filterClass - the filter class name
      pathSpec - the path spec
      dispatches - the dispatcher types for this filter
      Returns:
      the FilterHolder that was created
    • addFilter

      public FilterHolder addFilter(jakarta.servlet.Filter filter, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
      Convenience method to add a servlet.
      Parameters:
      filter - the filter instance
      pathSpec - the path spec
      dispatches - the dispatcher types for this filter
      Returns:
      the FilterHolder that was created
    • addServletContainerInitializer

      public ServletContainerInitializerHolder addServletContainerInitializer(jakarta.servlet.ServletContainerInitializer sci)
      Convenience method to programmatically add a ServletContainerInitializer.
      Parameters:
      sci - the ServletContainerInitializer to register.
      Returns:
      the ServletContainerInitializerHolder that was created
    • addServletContainerInitializer

      public ServletContainerInitializerHolder addServletContainerInitializer(jakarta.servlet.ServletContainerInitializer sci, Class<?>... classes)
      Convenience method to programmatically add a ServletContainerInitializer.
      Parameters:
      sci - the ServletContainerInitializer to register.
      classes - the Set of application classes.
      Returns:
      the ServletContainerInitializerHolder that was created
    • addServletContainerInitializer

      public void addServletContainerInitializer(ServletContainerInitializerHolder... sciHolders)
      Convenience method to programmatically add a list of ServletContainerInitializer. The initializers are guaranteed to be called in the order they are passed into this method.
      Parameters:
      sciHolders - the ServletContainerInitializerHolders
    • dynamicHolderAdded

      protected jakarta.servlet.ServletRegistration.Dynamic dynamicHolderAdded(ServletHolder holder)
      notification that a ServletRegistration has been created so we can track the annotations
      Parameters:
      holder - new holder created through the api.
      Returns:
      the ServletRegistration.Dynamic
    • addRoles

      protected void addRoles(String... roleNames)
      delegate for ServletContext.declareRole method
      Parameters:
      roleNames - role names to add
    • setServletSecurity

      public Set<String> setServletSecurity(jakarta.servlet.ServletRegistration.Dynamic registration, jakarta.servlet.ServletSecurityElement servletSecurityElement)
      Delegate for ServletRegistration.Dynamic.setServletSecurity method
      Parameters:
      registration - ServletRegistration.Dynamic instance that setServletSecurity was called on
      servletSecurityElement - new security info
      Returns:
      the set of exact URL mappings currently associated with the registration that are also present in the web.xml security constraints and thus will be unaffected by this call.
    • callContextInitialized

      public void callContextInitialized(jakarta.servlet.ServletContextListener l, jakarta.servlet.ServletContextEvent e)
    • callContextDestroyed

      public void callContextDestroyed(jakarta.servlet.ServletContextListener l, jakarta.servlet.ServletContextEvent e)
    • setSessionHandler

      public void setSessionHandler(SessionHandler sessionHandler)
      Parameters:
      sessionHandler - The sessionHandler to set.
    • setSecurityHandler

      public void setSecurityHandler(SecurityHandler securityHandler)
      Parameters:
      securityHandler - The SecurityHandler to set on this context.
    • setServletHandler

      public void setServletHandler(ServletHandler servletHandler)
      Parameters:
      servletHandler - The servletHandler to set.
    • insertHandler

      public void insertHandler(Handler.Singleton handler)
      Insert a HandlerWrapper before the first Session, Security or ServletHandler but after any other HandlerWrappers.
      Parameters:
      handler - the Handler to insert
    • getObjectFactory

      public DecoratedObjectFactory getObjectFactory()
      The DecoratedObjectFactory for use by IoC containers (weld / spring / etc)
      Returns:
      The DecoratedObjectFactory