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
HttpServletservice providers and annotated with theWebServletannotation. It also loads the registeredWebServerConfigurationobjects 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 voidaddLifeCycleListener(org.eclipse.jetty.util.component.LifeCycle.Listener ll)static WebServergetDefault()Get the default web server instance.intgetPort()WebServerPreferencesgetPreferences()booleanisFailed()booleanisRunning()booleanisStarted()booleanisStarting()booleanisStopped()booleanisStopping()voidlifeCycleFailure(org.eclipse.jetty.util.component.LifeCycle lc, java.lang.Throwable thrwbl)voidlifeCycleStarted(org.eclipse.jetty.util.component.LifeCycle lc)voidlifeCycleStarting(org.eclipse.jetty.util.component.LifeCycle lc)voidlifeCycleStopped(org.eclipse.jetty.util.component.LifeCycle lc)voidlifeCycleStopping(org.eclipse.jetty.util.component.LifeCycle lc)static java.lang.StringportablePathToURI(java.lang.String path)Get the public URI for a portable path.voidregisterDenial(java.lang.String urlPattern)Register a URL pattern to be denied access.voidregisterRedirection(java.lang.String urlPattern, java.lang.String redirection)Register a URL pattern to be redirected to another resource.voidregisterResource(java.lang.String urlPattern, java.lang.String filePath)Register a URL pattern to return resources from the file system.voidregisterServlet(java.lang.Class<? extends javax.servlet.http.HttpServlet> type)Register aHttpServletthat is annotated with theWebServletannotation.voidregisterServlet(java.lang.Class<? extends javax.servlet.http.HttpServlet> type, javax.servlet.http.HttpServlet instance)Register aHttpServletthat is annotated with theWebServletannotation.voidremoveLifeCycleListener(org.eclipse.jetty.util.component.LifeCycle.Listener ll)voidstart()Start the web server.voidstop()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:
startin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
stop
public void stop() throws java.lang.Exception
Stop the server.- Specified by:
stopin 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 aHttpServletthat is annotated with theWebServletannotation.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 aHttpServletthat is annotated with theWebServletannotation.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:
lifeCycleStartingin interfaceorg.eclipse.jetty.util.component.LifeCycle.Listener
-
lifeCycleStarted
public void lifeCycleStarted(org.eclipse.jetty.util.component.LifeCycle lc)
- Specified by:
lifeCycleStartedin interfaceorg.eclipse.jetty.util.component.LifeCycle.Listener
-
lifeCycleFailure
public void lifeCycleFailure(org.eclipse.jetty.util.component.LifeCycle lc, java.lang.Throwable thrwbl)
- Specified by:
lifeCycleFailurein interfaceorg.eclipse.jetty.util.component.LifeCycle.Listener
-
lifeCycleStopping
public void lifeCycleStopping(org.eclipse.jetty.util.component.LifeCycle lc)
- Specified by:
lifeCycleStoppingin interfaceorg.eclipse.jetty.util.component.LifeCycle.Listener
-
lifeCycleStopped
public void lifeCycleStopped(org.eclipse.jetty.util.component.LifeCycle lc)
- Specified by:
lifeCycleStoppedin interfaceorg.eclipse.jetty.util.component.LifeCycle.Listener
-
isRunning
public boolean isRunning()
- Specified by:
isRunningin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isStarted
public boolean isStarted()
- Specified by:
isStartedin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isStarting
public boolean isStarting()
- Specified by:
isStartingin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isStopping
public boolean isStopping()
- Specified by:
isStoppingin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isStopped
public boolean isStopped()
- Specified by:
isStoppedin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isFailed
public boolean isFailed()
- Specified by:
isFailedin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
addLifeCycleListener
public void addLifeCycleListener(org.eclipse.jetty.util.component.LifeCycle.Listener ll)
- Specified by:
addLifeCycleListenerin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
removeLifeCycleListener
public void removeLifeCycleListener(org.eclipse.jetty.util.component.LifeCycle.Listener ll)
- Specified by:
removeLifeCycleListenerin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
-