Package org.restlet

Class Server

All Implemented Interfaces:
Uniform

public class Server extends Connector
Connector acting as a generic server. It internally uses one of the available connector helpers registered with the Restlet engine.

Concurrency note: instances of this class or its subclasses can be invoked by several threads at the same time and therefore must be thread-safe. You should be especially careful when storing state in member variables.

For advanced cases, it is possible to obtained the wrapped RestletHelper instance that is used by this client to handle the calls via the "org.restlet.engine.helper" attribute stored in the Context object.
Author:
Jerome Louvel
  • Constructor Details

    • Server

      public Server(Context context, List<Protocol> protocols, int port, Restlet next)
      Constructor.
      Parameters:
      context - The context.
      protocols - The connector protocols.
      port - The listening port.
      next - The next Restlet.
    • Server

      public Server(Context context, List<Protocol> protocols, String address, int port, Restlet next)
      Constructor.
      Parameters:
      context - The context.
      protocols - The connector protocols.
      address - The optional listening IP address (useful if multiple IP addresses available). You can also use a domain name as an alias for the IP address to listen to.
      port - The listening port.
      next - The next Restlet.
    • Server

      public Server(Context context, List<Protocol> protocols, String address, int port, Restlet next, String helperClass)
      Constructor.
      Parameters:
      context - The context.
      protocols - The connector protocols.
      address - The optional listening IP address (useful if multiple IP addresses available). You can also use a domain name as an alias for the IP address to listen to.
      port - The listening port.
      next - The next Restlet.
      helperClass - Optional helper class name.
    • Server

      public Server(Context context, Protocol protocol)
      Constructor. Note that it uses the protocol's default port.
      Parameters:
      context - The parent context.
      protocol - The connector protocol.
    • Server

      public Server(Context context, Protocol protocol, Class<? extends ServerResource> nextClass)
      Constructor.
      Parameters:
      context - The context.
      protocol - The connector protocol.
      nextClass - The next server resource.
    • Server

      public Server(Context context, Protocol protocol, int port)
      Constructor.
      Parameters:
      context - The parent context.
      protocol - The connector protocol.
      port - The listening port.
    • Server

      public Server(Context context, Protocol protocol, int port, Class<? extends ServerResource> nextClass)
      Constructor.
      Parameters:
      context - The context.
      protocol - The connector protocol.
      port - The listening port.
      nextClass - The next server resource.
    • Server

      public Server(Context context, Protocol protocol, int port, Restlet next)
      Constructor.
      Parameters:
      context - The context.
      protocol - The connector protocol.
      port - The listening port.
      next - The next Restlet.
    • Server

      public Server(Context context, Protocol protocol, Restlet next)
      Constructor using the protocol's default port.
      Parameters:
      context - The context.
      protocol - The connector protocol.
      next - The next Restlet.
    • Server

      public Server(Context context, Protocol protocol, String address, int port, Restlet next)
      Constructor.
      Parameters:
      context - The context.
      protocol - The connector protocol.
      address - The optional listening IP address (useful if multiple IP addresses available). You can also use a domain name as an alias for the IP address to listen to.
      port - The listening port.
      next - The next Restlet.
    • Server

      public Server(List<Protocol> protocols, int port, Restlet next)
      Constructor.
      Parameters:
      protocols - The connector protocols.
      port - The listening port.
      next - The next Restlet.
    • Server

      public Server(List<Protocol> protocols, String address, int port, Restlet next)
      Constructor.
      Parameters:
      protocols - The connector protocols.
      address - The optional listening IP address (useful if multiple IP addresses available). You can also use a domain name as an alias for the IP address to listen to.
      port - The listening port.
      next - The next Restlet.
    • Server

      public Server(Protocol protocol)
      Constructor.
      Parameters:
      protocol - The connector protocol.
    • Server

      public Server(Protocol protocol, Class<? extends ServerResource> nextClass)
      Constructor using the protocol's default port.
      Parameters:
      protocol - The connector protocol.
      nextClass - The next server resource.
    • Server

      public Server(Protocol protocol, int port)
      Constructor.
      Parameters:
      protocol - The connector protocol.
      port - The listening port.
    • Server

      public Server(Protocol protocol, int port, Class<? extends ServerResource> nextClass)
      Constructor.
      Parameters:
      protocol - The connector protocol.
      port - The listening port.
      nextClass - The next server resource.
    • Server

      public Server(Protocol protocol, int port, Restlet next)
      Constructor.
      Parameters:
      protocol - The connector protocol.
      port - The listening port.
      next - The next Restlet.
    • Server

      public Server(Protocol protocol, Restlet next)
      Constructor using the protocol's default port.
      Parameters:
      protocol - The connector protocol.
      next - The next Restlet.
    • Server

      public Server(Protocol protocol, String address)
      Constructor using the protocol's default port.
      Parameters:
      protocol - The connector protocol.
      address - The listening IP address (useful if multiple IP addresses available). You can also use a domain name as an alias for the IP address to listen to.
    • Server

      public Server(Protocol protocol, String address, Class<? extends ServerResource> nextClass)
      Constructor using the protocol's default port.
      Parameters:
      protocol - The connector protocol.
      address - The listening IP address (useful if multiple IP addresses available). You can also use a domain name as an alias for the IP address to listen to.
      nextClass - The next server resource.
    • Server

      public Server(Protocol protocol, String address, int port)
      Constructor.
      Parameters:
      protocol - The connector protocol.
      address - The optional listening IP address (useful if multiple IP addresses available). You can also use a domain name as an alias for the IP address to listen to.
      port - The listening port.
    • Server

      public Server(Protocol protocol, String address, int port, Restlet next)
      Constructor.
      Parameters:
      protocol - The connector protocol.
      address - The optional listening IP address (useful if multiple IP addresses available). You can also use a domain name as an alias for the IP address to listen to.
      port - The listening port.
      next - The next Restlet.
    • Server

      public Server(Protocol protocol, String address, Restlet next)
      Constructor using the protocol's default port.
      Parameters:
      protocol - The connector protocol.
      address - The listening IP address (useful if multiple IP addresses available). You can also use a domain name as an alias for the IP address to listen to.
      next - The next Restlet.
  • Method Details

    • getActualPort

      public int getActualPort()
      Returns the actual server port after it has started. If an ephemeral port is used it will be returned, otherwise the fixed port will be provided.
      Returns:
      The actual server port.
    • getAddress

      public String getAddress()
      Returns the optional listening IP address (local host used if null).
      Returns:
      The optional listening IP address (local host used if null).
    • getEphemeralPort

      public int getEphemeralPort()
      Returns the actual ephemeral port used when the listening port is set to '0'. The default value is '-1' if no ephemeral port is known. See InetSocketAddress#InetSocketAddress(int) and ServerSocket#getLocalPort() methods for details.
      Returns:
      The actual ephemeral port used.
    • getNext

      public Restlet getNext()
      Returns the next Restlet.
      Returns:
      The next Restlet.
    • getPort

      public int getPort()
      Returns the listening port if specified.
      Returns:
      The listening port if specified.
    • handle

      public void handle(Request request, Response response)
      Description copied from class: Restlet
      Handles a call. The default behavior is to initialize the Restlet by setting the current context using the Context.setCurrent(Context) method and by attempting to start it, unless it was already started. If an exception is thrown during the start action, then the response status is set to Status.SERVER_ERROR_INTERNAL.

      Subclasses overriding this method should make sure that they call super.handle(request, response) before adding their own logic.

      Specified by:
      handle in interface Uniform
      Overrides:
      handle in class Restlet
      Parameters:
      request - The request to handle.
      response - The response to update.
    • hasNext

      public boolean hasNext()
      Indicates if a next Restlet is set.
      Returns:
      True if a next Restlet is set.
    • isAvailable

      public boolean isAvailable()
      Indicates the underlying connector helper is available.
      Specified by:
      isAvailable in class Connector
      Returns:
      True if the underlying connector helper is available.
    • setAddress

      public void setAddress(String address)
      Sets the optional listening IP address (local host used if null).
      Parameters:
      address - The optional listening IP address (local host used if null).
    • setNext

      public void setNext(Class<? extends ServerResource> nextClass)
      Sets the next Restlet as a Finder for a given resource class. When the call is delegated to the Finder instance, a new instance of the resource class will be created and will actually handle the request.
      Parameters:
      nextClass - The next resource class to attach.
    • setNext

      public void setNext(Restlet next)
      Sets the next Restlet.
      Parameters:
      next - The next Restlet.
    • setPort

      protected void setPort(int port)
      Sets the listening port if specified. Note that '0' means that the system will pick up an ephemeral port at the binding time. This ephemeral can be retrieved once the server is started using the getEphemeralPort() method.
      Parameters:
      port - The listening port if specified.
    • start

      public void start() throws Exception
      Description copied from class: Restlet
      Starts the Restlet. By default its only sets "started" internal property to true. WARNING: this method must be called at the end of the starting process by subclasses otherwise concurrent threads could enter into the call handling logic too early.
      Overrides:
      start in class Restlet
      Throws:
      Exception
    • stop

      public void stop() throws Exception
      Description copied from class: Restlet
      Stops the Restlet. By default its only sets "started" internal property to false. WARNING: this method must be called at the beginning of the stopping process by subclasses otherwise concurrent threads could continue to (improperly) handle calls.
      Overrides:
      stop in class Restlet
      Throws:
      Exception