SMILA (incubation) API documentation

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

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

public final class JsonHttpUtils
extends java.lang.Object

Utilities for processing data received via an HTTP interface.


Field Summary
static IpcSerializationUtils SERIALIZATION_UTILS
          for record<->BON/JSON serialization.
 
Method Summary
static Record convertGetRequest(HttpExchange he)
          Converts a HTTP GET request into a query record by setting all parameters as attributes and literal values.
static Record convertPostRequest(HttpExchange he)
          Converts a HTTP POST request into a query record by converting the posted JSON stream into a record.
static Record convertRequest(HttpExchange exchange)
          Create an input record from a request by either parse JSON from the request body, or by converting URI parameters.
static int getErrorStatus(java.lang.Throwable ex)
          Return a status code for an exception.
static ObjectMapper getObjectMapper()
          Creates an ObjectMapper instance with disabled JsonGenerator.Feature#AUTO_CLOSE_TARGET and default SMILA date/time format.
static boolean hasRequestContent(HttpExchange exchange)
          check if the request contains content in the body.
static boolean prepareResponseHeader(HttpExchange exchange, int statusCode, boolean hasResponseBody)
          set basic HTTP response headers.
static void writeErrorDetails(HttpExchange exchange, java.lang.Throwable error)
          write exception in JSON format to response stream.
static 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

SERIALIZATION_UTILS

public static final IpcSerializationUtils SERIALIZATION_UTILS
for record<->BON/JSON serialization.

Method Detail

convertRequest

public static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static ObjectMapper getObjectMapper()
Creates an ObjectMapper instance with disabled JsonGenerator.Feature#AUTO_CLOSE_TARGET and default SMILA date/time format.

Returns:
the new ObjectMapper

SMILA (incubation) API documentation