public abstract class JsonRequestHandler extends ARequestHandler
Modifier and Type | Field and Description |
---|---|
protected static DataFactory |
FACTORY
Record/Any factory.
|
protected static java.lang.String |
HEADER_REQUEST_HOST
Http header field containing the request host name.
|
protected static JsonHttpUtils |
HTTP_UTILS
Utilities for JSON-Request/Response handling.
|
Constructor and Description |
---|
JsonRequestHandler() |
Modifier and Type | Method and Description |
---|---|
protected int |
getErrorStatus(java.lang.String method,
java.lang.String requestUri,
java.lang.Throwable ex)
Return a status code for an exception.
|
protected java.lang.String |
getRequestHost(HttpExchange exchange)
Returns the host as defined in the HTTP request.
|
protected int |
getSuccessStatus(java.lang.String method,
java.lang.String requestUri) |
protected int |
getSuccessStatus(java.lang.String requestMethod,
java.lang.String requestUri,
Record inputRecord,
java.lang.Object resultObject)
Returns the success code.
|
void |
handle(HttpExchange exchange)
reads a record from the JSON request, calls
#process(Record) to do processing and get a result record and
writes JSON result. |
protected boolean |
isValidMethod(java.lang.String method,
java.lang.String requestUri)
Checks if the handler allows the HTTP method.
|
java.lang.Object |
process(java.lang.String method,
java.lang.String requestUri,
Record inputRecord)
process input record and return result object.
|
java.lang.Object |
process(java.lang.String method,
java.lang.String requestUri,
Record inputRecord,
HttpExchange exchange)
process input record and return result object.
|
protected boolean |
writeErrorResult(HttpExchange exchange,
java.lang.Throwable error,
Record inputRecord)
write an error result.
|
protected void |
writeResultObject(java.io.OutputStream responseStream,
java.lang.Object resultObject)
write result object.
|
protected boolean |
writeSuccessResult(HttpExchange exchange,
java.lang.Object resultObject,
Record inputRecord)
write a successful result.
|
activate, getDynamicUriParts, getRootContextPath, getStaticUriParts, getUriPattern, matches, urlDecode
protected static final java.lang.String HEADER_REQUEST_HOST
protected static final DataFactory FACTORY
protected static final JsonHttpUtils HTTP_UTILS
public java.lang.Object process(java.lang.String method, java.lang.String requestUri, Record inputRecord) throws java.lang.Exception
method
- HTTP methodrequestUri
- request URI.inputRecord
- input record parsed either from request body, if it has content, or from request URI parameters.Record
, you have to provide a
writeResultObject(OutputStream, Object)
method, too.java.lang.Exception
- any failure during processing.public java.lang.Object process(java.lang.String method, java.lang.String requestUri, Record inputRecord, HttpExchange exchange) throws java.lang.Exception
method
- HTTP methodrequestUri
- request URI.inputRecord
- input record parsed either from request body, if it has content, or from request URI parameters.exchange
- http exchange object, contains e.g. the http header fieldsRecord
, you have to provide a
writeResultObject(OutputStream, Object)
method, too.java.lang.Exception
- any failure during processing.public void handle(HttpExchange exchange)
#process(Record)
to do processing and get a result record and
writes JSON result.
Handles a HttpExchange containing a HttpRequest.exchange
- the HttpExchange objectprotected boolean isValidMethod(java.lang.String method, java.lang.String requestUri)
method
- HTTP methodrequestUri
- request URIprotected boolean writeSuccessResult(HttpExchange exchange, java.lang.Object resultObject, Record inputRecord) throws java.io.IOException
exchange
- HTTP exchangeresultObject
- result object, may be null.inputRecord
- input record parsed either from request body, if it has content, or from request URI parameters.java.io.IOException
- error writing the result.protected int getSuccessStatus(java.lang.String method, java.lang.String requestUri)
method
- HTTP methodrequestUri
- request URIprotected int getSuccessStatus(java.lang.String requestMethod, java.lang.String requestUri, Record inputRecord, java.lang.Object resultObject)
requestMethod
- HTTP methodrequestUri
- request URIinputRecord
- input recordresultObject
- result object returned from processprotected void writeResultObject(java.io.OutputStream responseStream, java.lang.Object resultObject) throws java.io.IOException
Record
results. Subclasses that produce
different result types must override this method.responseStream
- response streamresultObject
- result object.java.io.IOException
- error writing resultprotected boolean writeErrorResult(HttpExchange exchange, java.lang.Throwable error, Record inputRecord) throws java.io.IOException
exchange
- HTTP exchangeerror
- the exception causing the error.inputRecord
- the input record.java.io.IOException
- error writing the result.protected int getErrorStatus(java.lang.String method, java.lang.String requestUri, java.lang.Throwable ex)
HTTP_UTILS#getErrorStatus(String, String, Throwable)
protected java.lang.String getRequestHost(HttpExchange exchange)
exchange
- http exchange object, contains the request host in the http header