SMILA 1.0 API documentation

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

java.lang.Object
  extended by org.eclipse.smila.http.server.util.ARequestHandler
      extended by org.eclipse.smila.http.server.json.JsonRequestHandler
All Implemented Interfaces:
HttpHandler, RequestHandler
Direct Known Subclasses:
ADeltaHandler, AJobManagerHandler, AStoreHandler, AVisitedHandler, BaseConnectivityHandler, BulkbuilderHandler, DebugHandler, ImportingHandler, PipeletHandler, PipeletsHandler, PipelineHandler, PipelineProcessHandler, PipelinesHandler, StateHandler, TaskHandler, TaskPathStateHandler, TaskStateHandler, TaskTypeStateHandler, WorkerTaskHandler, ZooKeeperAdminHandler

public abstract class JsonRequestHandler
extends ARequestHandler

Common base class for request handlers that expect a single JSON record as input. The result can be void or a single object, this class assumes that a single object is a single record, but this can be overridden easily by a subclass.

Author:
jschumacher

Field Summary
protected static DataFactory FACTORY
          Record/Any factory.
protected static JsonHttpUtils HTTP_UTILS
          Utilities for JSON-Request/Response handling.
 
Constructor Summary
JsonRequestHandler()
           
 
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  java.lang.String getRequestHost()
          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.
abstract  java.lang.Object process(java.lang.String method, java.lang.String requestUri, Record inputRecord)
          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.
 
Methods inherited from class org.eclipse.smila.http.server.util.ARequestHandler
activate, getDynamicUriParts, getRootContextPath, getStaticUriParts, getUriPattern, matches, urlDecode
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FACTORY

protected static final DataFactory FACTORY
Record/Any factory. Helper shortcut for subclasses.


HTTP_UTILS

protected static final JsonHttpUtils HTTP_UTILS
Utilities for JSON-Request/Response handling.

Constructor Detail

JsonRequestHandler

public JsonRequestHandler()
Method Detail

process

public abstract java.lang.Object process(java.lang.String method,
                                         java.lang.String requestUri,
                                         Record inputRecord)
                                  throws java.lang.Exception
process input record and return result object.

Parameters:
method - HTTP method
requestUri - request URI.
inputRecord - input record parsed either from request body, if it has content, or from request URI parameters.
Returns:
result object, may be null. If this is not a Record, you have to provide a writeResultObject(OutputStream, Object) method, too.
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.

Parameters:
exchange - the HttpExchange object

isValidMethod

protected boolean isValidMethod(java.lang.String method,
                                java.lang.String requestUri)
Checks if the handler allows the HTTP method.

Parameters:
method - HTTP method
requestUri - request URI
Returns:
true if the method can be used for the URI, else false.

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, may be null.
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)
Parameters:
method - HTTP method
requestUri - request URI
Returns:
the status code to return for successful requests using the method and request URI. 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:
HTTP_UTILS#getErrorStatus(String, String, Throwable)

getRequestHost

protected java.lang.String getRequestHost()
Returns the host as defined in the HTTP request.

Returns:
the host from the request.

SMILA 1.0 API documentation