Class ServletHandler

All Implemented Interfaces:
Handler, Handler.Container, Handler.Singleton, Request.Handler, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle, Invocable

@ManagedObject("Servlet Handler") public class ServletHandler extends Handler.Wrapper
Servlet HttpHandler.

This handler maps requests to servlets that implement the jakarta.servlet.http.HttpServlet API.

This handler does not implement the full J2EE features and is intended to be used directly when a full web application is not required. If a Web application is required, then this handler should be used as part of a org.eclipse.jetty.webapp.WebAppContext.

Unless run as part of a ServletContextHandler or derivative, the initialize() method must be called manually after start().

  • Field Details

    • _chainCache

      protected final ConcurrentMap<String,jakarta.servlet.FilterChain>[] _chainCache
  • Constructor Details

    • ServletHandler

      public ServletHandler()
      Constructor.
  • Method Details

    • isDecodeAmbiguousURIs

      @ManagedAttribute("True if URIs with violations are decoded") public boolean isDecodeAmbiguousURIs()
    • setDecodeAmbiguousURIs

      public void setDecodeAmbiguousURIs(boolean decodeAmbiguousURIs)
      Parameters:
      decodeAmbiguousURIs - True if ambiguous URIs are decoded by ServletApiRequest.getServletPath() and ServletApiRequest.getPathInfo().
    • isDumpable

      public boolean isDumpable(Object o)
    • 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 ContainerLifeCycle
      Parameters:
      out - The appendable to dump to
      indent - The indent to apply after any new lines.
      Throws:
      IOException - if unable to write to Appendable
    • 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 Handler.Abstract
      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
    • isEnsureDefaultServlet

      public boolean isEnsureDefaultServlet()
      Returns:
      true if ServletHandler always has a default servlet, using ServletHandler.Default404Servlet if no other default servlet is configured.
    • setEnsureDefaultServlet

      public void setEnsureDefaultServlet(boolean ensureDefaultServlet)
      Parameters:
      ensureDefaultServlet - true if ServletHandler always has a default servlet, using ServletHandler.Default404Servlet if no other default servlet is configured.
    • start

      protected void start(LifeCycle l) throws Exception
      Description copied from class: ContainerLifeCycle
      Starts the given lifecycle.
      Overrides:
      start in class ContainerLifeCycle
      Parameters:
      l - the lifecycle to start
      Throws:
      Exception - if unable to start lifecycle
    • stop

      protected void stop(LifeCycle l) throws Exception
      Description copied from class: ContainerLifeCycle
      Stops the given lifecycle.
      Overrides:
      stop in class ContainerLifeCycle
      Parameters:
      l - the lifecycle to stop
      Throws:
      Exception - if unable to stop the lifecycle
    • 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 Handler.Abstract
      Throws:
      Exception - If there was a problem stopping. Will cause a transition to FAILED state
    • getFilterMappings

      @ManagedAttribute(value="filters", readonly=true) public FilterMapping[] getFilterMappings()
    • getFilters

      @ManagedAttribute(value="filters", readonly=true) public FilterHolder[] getFilters()
    • getServletContext

      public jakarta.servlet.ServletContext getServletContext()
    • getServletContextHandler

      public ServletContextHandler getServletContextHandler()
    • getServletMappings

      @ManagedAttribute(value="mappings of servlets", readonly=true) public ServletMapping[] getServletMappings()
    • getServletMapping

      public ServletMapping getServletMapping(String pathSpec)
      Get the ServletMapping matching the path
      Parameters:
      pathSpec - the path spec
      Returns:
      the servlet mapping for the path spec (or null if not found)
    • getServlets

      @ManagedAttribute(value="servlets", readonly=true) public ServletHolder[] getServlets()
    • getServlets

      public List<ServletHolder> getServlets(Class<?> clazz)
    • getServlet

      public ServletHolder getServlet(String name)
    • getIdentityService

      protected IdentityService getIdentityService()
    • handle

      public boolean handle(Request request, Response response, Callback callback) throws Exception
      Description copied from interface: Request.Handler

      Invoked to handle the passed HTTP request and response.

      The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.

      Exceptions thrown by this method may be subsequently handled by an error Request.Handler, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.

      The simplest implementation is:

       public boolean handle(Request request, Response response, Callback callback)
       {
           callback.succeeded();
           return true;
       }
       

      A HelloWorld implementation is:

       public boolean handle(Request request, Response response, Callback callback)
       {
           response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback);
           return true;
       }
       
      Specified by:
      handle in interface Request.Handler
      Overrides:
      handle in class Handler.Wrapper
      Parameters:
      request - the HTTP request to handle
      response - the HTTP response to handle
      callback - the callback to complete when the handling is complete
      Returns:
      True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.
      Throws:
      Exception - if there is a failure during the handling. Catchers cannot assume that the callback will be called and thus should attempt to complete the request as if a false had been returned.
    • getMatchedServlet

      public MatchedResource<ServletHandler.MappedServlet> getMatchedServlet(String target)
      ServletHolder matching target path.
      Parameters:
      target - Path within _context or servlet name
      Returns:
      MatchedResource, pointing to the MappedResource for the ServletHolder, and also the pathspec specific name/info sections for the match. Named servlets have a null PathSpec and MatchedResource.
    • getMappedServlet

      public ServletHandler.MappedServlet getMappedServlet(String target)
      ServletHolder matching path.
      Parameters:
      target - Path within _context or servlet name
      Returns:
      MappedResource to the ServletHolder. Named servlets have a null PathSpec
    • getFilterChain

      protected jakarta.servlet.FilterChain getFilterChain(jakarta.servlet.http.HttpServletRequest request, String pathInContext, ServletHolder servletHolder)
    • newFilterChain

      protected jakarta.servlet.FilterChain newFilterChain(FilterHolder filterHolder, jakarta.servlet.FilterChain chain)
      Create a FilterChain that calls the passed filter with the passed chain
      Parameters:
      filterHolder - The filter to invoke
      chain - The chain to pass to the filter
      Returns:
      A FilterChain that invokes the filter with the chain
    • invalidateChainsCache

      protected void invalidateChainsCache()
    • isAvailable

      public boolean isAvailable()
      Returns:
      true if the handler is started and there are no unavailable servlets
    • setStartWithUnavailable

      public void setStartWithUnavailable(boolean start)
      Parameters:
      start - True if this handler will start with unavailable servlets
    • isAllowDuplicateMappings

      public boolean isAllowDuplicateMappings()
      Returns:
      the allowDuplicateMappings
    • setAllowDuplicateMappings

      public void setAllowDuplicateMappings(boolean allowDuplicateMappings)
      Set the allowDuplicateMappings to set.
      Parameters:
      allowDuplicateMappings - the allowDuplicateMappings to set
    • isStartWithUnavailable

      public boolean isStartWithUnavailable()
      Returns:
      True if this handler will start with unavailable servlets
    • initialize

      public void initialize() throws Exception
      Initialize filters and load-on-startup servlets.
      Throws:
      Exception - if unable to initialize
    • isInitialized

      public boolean isInitialized()
      Returns:
      true if initialized has been called, false otherwise
    • initializeHolders

      protected void initializeHolders(Collection<? extends BaseHolder<?>> holders)
    • isFilterChainsCached

      public boolean isFilterChainsCached()
      Returns:
      whether the filter chains are cached.
    • addListener

      public void addListener(ListenerHolder listener)
      Add a holder for a listener
      Parameters:
      listener - the listener for the holder
    • getListeners

      public ListenerHolder[] getListeners()
    • setListeners

      public void setListeners(ListenerHolder[] holders)
    • newListenerHolder

      public ListenerHolder newListenerHolder(Source source)
    • newServletHolder

      public ServletHolder newServletHolder(Source source)
      Add a new servlet holder
      Parameters:
      source - the holder source
      Returns:
      the servlet holder
    • addServletWithMapping

      public ServletHolder addServletWithMapping(String className, String pathSpec)
      Convenience method to add a servlet.
      Parameters:
      className - the class name
      pathSpec - the path spec
      Returns:
      The servlet holder.
    • addServletWithMapping

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

      public void addServletWithMapping(ServletHolder servlet, String pathSpec)
      Convenience method to add a servlet.
      Parameters:
      servlet - servlet holder to add
      pathSpec - servlet mappings for the servletHolder
    • addServlet

      public void addServlet(ServletHolder holder)
      Convenience method to add a pre-constructed ServletHolder.
      Parameters:
      holder - the servlet holder
    • addServletMapping

      public void addServletMapping(ServletMapping mapping)
      Convenience method to add a pre-constructed ServletMapping.
      Parameters:
      mapping - the servlet mapping
    • setServletSecurity

      public Set<String> setServletSecurity(jakarta.servlet.ServletRegistration.Dynamic registration, jakarta.servlet.ServletSecurityElement servletSecurityElement)
    • newFilterHolder

      public FilterHolder newFilterHolder(Source source)
    • getFilter

      public FilterHolder getFilter(String name)
    • addFilterWithMapping

      public FilterHolder addFilterWithMapping(Class<? extends jakarta.servlet.Filter> filter, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
      Convenience method to add a filter.
      Parameters:
      filter - class of filter to create
      pathSpec - filter mappings for filter
      dispatches - see FilterMapping.setDispatches(int)
      Returns:
      The filter holder.
    • addFilterWithMapping

      public FilterHolder addFilterWithMapping(String className, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
      Convenience method to add a filter.
      Parameters:
      className - of filter
      pathSpec - filter mappings for filter
      dispatches - see FilterMapping.setDispatches(int)
      Returns:
      The filter holder.
    • addFilterWithMapping

      public void addFilterWithMapping(FilterHolder holder, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
      Convenience method to add a filter.
      Parameters:
      holder - filter holder to add
      pathSpec - filter mappings for filter
      dispatches - see FilterMapping.setDispatches(int)
    • addFilterWithMapping

      public FilterHolder addFilterWithMapping(Class<? extends jakarta.servlet.Filter> filter, String pathSpec, int dispatches)
      Convenience method to add a filter.
      Parameters:
      filter - class of filter to create
      pathSpec - filter mappings for filter
      dispatches - see FilterMapping.setDispatches(int)
      Returns:
      The filter holder.
    • addFilterWithMapping

      public FilterHolder addFilterWithMapping(String className, String pathSpec, int dispatches)
      Convenience method to add a filter.
      Parameters:
      className - of filter
      pathSpec - filter mappings for filter
      dispatches - see FilterMapping.setDispatches(int)
      Returns:
      The filter holder.
    • addFilterWithMapping

      public void addFilterWithMapping(FilterHolder holder, String pathSpec, int dispatches)
      Convenience method to add a filter.
      Parameters:
      holder - filter holder to add
      pathSpec - filter mappings for filter
      dispatches - see FilterMapping.setDispatches(int)
    • addFilter

      public void addFilter(FilterHolder filter, FilterMapping filterMapping)
      Convenience method to add a filter and mapping
      Parameters:
      filter - the filter holder
      filterMapping - the filter mapping
    • addFilter

      public void addFilter(FilterHolder filter)
      Convenience method to add a preconstructed FilterHolder
      Parameters:
      filter - the filter holder
    • prependFilter

      public void prependFilter(FilterHolder filter)
      Convenience method to add a preconstructed FilterHolder
      Parameters:
      filter - the filter holder
    • addFilterMapping

      public void addFilterMapping(FilterMapping mapping)
      Convenience method to add a preconstructed FilterMapping
      Parameters:
      mapping - the filter mapping
    • prependFilterMapping

      public void prependFilterMapping(FilterMapping mapping)
      Convenience method to add a preconstructed FilterMapping
      Parameters:
      mapping - the filter mapping
    • removeFilterHolder

      public void removeFilterHolder(FilterHolder holder)
    • removeFilterMapping

      public void removeFilterMapping(FilterMapping mapping)
    • updateNameMappings

      protected void updateNameMappings()
    • asPathSpec

      protected PathSpec asPathSpec(String pathSpec)
    • updateMappings

      protected void updateMappings()
    • notFound

      protected void notFound(Request baseRequest, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException
      Throws:
      IOException
      jakarta.servlet.ServletException
    • containsFilterHolder

      protected boolean containsFilterHolder(FilterHolder holder)
    • containsServletHolder

      protected boolean containsServletHolder(ServletHolder holder)
    • setFilterChainsCached

      public void setFilterChainsCached(boolean filterChainsCached)
      Parameters:
      filterChainsCached - The filterChainsCached to set.
    • setFilterMappings

      public void setFilterMappings(FilterMapping[] filterMappings)
      Parameters:
      filterMappings - The filterMappings to set.
    • setFilters

      public void setFilters(FilterHolder[] holders)
    • setServletMappings

      public void setServletMappings(ServletMapping[] servletMappings)
      Parameters:
      servletMappings - The servletMappings to set.
    • setServlets

      public void setServlets(ServletHolder[] holders)
      Set Servlets.
      Parameters:
      holders - Array of servlets to define
    • getMaxFilterChainsCacheSize

      public int getMaxFilterChainsCacheSize()
      Returns:
      The maximum entries in a filter chain cache.
    • setMaxFilterChainsCacheSize

      public void setMaxFilterChainsCacheSize(int maxFilterChainsCacheSize)
      Set the maximum filter chain cache size. Filter chains are cached if isFilterChainsCached() is true. If the max cache size is greater than zero, then the cache is flushed whenever it grows to be this size.
      Parameters:
      maxFilterChainsCacheSize - the maximum number of entries in a filter chain cache.