Class Server

java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
All Implemented Interfaces:
Handler, Handler.Container, Handler.Singleton, Request.Handler, org.eclipse.jetty.util.Attributes, org.eclipse.jetty.util.component.Container, org.eclipse.jetty.util.component.Destroyable, org.eclipse.jetty.util.component.Dumpable, org.eclipse.jetty.util.component.Dumpable.DumpableContainer, org.eclipse.jetty.util.component.LifeCycle, org.eclipse.jetty.util.thread.Invocable

@ManagedObject public class Server extends Handler.Wrapper implements org.eclipse.jetty.util.Attributes
  • Constructor Details

    • Server

      public Server()
    • Server

      public Server(@Name("port") int port)
      Convenience constructor Creates server and a ServerConnector at the passed port.
      Parameters:
      port - The port of a network HTTP connector (or 0 for a randomly allocated port).
      See Also:
    • Server

      public Server(@Name("address") InetSocketAddress addr)
      Convenience constructor

      Creates server and a ServerConnector at the passed address.

      Parameters:
      addr - the inet socket address to create the connector from
    • Server

      public Server(@Name("threadPool") org.eclipse.jetty.util.thread.ThreadPool pool)
    • Server

      public Server(@Name("threadPool") org.eclipse.jetty.util.thread.ThreadPool threadPool, @Name("scheduler") org.eclipse.jetty.util.thread.Scheduler scheduler, @Name("bufferPool") org.eclipse.jetty.io.ByteBufferPool bufferPool)
  • 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, org.eclipse.jetty.util.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.
    • 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 org.eclipse.jetty.http.MimeTypes.Mutable getMimeTypes()
    • getInvocationType

      public org.eclipse.jetty.util.thread.Invocable.InvocationType getInvocationType()
      Specified by:
      getInvocationType in interface org.eclipse.jetty.util.thread.Invocable
      Specified by:
      getInvocationType in interface Request.Handler
      Overrides:
      getInvocationType in class Handler.Wrapper
    • 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 org.eclipse.jetty.util.thread.ThreadPool getThreadPool()
      Returns:
      Returns the threadPool.
    • getScheduler

      @ManagedAttribute("The server Scheduler") public org.eclipse.jetty.util.thread.Scheduler getScheduler()
    • getByteBufferPool

      @ManagedAttribute("The server ByteBuffer pool") public org.eclipse.jetty.io.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 org.eclipse.jetty.http.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:
      • ResponseHttpFields.PersistentPreEncodedHttpField
    • doStart

      protected void doStart() throws Exception
      Overrides:
      doStart in class Handler.Abstract
      Throws:
      Exception
    • start

      protected void start(org.eclipse.jetty.util.component.LifeCycle l) throws Exception
      Overrides:
      start in class org.eclipse.jetty.util.component.ContainerLifeCycle
      Throws:
      Exception
    • doStop

      protected void doStop() throws Exception
      Overrides:
      doStop in class Handler.Abstract
      Throws:
      Exception
    • join

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

      public Object setAttribute(String name, Object attribute)
      Specified by:
      setAttribute in interface org.eclipse.jetty.util.Attributes
    • removeAttribute

      public Object removeAttribute(String name)
      Specified by:
      removeAttribute in interface org.eclipse.jetty.util.Attributes
    • getAttribute

      public Object getAttribute(String name)
      Specified by:
      getAttribute in interface org.eclipse.jetty.util.Attributes
    • getAttributeNameSet

      public Set<String> getAttributeNameSet()
      Specified by:
      getAttributeNameSet in interface org.eclipse.jetty.util.Attributes
    • clearAttributes

      public void clearAttributes()
      Specified by:
      clearAttributes in interface org.eclipse.jetty.util.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 org.eclipse.jetty.util.resource.Resource getDefaultStyleSheet()
      Get the Default CSS
      Returns:
      the default CSS
    • getDefaultFavicon

      public org.eclipse.jetty.util.resource.Resource getDefaultFavicon()
      Get the default Favicon
      Returns:
      the default Favicon
    • toString

      public String toString()
      Overrides:
      toString in class org.eclipse.jetty.util.component.AbstractLifeCycle
    • dump

      public void dump(Appendable out, String indent) throws IOException
      Specified by:
      dump in interface org.eclipse.jetty.util.component.Dumpable
      Overrides:
      dump in class org.eclipse.jetty.util.component.ContainerLifeCycle
      Throws:
      IOException
    • main

      public static void main(String... args)