Class WebServer

java.lang.Object
jmri.web.server.WebServer
All Implemented Interfaces:
EventListener, org.eclipse.jetty.util.component.LifeCycle, org.eclipse.jetty.util.component.LifeCycle.Listener

public final class WebServer extends 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.

  • Constructor Details

    • 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 Details

    • 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 Exception
      Stop the server.
      Specified by:
      stop in interface org.eclipse.jetty.util.component.LifeCycle
      Throws:
      Exception - if there is an error stopping the server; defined by Jetty superclass
    • portablePathToURI

      public static String portablePathToURI(String path)
      Get the public URI for a portable path. This method returns public URIs for only some portable paths, and does not check that the portable path is actually sane. Note that this refuses to return portable paths that are outside of FileUtil.PREFERENCES, FileUtil.PROFILE, FileUtil.SETTINGS, or FileUtil.PROGRAM.
      Parameters:
      path - the JMRI portable path
      Returns:
      The servable URI or null
      See Also:
    • getPort

      public int getPort()
    • getPreferences

    • registerDenial

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

      public void registerResource(String urlPattern, 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:
      IllegalArgumentException - if urlPattern is already registered to deny access or for a servlet or if filePath is not allowed
    • registerRedirection

      public void registerRedirection(String urlPattern, 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:
      IllegalArgumentException - if urlPattern is already registered for any other purpose
    • registerServlet

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

      This method calls registerServlet(java.lang.Class, javax.servlet.http.HttpServlet) with a null HttpServlet.

      Parameters:
      type - The actual class of the servlet.
    • registerServlet

      public void registerServlet(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, 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