org.restlet
Class Server

java.lang.Object
  extended by org.restlet.Restlet
      extended by org.restlet.Connector
          extended by org.restlet.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 Summary
Server(Context context, List<Protocol> protocols, int port, Restlet next)
          Constructor.
Server(Context context, List<Protocol> protocols, String address, int port, Restlet next)
          Constructor.
Server(Context context, List<Protocol> protocols, String address, int port, Restlet next, String helperClass)
          Constructor.
Server(Context context, Protocol protocol)
          Constructor.
Server(Context context, Protocol protocol, Class<? extends ServerResource> nextClass)
          Constructor.
Server(Context context, Protocol protocol, int port)
          Constructor.
Server(Context context, Protocol protocol, int port, Class<? extends ServerResource> nextClass)
          Constructor.
Server(Context context, Protocol protocol, int port, Restlet next)
          Constructor.
Server(Context context, Protocol protocol, Restlet next)
          Constructor using the protocol's default port.
Server(Context context, Protocol protocol, String address, int port, Restlet next)
          Constructor.
Server(List<Protocol> protocols, int port, Restlet next)
          Constructor.
Server(List<Protocol> protocols, String address, int port, Restlet next)
          Constructor.
Server(Protocol protocol)
          Constructor.
Server(Protocol protocol, Class<? extends ServerResource> nextClass)
          Constructor using the protocol's default port.
Server(Protocol protocol, int port)
          Constructor.
Server(Protocol protocol, int port, Class<? extends ServerResource> nextClass)
          Constructor.
Server(Protocol protocol, int port, Restlet next)
          Constructor.
Server(Protocol protocol, Restlet next)
          Constructor using the protocol's default port.
Server(Protocol protocol, String address)
          Constructor using the protocol's default port.
Server(Protocol protocol, String address, Class<? extends ServerResource> nextClass)
          Constructor using the protocol's default port.
Server(Protocol protocol, String address, int port)
          Constructor.
Server(Protocol protocol, String address, int port, Restlet next)
          Constructor.
Server(Protocol protocol, String address, Restlet next)
          Constructor using the protocol's default port.
 
Method Summary
 int getActualPort()
          Returns the actual server port after it has started.
 String getAddress()
          Returns the optional listening IP address (local host used if null).
 int getEphemeralPort()
          Returns the actual ephemeral port used when the listening port is set to '0'.
 Restlet getNext()
          Returns the next Restlet.
 int getPort()
          Returns the listening port if specified.
 void handle(Request request, Response response)
          Handles a call.
 boolean hasNext()
          Indicates if a next Restlet is set.
 boolean isAvailable()
          Indicates the underlying connector helper is available.
 void setAddress(String address)
          Sets the optional listening IP address (local host used if null).
 void setNext(Class<? extends ServerResource> nextClass)
          Sets the next Restlet as a Finder for a given resource class.
 void setNext(Restlet next)
          Sets the next Restlet.
protected  void setPort(int port)
          Sets the listening port if specified.
 void start()
          Starts the Restlet.
 void stop()
          Stops the Restlet.
 
Methods inherited from class org.restlet.Connector
getProtocols, setProtocols
 
Methods inherited from class org.restlet.Restlet
createFinder, finalize, getApplication, getAuthor, getContext, getDescription, getFinderClass, getLogger, getName, getOwner, handle, handle, handle, isStarted, isStopped, setAuthor, setContext, setDescription, setFinderClass, setName, setOwner
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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 Detail

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.

Overrides:
start in class Restlet
Throws:
Exception

stop

public void stop()
          throws Exception
Description copied from class: Restlet
Stops the Restlet.

Overrides:
stop in class Restlet
Throws:
Exception


Copyright © 2005-2013 Restlet.