SMILA 1.0 API documentation

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

java.lang.Object
  extended by org.eclipse.smila.http.server.json.JsonHttpUtils

public class JsonHttpUtils
extends java.lang.Object

Utilities for processing data received via an HTTP interface.


Field Summary
static IpcAnyWriter IPC_ANY_WRITER
          for Any -> BON/JSON serialization.
static IpcRecordReader IPC_RECORD_READER
          for BON/JSON -> record parsing.
static IpcRecordWriter IPC_RECORD_WRITER
          for record -> BON/JSON serialization.
 
Constructor Summary
JsonHttpUtils()
           
 
Method Summary
 Record convertGetRequest(HttpExchange he)
          Converts a HTTP GET request into a query record by setting all parameters as attributes and literal values.
 Record convertPostRequest(HttpExchange he)
          Converts a HTTP POST request into a query record by converting the posted JSON stream into a record.
 Record convertRequest(HttpExchange exchange)
          Create an input record from a request by either parse JSON from the request body, or by converting URI parameters.
 int getErrorStatus(java.lang.Throwable ex)
          Return a status code for an exception.
 ObjectMapper getObjectMapper()
          Creates an ObjectMapper instance with disabled JsonGenerator.Feature#AUTO_CLOSE_TARGET and default SMILA date/time format.
 boolean hasRequestContent(HttpExchange exchange)
          check if the request contains content in the body.
 boolean prepareResponseHeader(HttpExchange exchange, int statusCode, boolean hasResponseBody)
          set basic HTTP response headers.
 void writeErrorDetails(HttpExchange exchange, java.lang.Throwable error)
          write exception in JSON format to response stream.
 void writeResultObject(java.io.OutputStream responseStream, java.lang.Object resultObject)
          write result object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IPC_RECORD_WRITER

public static final IpcRecordWriter IPC_RECORD_WRITER
for record -> BON/JSON serialization.


IPC_RECORD_READER

public static final IpcRecordReader IPC_RECORD_READER
for BON/JSON -> record parsing.


IPC_ANY_WRITER

public static final IpcAnyWriter IPC_ANY_WRITER
for Any -> BON/JSON serialization.

Constructor Detail

JsonHttpUtils

public JsonHttpUtils()
Method Detail

convertRequest

public Record convertRequest(HttpExchange exchange)
                      throws IpcException
Create an input record from a request by either parse JSON from the request body, or by converting URI parameters.

Parameters:
exchange - the HttpExchange object
Returns:
the created query record
Throws:
IpcException - if any error occurs during conversion

convertGetRequest

public Record convertGetRequest(HttpExchange he)
                         throws IpcException
Converts a HTTP GET request into a query record by setting all parameters as attributes and literal values.

Parameters:
he - the HttpExchange object
Returns:
the created query record
Throws:
IpcException - if any error occurs during conversion

convertPostRequest

public Record convertPostRequest(HttpExchange he)
                          throws IpcException
Converts a HTTP POST request into a query record by converting the posted JSON stream into a record.

Parameters:
he - the HttpExchange object
Returns:
the created query record
Throws:
IpcException - if any error occurs during conversion from JSON to Record

hasRequestContent

public boolean hasRequestContent(HttpExchange exchange)
check if the request contains content in the body.

Parameters:
exchange - HTTP exchange
Returns:
true if there is content to parse.

prepareResponseHeader

public boolean prepareResponseHeader(HttpExchange exchange,
                                     int statusCode,
                                     boolean hasResponseBody)
set basic HTTP response headers.

Parameters:
exchange - HTTP exchange
statusCode - HTTP response status code.
hasResponseBody - true if the response will have data in body, else false.
Returns:
if the connection should be closed after everything is finished.

writeResultObject

public 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

getErrorStatus

public int getErrorStatus(java.lang.Throwable ex)
Return a status code for an exception. By default this is If a subclass overrides this method it should always call also this implementation for consistent handling of non-special cases.

Parameters:
ex - an exception
Returns:
error status code.

writeErrorDetails

public void writeErrorDetails(HttpExchange exchange,
                              java.lang.Throwable error)
                       throws java.io.IOException
write exception in JSON format to response stream.

Throws:
java.io.IOException

getObjectMapper

public ObjectMapper getObjectMapper()
Creates an ObjectMapper instance with disabled JsonGenerator.Feature#AUTO_CLOSE_TARGET and default SMILA date/time format.

Returns:
the new ObjectMapper

SMILA 1.0 API documentation