org.sr.common.json
Class JSONResponse

java.lang.Object
  extended by org.sr.common.json.JSONResponse
All Implemented Interfaces:
java.io.Serializable

public class JSONResponse
extends java.lang.Object
implements java.io.Serializable

  This class represents a response from a remote service. The response of a service call may contain a result or an error. In any cases call responses contains a message identifier which is the same as incoming message for message correlation on the caller side. The response may contain a header message part which contains additional fields beside error or result. The following Javascript class represents a response wrapper which contains an operation result within it.
{
 "id":"64f4f4b0-79ee-45f0-9b47-77836bc74ce8",
 "header":{
  "headerField1": "headerVal",
  "headerField2": "headerVal",
 },
 "result":"resultObject"
}
  JSONTransformer doesn't use class identifiers for simple & primitive java types. For the complex classes like beans, transformer puts additional hints into the wrapper class to easy transformation process. A complex object representation would be like this:
{"classHintAttribute":"test.bean.SimpleBean","beanField":"testValue"}
The following Javascript class represents a response wrapper which contains an error within it.
{
 "id":"C3F236B3-D450-0001-B8E3-4F8D528C5390",
 "error":
  {"errorName":"org.sr.server.AuhenticationException",
  "errorCode":491,
  "errorMsg":"Authentication is required for securityTestBean.authenticationRequiredMethod calls"
  }
}


Message Parts

id

 This part should contain a unique message identifier for the message correlation. Results of operations will be sent to the clients by using same identifier as requests. This would help clients to correlate their responses from server.

header

  The header part of the message contains additional fields that sent by the server beside parameters.

result

 The result of the operation. In the case of an exception raises, the wrapper class wouldn't contains result.

error

 The wrapper class contains error part only if an exception raises from the service. Error object has three attributes as errorCode,errorMsg and errorName.

Author:
Serhat Dirik
See Also:
Serialized Form

Field Summary
static int CODE_AUTHENTICATION_REQUIRED
           
static int CODE_ERR_NOLOCATOR
           
static int CODE_ERR_NOMETHOD
           
static int CODE_ERR_PARSE
           
static int CODE_REMOTE_EXCEPTION
           
static int CODE_SUCCESS
           
static int CODE_TRANSPORT_SECURITY_REQUIRED
           
static int CODE_UNAUTHORIZED
           
static java.lang.String ERROR_ATTRIBUTE
           
static java.lang.String ERRORCODE_ATTRIBUTE
           
static java.lang.String ERRORMESSAGE_ATTRIBUTE
           
static java.lang.String ERRORNAME_ATTRIBUTE
           
static java.lang.String HEADER_ATTRIBUTE
           
static java.lang.String RESPONSEID_ATTRIBUTE
           
static java.lang.String RESULT_ATTRIBUTE
           
 
Constructor Summary
JSONResponse(JSONObject response)
          Create a JSONResponse wrapper java class to the given JSONObject (Javascript class)
JSONResponse(java.lang.String response)
          Create a JSONResponse wrapper java class to the given String object which obeys JSON Rules
JSONResponse(java.lang.String id, int errorCode, java.lang.String errorName, java.lang.String errorMessage)
          Creates a response wrapper class which contains the given error
JSONResponse(java.lang.String id, java.lang.Object result)
          Creates a response wrapper class for the given result for the given message id
 
Method Summary
 int getErrorCode()
           
 java.lang.String getErrorMessage()
           
 java.lang.String getErrorName()
           
 java.util.Map<java.lang.String,java.lang.Object> getHeader()
           
 java.lang.String getResponseId()
           
 java.lang.Object getResult()
           
 boolean hasError()
           
 boolean hasHeaders()
           
 boolean hasResult()
           
 void putToHeader(java.lang.String fieldName, java.lang.Object value)
           
 void removeFromHeader(java.lang.String fieldName)
           
 void setHeader(java.util.Map<java.lang.String,java.lang.Object> header)
           
 JSONObject toJSON()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

RESPONSEID_ATTRIBUTE

public static final java.lang.String RESPONSEID_ATTRIBUTE
See Also:
Constant Field Values

HEADER_ATTRIBUTE

public static final java.lang.String HEADER_ATTRIBUTE
See Also:
Constant Field Values

RESULT_ATTRIBUTE

public static final java.lang.String RESULT_ATTRIBUTE
See Also:
Constant Field Values

ERROR_ATTRIBUTE

public static final java.lang.String ERROR_ATTRIBUTE
See Also:
Constant Field Values

ERRORCODE_ATTRIBUTE

public static final java.lang.String ERRORCODE_ATTRIBUTE
See Also:
Constant Field Values

ERRORNAME_ATTRIBUTE

public static final java.lang.String ERRORNAME_ATTRIBUTE
See Also:
Constant Field Values

ERRORMESSAGE_ATTRIBUTE

public static final java.lang.String ERRORMESSAGE_ATTRIBUTE
See Also:
Constant Field Values

CODE_SUCCESS

public static final int CODE_SUCCESS
See Also:
Constant Field Values

CODE_REMOTE_EXCEPTION

public static final int CODE_REMOTE_EXCEPTION
See Also:
Constant Field Values

CODE_TRANSPORT_SECURITY_REQUIRED

public static final int CODE_TRANSPORT_SECURITY_REQUIRED
See Also:
Constant Field Values

CODE_AUTHENTICATION_REQUIRED

public static final int CODE_AUTHENTICATION_REQUIRED
See Also:
Constant Field Values

CODE_UNAUTHORIZED

public static final int CODE_UNAUTHORIZED
See Also:
Constant Field Values

CODE_ERR_PARSE

public static final int CODE_ERR_PARSE
See Also:
Constant Field Values

CODE_ERR_NOMETHOD

public static final int CODE_ERR_NOMETHOD
See Also:
Constant Field Values

CODE_ERR_NOLOCATOR

public static final int CODE_ERR_NOLOCATOR
See Also:
Constant Field Values
Constructor Detail

JSONResponse

public JSONResponse(java.lang.String response)
             throws JSONException,
                    TransformationException,
                    TransformationNotSupportedException
Create a JSONResponse wrapper java class to the given String object which obeys JSON Rules

Parameters:
response -
Throws:
JSONException
TransformationException
TransformationNotSupportedException

JSONResponse

public JSONResponse(JSONObject response)
             throws JSONException,
                    TransformationException,
                    TransformationNotSupportedException
Create a JSONResponse wrapper java class to the given JSONObject (Javascript class)

Parameters:
response -
Throws:
JSONException
TransformationException
TransformationNotSupportedException

JSONResponse

public JSONResponse(java.lang.String id,
                    java.lang.Object result)
             throws JSONException
Creates a response wrapper class for the given result for the given message id

Parameters:
id -
result -
Throws:
JSONException

JSONResponse

public JSONResponse(java.lang.String id,
                    int errorCode,
                    java.lang.String errorName,
                    java.lang.String errorMessage)
             throws JSONException
Creates a response wrapper class which contains the given error

Parameters:
id -
errorCode -
errorName -
errorMessage -
Throws:
JSONException
Method Detail

hasResult

public boolean hasResult()

hasHeaders

public boolean hasHeaders()

hasError

public boolean hasError()

getResponseId

public java.lang.String getResponseId()

getErrorName

public java.lang.String getErrorName()

getErrorCode

public int getErrorCode()

getErrorMessage

public java.lang.String getErrorMessage()

getResult

public java.lang.Object getResult()

setHeader

public void setHeader(java.util.Map<java.lang.String,java.lang.Object> header)
               throws JSONException,
                      TransformationException,
                      TransformationNotSupportedException
Throws:
JSONException
TransformationException
TransformationNotSupportedException

getHeader

public java.util.Map<java.lang.String,java.lang.Object> getHeader()

putToHeader

public void putToHeader(java.lang.String fieldName,
                        java.lang.Object value)
                 throws JSONException,
                        TransformationException,
                        TransformationNotSupportedException
Throws:
JSONException
TransformationException
TransformationNotSupportedException

removeFromHeader

public void removeFromHeader(java.lang.String fieldName)
                      throws JSONException,
                             TransformationException,
                             TransformationNotSupportedException
Throws:
JSONException
TransformationException
TransformationNotSupportedException

toJSON

public JSONObject toJSON()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object