001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.kernel.util.Validator;
027:
028: import java.io.UnsupportedEncodingException;
029:
030: import java.net.URLDecoder;
031: import java.net.URLEncoder;
032:
033: import java.util.ArrayList;
034: import java.util.Iterator;
035: import java.util.LinkedHashMap;
036: import java.util.List;
037: import java.util.Map;
038: import java.util.StringTokenizer;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042:
043: /**
044: * <a href="HttpUtil.java.html"><b><i>View Source</i></b></a>
045: *
046: * @author Brian Wing Shun Chan
047: * @author Jorge Ferrer
048: *
049: */
050: public class HttpUtil {
051:
052: public static final String ENCODING = "UTF-8";
053:
054: public static String decodeURL(String url) {
055: if (url == null) {
056: return null;
057: }
058:
059: try {
060: return URLDecoder.decode(url, ENCODING);
061: } catch (UnsupportedEncodingException uee) {
062: _log.error(uee, uee);
063:
064: return StringPool.BLANK;
065: }
066: }
067:
068: public static String encodeURL(String url) {
069: if (url == null) {
070: return null;
071: }
072:
073: try {
074: return URLEncoder.encode(url, ENCODING);
075: } catch (UnsupportedEncodingException uee) {
076: _log.error(uee, uee);
077:
078: return StringPool.BLANK;
079: }
080: }
081:
082: public static String getQueryString(String url) {
083: if (Validator.isNull(url)) {
084: return url;
085: }
086:
087: int pos = url.indexOf(StringPool.QUESTION);
088:
089: if (pos == -1) {
090: return StringPool.BLANK;
091: } else {
092: return url.substring(pos + 1, url.length());
093: }
094: }
095:
096: public static Map parameterMapFromString(String queryString) {
097: Map parameterMap = new LinkedHashMap();
098:
099: if (Validator.isNull(queryString)) {
100: return parameterMap;
101: }
102:
103: StringTokenizer st = new StringTokenizer(queryString,
104: StringPool.AMPERSAND);
105:
106: while (st.hasMoreTokens()) {
107: String token = st.nextToken();
108:
109: if (Validator.isNotNull(token)) {
110: String[] kvp = StringUtil
111: .split(token, StringPool.EQUAL);
112:
113: String key = kvp[0];
114:
115: String value = StringPool.BLANK;
116:
117: if (kvp.length > 1) {
118: value = kvp[1];
119: }
120:
121: List values = (List) parameterMap.get(key);
122:
123: if (values == null) {
124: values = new ArrayList();
125:
126: parameterMap.put(key, values);
127: }
128:
129: values.add(value);
130: }
131: }
132:
133: Iterator itr = parameterMap.entrySet().iterator();
134:
135: while (itr.hasNext()) {
136: Map.Entry entry = (Map.Entry) itr.next();
137:
138: String key = (String) entry.getKey();
139: List values = (List) entry.getValue();
140:
141: parameterMap.put(key, (String[]) values
142: .toArray(new String[0]));
143: }
144:
145: return parameterMap;
146: }
147:
148: public static String parameterMapToString(Map parameterMap) {
149: return parameterMapToString(parameterMap, true);
150: }
151:
152: public static String parameterMapToString(Map parameterMap,
153: boolean addQuestion) {
154:
155: StringMaker sm = new StringMaker();
156:
157: if (parameterMap.size() > 0) {
158: if (addQuestion) {
159: sm.append(StringPool.QUESTION);
160: }
161:
162: Iterator itr = parameterMap.entrySet().iterator();
163:
164: while (itr.hasNext()) {
165: Map.Entry entry = (Map.Entry) itr.next();
166:
167: String name = (String) entry.getKey();
168: Object value = entry.getValue();
169:
170: String[] values = null;
171:
172: if (value instanceof String[]) {
173: values = (String[]) entry.getValue();
174: } else if (value instanceof String) {
175: values = new String[] { (String) value };
176: } else {
177: throw new IllegalArgumentException(
178: "Values of type " + value.getClass()
179: + " are not " + "supported");
180: }
181:
182: for (int i = 0; i < values.length; i++) {
183: sm.append(name);
184: sm.append(StringPool.EQUAL);
185: sm.append(encodeURL(values[i]));
186: sm.append(StringPool.AMPERSAND);
187: }
188: }
189:
190: sm.deleteCharAt(sm.length() - 1);
191: }
192:
193: return sm.toString();
194: }
195:
196: private static Log _log = LogFactory.getLog(HttpUtil.class);
197:
198: }
|