water.api
Class RequestBuilders.Response

java.lang.Object
  extended by water.api.RequestBuilders.Response
Enclosing class:
RequestBuilders

public static final class RequestBuilders.Response
extends java.lang.Object

This is a response class for the JSON. Instead of simply returning a JsonObject, each request returns a new response object that it must create. This is (a) cleaner (b) more explicit and (c) allows to specify response states used for proper error reporting, stateless and statefull processed and so on, and (d) allows specification of HTML builder hooks in a nice clean interface. The work pattern should be that in the serve() method, a JsonObject is created and populated with the variables. Then if any error occurs, an error response should be returned. Otherwise a correct state response should be created at the end from the json object and returned. JSON response structure: response -> status = (done,error,redirect, ...) h2o = name of the cloud node = answering node time = time in MS it took to process the request serve() other fields as per the response type other fields that should go to the user if error: error -> error reported


Nested Class Summary
static class RequestBuilders.Response.Status
          Status of the response.
 
Field Summary
protected  java.util.HashMap<java.lang.String,RequestBuilders.Builder> _builders
          Custom builders for JSON elements when converting to HTML automatically.
protected  java.util.List<java.lang.String> _headers
          Custom headers to show in the html.
protected  int _pollProgress
          Poll progress in terms of finished elements.
protected  int _pollProgressElements
          Total elements to be finished before the poll will be done.
protected  java.lang.Object[] _redirArgs
           
protected  com.google.gson.JsonObject _redirectArgs
          Arguments of the redirect object.
protected  java.lang.String _redirectName
          Name of the redirected request.
protected  Request _req
           
protected  com.google.gson.JsonObject _response
          Response object for JSON requests.
protected  RequestBuilders.Response.Status _status
          Status of the request.
protected  boolean _strictJsonCompliance
           
protected  long _time
          Time it took the request to finish.
static RequestBuilders.Response EMPTY_RESPONSE
          A unique empty response which carries an empty JSON object
 
Constructor Summary
RequestBuilders.Response(RequestBuilders.Response.Status status, Request req, int progress, int total, java.lang.String redirTo, java.lang.Object... args)
           
 
Method Summary
 void addHeader(java.lang.String h)
           
static RequestBuilders.Response done(com.google.gson.JsonObject response)
          Returns new done response with given JSON response object.
static RequestBuilders.Response doneEmpty()
          Returns new done empty done response.
 java.lang.String error()
          Returns the error of the request object if any.
static RequestBuilders.Response error(java.lang.String message)
          Returns new error response with given error message.
 void escapeIllegalJsonElements()
           
protected  RequestBuilders.Builder getBuilderFor(java.lang.String contextName)
          Returns the builder for given JSON context element.
 java.util.List<java.lang.String> getHeaders()
           
static RequestBuilders.Response poll(com.google.gson.JsonObject response, float progress)
          Returns the poll response object initialized by percents completed.
static RequestBuilders.Response poll(com.google.gson.JsonObject response, int progress, int total)
          Returns the poll response object.
static RequestBuilders.Response poll(com.google.gson.JsonObject response, int progress, int total, com.google.gson.JsonObject pollArgs)
          returns the poll response object with different arguments that was this call.
static RequestBuilders.Response redirect(com.google.gson.JsonObject response, java.lang.Class<? extends Request> req, com.google.gson.JsonObject args)
          Creates the new response with status redirect.
protected  com.google.gson.JsonObject responseToJson()
          Returns the response system json.
 RequestBuilders.Response setBuilder(java.lang.String contextName, RequestBuilders.Builder builder)
          Associates a given builder with the specified JSON context.
 void setTimeStart(long timeStart)
          Sets the time of the response as a difference between the given time and now.
 com.google.gson.JsonObject toJson()
          Returns the JSONified version of the request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_time

protected long _time
Time it took the request to finish. In ms.


_status

protected final RequestBuilders.Response.Status _status
Status of the request.


_redirectName

protected final java.lang.String _redirectName
Name of the redirected request. This is only valid if the response is redirect status.


_redirectArgs

protected final com.google.gson.JsonObject _redirectArgs
Arguments of the redirect object. These will be given to the redirect object when called.


_redirArgs

protected final java.lang.Object[] _redirArgs

_pollProgress

protected final int _pollProgress
Poll progress in terms of finished elements.


_pollProgressElements

protected final int _pollProgressElements
Total elements to be finished before the poll will be done.


_response

protected final com.google.gson.JsonObject _response
Response object for JSON requests.


_req

protected final Request _req

_strictJsonCompliance

protected boolean _strictJsonCompliance

_builders

protected final java.util.HashMap<java.lang.String,RequestBuilders.Builder> _builders
Custom builders for JSON elements when converting to HTML automatically.


_headers

protected final java.util.List<java.lang.String> _headers
Custom headers to show in the html.


EMPTY_RESPONSE

public static final RequestBuilders.Response EMPTY_RESPONSE
A unique empty response which carries an empty JSON object

Constructor Detail

RequestBuilders.Response

public RequestBuilders.Response(RequestBuilders.Response.Status status,
                                Request req,
                                int progress,
                                int total,
                                java.lang.String redirTo,
                                java.lang.Object... args)
Method Detail

error

public static RequestBuilders.Response error(java.lang.String message)
Returns new error response with given error message.


done

public static RequestBuilders.Response done(com.google.gson.JsonObject response)
Returns new done response with given JSON response object.


doneEmpty

public static RequestBuilders.Response doneEmpty()
Returns new done empty done response. Should be called only in cases which does not need json response.


redirect

public static RequestBuilders.Response redirect(com.google.gson.JsonObject response,
                                                java.lang.Class<? extends Request> req,
                                                com.google.gson.JsonObject args)
Creates the new response with status redirect. This response will be redirected to another request specified by redirectRequest with the redirection arguments provided in redirectArgs.


poll

public static RequestBuilders.Response poll(com.google.gson.JsonObject response,
                                            int progress,
                                            int total)
Returns the poll response object.


poll

public static RequestBuilders.Response poll(com.google.gson.JsonObject response,
                                            float progress)
Returns the poll response object initialized by percents completed.


poll

public static RequestBuilders.Response poll(com.google.gson.JsonObject response,
                                            int progress,
                                            int total,
                                            com.google.gson.JsonObject pollArgs)
returns the poll response object with different arguments that was this call.


setTimeStart

public final void setTimeStart(long timeStart)
Sets the time of the response as a difference between the given time and now. Called automatically by serving request. Only available in JSON and HTML.


setBuilder

public RequestBuilders.Response setBuilder(java.lang.String contextName,
                                           RequestBuilders.Builder builder)
Associates a given builder with the specified JSON context. JSON context is a dot separated path to the JSON object/element starting from root. One exception is an array row element, which does not really have a distinct name in JSON and is thus identified as the context name of the array + "_ROW" appended to it. The builder object will then be called to build the HTML for the particular JSON element. By wise subclassing of the preexisting builders and changing their behavior an arbitrarily complex webpage can be created.


getBuilderFor

protected RequestBuilders.Builder getBuilderFor(java.lang.String contextName)
Returns the builder for given JSON context element. Null if not found in which case a default builder object will be used. These default builders are specified by the builders themselves.


addHeader

public void addHeader(java.lang.String h)

getHeaders

public java.util.List<java.lang.String> getHeaders()

responseToJson

protected com.google.gson.JsonObject responseToJson()
Returns the response system json. That is the response type, time, h2o basics and other automatic stuff.

Returns:

toJson

public com.google.gson.JsonObject toJson()
Returns the JSONified version of the request. At the moment just returns the response.


error

public java.lang.String error()
Returns the error of the request object if any. Returns null if the response is not in error state.


escapeIllegalJsonElements

public void escapeIllegalJsonElements()