public abstract class HTTPServer extends TCPServer
Abstract HTTP Server. Subclasses should override the newHTTPSession()
method to add specific
session handling behavior.
Features + limitations:
Override HTTPSession.answer(HTTPRequest)
and redefine the server behavior for your own application
Example:
// get a new server which handle a Default HTTP Session HTTPServer server = new HTTPServer(serverSocket, 10, 1) { protected HTTPSession newHTTPSession() { return new DefaultHTTPSession(this); } }; // start the server server.start();
Modifier and Type | Field and Description |
---|---|
protected long |
keepAliveDuration
The keep-alive duration in ms (not used).
|
Constructor and Description |
---|
HTTPServer(java.net.ServerSocket connection,
int maxSimultaneousConnection,
int jobCountBySession)
Creates a
HTTPServer using the given ServerSocket . |
Modifier and Type | Method and Description |
---|---|
protected void |
addConnection(java.net.Socket connection)
Add a connection to the list of current connections.
|
BodyParserFactory |
getBodyParserFactory()
Gets the bodyParserFactory.
|
protected IHTTPTransferCodingHandler |
getChunkedTransferCodingHandler()
Return the
IHTTPEncodingHandler corresponding to chunked transfer coding. |
protected IHTTPEncodingHandler |
getEncodingHandler(java.lang.String encoding)
Return the
IHTTPEncodingHandler corresponding to the given encoding. |
protected IHTTPTransferCodingHandler |
getIdentityTransferCodingHandler()
Return the
IHTTPEncodingHandler corresponding to identity transfer coding (i.e. |
protected java.net.Socket |
getNextStreamConnection()
Called by HTTPSession.
|
protected IHTTPTransferCodingHandler |
getTransferCodingHandler(java.lang.String encoding)
Return the
IHTTPEncodingHandler corresponding to the given encoding. |
protected abstract HTTPSession |
newHTTPSession()
This method should be overridden by subclasses to add functionality to the
HTTPServer . |
void |
registerEncodingHandler(IHTTPEncodingHandler handler)
Registers a new HTTP content encoding handler.
|
void |
registerTransferCodingHandler(IHTTPTransferCodingHandler handler)
Registers a new HTTP transfer coding handler.
|
void |
setBodyParserFactory(BodyParserFactory bodyParserFactory)
Sets the bodyParserFactory.
|
void |
start()
Start the
HTTPServer (in a dedicated thread): start listening for connections and start session jobs.Multiple start is not allowed. |
void |
stop()
Stops the
HTTPServer . |
protected void |
tooManyOpenConnections(java.net.Socket connection)
Called when a connection cannot be added to the buffer.
|
getCurrentConnection, getName, isStopped
protected final long keepAliveDuration
public HTTPServer(java.net.ServerSocket connection, int maxSimultaneousConnection, int jobCountBySession)
Creates a HTTPServer
using the given ServerSocket
.
The default encoding to be used is the identity encoding. Further encodings may be registered using
registerEncodingHandler(IHTTPEncodingHandler)
.
Server is not started until start()
is called.
connection
- the ServerSocket
connection used by the servermaxSimultaneousConnection
- the maximal number of simultaneously opened connections.jobCountBySession
- the number of parallel jobs to process by opened sessions. if jobCountBySession
== 1, the
jobs are processed sequentially.protected void addConnection(java.net.Socket connection)
addConnection
in class TCPServer
connection
- Socket
to addprotected IHTTPTransferCodingHandler getChunkedTransferCodingHandler()
IHTTPEncodingHandler
corresponding to chunked transfer coding.IHTTPEncodingHandler
corresponding to chunked transfer codingprotected IHTTPEncodingHandler getEncodingHandler(java.lang.String encoding)
IHTTPEncodingHandler
corresponding to the given encoding.encoding
- case insensitive (See RFC2616, 3.5)protected IHTTPTransferCodingHandler getIdentityTransferCodingHandler()
IHTTPEncodingHandler
corresponding to identity transfer coding (i.e. no transfer coding)IHTTPEncodingHandler
corresponding to identity transfer coding (i.e. no transfer
coding)protected java.net.Socket getNextStreamConnection()
Socket
to process. Block until a new connection is available or server
is stopped.protected IHTTPTransferCodingHandler getTransferCodingHandler(java.lang.String encoding)
IHTTPEncodingHandler
corresponding to the given encoding.encoding
- case insensitive (See RFC2616, 3.5)protected abstract HTTPSession newHTTPSession()
This method should be overridden by subclasses to add functionality to the HTTPServer
.
HTTPSession
HTTPSession
,
DefaultHTTPSession
public void registerEncodingHandler(IHTTPEncodingHandler handler)
Registers a new HTTP content encoding handler.
Should be called before start()
, otherwise a RuntimeException
is thrown.
handler
- the IHTTPEncodingHandler
to registerpublic void registerTransferCodingHandler(IHTTPTransferCodingHandler handler)
Registers a new HTTP transfer coding handler.
Should be called before start()
, otherwise a RuntimeException
is raised.
handler
- the IHTTPTransferCodingHandler
to registerpublic void start()
Start the HTTPServer
(in a dedicated thread): start listening for connections and start session jobs.
Multiple start is not allowed.
public void stop()
Stops the HTTPServer
. Stops listening for connections. This method blocks until all session jobs are
stopped.
protected void tooManyOpenConnections(java.net.Socket connection)
connection
- Socket
that can not be addedpublic BodyParserFactory getBodyParserFactory()
public void setBodyParserFactory(BodyParserFactory bodyParserFactory)
bodyParserFactory
- the bodyParserFactory to set.