Class Webservice

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected android.content.Context applicationContext
      Context of the application
      protected java.util.Map<java.lang.String,​java.lang.String> headers
      Headers of the request
      protected boolean isDowngradedCipher
      Is the webservice using a cipher v1 fallback
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Webservice​(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 void addDefaultHeaders()
      Add default headers to the WS request
      protected void addDefaultParameters()
      Add default parameters to the request Those are device/user/system locale
      protected void addGetParameter​(java.lang.String key, java.lang.String value)
      Add a specific GET parameter.
      protected void addHeaders()
      Add the headers returned by getHeaders() if any
      protected void addParameters()
      Add the parameters returned by getParameters() to the URL if any
      protected void addRequestSignatures​(java.net.HttpURLConnection connection, byte[] body)  
      protected java.net.HttpURLConnection buildConnection()
      Build a new and not already opened HttpURLConnection with all parameters set
      protected void buildParameters()
      Build parameters by executing all add methods
      protected java.net.URL buildURL()
      Build the url used by the connection with all parameters set
      protected boolean canBypassOptOut()
      Returns whether this webservice is allowed to bypass the global opt out
      static java.lang.String encode​(java.lang.String part)
      URLEncode the part
      byte[] executeRequest()
      Execute the WS call synchronously and return the response
      static java.lang.String formatDate​(java.util.Date date)
      Return the date formatted with RFC 3339 format
      protected JSONObject getBasicJsonResponseBody()
      Return the JSON body if the response is valid
      protected abstract java.lang.String getCryptorModeParameterKey()
      Return the parameter key for the cryptor mode
      protected abstract java.lang.String getCryptorTypeParameterKey()
      Return the type of the get cryptor wanted for this webservice
      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
      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
      protected abstract java.lang.String getPostCryptorTypeParameterKey()
      Return the type of the post cryptor wanted for this webservice
      protected abstract PostDataProvider<?> getPostDataProvider()
      Return the POST body you want to add to the request
      protected abstract java.lang.String getReadCryptorTypeParameterKey()
      Return the type of the read cryptor wanted for this webservice
      static Webservice.WebserviceError.Reason getResponseErrorCause​(int statusCode)
      Get error reason depending on the status code
      protected java.lang.String getSignatureBody​(java.net.HttpURLConnection connection, java.util.List<java.lang.String> keys)  
      protected abstract java.lang.String getSpecificConnectTimeoutKey()
      Return the Parameters key to search specific connect timeout for this webservice
      protected abstract java.lang.String getSpecificReadTimeoutKey()
      Return the Parameters key to search specific read timeout for this webservice
      protected abstract java.lang.String getSpecificRetryCountKey()
      Return the Parameters key to search specific number of retry for this webservice
      protected JSONObject getStandardResponseBodyIfValid()
      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 use getBasicJsonResponseBody()
      protected abstract java.lang.String getURLSorterPatternParameterKey()
      Return the parameter key to get the URL sorter pattern
      static boolean isResponseValid​(int statusCode)
      Tell if an http response code is valid or not
      protected void onRetry​(WebserviceErrorCause cause)
      Called when a retry is made after a failure
      Nothing done here, but child may want to override it
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 by getParameters() 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 out

        Almost 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 by getHeaders() 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.ALL will 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
      • 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 -
      • 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.Exception
        Build 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 the Parameters key 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 the Parameters key 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 the Parameters key 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: