SMILA (incubation) API documentation

org.eclipse.smila.http.server.json
Class JsonHttpHandler

java.lang.Object
  extended by org.eclipse.smila.http.server.json.JsonHttpHandler
All Implemented Interfaces:
HttpHandler

public abstract class JsonHttpHandler
extends java.lang.Object
implements HttpHandler

Common base class for HTTP handlers that expect a single JSON CEO as input and produce a single CEO in case of successful processing.

Author:
jschumacher

Constructor Summary
JsonHttpHandler()
           
 
Method Summary
protected  int getErrorStatus(java.lang.String method, java.lang.String requestUri, java.lang.Throwable ex)
          Return a status code for an exception.
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)
           
abstract  java.lang.Object process(Record inputRecord)
          process input record and return result record.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.eclipse.smila.http.server.HttpHandler
getRootContextPath
 

Constructor Detail

JsonHttpHandler

public JsonHttpHandler()
Method Detail

process

public abstract java.lang.Object process(Record inputRecord)
                                  throws java.lang.Exception
process input record and return result record.

Parameters:
inputRecord - input Record
Returns:
result record. May be null.
Throws:
java.lang.Exception - any failure during processing.

handle

public 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. Handles a HttpExchange containing a HttpRequest.

Specified by:
handle in interface HttpHandler
Parameters:
exchange - the HttpExchange object

isValidMethod

protected boolean isValidMethod(java.lang.String method,
                                java.lang.String requestUri)
Parameters:
method - HTTP method
requestUri - request URI
Returns:
true if the method is valid for this handler. Default is "POST".

writeSuccessResult

protected boolean writeSuccessResult(HttpExchange exchange,
                                     java.lang.Object resultObject,
                                     Record inputRecord)
                              throws java.io.IOException
write a successful result.

Parameters:
exchange - HTTP exchange
resultObject - result object
inputRecord - input record parsed either from request body, if it has content, or from request URI parameters.
Returns:
if the connection should be closed after everything is finished.
Throws:
java.io.IOException - error writing the result.

getSuccessStatus

protected int getSuccessStatus(java.lang.String method,
                               java.lang.String requestUri)
Returns:
the status code to return for successful requests. By default it is "OK" (200).

getSuccessStatus

protected int getSuccessStatus(java.lang.String requestMethod,
                               java.lang.String requestUri,
                               Record inputRecord,
                               java.lang.Object resultObject)
Returns the success code.

Parameters:
requestMethod - HTTP method
requestUri - request URI
inputRecord - input record
resultObject - result object returned from process
Returns:
the status code to return for successful requests using the method and request URI.

writeResultObject

protected void writeResultObject(java.io.OutputStream responseStream,
                                 java.lang.Object resultObject)
                          throws java.io.IOException
write result object. The default implementation writes back single Record results. Subclasses that produce different result types must override this method.

Parameters:
responseStream - response stream
resultObject - result object.
Throws:
java.io.IOException - error writing result

writeErrorResult

protected boolean writeErrorResult(HttpExchange exchange,
                                   java.lang.Throwable error,
                                   Record inputRecord)
                            throws java.io.IOException
write an error result.

Parameters:
exchange - HTTP exchange
error - the exception causing the error.
inputRecord - the input record.
Returns:
if the connection should be closed after everything is finished.
Throws:
java.io.IOException - error writing the result.

getErrorStatus

protected int getErrorStatus(java.lang.String method,
                             java.lang.String requestUri,
                             java.lang.Throwable ex)
Return a status code for an exception.

See Also:
JsonHttpUtils#getErrorStatus(String, String, Throwable)

SMILA (incubation) API documentation