com.sponsorpay.sdk.android.publisher.currency
Class CurrencyServerAbstractResponse

java.lang.Object
  extended by com.sponsorpay.sdk.android.publisher.currency.CurrencyServerAbstractResponse
Direct Known Subclasses:
CurrencyServerDeltaOfCoinsResponse, RequestErrorResponse

public abstract class CurrencyServerAbstractResponse
extends java.lang.Object

Encloses a basic response received from the SponsorPay's Virtual Currency Server and methods to perform parsing of the returned JSON-encoded data.


Nested Class Summary
static class CurrencyServerAbstractResponse.RequestErrorType
          Types of error condition which a request / response might result in.
 
Field Summary
private static java.lang.String ERROR_CODE_KEY
           
private static java.lang.String ERROR_MESSAGE_KEY
           
protected  java.lang.String mErrorCode
          Error code provided by the server.
protected  java.lang.String mErrorMessage
          Error message provided by the server.
protected  CurrencyServerAbstractResponse.RequestErrorType mErrorType
          Type of error condition in which the request / response has resulted.
protected  SPCurrencyServerListener mListener
          Listener which will be notified after parsing the response.
protected  java.lang.String mResponseBody
          Body of the HTTP response.
protected  java.lang.String mResponseSignature
          Server-provided signature of the response.
protected  int mResponseStatusCode
          Status code of the HTTP response.
 
Constructor Summary
CurrencyServerAbstractResponse()
           
 
Method Summary
 java.lang.String getErrorCode()
          Gets the error code returned by the server.
 java.lang.String getErrorMessage()
          Gets the error message returned by the server.
 CurrencyServerAbstractResponse.RequestErrorType getErrorType()
          Gets the error condition in which this request / response has resulted.
static CurrencyServerAbstractResponse getParsingInstance(VirtualCurrencyConnector.RequestType requestType)
          Returns the appropriate subclass to enclose and parse the response to a request of the passed type.
 boolean hasErrorStatusCode()
          Returns true if the response contains an HTTP status code out of the 200s.
 void invokeOnErrorCallback()
          Invokes the SPCurrencyServerListener.onSPCurrencyServerError(CurrencyServerAbstractResponse) of the registered callback.
abstract  void invokeOnSuccessCallback()
          Implement to invoke the response-type-specific on success callback notification.
 void parseAndCallListener(java.lang.String securityToken)
          Performs a second-stage error checking, parses the response and invokes the relevant method of the listener registered with setResponseListener(SPCurrencyServerListener).
 void parseErrorResponse()
          Parses a response containing a non-successful HTTP status code.
abstract  void parseSuccessfulResponse()
          Implement to parse a successful-HTTP-status-code containing response.
 void setResponseData(int responseStatusCode, java.lang.String responseBody, java.lang.String responseSignature)
          Sets the raw response data.
 void setResponseListener(SPCurrencyServerListener listener)
          Set the response listener which will be notified when the parsing is complete.
 boolean verifySignature(java.lang.String securityToken)
          Verify calculate the signature of the response with the provided security token and compare it against the server-provided response signature.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ERROR_CODE_KEY

private static final java.lang.String ERROR_CODE_KEY
See Also:
Constant Field Values

ERROR_MESSAGE_KEY

private static final java.lang.String ERROR_MESSAGE_KEY
See Also:
Constant Field Values

mResponseStatusCode

protected int mResponseStatusCode
Status code of the HTTP response.


mResponseBody

protected java.lang.String mResponseBody
Body of the HTTP response.


mResponseSignature

protected java.lang.String mResponseSignature
Server-provided signature of the response.


mListener

protected SPCurrencyServerListener mListener
Listener which will be notified after parsing the response. Every response type may call a different listener method.


mErrorType

protected CurrencyServerAbstractResponse.RequestErrorType mErrorType
Type of error condition in which the request / response has resulted.


mErrorCode

protected java.lang.String mErrorCode
Error code provided by the server.


mErrorMessage

protected java.lang.String mErrorMessage
Error message provided by the server.

Constructor Detail

CurrencyServerAbstractResponse

public CurrencyServerAbstractResponse()
Method Detail

parseSuccessfulResponse

public abstract void parseSuccessfulResponse()
Implement to parse a successful-HTTP-status-code containing response.


invokeOnSuccessCallback

public abstract void invokeOnSuccessCallback()
Implement to invoke the response-type-specific on success callback notification.


setResponseData

public void setResponseData(int responseStatusCode,
                            java.lang.String responseBody,
                            java.lang.String responseSignature)
Sets the raw response data.

Parameters:
responseStatusCode - HTTP status code returned by the server.
responseBody - Body of the HTTP response.
responseSignature - Signature of the response extracted from the response headers.

setResponseListener

public void setResponseListener(SPCurrencyServerListener listener)
Set the response listener which will be notified when the parsing is complete. Every response type may call a different listener method.

Parameters:
listener -

verifySignature

public boolean verifySignature(java.lang.String securityToken)
Verify calculate the signature of the response with the provided security token and compare it against the server-provided response signature.

Parameters:
securityToken - Security token which will be used to calculate the signature.
Returns:
true if the calculated signature matches the server-provided signature. false otherwise.

parseAndCallListener

public void parseAndCallListener(java.lang.String securityToken)
Performs a second-stage error checking, parses the response and invokes the relevant method of the listener registered with setResponseListener(SPCurrencyServerListener).

Parameters:
securityToken - Security token used to verify the authenticity of the response.

hasErrorStatusCode

public boolean hasErrorStatusCode()
Returns true if the response contains an HTTP status code out of the 200s.

Returns:
false if HTTP status code is between 200 and 299. True otherwise.

parseErrorResponse

public void parseErrorResponse()
Parses a response containing a non-successful HTTP status code. Tries to extract the error code and error message from the response body.


invokeOnErrorCallback

public void invokeOnErrorCallback()
Invokes the SPCurrencyServerListener.onSPCurrencyServerError(CurrencyServerAbstractResponse) of the registered callback.


getErrorType

public CurrencyServerAbstractResponse.RequestErrorType getErrorType()
Gets the error condition in which this request / response has resulted.

Returns:
A CurrencyServerAbstractResponse.RequestErrorType.

getErrorCode

public java.lang.String getErrorCode()
Gets the error code returned by the server.

Returns:

getErrorMessage

public java.lang.String getErrorMessage()
Gets the error message returned by the server.

Returns:

getParsingInstance

public static CurrencyServerAbstractResponse getParsingInstance(VirtualCurrencyConnector.RequestType requestType)
Returns the appropriate subclass to enclose and parse the response to a request of the passed type.

Parameters:
requestType - The type of the request whose response can be parsed by the returned instance.
Returns:
An instance of a subclass of CurrencyServerAbstractResponse, or null if no subclass is known for parsing the provided VirtualCurrencyConnector.RequestType.