public class HTTPServer extends TCPServer
HTTP Server.
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, new DefaultHTTPSession.Factory(), 10, 1); // 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(int port,
int maxSimultaneousConnection,
int jobCountBySession)
Creates a
HTTPServer on the given port with the DefaultHTTPSession . |
HTTPServer(int port,
int maxSimultaneousConnection,
int jobCountBySession,
HTTPSession.Factory httpSessionFactory)
Creates a
HTTPServer on the given port. |
HTTPServer(int port,
int maxSimultaneousConnection,
int jobCountBySession,
HTTPSession.Factory httpSessionFactory,
javax.net.ServerSocketFactory serverSocketFactory)
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. |
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(int port, int maxSimultaneousConnection, int jobCountBySession) throws java.io.IOException
Creates a HTTPServer
on the given port with the DefaultHTTPSession
.
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.
port
- the port.maxSimultaneousConnection
- 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.java.io.IOException
- if server cannot be bind to given port.public HTTPServer(int port, int maxSimultaneousConnection, int jobCountBySession, HTTPSession.Factory httpSessionFactory) throws java.io.IOException
Creates a HTTPServer
on the given port.
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.
port
- the port.httpSessionFactory
- the HTTP session factory.maxSimultaneousConnection
- 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.java.io.IOException
- if server cannot be bind to given port.public HTTPServer(int port, int maxSimultaneousConnection, int jobCountBySession, HTTPSession.Factory httpSessionFactory, javax.net.ServerSocketFactory serverSocketFactory) throws java.io.IOException
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.
port
- the port.maxSimultaneousConnection
- 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.httpSessionFactory
- the HTTP session factory.serverSocketFactory
- the serverSocketFactory to use.java.io.IOException
- if server cannot be bind to given port.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)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.