Class FormEncoder


  • public final class FormEncoder
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      FormEncoder()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static HttpContent createHttpContent​(java.util.Map<java.lang.String,​java.lang.Object> params)  
      static java.lang.String createQueryString​(java.util.Collection<KeyValuePair<java.lang.String,​java.lang.String>> nameValueCollection)
      Creates the HTTP query string for a collection of name/value tuples.
      static java.lang.String createQueryString​(java.util.Map<java.lang.String,​java.lang.Object> params)
      Creates the HTTP query string for a given map of parameters.
      static java.util.List<KeyValuePair<java.lang.String,​java.lang.Object>> flattenParams​(java.util.Map<java.lang.String,​java.lang.Object> params)
      Returns a list of flattened parameters for the given map of parameters.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FormEncoder

        public FormEncoder()
    • Method Detail

      • createHttpContent

        public static HttpContent createHttpContent​(java.util.Map<java.lang.String,​java.lang.Object> params)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • createQueryString

        public static java.lang.String createQueryString​(java.util.Map<java.lang.String,​java.lang.Object> params)
        Creates the HTTP query string for a given map of parameters.
        Parameters:
        params - The map of parameters.
        Returns:
        The query string.
      • createQueryString

        public static java.lang.String createQueryString​(java.util.Collection<KeyValuePair<java.lang.String,​java.lang.String>> nameValueCollection)
        Creates the HTTP query string for a collection of name/value tuples.
        Parameters:
        nameValueCollection - The collection of name/value tuples.
        Returns:
        The query string.
      • flattenParams

        public static java.util.List<KeyValuePair<java.lang.String,​java.lang.Object>> flattenParams​(java.util.Map<java.lang.String,​java.lang.Object> params)
        Returns a list of flattened parameters for the given map of parameters.

        This is a "pre-encoding" step necessary to send requests to Stripe's API. Form encoding can be ambiguous when it comes to nested parameters (lists or maps). Stripe's API relies heavily on such parameters and expects them to be encoded in a certain way. This method takes a map of parameters that can contain deeply nested parameters and return a flat list of key/value pairs.

        Values are always encoded as Strings, except for File and InputStream values that are left as-is. When there is at least one File or InputStream value, the request should be encoded using multipart/form-data MIME type; otherwise (i.e. if all values are Strings), the request should be encoded using application/x-www-form-urlencoded MIME type.

        
         Map<String, Object> item1 = new HashMap<>() { put("plan", "gold"); };
         Map<String, Object> item2 = new HashMap<>() { put("plan", "silver"); };
         List<Map<String, Object>> items = new ArrayList<>() { add(item1); add(item2); };
         Map<String, Object> params = new HashMap<>() { put("amount", 234); put("items", items); };
        
         List<KeyValuePair<String, Object>> flattenedParams = FormEncoder.flattenParams(params);
         // flattenedParams is a list of KeyValuePair<String, Object> with 3 elements:
         // 1. key="amount" value="234"
         // 2. key="items[0][plan]" value="gold"
         // 2. key="items[1][plan]" value="silver"
         
        Parameters:
        params - The map of parameters.
        Returns:
        The flattened list of parameters.