|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjuglr.net.HTTPServer
public class HTTPServer
A generic light weight HTTP server handling requests by forwarding them to a set of actors and sending the reponse from the actor back to the client.
The "backend" actors functions much as bottom halves in system level interrupt programming. The HTTPServer manages an actor per active HTTP connection which is considered the upper half. The upper half dispatches incoming request to the right bottom half. When the bottom half responds the upper half sends back the reply to the HTTP client.
Constructor Summary | |
---|---|
HTTPServer(int port)
Create a new HTTP server listening on port port . |
|
HTTPServer(int port,
MessageBus messageBus)
Create a new HTTP server listening on port port . |
Method Summary | |
---|---|
void |
registerHandler(java.lang.String urlRegex,
Address handler,
HTTP.Method... methods)
Configure all requests with URLs matching pathRegex to be handled
by the actor living at the handler address. |
void |
start()
Start listening on the configured port. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public HTTPServer(int port) throws java.io.IOException
port
. To start
listening you must invoke the server's start()
method. Before
doing so you must install a set of handlers with the
registerHandler(java.lang.String, juglr.Address, juglr.net.HTTP.Method...)
method.
The HTTP server will be configured to spawn, and talk to, actors on
the default message bus.
port
- the port to listen on
java.io.IOException
- if there is an error setting up the server socketMessageBus.getDefault()
public HTTPServer(int port, MessageBus messageBus) throws java.io.IOException
port
. To start
listening you must invoke the server's start()
method. Before
doing so you must install a set of handlers with the
registerHandler(java.lang.String, juglr.Address, juglr.net.HTTP.Method...)
method.
The HTTP server will be configured to spawn, and talk to, actors on
the message bus messageBus
.
port
- the port to listen onmessageBus
- the MessageBus to use
java.io.IOException
- if there is an error setting up the server socketMethod Detail |
---|
public void registerHandler(java.lang.String urlRegex, Address handler, HTTP.Method... methods)
pathRegex
to be handled
by the actor living at the handler
address.
Messages sent from the HTTPServer to the handler are guaranteed to be
instances of HTTPRequest
s.
The handler
must reply with a HTTPResponse
or
Box
to any incoming message, sending the reply to
msg.getReplyTo()
. Sending a Box
as response will
automatically result in a HTTP status code 200 OK. To alter the response
code you must wrap the response box in a HTTPResponse
.
It is strongly recommended that the handler replies in a finally
clause. Failing to reply will result in dangling connections, and empty
replies or timeouts for clients.
When looking up a matching handler the first one with a matching path
will be used. The handlers are checked in the order they are registered.
urlRegex
- A regular expression the request URL must match
for the request to be forwarded to handler
handler
- the address of the actor that handles the requestmethods
- the HTTP methods the handler can accept
java.lang.IllegalStateException
- if the server has had its start()
method invokedpublic void start()
start()
the registerHandler(java.lang.String, juglr.Address, juglr.net.HTTP.Method...)
method should not be called again
java.lang.IllegalStateException
- if the server has already been started
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |