public abstract class HTTPSession
extends java.lang.Object
Abstract HTTP Session. Subclasses implements the answer(HTTPRequest)
method to generate
HTTPResponse
to a HTTPRequest
.
Modifier and Type | Class and Description |
---|---|
static interface |
HTTPSession.Factory
This factory is used by the HTTPServer to create sessions for jobs.
|
Constructor and Description |
---|
HTTPSession(HTTPServer server)
Creates a new HTTP Session in the given
HTTPServer . |
Modifier and Type | Method and Description |
---|---|
protected abstract HTTPResponse |
answer(HTTPRequest request)
Generates
HTTPResponse to a HTTPRequest . |
protected boolean |
checkHTTPError(HTTPResponse response)
Returns true if the
HTTPResponse contains a "HTTP 200 OK" response code. |
protected void |
closeConnection()
Close the current server connection.
|
static HTTPResponse |
createErrorResponse(java.lang.String status,
java.lang.String msg)
|
protected IHTTPEncodingHandler |
getAcceptEncodingHandler(java.lang.String encodingParam)
Returns the most suitable
IHTTPEncodingHandler to match the encodings described in
Accept-Encoding header. |
BodyParserFactory |
getBodyParserFactory()
Gets the bodyParserFactory.
|
protected java.lang.Runnable |
getRunnable()
Runs the HTTPServer by waiting for incoming connections and build the response.
|
protected void |
sendError(java.lang.String status)
Send a HTTP error code with given status.
|
protected void |
sendError(java.lang.String status,
java.lang.String msg)
Sends a HTTP response with given status and optional message.
|
protected void |
sendResponse(HTTPResponse response,
IHTTPEncodingHandler encodingHandler)
Sends the given
HTTPResponse to the previously initialized Socket . |
void |
setBodyParserFactory(BodyParserFactory bodyParserFactory)
Sets the bodyParserFactory.
|
protected void |
setCurrentConnection(java.net.Socket c)
Sets the
Socket to be used. |
protected void |
writeResponse(HTTPResponse response,
IHTTPEncodingHandler encodingHandler,
java.io.OutputStream output)
|
public HTTPSession(HTTPServer server)
Creates a new HTTP Session in the given HTTPServer
.
server
- a HTTPServer
public static HTTPResponse createErrorResponse(java.lang.String status, java.lang.String msg)
status
- the error status. One of HTTP_STATUS_*
constant of the HTTPConstants
interface.msg
- an optional error message to add in response.HTTPResponse
that represent the error.HTTPConstants.HTTP_STATUS_BADREQUEST
,
HTTPConstants.HTTP_STATUS_FORBIDDEN
,
HTTPConstants.HTTP_STATUS_INTERNALERROR
,
HTTPConstants.HTTP_STATUS_MEDIA_TYPE
,
HTTPConstants.HTTP_STATUS_METHOD
,
HTTPConstants.HTTP_STATUS_NOTACCEPTABLE
,
HTTPConstants.HTTP_STATUS_NOTFOUND
,
HTTPConstants.HTTP_STATUS_NOTIMPLEMENTED
,
HTTPConstants.HTTP_STATUS_NOTMODIFIED
,
HTTPConstants.HTTP_STATUS_OK
,
HTTPConstants.HTTP_STATUS_REDIRECT
protected abstract HTTPResponse answer(HTTPRequest request)
Generates HTTPResponse
to a HTTPRequest
. Subclasses implements this method to add functionality
to the HTTP Server.
request
- the requestHTTPResponse
protected boolean checkHTTPError(HTTPResponse response)
HTTPResponse
contains a "HTTP 200 OK" response code.response
- the HTTPResponse
HTTPResponse
contains a "HTTP 200 OK" response code.protected void closeConnection()
protected IHTTPEncodingHandler getAcceptEncodingHandler(java.lang.String encodingParam)
IHTTPEncodingHandler
to match the encodings described in
Accept-Encoding
header.encodingParam
- is on the form gzip, identity
or gzip; q=0.8, identity; q=0.2
IHTTPEncodingHandler
, or null
if no suitable handler can be foundprotected java.lang.Runnable getRunnable()
Runnable
interface for core HTTP Session functionality.protected void sendError(java.lang.String status)
status
- the HTTP statussendError(String, String)
protected void sendError(java.lang.String status, java.lang.String msg)
msg
- the message, could be null
status
- the error statussendResponse(HTTPResponse, IHTTPEncodingHandler)
protected void sendResponse(HTTPResponse response, IHTTPEncodingHandler encodingHandler)
HTTPResponse
to the previously initialized Socket
.response
- the HTTPResponse
to be sentencodingHandler
- the encoding handler to be used to encode the response. If null
the
IdentityEncodingHandler
is used to encode the response.protected void setCurrentConnection(java.net.Socket c)
Socket
to be used.c
- the Socket
to be used.protected void writeResponse(HTTPResponse response, IHTTPEncodingHandler encodingHandler, java.io.OutputStream output) throws java.io.IOException
response
- the HTTPResponse
encodingHandler
- the IHTTPEncodingHandler
to encode the response. If null
, the
IHTTPTransferCodingHandler
is used.output
- OutputStream
used to write the response tojava.io.IOException
- if the connection has been lostpublic BodyParserFactory getBodyParserFactory()
public void setBodyParserFactory(BodyParserFactory bodyParserFactory)
bodyParserFactory
- the bodyParserFactory to set.