Package org.restlet
Class Server
- java.lang.Object
-
- org.restlet.Restlet
-
- org.restlet.Connector
-
- 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 wrappedRestletHelper
instance that is used by this client to handle the calls via the "org.restlet.engine.helper" attribute stored in theContext
object.- Author:
- Jerome Louvel
-
-
Constructor Summary
Constructors Constructor Description Server(java.util.List<Protocol> protocols, int port, Restlet next)
Constructor.Server(java.util.List<Protocol> protocols, java.lang.String address, int port, Restlet next)
Constructor.Server(Context context, java.util.List<Protocol> protocols, int port, Restlet next)
Constructor.Server(Context context, java.util.List<Protocol> protocols, java.lang.String address, int port, Restlet next)
Constructor.Server(Context context, java.util.List<Protocol> protocols, java.lang.String address, int port, Restlet next, java.lang.String helperClass)
Constructor.Server(Context context, Protocol protocol)
Constructor.Server(Context context, Protocol protocol, int port)
Constructor.Server(Context context, Protocol protocol, int port, java.lang.Class<? extends ServerResource> nextClass)
Constructor.Server(Context context, Protocol protocol, int port, Restlet next)
Constructor.Server(Context context, Protocol protocol, java.lang.Class<? extends ServerResource> nextClass)
Constructor.Server(Context context, Protocol protocol, java.lang.String address, int port, Restlet next)
Constructor.Server(Context context, Protocol protocol, Restlet next)
Constructor using the protocol's default port.Server(Protocol protocol)
Constructor.Server(Protocol protocol, int port)
Constructor.Server(Protocol protocol, int port, java.lang.Class<? extends ServerResource> nextClass)
Constructor.Server(Protocol protocol, int port, Restlet next)
Constructor.Server(Protocol protocol, java.lang.Class<? extends ServerResource> nextClass)
Constructor using the protocol's default port.Server(Protocol protocol, java.lang.String address)
Constructor using the protocol's default port.Server(Protocol protocol, java.lang.String address, int port)
Constructor.Server(Protocol protocol, java.lang.String address, int port, Restlet next)
Constructor.Server(Protocol protocol, java.lang.String address, java.lang.Class<? extends ServerResource> nextClass)
Constructor using the protocol's default port.Server(Protocol protocol, java.lang.String address, Restlet next)
Constructor using the protocol's default port.Server(Protocol protocol, Restlet next)
Constructor using the protocol's default port.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getActualPort()
Returns the actual server port after it has started.java.lang.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(java.lang.String address)
Sets the optional listening IP address (local host used if null).void
setNext(java.lang.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
-
-
-
-
Constructor Detail
-
Server
public Server(Context context, java.util.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, java.util.List<Protocol> protocols, java.lang.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, java.util.List<Protocol> protocols, java.lang.String address, int port, Restlet next, java.lang.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, java.lang.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, java.lang.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, java.lang.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(java.util.List<Protocol> protocols, int port, Restlet next)
Constructor.- Parameters:
protocols
- The connector protocols.port
- The listening port.next
- The next Restlet.
-
Server
public Server(java.util.List<Protocol> protocols, java.lang.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, java.lang.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, java.lang.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, java.lang.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, java.lang.String address, java.lang.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, java.lang.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, java.lang.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, java.lang.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 java.lang.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 theContext.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 toStatus.SERVER_ERROR_INTERNAL
.Subclasses overriding this method should make sure that they call super.handle(request, response) before adding their own logic.
-
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 classConnector
- Returns:
- True if the underlying connector helper is available.
-
setAddress
public void setAddress(java.lang.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(java.lang.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 thegetEphemeralPort()
method.- Parameters:
port
- The listening port if specified.
-
start
public void start() throws java.lang.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.
-
stop
public void stop() throws java.lang.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.
-
-