Class WebServer

  • All Implemented Interfaces:
    java.util.EventListener, org.eclipse.jetty.util.component.LifeCycle, org.eclipse.jetty.util.component.LifeCycle.Listener

    public final class WebServer
    extends java.lang.Object
    implements org.eclipse.jetty.util.component.LifeCycle, org.eclipse.jetty.util.component.LifeCycle.Listener
    An HTTP server that handles requests for HTTPServlets.

    This server loads HttpServlets registered as HttpServlet service providers and annotated with the WebServlet annotation. It also loads the registered WebServerConfiguration objects to get configuration for file handling, redirection, and denial of access to resources.

    When there is a conflict over how a path should be handled, denials take precedence, followed by servlets, redirections, and lastly direct access to files.

    • Nested Class Summary

      • Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle

        org.eclipse.jetty.util.component.LifeCycle.Listener
    • Constructor Summary

      Constructors 
      Constructor Description
      WebServer()
      Create a WebServer instance with the default preferences.
      WebServer​(WebServerPreferences preferences)
      Create a WebServer instance with the specified preferences.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addLifeCycleListener​(org.eclipse.jetty.util.component.LifeCycle.Listener ll)  
      static WebServer getDefault()
      Get the default web server instance.
      int getPort()  
      WebServerPreferences getPreferences()  
      boolean isFailed()  
      boolean isRunning()  
      boolean isStarted()  
      boolean isStarting()  
      boolean isStopped()  
      boolean isStopping()  
      void lifeCycleFailure​(org.eclipse.jetty.util.component.LifeCycle lc, java.lang.Throwable thrwbl)  
      void lifeCycleStarted​(org.eclipse.jetty.util.component.LifeCycle lc)  
      void lifeCycleStarting​(org.eclipse.jetty.util.component.LifeCycle lc)  
      void lifeCycleStopped​(org.eclipse.jetty.util.component.LifeCycle lc)  
      void lifeCycleStopping​(org.eclipse.jetty.util.component.LifeCycle lc)  
      static java.lang.String portablePathToURI​(java.lang.String path)
      Get the public URI for a portable path.
      void registerDenial​(java.lang.String urlPattern)
      Register a URL pattern to be denied access.
      void registerRedirection​(java.lang.String urlPattern, java.lang.String redirection)
      Register a URL pattern to be redirected to another resource.
      void registerResource​(java.lang.String urlPattern, java.lang.String filePath)
      Register a URL pattern to return resources from the file system.
      void registerServlet​(java.lang.Class<? extends javax.servlet.http.HttpServlet> type)
      Register a HttpServlet that is annotated with the WebServlet annotation.
      void registerServlet​(java.lang.Class<? extends javax.servlet.http.HttpServlet> type, javax.servlet.http.HttpServlet instance)
      Register a HttpServlet that is annotated with the WebServlet annotation.
      void removeLifeCycleListener​(org.eclipse.jetty.util.component.LifeCycle.Listener ll)  
      void start()
      Start the web server.
      void stop()
      Stop the server.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • WebServer

        public WebServer()
        Create a WebServer instance with the default preferences.
      • WebServer

        public WebServer​(WebServerPreferences preferences)
        Create a WebServer instance with the specified preferences.
        Parameters:
        preferences - the preferences
    • Method Detail

      • getDefault

        @Nonnull
        public static WebServer getDefault()
        Get the default web server instance.
        Returns:
        a WebServer instance, either the existing instance or a new instance created with the default constructor.
      • start

        public void start()
        Start the web server.
        Specified by:
        start in interface org.eclipse.jetty.util.component.LifeCycle
      • stop

        public void stop()
                  throws java.lang.Exception
        Stop the server.
        Specified by:
        stop in interface org.eclipse.jetty.util.component.LifeCycle
        Throws:
        java.lang.Exception - if there is an error stopping the server; defined by Jetty superclass
      • registerDenial

        public void registerDenial​(java.lang.String urlPattern)
        Register a URL pattern to be denied access.
        Parameters:
        urlPattern - the pattern to deny access to
      • registerResource

        public void registerResource​(java.lang.String urlPattern,
                                     java.lang.String filePath)
        Register a URL pattern to return resources from the file system. The filePath may start with any of the following:
        1. FileUtil.PREFERENCES
        2. FileUtil.PROFILE
        3. FileUtil.SETTINGS
        4. FileUtil.PROGRAM
        Note that the filePath can be overridden by an otherwise identical filePath starting with any of the portable paths above it in the preceding list.
        Parameters:
        urlPattern - the pattern to get resources for
        filePath - the portable path for the resources
        Throws:
        java.lang.IllegalArgumentException - if urlPattern is already registered to deny access or for a servlet or if filePath is not allowed
      • registerRedirection

        public void registerRedirection​(java.lang.String urlPattern,
                                        java.lang.String redirection)
        Register a URL pattern to be redirected to another resource.
        Parameters:
        urlPattern - the pattern to be redirected
        redirection - the path to which the pattern is redirected
        Throws:
        java.lang.IllegalArgumentException - if urlPattern is already registered for any other purpose
      • registerServlet

        public void registerServlet​(java.lang.Class<? extends javax.servlet.http.HttpServlet> type,
                                    javax.servlet.http.HttpServlet instance)
        Register a HttpServlet that is annotated with the WebServlet annotation.

        Registration reads the WebServlet annotation to get the list of paths the servlet should handle and creates instances of the Servlet to handle each path.

        Note that all HttpServlets registered using this mechanism must have a default constructor.

        Parameters:
        type - The actual class of the servlet.
        instance - An un-initialized, un-registered instance of the servlet.
      • lifeCycleStarting

        public void lifeCycleStarting​(org.eclipse.jetty.util.component.LifeCycle lc)
        Specified by:
        lifeCycleStarting in interface org.eclipse.jetty.util.component.LifeCycle.Listener
      • lifeCycleStarted

        public void lifeCycleStarted​(org.eclipse.jetty.util.component.LifeCycle lc)
        Specified by:
        lifeCycleStarted in interface org.eclipse.jetty.util.component.LifeCycle.Listener
      • lifeCycleFailure

        public void lifeCycleFailure​(org.eclipse.jetty.util.component.LifeCycle lc,
                                     java.lang.Throwable thrwbl)
        Specified by:
        lifeCycleFailure in interface org.eclipse.jetty.util.component.LifeCycle.Listener
      • lifeCycleStopping

        public void lifeCycleStopping​(org.eclipse.jetty.util.component.LifeCycle lc)
        Specified by:
        lifeCycleStopping in interface org.eclipse.jetty.util.component.LifeCycle.Listener
      • lifeCycleStopped

        public void lifeCycleStopped​(org.eclipse.jetty.util.component.LifeCycle lc)
        Specified by:
        lifeCycleStopped in interface org.eclipse.jetty.util.component.LifeCycle.Listener
      • isRunning

        public boolean isRunning()
        Specified by:
        isRunning in interface org.eclipse.jetty.util.component.LifeCycle
      • isStarted

        public boolean isStarted()
        Specified by:
        isStarted in interface org.eclipse.jetty.util.component.LifeCycle
      • isStarting

        public boolean isStarting()
        Specified by:
        isStarting in interface org.eclipse.jetty.util.component.LifeCycle
      • isStopping

        public boolean isStopping()
        Specified by:
        isStopping in interface org.eclipse.jetty.util.component.LifeCycle
      • isStopped

        public boolean isStopped()
        Specified by:
        isStopped in interface org.eclipse.jetty.util.component.LifeCycle
      • isFailed

        public boolean isFailed()
        Specified by:
        isFailed in interface org.eclipse.jetty.util.component.LifeCycle
      • addLifeCycleListener

        public void addLifeCycleListener​(org.eclipse.jetty.util.component.LifeCycle.Listener ll)
        Specified by:
        addLifeCycleListener in interface org.eclipse.jetty.util.component.LifeCycle
      • removeLifeCycleListener

        public void removeLifeCycleListener​(org.eclipse.jetty.util.component.LifeCycle.Listener ll)
        Specified by:
        removeLifeCycleListener in interface org.eclipse.jetty.util.component.LifeCycle