View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.caravan.io.http.impl;
21  
22  import java.io.UnsupportedEncodingException;
23  import java.net.URLDecoder;
24  import java.net.URLEncoder;
25  import java.util.Collection;
26  import java.util.LinkedHashMap;
27  import java.util.Map;
28  
29  import org.apache.commons.lang3.StringUtils;
30  
31  import com.google.common.base.Charsets;
32  
33  /**
34   * Helper class for standard URL tasks.
35   */
36  public final class CaravanHttpHelper {
37  
38    private CaravanHttpHelper() {
39      // nothing to do
40    }
41  
42    /**
43     * @param token Token to decode
44     * @return Decoded token
45     */
46    public static String urlDecode(final String token) {
47      try {
48        return URLDecoder.decode(token, Charsets.UTF_8.name());
49      }
50      catch (UnsupportedEncodingException e) {
51        throw new RuntimeException(e);
52      }
53    }
54  
55    /**
56     * @param token Token to encode
57     * @return Encoded token
58     */
59    public static String urlEncode(final Object token) {
60      try {
61        return URLEncoder.encode(String.valueOf(token), Charsets.UTF_8.name());
62      }
63      catch (UnsupportedEncodingException e) {
64        throw new RuntimeException(e);
65      }
66    }
67  
68    /**
69     * Constructs a Map from a multi-value header (i.e. one that can have multiple values that either split up into
70     * multiple lines with the same name, or given as a comma-separated list of values in a single header line)
71     * For example, see the following header:
72     *
73     * <pre>
74     * Cache-Control: public, max-age=120
75     * </pre>
76     *
77     * That will return a map with two entries: ("public" -&gt; "true", "max-age" -&gt; "120")
78     * The following header will result in the same map.
79     *
80     * <pre>
81     * Cache-Control: public
82     * Cache-Control: max-age=120
83     * </pre>
84     * @param header the collection of header values (one entry for each line)
85     * @return Header map
86     */
87    public static Map<String, String> convertMultiValueHeaderToMap(final Collection<String> header) {
88      // we use linked hash map, because the order of entries is important
89      Map<String, String> headerMap = new LinkedHashMap<>();
90      for (String line : header) {
91        String[] tokens = StringUtils.split(line, ',');
92        for (String token : tokens) {
93          String[] keyValue = StringUtils.split(token, '=');
94          headerMap.put(StringUtils.trim(keyValue[0]), keyValue.length == 1 ? "true" : StringUtils.trim(keyValue[1]));
95        }
96      }
97      return headerMap;
98    }
99  
100 }