Class Server

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

@ManagedObject public class Server extends Handler.Wrapper implements Attributes
  • Constructor Details

  • Method Details

    • getDefaultHandler

      public Handler getDefaultHandler()
    • setDefaultHandler

      public void setDefaultHandler(Handler defaultHandler)
      Parameters:
      defaultHandler - The handler to use if no other handler is set or has handled the request. This handler should always accept the request, even if only to send a 404.
    • 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.
      See Also:
    • getServerInfo

      public String getServerInfo()
    • setTempDirectory

      public void setTempDirectory(String temp)

      Convenience method to call setTempDirectory(File) from a String representation of the temporary directory.

      Parameters:
      temp - A string representation of the temporary directory.
      See Also:
    • setTempDirectory

      public void setTempDirectory(File temp)

      Set the temporary directory returned by Context.getTempDirectory() for the root Context returned getContext(). If not set explicitly here, then the root Context.getTempDirectory() will return either the directory found at new File(IO.asFile(System.getProperty("jetty.base")), "work") if it exists, else the JVMs temporary directory as IO.asFile(System.getProperty("java.io.tmpdir")).

      Parameters:
      temp - A directory that must exist and be writable or null to get the default.
    • getTempDirectory

      @ManagedAttribute(value="The server temporary directory", readonly=true) public File getTempDirectory()
      Returns:
      The server temporary directory if set, else null. To always obtain a non-null temporary directory use Context.getTempDirectory() on getContext().
      See Also:
    • setServerInfo

      public void setServerInfo(String serverInfo)
    • getContext

      public Context getContext()
      Get the Context associated with all Requests prior to being handled by a ContextHandler. A Server's Context:
    • getMimeTypes

      public MimeTypes.Mutable getMimeTypes()
    • getInvocationType

      public Invocable.InvocationType getInvocationType()
      Specified by:
      getInvocationType in interface Invocable
      Specified by:
      getInvocationType in interface Request.Handler
      Overrides:
      getInvocationType in class Handler.Wrapper
      Returns:
      The InvocationType of this object
    • isOpenEarly

      public boolean isOpenEarly()
    • setOpenEarly

      public void setOpenEarly(boolean openEarly)
      Allows to disable early opening of network sockets. Network sockets are opened early by default.
      Parameters:
      openEarly - If openEarly is true (default), network sockets are opened before starting other components. If openEarly is false, network connectors open sockets when they're started.
    • isDryRun

      public boolean isDryRun()
    • setDryRun

      public void setDryRun(boolean dryRun)
    • getRequestLog

      public RequestLog getRequestLog()
    • getErrorHandler

      public Request.Handler getErrorHandler()
    • setRequestLog

      public void setRequestLog(RequestLog requestLog)
    • setErrorHandler

      public void setErrorHandler(Request.Handler errorHandler)
    • getVersion

      @ManagedAttribute("The version of this server") public static String getVersion()
    • setStopTimeout

      public void setStopTimeout(long stopTimeout)
    • getStopTimeout

      public long getStopTimeout()
    • getStopAtShutdown

      public boolean getStopAtShutdown()
    • setStopAtShutdown

      public void setStopAtShutdown(boolean stop)
      Set stop server at shutdown behaviour.
      Parameters:
      stop - If true, this server instance will be explicitly stopped when the JVM is shutdown. Otherwise the JVM is stopped with the server running.
      See Also:
    • getConnectors

      @ManagedAttribute(value="connectors for this server", readonly=true) public Connector[] getConnectors()
      Returns:
      Returns the connectors.
    • addConnector

      public void addConnector(Connector connector)
    • removeConnector

      public void removeConnector(Connector connector)
      Convenience method which calls getConnectors() and setConnectors(Connector[]) to remove a connector.
      Parameters:
      connector - The connector to remove.
    • setConnectors

      public void setConnectors(Connector[] connectors)
      Set the connectors for this server. Each connector has this server set as it's ThreadPool and its Handler.
      Parameters:
      connectors - The connectors to set.
    • addBeanToAllConnectors

      public void addBeanToAllConnectors(Object bean)
      Add a bean to all connectors on the server. If the bean is an instance of Connection.Listener it will also be registered as a listener on all connections accepted by the connectors.
      Parameters:
      bean - the bean to be added.
    • getThreadPool

      @ManagedAttribute("The server Thread pool") public ThreadPool getThreadPool()
      Returns:
      Returns the threadPool.
    • getScheduler

      @ManagedAttribute("The server Scheduler") public Scheduler getScheduler()
    • getByteBufferPool

      @ManagedAttribute("The server ByteBuffer pool") public ByteBufferPool getByteBufferPool()
    • isDumpAfterStart

      @ManagedAttribute("Whether to dump the server to stderr after start") public boolean isDumpAfterStart()
      Returns:
      true if ContainerLifeCycle.dumpStdErr() is called after starting
    • setDumpAfterStart

      public void setDumpAfterStart(boolean dumpAfterStart)
      Set true if ContainerLifeCycle.dumpStdErr() is called after starting.
      Parameters:
      dumpAfterStart - true if ContainerLifeCycle.dumpStdErr() is called after starting
    • isDumpBeforeStop

      @ManagedAttribute("Whether to dump the server to stderr before stop") public boolean isDumpBeforeStop()
      Returns:
      true if ContainerLifeCycle.dumpStdErr() is called before stopping
    • setDumpBeforeStop

      public void setDumpBeforeStop(boolean dumpBeforeStop)
      Set true if ContainerLifeCycle.dumpStdErr() is called before stopping.
      Parameters:
      dumpBeforeStop - true if ContainerLifeCycle.dumpStdErr() is called before stopping
    • getDateField

      public HttpField getDateField()
      Returns:
      A HttpField instance efficiently recording the current time to a second resolution, that cannot be cleared from a ResponseHttpFields instance.
      See Also:
    • 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
    • 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
    • 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
    • join

      public void join() throws InterruptedException
      Throws:
      InterruptedException
    • setAttribute

      public Object setAttribute(String name, Object attribute)
      Description copied from interface: Attributes
      Set an attribute
      Specified by:
      setAttribute in interface Attributes
      Parameters:
      name - the attribute to set
      attribute - the value to set. A null value is equivalent to removing the attribute.
      Returns:
      the previous value of the attribute if set, else null
    • removeAttribute

      public Object removeAttribute(String name)
      Description copied from interface: Attributes
      Remove an attribute
      Specified by:
      removeAttribute in interface Attributes
      Parameters:
      name - the attribute to remove
      Returns:
      the value of the attribute if removed, else null
    • getAttribute

      public Object getAttribute(String name)
      Description copied from interface: Attributes
      Get an attribute
      Specified by:
      getAttribute in interface Attributes
      Parameters:
      name - the attribute to get
      Returns:
      the value of the attribute, or null if no such attribute exists
    • getAttributeNameSet

      public Set<String> getAttributeNameSet()
      Description copied from interface: Attributes
      Get the immutable set of attribute names.
      Specified by:
      getAttributeNameSet in interface Attributes
      Returns:
      Set of attribute names, or an empty set if there are no attributes.
    • clearAttributes

      public void clearAttributes()
      Description copied from interface: Attributes
      Clear all attribute names
      Specified by:
      clearAttributes in interface Attributes
    • getURI

      public URI getURI()
      Returns:
      The URI of the first NetworkConnector and first ContextHandler, or null
    • getServer

      public Server getServer()
      Specified by:
      getServer in interface Handler
      Overrides:
      getServer in class Handler.Abstract
      Returns:
      the Server associated with this Handler
    • getDefaultStyleSheet

      public Resource getDefaultStyleSheet()
      Get the Default CSS
      Returns:
      the default CSS
    • getDefaultFavicon

      public Resource getDefaultFavicon()
      Get the default Favicon
      Returns:
      the default Favicon
    • toString

      public String toString()
      Overrides:
      toString in class AbstractLifeCycle
    • 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
    • main

      public static void main(String... args)