Package com.batch.android.core
Class Webservice
- java.lang.Object
-
- com.batch.android.core.Webservice
-
- Direct Known Subclasses:
BatchWebservice,ImageDownloadWebservice
public abstract class Webservice extends java.lang.ObjectAbstract webservice class designed for any WS call
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classWebservice.RequestTypeType of the HTTP Requeststatic classWebservice.WebserviceErrorError thrown by Webservice on error
-
Field Summary
Fields Modifier and Type Field Description protected android.content.ContextapplicationContextContext of the applicationprotected java.util.Map<java.lang.String,java.lang.String>headersHeaders of the requestprotected booleanisDowngradedCipherIs the webservice using a cipher v1 fallback
-
Constructor Summary
Constructors Modifier Constructor Description protectedWebservice(android.content.Context context, Webservice.RequestType type, java.lang.String urlPattern, java.lang.String... parameters)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddDefaultHeaders()Add default headers to the WS requestprotected voidaddDefaultParameters()Add default parameters to the request Those are device/user/system localeprotected voidaddGetParameter(java.lang.String key, java.lang.String value)Add a specific GET parameter.protected voidaddHeaders()Add the headers returned bygetHeaders()if anyprotected voidaddParameters()Add the parameters returned bygetParameters()to the URL if anyprotected voidaddRequestSignatures(java.net.HttpURLConnection connection, byte[] body)protected java.net.HttpURLConnectionbuildConnection()Build a new and not already opened HttpURLConnection with all parameters setprotected voidbuildParameters()Build parameters by executing all add methodsprotected java.net.URLbuildURL()Build the url used by the connection with all parameters setprotected booleancanBypassOptOut()Returns whether this webservice is allowed to bypass the global opt outstatic java.lang.Stringencode(java.lang.String part)URLEncode the partbyte[]executeRequest()Execute the WS call synchronously and return the responsestatic java.lang.StringformatDate(java.util.Date date)Return the date formatted with RFC 3339 formatprotected JSONObjectgetBasicJsonResponseBody()Return the JSON body if the response is validprotected abstract java.lang.StringgetCryptorModeParameterKey()Return the parameter key for the cryptor modeprotected abstract java.lang.StringgetCryptorTypeParameterKey()Return the type of the get cryptor wanted for this webserviceprotected java.util.Map<java.lang.String,java.lang.String>getHeaders()Return the specific header you want to add to the request
You should override this method if you want to provide headersprotected java.util.Map<java.lang.String,java.lang.String>getParameters()Return the specific GET parameters you want to add to the request
You should override this method to provide custom get parametersprotected abstract java.lang.StringgetPostCryptorTypeParameterKey()Return the type of the post cryptor wanted for this webserviceprotected abstract PostDataProvider<?>getPostDataProvider()Return the POST body you want to add to the requestprotected abstract java.lang.StringgetReadCryptorTypeParameterKey()Return the type of the read cryptor wanted for this webservicestatic Webservice.WebserviceError.ReasongetResponseErrorCause(int statusCode)Get error reason depending on the status codeprotected java.lang.StringgetSignatureBody(java.net.HttpURLConnection connection, java.util.List<java.lang.String> keys)protected abstract java.lang.StringgetSpecificConnectTimeoutKey()Return theParameterskey to search specific connect timeout for this webserviceprotected abstract java.lang.StringgetSpecificReadTimeoutKey()Return theParameterskey to search specific read timeout for this webserviceprotected abstract java.lang.StringgetSpecificRetryCountKey()Return theParameterskey to search specific number of retry for this webserviceprotected JSONObjectgetStandardResponseBodyIfValid()Return the JSON body if the response is valid Note that this requires the body to be in the "standard" form, mostly used by queries That is {"status": "OK", "header": ..., "ts": ..., "body": {...}} For webservice clients that use a different format, please usegetBasicJsonResponseBody()protected abstract java.lang.StringgetURLSorterPatternParameterKey()Return the parameter key to get the URL sorter patternstatic booleanisResponseValid(int statusCode)Tell if an http response code is valid or notprotected voidonRetry(WebserviceErrorCause cause)Called when a retry is made after a failure
Nothing done here, but child may want to override it
-
-
-
Field Detail
-
headers
protected java.util.Map<java.lang.String,java.lang.String> headers
Headers of the request
-
applicationContext
@NonNull protected android.content.Context applicationContext
Context of the application
-
isDowngradedCipher
protected boolean isDowngradedCipher
Is the webservice using a cipher v1 fallback
-
-
Constructor Detail
-
Webservice
protected Webservice(android.content.Context context, Webservice.RequestType type, java.lang.String urlPattern, java.lang.String... parameters) throws java.net.MalformedURLException- Parameters:
context-urlPattern-- Throws:
java.net.MalformedURLException
-
-
Method Detail
-
addDefaultParameters
protected void addDefaultParameters()
Add default parameters to the request Those are device/user/system locale
-
addParameters
protected void addParameters()
Add the parameters returned bygetParameters()to the URL if any
-
addGetParameter
protected void addGetParameter(java.lang.String key, java.lang.String value)Add a specific GET parameter.- Parameters:
key-value-
-
getParameters
protected java.util.Map<java.lang.String,java.lang.String> getParameters()
Return the specific GET parameters you want to add to the request
You should override this method to provide custom get parameters- Returns:
- Map of parameters if any, null if you don't want any
-
canBypassOptOut
protected boolean canBypassOptOut()
Returns whether this webservice is allowed to bypass the global opt outAlmost none of the webservice implementations should return true
-
addDefaultHeaders
protected void addDefaultHeaders()
Add default headers to the WS request
-
addHeaders
protected void addHeaders()
Add the headers returned bygetHeaders()if any
-
getHeaders
protected java.util.Map<java.lang.String,java.lang.String> getHeaders()
Return the specific header you want to add to the request
You should override this method if you want to provide headers- Returns:
- Map of headers if any, null if you don't want any
-
getPostDataProvider
protected abstract PostDataProvider<?> getPostDataProvider()
Return the POST body you want to add to the request- Returns:
- the post data provider if any, null otherwise
-
getURLSorterPatternParameterKey
protected abstract java.lang.String getURLSorterPatternParameterKey()
Return the parameter key to get the URL sorter pattern- Returns:
- key of the parameter or null if no sortor is wanted
-
getCryptorTypeParameterKey
protected abstract java.lang.String getCryptorTypeParameterKey()
Return the type of the get cryptor wanted for this webservice- Returns:
- type or null if no encryption is wanted
-
getCryptorModeParameterKey
protected abstract java.lang.String getCryptorModeParameterKey()
Return the parameter key for the cryptor mode- Returns:
- param key or null if no mode is provided (
URLBuilder.CryptorMode.ALLwill be used)
-
getPostCryptorTypeParameterKey
protected abstract java.lang.String getPostCryptorTypeParameterKey()
Return the type of the post cryptor wanted for this webservice- Returns:
- type or null if no encryption is wanted
-
getReadCryptorTypeParameterKey
protected abstract java.lang.String getReadCryptorTypeParameterKey()
Return the type of the read cryptor wanted for this webservice- Returns:
- type or null if no encryption is wanted
-
executeRequest
public byte[] executeRequest() throws Webservice.WebserviceErrorExecute the WS call synchronously and return the response- Returns:
- byte[] on success, throws error on failure
- Throws:
Webservice.WebserviceError- on error
-
onRetry
protected void onRetry(WebserviceErrorCause cause)
Called when a retry is made after a failure
Nothing done here, but child may want to override it- Parameters:
cause-
-
getStandardResponseBodyIfValid
protected JSONObject getStandardResponseBodyIfValid() throws Webservice.WebserviceError, JSONException
Return the JSON body if the response is valid Note that this requires the body to be in the "standard" form, mostly used by queries That is {"status": "OK", "header": ..., "ts": ..., "body": {...}} For webservice clients that use a different format, please usegetBasicJsonResponseBody()- Returns:
- JSONBody if valid
- Throws:
Webservice.WebserviceError- on error on the webserviceJSONException- on error validating the JSON
-
getBasicJsonResponseBody
protected JSONObject getBasicJsonResponseBody() throws Webservice.WebserviceError, JSONException
Return the JSON body if the response is valid- Returns:
- JSONBody if valid
- Throws:
Webservice.WebserviceError- on error on the webserviceJSONException- on error validating the JSON
-
isResponseValid
public static boolean isResponseValid(int statusCode)
Tell if an http response code is valid or not- Parameters:
statusCode-- Returns:
-
getResponseErrorCause
public static Webservice.WebserviceError.Reason getResponseErrorCause(int statusCode)
Get error reason depending on the status code- Parameters:
statusCode-- Returns:
-
encode
public static java.lang.String encode(java.lang.String part)
URLEncode the part- Parameters:
part-- Returns:
-
buildURL
protected java.net.URL buildURL()
Build the url used by the connection with all parameters set- Returns:
-
buildConnection
protected java.net.HttpURLConnection buildConnection() throws java.lang.ExceptionBuild a new and not already opened HttpURLConnection with all parameters set- Returns:
- HttpURLConnection on success|null otherwise
- Throws:
java.lang.Exception
-
buildParameters
protected void buildParameters()
Build parameters by executing all add methods
-
addRequestSignatures
protected void addRequestSignatures(java.net.HttpURLConnection connection, @Nullable byte[] body)
-
getSignatureBody
protected java.lang.String getSignatureBody(java.net.HttpURLConnection connection, java.util.List<java.lang.String> keys)
-
getSpecificConnectTimeoutKey
protected abstract java.lang.String getSpecificConnectTimeoutKey()
Return theParameterskey to search specific connect timeout for this webservice- Returns:
- key if we need specific value, null otherwise
-
getSpecificReadTimeoutKey
protected abstract java.lang.String getSpecificReadTimeoutKey()
Return theParameterskey to search specific read timeout for this webservice- Returns:
- key if we need specific value, null otherwise
-
getSpecificRetryCountKey
protected abstract java.lang.String getSpecificRetryCountKey()
Return theParameterskey to search specific number of retry for this webservice- Returns:
- key if we need specific value, null otherwise
-
formatDate
public static java.lang.String formatDate(java.util.Date date)
Return the date formatted with RFC 3339 format- Parameters:
date-- Returns:
-
-