1   /**
2    * Copyright (c) 2000-2008 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 String getQueryString(String url) {
87          if (Validator.isNull(url)) {
88              return url;
89          }
90  
91          int pos = url.indexOf(StringPool.QUESTION);
92  
93          if (pos == -1) {
94              return StringPool.BLANK;
95          }
96          else {
97              return url.substring(pos + 1, url.length());
98          }
99      }
100 
101     public static Map parameterMapFromString(String queryString) {
102         Map parameterMap = new LinkedHashMap();
103 
104         if (Validator.isNull(queryString)) {
105             return parameterMap;
106         }
107 
108         StringTokenizer st = new StringTokenizer(
109             queryString, StringPool.AMPERSAND);
110 
111         while (st.hasMoreTokens()) {
112             String token = st.nextToken();
113 
114             if (Validator.isNotNull(token)) {
115                 String[] kvp = StringUtil.split(token, StringPool.EQUAL);
116 
117                 String key = kvp[0];
118 
119                 String value = StringPool.BLANK;
120 
121                 if (kvp.length > 1) {
122                     value = kvp[1];
123                 }
124 
125                 List values = (List)parameterMap.get(key);
126 
127                 if (values == null) {
128                     values = new ArrayList();
129 
130                     parameterMap.put(key, values);
131                 }
132 
133                 values.add(value);
134             }
135         }
136 
137         Iterator itr = parameterMap.entrySet().iterator();
138 
139         while (itr.hasNext()) {
140             Map.Entry entry = (Map.Entry)itr.next();
141 
142             String key = (String)entry.getKey();
143             List values = (List)entry.getValue();
144 
145             parameterMap.put(key, (String[])values.toArray(new String[0]));
146         }
147 
148         return parameterMap;
149     }
150 
151     public static String parameterMapToString(Map parameterMap) {
152         return parameterMapToString(parameterMap, true);
153     }
154 
155     public static String parameterMapToString(
156         Map parameterMap, boolean addQuestion) {
157 
158         StringMaker sm = new StringMaker();
159 
160         if (parameterMap.size() > 0) {
161             if (addQuestion) {
162                 sm.append(StringPool.QUESTION);
163             }
164 
165             Iterator itr = parameterMap.entrySet().iterator();
166 
167             while (itr.hasNext()) {
168                 Map.Entry entry = (Map.Entry)itr.next();
169 
170                 String name = (String)entry.getKey();
171                 Object value = entry.getValue();
172 
173                 String[] values = null;
174 
175                 if (value instanceof String[]) {
176                     values = (String[])entry.getValue();
177                 }
178                 else if (value instanceof String) {
179                     values = new String[] {(String)value};
180                 }
181                 else {
182                     throw new IllegalArgumentException(
183                         "Values of type " + value.getClass() + " are not " +
184                             "supported");
185                 }
186 
187                 for (int i = 0; i < values.length; i++) {
188                     sm.append(name);
189                     sm.append(StringPool.EQUAL);
190                     sm.append(encodeURL(values[i]));
191                     sm.append(StringPool.AMPERSAND);
192                 }
193             }
194 
195             sm.deleteCharAt(sm.length() - 1);
196         }
197 
198         return sm.toString();
199     }
200 
201     private static Log _log = LogFactory.getLog(HttpUtil.class);
202 
203 }