Android Open Source - MobileSampleSDKs A P I Helper






From Project

Back to project page MobileSampleSDKs.

License

The source code is released under:

GNU General Public License

If you think the Android project MobileSampleSDKs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * CustomersLib/* w ww.j  a va 2 s . c  om*/
 *
 * This file was automatically generated by APIMATIC BETA v2.0 on 09/17/2014
 */
package io.apimatic.customerslib;

import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

public class APIHelper {
    /*used for deserialization of json data*/
    private static ObjectMapper mapper = new ObjectMapper();

    /**
    * JSON Serialization of a given object.
    * @param  obj    The object to serialize into JSON
    * @return  The    serialized Json string representation of the given object */
    public static String jsonSerialize(Object obj)
            throws JsonProcessingException {
        if(null == obj)
            return null;

        return mapper.writeValueAsString(obj);
    }

    /**
    * JSON Deserialization of the given json string.
    * @param  json  The json string to deserialize
    * @param  <T>  The type of the object to deserialize into
    * @return  The deserialized object */
    public static <T extends Object> T jsonDeserialize(String json, TypeReference<T> typeReference)
            throws IOException {
        if (isNullOrWhiteSpace(json))
            return null;

        return mapper.readValue(json, typeReference);
    }

    /**
     * JSON Deserialization of the given json string.
     * @param  json  The json string to deserialize
     * @return  The deserialized json tree */
    public static JsonNode jsonDeserialize(String json)
            throws IOException {
        if (isNullOrWhiteSpace(json))
            return null;

        return mapper.readTree(json);
    }
    
    /**
    * Replaces template parameters in the given url
    * @param  queryBuilder    The query string builder to replace the template parameters
    * @param  parameters  The parameters to replace in the url */
    public static void appendUrlWithTemplateParameters(StringBuilder queryBuilder, Map<String, Object> parameters) {
        //perform parameter validation
        if (null == queryBuilder)
            throw new IllegalArgumentException("Given value for parameter \"queryBuilder\" is invalid." );

        if (null == parameters)
            return;

        //iterate and append parameters
        for (Map.Entry<String, Object> pair : parameters.entrySet()) {
             String replaceValue = "";

             //load parameter value
             if (null != pair.getValue())
                 replaceValue = pair.getValue().toString();

             //find the template parameter and replace it with its value
            replaceAll(queryBuilder, "{" + pair.getKey() + "}", replaceValue);
        }
    }

    /**
    * Appends the given set of parameters to the given query string
    * @param  queryBuilder  The query url string to append the parameters
    * @param  parameters  The parameters to append
    * @return  The modified query url string */
    public static void appendUrlWithQueryParameters(StringBuilder queryBuilder, Map<String, Object> parameters) {
        //perform parameter validation
        if (null == queryBuilder)
            throw new IllegalArgumentException("Given value for parameter \"queryBuilder\" is invalid." );

        if (null == parameters)
            return;

        //does the query string already has parameters
        boolean hasParams = (queryBuilder.indexOf("?") > 0);

        //iterate and append parameters
       for (Map.Entry<String, Object> pair : parameters.entrySet()) {
            //ignore null values
            if (null == pair.getValue())
                continue;
            //if already has parameters, use the &amp; to append new parameters
            char separator = (hasParams) ? '&' : '?';

            queryBuilder.append(separator + pair.getKey() + "=" + pair.getValue());

            //indicate that now the query has some params
            hasParams = true;
        }
    }

    /**
    * Validates if the string is null, empty or whitespace
    * @param  s  The string to validate
    * @return  The result of validation */
    public static boolean isNullOrWhiteSpace(String s) {
        if(s == null)
            return  true;

        int length = s.length();
        if (length > 0) {
            for (int start = 0, middle = length / 2, end = length - 1; start <= middle; start++, end--) {
                if (s.charAt(start) > ' ' || s.charAt(end) > ' ') {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    /**
     * Replaces all occurrences of the given string in the string builder
     * @param stringBuilder The string builder to update with replaced strings
     * @param toReplace The string to replace in the string builder
     * @param replaceWith   The string to replace with*/
    public static void replaceAll(StringBuilder stringBuilder, String toReplace, String replaceWith) {
        int index = stringBuilder.indexOf(toReplace);
        
        while (index != -1) {
            stringBuilder.replace(index, index + toReplace.length(), replaceWith);
            index += replaceWith.length(); // Move to the end of the replacement
            index = stringBuilder.indexOf(toReplace, index);
        }
    }
    
    /**
    * Validates and processes the given Url
    * @param    url The given Url to process
    * @return   Pre-process Url as string */
    public static String cleanUrl(StringBuilder url)
    {
        //ensure that the urls are absolute
        Pattern pattern = Pattern.compile("^(https?://[^/]+)");
        Matcher matcher = pattern.matcher(url);
        if (!matcher.find())
            throw new IllegalArgumentException("Invalid Url format.");

        //get the http protocol match
        String protocol = matcher.group(1);

        //remove redundant forward slashes
        String query = url.substring(protocol.length());
        query = query.replaceAll("//+", "/");

        //return process url
        return protocol.concat(query);
    }
}




Java Source Code List

io.apimatic.customerslib.APIException.java
io.apimatic.customerslib.APIException.java
io.apimatic.customerslib.APIHelper.java
io.apimatic.customerslib.APIHelper.java
io.apimatic.customerslib.Configuration.java
io.apimatic.customerslib.Configuration.java
io.apimatic.customerslib.controllers.APIController.java
io.apimatic.customerslib.controllers.APIController.java
io.apimatic.customerslib.models.CustomersModel.java
io.apimatic.customerslib.models.CustomersModel.java