001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
018 import com.liferay.portal.kernel.security.pacl.permission.PortalSocketPermission;
019
020 import java.io.IOException;
021
022 import java.net.URL;
023
024 import java.util.ArrayList;
025 import java.util.List;
026 import java.util.Map;
027
028 import javax.portlet.ActionRequest;
029 import javax.portlet.RenderRequest;
030
031 import javax.servlet.http.Cookie;
032 import javax.servlet.http.HttpServletRequest;
033
034
037 public class HttpUtil {
038
039 public static String addParameter(String url, String name, boolean value) {
040 return getHttp().addParameter(url, name, value);
041 }
042
043 public static String addParameter(String url, String name, double value) {
044 return getHttp().addParameter(url, name, value);
045 }
046
047 public static String addParameter(String url, String name, int value) {
048 return getHttp().addParameter(url, name, value);
049 }
050
051 public static String addParameter(String url, String name, long value) {
052 return getHttp().addParameter(url, name, value);
053 }
054
055 public static String addParameter(String url, String name, short value) {
056 return getHttp().addParameter(url, name, value);
057 }
058
059 public static String addParameter(String url, String name, String value) {
060 return getHttp().addParameter(url, name, value);
061 }
062
063 public static String decodePath(String path) {
064 return getHttp().decodePath(path);
065 }
066
067 public static String decodeURL(String url) {
068 return getHttp().decodeURL(url);
069 }
070
071 public static String decodeURL(String url, boolean unescapeSpaces) {
072 return getHttp().decodeURL(url, unescapeSpaces);
073 }
074
075 public static String encodeParameters(String url) {
076 return getHttp().encodeParameters(url);
077 }
078
079 public static String encodePath(String path) {
080 return getHttp().encodePath(path);
081 }
082
083 public static String encodeURL(String url) {
084 return getHttp().encodeURL(url);
085 }
086
087 public static String encodeURL(String url, boolean escapeSpaces) {
088 return getHttp().encodeURL(url, escapeSpaces);
089 }
090
091 public static String fixPath(String path) {
092 return getHttp().fixPath(path);
093 }
094
095 public static String fixPath(
096 String path, boolean leading, boolean trailing) {
097
098 return getHttp().fixPath(path, leading, trailing);
099 }
100
101 public static String getCompleteURL(HttpServletRequest request) {
102 return getHttp().getCompleteURL(request);
103 }
104
105 public static Cookie[] getCookies() {
106 return getHttp().getCookies();
107 }
108
109 public static String getDomain(String url) {
110 return getHttp().getDomain(url);
111 }
112
113 public static Http getHttp() {
114 PortalRuntimePermission.checkGetBeanProperty(HttpUtil.class);
115
116 return _http;
117 }
118
119 public static String getIpAddress(String url) {
120 return getHttp().getIpAddress(url);
121 }
122
123 public static String getParameter(String url, String name) {
124 return getHttp().getParameter(url, name);
125 }
126
127 public static String getParameter(
128 String url, String name, boolean escaped) {
129
130 return getHttp().getParameter(url, name, escaped);
131 }
132
133 public static Map<String, String[]> getParameterMap(String queryString) {
134 return getHttp().getParameterMap(queryString);
135 }
136
137 public static String getPath(String url) {
138 return getHttp().getPath(url);
139 }
140
141 public static String getProtocol(ActionRequest actionRequest) {
142 return getHttp().getProtocol(actionRequest);
143 }
144
145 public static String getProtocol(boolean secure) {
146 return getHttp().getProtocol(secure);
147 }
148
149 public static String getProtocol(HttpServletRequest request) {
150 return getHttp().getProtocol(request);
151 }
152
153 public static String getProtocol(RenderRequest renderRequest) {
154 return getHttp().getProtocol(renderRequest);
155 }
156
157 public static String getProtocol(String url) {
158 return getHttp().getProtocol(url);
159 }
160
161 public static String getQueryString(String url) {
162 return getHttp().getQueryString(url);
163 }
164
165 public static String getRequestURL(HttpServletRequest request) {
166 return getHttp().getRequestURL(request);
167 }
168
169 public static boolean hasDomain(String url) {
170 return getHttp().hasDomain(url);
171 }
172
173 public static boolean hasProtocol(String url) {
174 return getHttp().hasProtocol(url);
175 }
176
177 public static boolean hasProxyConfig() {
178 return getHttp().hasProxyConfig();
179 }
180
181 public static boolean isNonProxyHost(String host) {
182 return getHttp().isNonProxyHost(host);
183 }
184
185 public static boolean isProxyHost(String host) {
186 return getHttp().isProxyHost(host);
187 }
188
189 public static boolean isSecure(String url) {
190 return getHttp().isSecure(url);
191 }
192
193 public static String normalizePath(String uri) {
194 if (Validator.isNull(uri)) {
195 return uri;
196 }
197
198 uri = removePathParameters(uri);
199
200 for (int i = 0; i < uri.length(); i++) {
201 char c = uri.charAt(i);
202
203 if ((c == CharPool.PERCENT) || (c == CharPool.PERIOD) ||
204 ((c == CharPool.SLASH) && ((i + 1) < uri.length()) &&
205 (uri.charAt(i + 1) == CharPool.SLASH))) {
206
207 break;
208 }
209
210 if (i == (uri.length() - 1)) {
211 if (c == CharPool.QUESTION) {
212 return uri.substring(0, uri.length() - 1);
213 }
214
215 return uri;
216 }
217 }
218
219 String path = null;
220 String queryString = null;
221
222 int pos = uri.indexOf('?');
223
224 if (pos != -1) {
225 path = uri.substring(0, pos);
226 queryString = uri.substring(pos + 1);
227 }
228 else {
229 path = uri;
230 }
231
232 String[] uriParts = StringUtil.split(
233 path.substring(1), StringPool.SLASH);
234
235 List<String> parts = new ArrayList<String>(uriParts.length);
236
237 for (int i = 0; i < uriParts.length; i++) {
238 String curUriPart = URLCodec.decodeURL(uriParts[i]);
239 String prevUriPart = null;
240
241 if (i > 0) {
242 prevUriPart = URLCodec.decodeURL(uriParts[i - 1]);
243 }
244
245 if (curUriPart.equals(StringPool.DOUBLE_PERIOD)) {
246 if (!prevUriPart.equals(StringPool.PERIOD)) {
247 parts.remove(parts.size() - 1);
248 }
249 }
250 else if ((curUriPart.length() > 0) &&
251 !curUriPart.equals(StringPool.PERIOD)) {
252
253 parts.add(URLCodec.encodeURL(curUriPart));
254 }
255 }
256
257 if (parts.isEmpty()) {
258 return StringPool.SLASH;
259 }
260
261 StringBundler sb = new StringBundler(parts.size() * 2 + 2);
262
263 for (String part : parts) {
264 sb.append(StringPool.SLASH);
265 sb.append(part);
266 }
267
268 if (Validator.isNotNull(queryString)) {
269 sb.append(StringPool.QUESTION);
270 sb.append(queryString);
271 }
272
273 return sb.toString();
274 }
275
276 public static Map<String, String[]> parameterMapFromString(
277 String queryString) {
278
279 return getHttp().parameterMapFromString(queryString);
280 }
281
282 public static String parameterMapToString(
283 Map<String, String[]> parameterMap) {
284
285 return getHttp().parameterMapToString(parameterMap);
286 }
287
288 public static String parameterMapToString(
289 Map<String, String[]> parameterMap, boolean addQuestion) {
290
291 return getHttp().parameterMapToString(parameterMap, addQuestion);
292 }
293
294 public static String protocolize(String url, ActionRequest actionRequest) {
295 return getHttp().protocolize(url, actionRequest);
296 }
297
298 public static String protocolize(String url, boolean secure) {
299 return getHttp().protocolize(url, secure);
300 }
301
302 public static String protocolize(String url, HttpServletRequest request) {
303 return getHttp().protocolize(url, request);
304 }
305
306 public static String protocolize(String url, int port, boolean secure) {
307 return getHttp().protocolize(url, port, secure);
308 }
309
310 public static String protocolize(String url, RenderRequest renderRequest) {
311 return getHttp().protocolize(url, renderRequest);
312 }
313
314 public static String removeDomain(String url) {
315 return getHttp().removeDomain(url);
316 }
317
318 public static String removeParameter(String url, String name) {
319 return getHttp().removeParameter(url, name);
320 }
321
322 public static String removePathParameters(String uri) {
323 if (Validator.isNull(uri)) {
324 return uri;
325 }
326
327 int pos = uri.indexOf(StringPool.SEMICOLON);
328
329 if (pos == -1) {
330 return uri;
331 }
332
333 if (pos == 0) {
334 throw new IllegalArgumentException("Unable to handle URI: " + uri);
335 }
336
337 String[] uriParts = StringUtil.split(
338 uri.substring(1), StringPool.SLASH);
339
340 StringBundler sb = new StringBundler(uriParts.length * 2);
341
342 for (String uriPart : uriParts) {
343 pos = uriPart.indexOf(StringPool.SEMICOLON);
344
345 if (pos == -1) {
346 sb.append(StringPool.SLASH);
347 sb.append(uriPart);
348 }
349 else if (pos == 0) {
350 continue;
351 }
352 else {
353 sb.append(StringPool.SLASH);
354 sb.append(uriPart.substring(0, pos));
355 }
356 }
357
358 if (sb.length() == 0) {
359 return StringPool.SLASH;
360 }
361
362 return sb.toString();
363 }
364
365 public static String removeProtocol(String url) {
366 return getHttp().removeProtocol(url);
367 }
368
369 public static String sanitizeHeader(String header) {
370 return getHttp().sanitizeHeader(header);
371 }
372
373 public static String setParameter(String url, String name, boolean value) {
374 return getHttp().setParameter(url, name, value);
375 }
376
377 public static String setParameter(String url, String name, double value) {
378 return getHttp().setParameter(url, name, value);
379 }
380
381 public static String setParameter(String url, String name, int value) {
382 return getHttp().setParameter(url, name, value);
383 }
384
385 public static String setParameter(String url, String name, long value) {
386 return getHttp().setParameter(url, name, value);
387 }
388
389 public static String setParameter(String url, String name, short value) {
390 return getHttp().setParameter(url, name, value);
391 }
392
393 public static String setParameter(String url, String name, String value) {
394 return getHttp().setParameter(url, name, value);
395 }
396
397 public static String shortenURL(String url, int count) {
398 return getHttp().shortenURL(url, count);
399 }
400
401 public static byte[] URLtoByteArray(Http.Options options)
402 throws IOException {
403
404 PortalSocketPermission.checkConnect(options);
405
406 return getHttp().URLtoByteArray(options);
407 }
408
409 public static byte[] URLtoByteArray(String location) throws IOException {
410 PortalSocketPermission.checkConnect(location);
411
412 return getHttp().URLtoByteArray(location);
413 }
414
415 public static byte[] URLtoByteArray(String location, boolean post)
416 throws IOException {
417
418 PortalSocketPermission.checkConnect(location);
419
420 return getHttp().URLtoByteArray(location, post);
421 }
422
423 public static String URLtoString(Http.Options options) throws IOException {
424 PortalSocketPermission.checkConnect(options);
425
426 return getHttp().URLtoString(options);
427 }
428
429 public static String URLtoString(String location) throws IOException {
430 PortalSocketPermission.checkConnect(location);
431
432 return getHttp().URLtoString(location);
433 }
434
435 public static String URLtoString(String location, boolean post)
436 throws IOException {
437
438 PortalSocketPermission.checkConnect(location);
439
440 return getHttp().URLtoString(location, post);
441 }
442
443
454 public static String URLtoString(URL url) throws IOException {
455 PortalSocketPermission.checkConnect(url);
456
457 return getHttp().URLtoString(url);
458 }
459
460 public void setHttp(Http http) {
461 PortalRuntimePermission.checkSetBeanProperty(getClass());
462
463 _http = http;
464 }
465
466 private static Http _http;
467
468 }