View Javadoc

1   package com.atlassian.httpclient.api;
2   
3   import java.util.List;
4   import java.util.Map;
5   
6   /**
7    * Provides access to various entity builders
8    */
9   public final class EntityBuilders {
10      private EntityBuilders() {
11      }
12  
13      /**
14       * Creates a new form entity builder for content-type "application/x-www-form-urlencoded".
15       *
16       * @return The new form builder
17       */
18      public static FormBuilder newForm() {
19          return new DefaultFormBuilder();
20      }
21  
22      /**
23       * Creates a form entity builder for content-type "application/x-www-form-urlencoded"
24       * from a parameter map.
25       *
26       * @param params The parameter map
27       * @return The new form builder
28       */
29      FormBuilder newFormWithParams(Map<String, String> params) {
30          FormBuilder form = newForm();
31          for (Map.Entry<String, String> param : params.entrySet()) {
32              form.addParam(param.getKey(), param.getValue());
33          }
34          return form;
35      }
36  
37      /**
38       * Creates a form entity builder for content-type "application/x-www-form-urlencoded"
39       * from a multi-valued parameter map.
40       *
41       * @param params The multi-valued parameter map
42       * @return The new form builder
43       */
44      FormBuilder newFormWithListParams(Map<String, List<String>> params) {
45          FormBuilder form = newForm();
46          for (Map.Entry<String, List<String>> param : params.entrySet()) {
47              String key = param.getKey();
48              List<String> values = param.getValue();
49              if (values != null && values.size() > 0) {
50                  for (String value : values) {
51                      form.addParam(key, value);
52                  }
53              } else {
54                  form.addParam(key);
55              }
56          }
57          return form;
58      }
59  }