1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util;
24  
25  import com.liferay.portal.kernel.util.StringMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  
30  import java.io.UnsupportedEncodingException;
31  
32  import java.net.URLDecoder;
33  import java.net.URLEncoder;
34  
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.StringTokenizer;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="HttpUtil.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Jorge Ferrer
50   *
51   */
52  public class HttpUtil {
53  
54      public static final String ENCODING = "UTF-8";
55  
56      public static String decodeURL(String url) {
57          if (url == null) {
58              return null;
59          }
60  
61          try {
62              return URLDecoder.decode(url, ENCODING);
63          }
64          catch (UnsupportedEncodingException uee) {
65              _log.error(uee, uee);
66  
67              return StringPool.BLANK;
68          }
69      }
70  
71      public static String encodeURL(String url) {
72          if (url == null) {
73              return null;
74          }
75  
76          try {
77              return URLEncoder.encode(url, ENCODING);
78          }
79          catch (UnsupportedEncodingException uee) {
80              _log.error(uee, uee);
81  
82              return StringPool.BLANK;
83          }
84      }
85  
86      public static Map parameterMapFromString(String queryString) {
87          Map parameterMap = new LinkedHashMap();
88  
89          if (Validator.isNull(queryString)) {
90              return parameterMap;
91          }
92  
93          StringTokenizer st = new StringTokenizer(
94              queryString, StringPool.AMPERSAND);
95  
96          while (st.hasMoreTokens()) {
97              String token = st.nextToken();
98  
99              if (Validator.isNotNull(token)) {
100                 String[] kvp = StringUtil.split(token, StringPool.EQUAL);
101 
102                 String key = kvp[0];
103 
104                 String value = StringPool.BLANK;
105 
106                 if (kvp.length > 1) {
107                     value = kvp[1];
108                 }
109 
110                 List values = (List)parameterMap.get(key);
111 
112                 if (values == null) {
113                     values = new ArrayList();
114 
115                     parameterMap.put(key, values);
116                 }
117 
118                 values.add(value);
119             }
120         }
121 
122         Iterator itr = parameterMap.entrySet().iterator();
123 
124         while (itr.hasNext()) {
125             Map.Entry entry = (Map.Entry)itr.next();
126 
127             String key = (String)entry.getKey();
128             List values = (List)entry.getValue();
129 
130             parameterMap.put(key, (String[])values.toArray(new String[0]));
131         }
132 
133         return parameterMap;
134     }
135 
136     public static String parameterMapToString(Map parameterMap) {
137         return parameterMapToString(parameterMap, true);
138     }
139 
140     public static String parameterMapToString(
141         Map parameterMap, boolean addQuestion) {
142 
143         StringMaker sm = new StringMaker();
144 
145         if (parameterMap.size() > 0) {
146             if (addQuestion) {
147                 sm.append(StringPool.QUESTION);
148             }
149 
150             Iterator itr = parameterMap.entrySet().iterator();
151 
152             while (itr.hasNext()) {
153                 Map.Entry entry = (Map.Entry)itr.next();
154 
155                 String name = (String)entry.getKey();
156                 Object value = entry.getValue();
157 
158                 String[] values = null;
159 
160                 if (value instanceof String[]) {
161                     values = (String[])entry.getValue();
162                 }
163                 else if (value instanceof String) {
164                     values = new String[] {(String)value};
165                 }
166                 else {
167                     throw new IllegalArgumentException(
168                         "Values of type " + value.getClass() + " are not " +
169                             "supported");
170                 }
171 
172                 for (int i = 0; i < values.length; i++) {
173                     sm.append(name);
174                     sm.append(StringPool.EQUAL);
175                     sm.append(encodeURL(values[i]));
176                     sm.append(StringPool.AMPERSAND);
177                 }
178             }
179 
180             sm.deleteCharAt(sm.length() - 1);
181         }
182 
183         return sm.toString();
184     }
185 
186     private static Log _log = LogFactory.getLog(HttpUtil.class);
187 
188 }