|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.flat502.rox.processing.HttpRpcProcessor com.flat502.rox.server.HttpRpcServer
public abstract class HttpRpcServer
This is the server-side RPC interface.
This class supports both synchronous and asynchronous handling of method calls. An instance is backed by at least two threads: a selecting thread and a worker thread. Both threads are daemon threads.
The selecting thread handles all low-level network I/O. As soon as this thread identifies a complete HTTP request the request is passed to the worker thread (or threads).
The number of worker threads may be adjusted dynamically
using the inherited
HttpRpcProcessor.addWorker()
and HttpRpcProcessor.removeWorker()
methods. Worker threads are responsible for notification
of registered RequestHandler
s.
Field Summary |
---|
Fields inherited from class com.flat502.rox.processing.HttpRpcProcessor |
---|
ALL_CIPHER_SUITES, ANON_CIPHER_SUITES |
Constructor Summary | |
---|---|
HttpRpcServer(java.net.InetAddress hostAddress,
int port,
boolean useHttps,
ServerResourcePool workerPool)
Initialize a new HTTP RPC server. |
|
HttpRpcServer(java.net.InetAddress hostAddress,
int port,
boolean useHttps,
ServerResourcePool workerPool,
SSLConfiguration sslCfg)
|
Method Summary | |
---|---|
protected void |
accept(java.nio.channels.SelectionKey key)
Called when a new connection is pending on the underlying ServerSocketChannel . |
protected void |
deregisterResponseCoordinator(com.flat502.rox.server.ResponseCoordinator coordinator)
|
protected void |
deregisterSocket(java.net.Socket socket)
|
protected HttpMessageBuffer |
getReadBuffer(java.net.Socket socket)
Called when data is available on a socket. |
protected java.lang.String |
getServerName()
|
protected java.nio.ByteBuffer |
getWriteBuffer(java.net.Socket socket)
Called when a socket becomes available for writing. |
protected void |
handleMessageException(HttpMessageBuffer msg,
java.lang.Exception e)
Converts an exception raised while processing an HTTP request into a suitable HTTP response. |
protected void |
handleProcessingException(java.net.Socket socket,
java.lang.Exception e)
An error handler invoked when an error occurs within the main processing loop. |
protected void |
handleSelectionKeyOperation(java.nio.channels.SelectionKey key)
This implementation defers to the accept(SelectionKey) method if a connection
is pending. |
protected void |
handleSSLHandshakeFinished(java.net.Socket socket,
javax.net.ssl.SSLEngine engine)
|
protected void |
handleTimeout(java.net.Socket socket,
java.lang.Exception cause)
|
protected void |
initSelector(java.nio.channels.Selector selector)
Creates and initializes a ServerSocketChannel
for accepting connections on. |
protected javax.net.ssl.SSLEngine |
initSocketSSLEngine(java.net.Socket socket)
|
protected boolean |
isWriteQueued(java.net.Socket socket)
Called to find out if data is queued to be written to a socket. |
protected HttpResponse |
newHttpResponse(HttpMessageBuffer msg,
HttpResponseException e)
|
protected HttpResponse |
newHttpResponse(HttpMessageBuffer origMsg,
int statusCode,
java.lang.String reasonPhrase,
Encoding encoding)
|
protected RpcCallContext |
newRpcCallContext(java.net.Socket socket,
HttpRequestBuffer req)
|
protected ResourcePool |
newWorkerPool()
Factory method for new worker pools. |
protected void |
putWriteBuffer(java.net.Socket socket,
java.nio.ByteBuffer data)
Called when data is available to be written to a socket. |
protected void |
read(java.nio.channels.SelectionKey key)
Reads any pending data from the socket indicated by the given SelectionKey . |
void |
registerAcceptPolicy(AcceptPolicy policy)
|
void |
registerContentEncoding(Encoding encoding)
|
RequestHandler |
registerHandler(java.lang.String uriPath,
java.lang.String method,
AsynchronousRequestHandler handler)
Register an asynchronous XML-RPC method call handler. |
RequestHandler |
registerHandler(java.lang.String uriPath,
java.lang.String method,
AsynchronousRequestHandler handler,
MethodCallUnmarshallerAid aid)
Register an asynchronous XML-RPC method call handler. |
RequestHandler |
registerHandler(java.lang.String uriPath,
java.lang.String method,
AsyncRequestHandler handler)
Deprecated. Use registerHandler(String, String, AsynchronousRequestHandler) instead. |
RequestHandler |
registerHandler(java.lang.String uriPath,
java.lang.String method,
AsyncRequestHandler handler,
MethodCallUnmarshallerAid aid)
Deprecated. Use registerHandler(String, String, AsynchronousRequestHandler, MethodCallUnmarshallerAid) instead. |
RequestHandler |
registerHandler(java.lang.String uriPath,
java.lang.String method,
SynchronousRequestHandler handler)
Register a synchronous XML-RPC method call handler. |
RequestHandler |
registerHandler(java.lang.String uriPath,
java.lang.String method,
SynchronousRequestHandler handler,
MethodCallUnmarshallerAid aid)
Register a synchronous XML-RPC method call handler. |
RequestHandler |
registerHandler(java.lang.String uriPath,
java.lang.String method,
SyncRequestHandler handler)
Deprecated. Use registerHandler(String, String, SynchronousRequestHandler) instead. |
RequestHandler |
registerHandler(java.lang.String uriPath,
java.lang.String method,
SyncRequestHandler handler,
MethodCallUnmarshallerAid aid)
Deprecated. Use registerHandler(String, String, SynchronousRequestHandler, MethodCallUnmarshallerAid) instead. |
HttpRequestUnmarshaller |
registerRequestUnmarshaller(java.lang.String httpMethod,
HttpRequestUnmarshaller unmarshaller)
Register an HttpRequestUnmarshaller instance
for a given HTTP method. |
protected void |
removeReadBuffer(java.net.Socket socket)
Called when a complete HTTP message has been identified. |
protected void |
removeReadBuffers(java.net.Socket socket)
Called when a a socket is deregistered and any buffers associated with it should be released. |
protected void |
removeWriteBuffer(java.net.Socket socket)
Called when a all of the data in a pending write buffer has been written to a socket. |
protected void |
removeWriteBuffers(java.net.Socket socket)
Called when a a socket is deregistered and any buffers associated with it should be released. |
protected Encoding |
selectResponseEncoding(HttpRequestBuffer request)
|
void |
setEncodeResponses(boolean encode)
|
void |
setIdleClientTimeout(int timeout)
|
protected void |
stopImpl()
|
protected HttpResponse |
toHttpResponse(HttpMessageBuffer origMsg,
RpcResponse methodRsp,
Encoding encoding)
Constructs a new HttpResponse containing the
given XML-RPC method response. |
protected void |
write(java.nio.channels.SelectionKey key)
Writes any pending data to the socket indicated by the given SelectionKey . |
Methods inherited from class com.flat502.rox.processing.HttpRpcProcessor |
---|
addWorker, addWorkers, deregisterChannel, getQueue, getSocketSelector, getSSLConfiguration, getSSLSession, getTimer, getWorkerCount, initialize, isSharedWorkerPool, isStarted, newSSLSession, queueCancellation, queueRead, queueRegistration, queueWrite, queueWrite, registerChannel, registerProfiler, registerSocket, registerSSLSessionPolicy, removeWorker, setCipherSuitePattern, setSSLHandshakeTimeout, shouldUseHTTPS, start, stop |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public HttpRpcServer(java.net.InetAddress hostAddress, int port, boolean useHttps, ServerResourcePool workerPool) throws java.io.IOException
The server will not attempt to bind to a local port
or being accepting connections (and by implication
processing requests) until Thread.start()
is invoked on this instance.
hostAddress
- An InetAddress
this instance should bind to
when listening for connections. null
is
interpreted as "listen on all interfaces".port
- useHttps
- workerPool
-
java.io.IOException
public HttpRpcServer(java.net.InetAddress hostAddress, int port, boolean useHttps, ServerResourcePool workerPool, SSLConfiguration sslCfg) throws java.io.IOException
java.io.IOException
Method Detail |
---|
public void registerAcceptPolicy(AcceptPolicy policy)
public void registerContentEncoding(Encoding encoding)
public void setEncodeResponses(boolean encode)
public void setIdleClientTimeout(int timeout)
public HttpRequestUnmarshaller registerRequestUnmarshaller(java.lang.String httpMethod, HttpRequestUnmarshaller unmarshaller)
HttpRequestUnmarshaller
instance
for a given HTTP method.
If a prior unmarshaller instance exists for the named HTTP method the unmarshaller instance is replaced and the previously registered instance is returned.
httpMethod
- The HTTP method to register the unmarshaller to handle.
Custom HTTP methods may be used. No validation is performed
on this value, it is only used as a mapping key.unmarshaller
- The HttpRequestUnmarshaller
instance to register
for the given HTTP method. This may be null
which effectively deregisters any existing handler for
the given HTTP method.
HttpRequestUnmarshaller
instance previously
registered to handle the given HTTP method, or null
if no prior instance was registered.CgiRequestUnmarshaller
,
XmlRpcRequestUnmarshaller
,
HttpConstants.Methods
public RequestHandler registerHandler(java.lang.String uriPath, java.lang.String method, AsyncRequestHandler handler)
registerHandler(String, String, AsynchronousRequestHandler)
instead.
uriPath
- URIs for which this handler is responsible. null
indicates all URIs.method
- A regular expression (see Pattern
) to
match method names on. Matching is performed as for the
Matcher.find()
method.handler
- The handler to call back when a request is received matching
the indicated URI and method name pattern.
registerHandler(String, String, SyncRequestHandler, MethodCallUnmarshallerAid)
public RequestHandler registerHandler(java.lang.String uriPath, java.lang.String method, AsynchronousRequestHandler handler)
uriPath
- URIs for which this handler is responsible. null
indicates all URIs.method
- A regular expression (see Pattern
) to
match method names on. Matching is performed as for the
Matcher.find()
method.handler
- The handler to call back when a request is received matching
the indicated URI and method name pattern.
registerHandler(String, String, SyncRequestHandler, MethodCallUnmarshallerAid)
public RequestHandler registerHandler(java.lang.String uriPath, java.lang.String method, AsyncRequestHandler handler, MethodCallUnmarshallerAid aid)
registerHandler(String, String, AsynchronousRequestHandler, MethodCallUnmarshallerAid)
instead.
uriPath
- URIs for which this handler is responsible. null
indicates all URIs.method
- A regular expression (see Pattern
) to
match method names on. Matching is performed as for the
Matcher.find()
method.handler
- The handler to call back when a request is received matching
the indicated URI and method name pattern.aid
- A mapper to be used during the unmarshalling of parameters
of incoming method calls. May be null
.
registerHandler(String, String, SyncRequestHandler, MethodCallUnmarshallerAid)
public RequestHandler registerHandler(java.lang.String uriPath, java.lang.String method, AsynchronousRequestHandler handler, MethodCallUnmarshallerAid aid)
uriPath
- URIs for which this handler is responsible. null
indicates all URIs.method
- A regular expression (see Pattern
) to
match method names on. Matching is performed as for the
Matcher.find()
method.handler
- The handler to call back when a request is received matching
the indicated URI and method name pattern.aid
- A mapper to be used during the unmarshalling of parameters
of incoming method calls. May be null
.
registerHandler(String, String, SyncRequestHandler, MethodCallUnmarshallerAid)
public RequestHandler registerHandler(java.lang.String uriPath, java.lang.String method, SyncRequestHandler handler)
registerHandler(String, String, SynchronousRequestHandler)
instead.
uriPath
- URIs for which this handler is responsible. null
indicates all URIs.method
- A regular expression (see Pattern
) to
match method names on. Matching is performed as for the
Matcher.find()
method.handler
- The handler to call back when a request is received matching
the indicated URI and method name pattern.
registerHandler(String, String, SyncRequestHandler, MethodCallUnmarshallerAid)
public RequestHandler registerHandler(java.lang.String uriPath, java.lang.String method, SynchronousRequestHandler handler)
uriPath
- URIs for which this handler is responsible. null
indicates all URIs.method
- A regular expression (see Pattern
) to
match method names on. Matching is performed as for the
Matcher.find()
method.handler
- The handler to call back when a request is received matching
the indicated URI and method name pattern.
registerHandler(String, String, SyncRequestHandler, MethodCallUnmarshallerAid)
public RequestHandler registerHandler(java.lang.String uriPath, java.lang.String method, SyncRequestHandler handler, MethodCallUnmarshallerAid aid)
registerHandler(String, String, SynchronousRequestHandler, MethodCallUnmarshallerAid)
instead.
Handlers are indexed first using the normalized
URI and then using the method name regular expression.
When a request is available the handler for the request is chosen by
looking up the list of handlers registered under the associated URI (after
normalization). This list is then scanned until a regular expression
matches the given method name. If no match is found this is repeated using
a null
URI (i.e. a more specific URI match takes
precedence).
If no handler is found an HTTP "404 Not Found" response is sent to the client.
uriPath
- URIs for which this handler is responsible. null
indicates all URIs.method
- A regular expression (see Pattern
) to
match method names on. Matching is performed as for the
Matcher.find()
method.handler
- The handler to call back when a request is received matching
the indicated URI and method name pattern.aid
- A mapper to be used during the unmarshalling of parameters
of incoming method calls. May be null
.
public RequestHandler registerHandler(java.lang.String uriPath, java.lang.String method, SynchronousRequestHandler handler, MethodCallUnmarshallerAid aid)
Handlers are indexed first using the normalized
URI and then using the method name regular expression.
When a request is available the handler for the request is chosen by
looking up the list of handlers registered under the associated URI (after
normalization). This list is then scanned until a regular expression
matches the given method name. If no match is found this is repeated using
a null
URI (i.e. a more specific URI match takes
precedence).
If no handler is found an HTTP "404 Not Found" response is sent to the client.
uriPath
- URIs for which this handler is responsible. null
indicates all URIs.method
- A regular expression (see Pattern
) to
match method names on. Matching is performed as for the
Matcher.find()
method.handler
- The handler to call back when a request is received matching
the indicated URI and method name pattern.aid
- A mapper to be used during the unmarshalling of parameters
of incoming method calls. May be null
.
protected RpcCallContext newRpcCallContext(java.net.Socket socket, HttpRequestBuffer req)
protected HttpResponse toHttpResponse(HttpMessageBuffer origMsg, RpcResponse methodRsp, Encoding encoding) throws java.io.IOException, MarshallingException
HttpResponse
containing the
given XML-RPC method response.
This implementation encodes the response using UTF-8
,
sets the status code to 200
, and sets Content-Type
header to text/xml
as required. No other headers are set.
methodRsp
- The XML-RPC method response to be returned in the HTTP response.encoding
- An Encoding
describing the encoding to use when
constructing this message. This also informs the
Content-Encoding
header value. May be
null
.
HttpResponse
with the marshalled XML-RPC response
as its content.
java.io.IOException
- if an error occurs while marshalling the XML-RPC response.
MarshallingException
protected Encoding selectResponseEncoding(HttpRequestBuffer request)
protected ResourcePool newWorkerPool()
HttpRpcProcessor
This is invoked from the constructor to create a new worker pool when one is not provided.
newWorkerPool
in class HttpRpcProcessor
ResourcePool
.protected void handleSelectionKeyOperation(java.nio.channels.SelectionKey key) throws java.io.IOException
accept(SelectionKey)
method if a connection
is pending. In all other cases it defers to it's
parent.
handleSelectionKeyOperation
in class HttpRpcProcessor
key
- The SelectionKey
for the socket on which
an I/O operation is pending.
java.io.IOException
- if an error occurs while processing the pending event.protected void read(java.nio.channels.SelectionKey key) throws java.io.IOException
HttpRpcProcessor
SelectionKey
.
This implementation retrieves an
HttpMessageBuffer
instance for the
indicated socket using the HttpRpcProcessor.getReadBuffer(Socket)
method. Data present on the socket is added to
this buffer and if a complete HTTP message has
been received it is enqueued on the
shared queue
.
read
in class HttpRpcProcessor
key
- The SelectionKey
indicating the socket
available for writing.
java.io.IOException
- If an error occurs while attempting to read from
the indicated socket.protected void write(java.nio.channels.SelectionKey key) throws java.io.IOException
HttpRpcProcessor
SelectionKey
.
This implementation checks for data using the
HttpRpcProcessor.getWriteBuffer(Socket)
method. If data
is available as much as possible is written
to the socket.
write
in class HttpRpcProcessor
key
- The SelectionKey
indicating the socket
available for writing.
java.io.IOException
- If an error occurs while attempting to write to
the indicated socket.protected void deregisterSocket(java.net.Socket socket)
deregisterSocket
in class HttpRpcProcessor
protected void deregisterResponseCoordinator(com.flat502.rox.server.ResponseCoordinator coordinator)
protected void accept(java.nio.channels.SelectionKey key) throws java.io.IOException
ServerSocketChannel
.
key
- The SelectionKey
for the socket on which
a connection is pending.
java.io.IOException
- if an error occurs while accepting the
new connection.protected void handleMessageException(HttpMessageBuffer msg, java.lang.Exception e) throws java.io.IOException
The response is marshalled and queued for writing on the socket associated with the original request.
handleMessageException
in class HttpRpcProcessor
msg
- The HTTP request being processed when the exception
occurred.e
- The exception that was raised.
java.io.IOException
- if an error occurs marshalling or writing
the response.protected void handleProcessingException(java.net.Socket socket, java.lang.Exception e)
HttpRpcProcessor
handleProcessingException
in class HttpRpcProcessor
socket
- The socket the exception is associated with. This
may be null
if the exception is not
specific to a particular socket.e
- The exception that was raised.protected void handleTimeout(java.net.Socket socket, java.lang.Exception cause)
handleTimeout
in class HttpRpcProcessor
protected void handleSSLHandshakeFinished(java.net.Socket socket, javax.net.ssl.SSLEngine engine)
handleSSLHandshakeFinished
in class HttpRpcProcessor
protected void stopImpl() throws java.io.IOException
stopImpl
in class HttpRpcProcessor
java.io.IOException
protected void initSelector(java.nio.channels.Selector selector) throws java.io.IOException
ServerSocketChannel
for accepting connections on.
initSelector
in class HttpRpcProcessor
java.io.IOException
- If an error occurs while attempting to create the
Selector
.protected javax.net.ssl.SSLEngine initSocketSSLEngine(java.net.Socket socket) throws javax.net.ssl.SSLException
initSocketSSLEngine
in class HttpRpcProcessor
javax.net.ssl.SSLException
protected HttpMessageBuffer getReadBuffer(java.net.Socket socket)
HttpRpcProcessor
Implementations must return a buffer, even if this
means creating a new instance. The same buffer should
be returned for a given socket until the
HttpRpcProcessor.removeReadBuffer(Socket)
method is invoked,
ensuring that message fragmentation is correctly handled.
getReadBuffer
in class HttpRpcProcessor
socket
- The socket on which data has arrived.
protected void removeReadBuffer(java.net.Socket socket)
HttpRpcProcessor
removeReadBuffer
in class HttpRpcProcessor
socket
- The socket on which a complete message has been
received.protected void removeReadBuffers(java.net.Socket socket)
HttpRpcProcessor
removeReadBuffers
in class HttpRpcProcessor
socket
- The socket on which a complete message has been
received.protected void putWriteBuffer(java.net.Socket socket, java.nio.ByteBuffer data)
HttpRpcProcessor
putWriteBuffer
in class HttpRpcProcessor
socket
- The socket on which the data should be sent.data
- The data to be written.protected boolean isWriteQueued(java.net.Socket socket)
HttpRpcProcessor
isWriteQueued
in class HttpRpcProcessor
socket
- The socket on which the data should be sent.
true
if there is a buffer waiting to be written on
the given socketprotected java.nio.ByteBuffer getWriteBuffer(java.net.Socket socket)
HttpRpcProcessor
Implementations should return a buffer only if one
has been added using HttpRpcProcessor.putWriteBuffer(Socket, ByteBuffer)
.
The same buffer should be returned for a given socket until the
HttpRpcProcessor.removeWriteBuffer(Socket)
method is invoked,
ensuring that large messages that must be written in
multiple fragments are correctly handled.
getWriteBuffer
in class HttpRpcProcessor
socket
- The socket that is available for writing.
protected void removeWriteBuffer(java.net.Socket socket)
HttpRpcProcessor
removeWriteBuffer
in class HttpRpcProcessor
socket
- The socket the buffer was associated with.protected void removeWriteBuffers(java.net.Socket socket)
HttpRpcProcessor
removeWriteBuffers
in class HttpRpcProcessor
socket
- The socket the buffer was associated with.protected HttpResponse newHttpResponse(HttpMessageBuffer msg, HttpResponseException e)
protected HttpResponse newHttpResponse(HttpMessageBuffer origMsg, int statusCode, java.lang.String reasonPhrase, Encoding encoding)
protected java.lang.String getServerName()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |