Class WebServer
- java.lang.Object
-
- jmri.web.server.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 theWebServlet
annotation. It also loads the registeredWebServerConfiguration
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 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 aHttpServlet
that is annotated with theWebServlet
annotation.void
registerServlet(java.lang.Class<? extends javax.servlet.http.HttpServlet> type, javax.servlet.http.HttpServlet instance)
Register aHttpServlet
that is annotated with theWebServlet
annotation.void
removeLifeCycleListener(org.eclipse.jetty.util.component.LifeCycle.Listener ll)
void
start()
Start the web server.void
stop()
Stop the server.
-
-
-
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 interfaceorg.eclipse.jetty.util.component.LifeCycle
-
stop
public void stop() throws java.lang.Exception
Stop the server.- Specified by:
stop
in interfaceorg.eclipse.jetty.util.component.LifeCycle
- Throws:
java.lang.Exception
- if there is an error stopping the server; defined by Jetty superclass
-
portablePathToURI
public static java.lang.String portablePathToURI(java.lang.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 ofFileUtil.PREFERENCES
,FileUtil.PROFILE
,FileUtil.SETTINGS
, orFileUtil.PROGRAM
.- Parameters:
path
- the JMRI portable path- Returns:
- The servable URI or null
- See Also:
FileUtil.getPortableFilename(java.io.File)
-
getPort
public int getPort()
-
getPreferences
public WebServerPreferences getPreferences()
-
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: 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 forfilePath
- 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 redirectedredirection
- 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)
Register aHttpServlet
that is annotated with theWebServlet
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(java.lang.Class<? extends javax.servlet.http.HttpServlet> type, javax.servlet.http.HttpServlet instance)
Register aHttpServlet
that is annotated with theWebServlet
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 interfaceorg.eclipse.jetty.util.component.LifeCycle.Listener
-
lifeCycleStarted
public void lifeCycleStarted(org.eclipse.jetty.util.component.LifeCycle lc)
- Specified by:
lifeCycleStarted
in interfaceorg.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 interfaceorg.eclipse.jetty.util.component.LifeCycle.Listener
-
lifeCycleStopping
public void lifeCycleStopping(org.eclipse.jetty.util.component.LifeCycle lc)
- Specified by:
lifeCycleStopping
in interfaceorg.eclipse.jetty.util.component.LifeCycle.Listener
-
lifeCycleStopped
public void lifeCycleStopped(org.eclipse.jetty.util.component.LifeCycle lc)
- Specified by:
lifeCycleStopped
in interfaceorg.eclipse.jetty.util.component.LifeCycle.Listener
-
isRunning
public boolean isRunning()
- Specified by:
isRunning
in interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isStarted
public boolean isStarted()
- Specified by:
isStarted
in interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isStarting
public boolean isStarting()
- Specified by:
isStarting
in interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isStopping
public boolean isStopping()
- Specified by:
isStopping
in interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isStopped
public boolean isStopped()
- Specified by:
isStopped
in interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isFailed
public boolean isFailed()
- Specified by:
isFailed
in interfaceorg.eclipse.jetty.util.component.LifeCycle
-
addLifeCycleListener
public void addLifeCycleListener(org.eclipse.jetty.util.component.LifeCycle.Listener ll)
- Specified by:
addLifeCycleListener
in interfaceorg.eclipse.jetty.util.component.LifeCycle
-
removeLifeCycleListener
public void removeLifeCycleListener(org.eclipse.jetty.util.component.LifeCycle.Listener ll)
- Specified by:
removeLifeCycleListener
in interfaceorg.eclipse.jetty.util.component.LifeCycle
-
-