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 String path = null;
201 String queryString = null;
202
203 int pos = uri.indexOf('?');
204
205 if (pos != -1) {
206 path = uri.substring(0, pos);
207 queryString = uri.substring(pos + 1);
208 }
209 else {
210 path = uri;
211 }
212
213 String[] uriParts = StringUtil.split(
214 path.substring(1), StringPool.SLASH);
215
216 List<String> parts = new ArrayList<String>(uriParts.length);
217
218 for (int i = 0; i < uriParts.length; i++) {
219 String curUriPart = URLCodec.decodeURL(uriParts[i]);
220 String prevUriPart = null;
221
222 if (i > 0) {
223 prevUriPart = URLCodec.decodeURL(uriParts[i - 1]);
224 }
225
226 if (curUriPart.equals(StringPool.DOUBLE_PERIOD)) {
227 if (!prevUriPart.equals(StringPool.PERIOD)) {
228 parts.remove(parts.size() - 1);
229 }
230 }
231 else if ((curUriPart.length() > 0) &&
232 !curUriPart.equals(StringPool.PERIOD)) {
233
234 parts.add(URLCodec.encodeURL(curUriPart));
235 }
236 }
237
238 StringBundler sb = new StringBundler(parts.size() * 2 + 2);
239
240 for (String part : parts) {
241 sb.append(StringPool.SLASH);
242 sb.append(part);
243 }
244
245 if (Validator.isNotNull(queryString)) {
246 sb.append(StringPool.QUESTION);
247 sb.append(queryString);
248 }
249
250 return sb.toString();
251 }
252
253 public static Map<String, String[]> parameterMapFromString(
254 String queryString) {
255
256 return getHttp().parameterMapFromString(queryString);
257 }
258
259 public static String parameterMapToString(
260 Map<String, String[]> parameterMap) {
261
262 return getHttp().parameterMapToString(parameterMap);
263 }
264
265 public static String parameterMapToString(
266 Map<String, String[]> parameterMap, boolean addQuestion) {
267
268 return getHttp().parameterMapToString(parameterMap, addQuestion);
269 }
270
271 public static String protocolize(String url, ActionRequest actionRequest) {
272 return getHttp().protocolize(url, actionRequest);
273 }
274
275 public static String protocolize(String url, boolean secure) {
276 return getHttp().protocolize(url, secure);
277 }
278
279 public static String protocolize(String url, HttpServletRequest request) {
280 return getHttp().protocolize(url, request);
281 }
282
283 public static String protocolize(String url, int port, boolean secure) {
284 return getHttp().protocolize(url, port, secure);
285 }
286
287 public static String protocolize(String url, RenderRequest renderRequest) {
288 return getHttp().protocolize(url, renderRequest);
289 }
290
291 public static String removeDomain(String url) {
292 return getHttp().removeDomain(url);
293 }
294
295 public static String removeParameter(String url, String name) {
296 return getHttp().removeParameter(url, name);
297 }
298
299 public static String removePathParameters(String uri) {
300 if (Validator.isNull(uri)) {
301 return uri;
302 }
303
304 int pos = uri.indexOf(StringPool.SEMICOLON);
305
306 if (pos == -1) {
307 return uri;
308 }
309
310 String[] uriParts = StringUtil.split(
311 uri.substring(1), StringPool.SLASH);
312
313 StringBundler sb = new StringBundler(uriParts.length * 2);
314
315 for (String uriPart : uriParts) {
316 pos = uriPart.indexOf(StringPool.SEMICOLON);
317
318 if (pos == -1) {
319 sb.append(StringPool.SLASH);
320 sb.append(uriPart);
321 }
322 else if (pos == 0) {
323 continue;
324 }
325 else {
326 sb.append(StringPool.SLASH);
327 sb.append(uriPart.substring(0, pos));
328 }
329 }
330
331 if (sb.length() == 0) {
332 return StringPool.SLASH;
333 }
334
335 return sb.toString();
336 }
337
338 public static String removeProtocol(String url) {
339 return getHttp().removeProtocol(url);
340 }
341
342 public static String sanitizeHeader(String header) {
343 return getHttp().sanitizeHeader(header);
344 }
345
346 public static String setParameter(String url, String name, boolean value) {
347 return getHttp().setParameter(url, name, value);
348 }
349
350 public static String setParameter(String url, String name, double value) {
351 return getHttp().setParameter(url, name, value);
352 }
353
354 public static String setParameter(String url, String name, int value) {
355 return getHttp().setParameter(url, name, value);
356 }
357
358 public static String setParameter(String url, String name, long value) {
359 return getHttp().setParameter(url, name, value);
360 }
361
362 public static String setParameter(String url, String name, short value) {
363 return getHttp().setParameter(url, name, value);
364 }
365
366 public static String setParameter(String url, String name, String value) {
367 return getHttp().setParameter(url, name, value);
368 }
369
370 public static byte[] URLtoByteArray(Http.Options options)
371 throws IOException {
372
373 PortalSocketPermission.checkConnect(options);
374
375 return getHttp().URLtoByteArray(options);
376 }
377
378 public static byte[] URLtoByteArray(String location) throws IOException {
379 PortalSocketPermission.checkConnect(location);
380
381 return getHttp().URLtoByteArray(location);
382 }
383
384 public static byte[] URLtoByteArray(String location, boolean post)
385 throws IOException {
386
387 PortalSocketPermission.checkConnect(location);
388
389 return getHttp().URLtoByteArray(location, post);
390 }
391
392 public static String URLtoString(Http.Options options) throws IOException {
393 PortalSocketPermission.checkConnect(options);
394
395 return getHttp().URLtoString(options);
396 }
397
398 public static String URLtoString(String location) throws IOException {
399 PortalSocketPermission.checkConnect(location);
400
401 return getHttp().URLtoString(location);
402 }
403
404 public static String URLtoString(String location, boolean post)
405 throws IOException {
406
407 PortalSocketPermission.checkConnect(location);
408
409 return getHttp().URLtoString(location, post);
410 }
411
412
423 public static String URLtoString(URL url) throws IOException {
424 PortalSocketPermission.checkConnect(url);
425
426 return getHttp().URLtoString(url);
427 }
428
429 public void setHttp(Http http) {
430 PortalRuntimePermission.checkSetBeanProperty(getClass());
431
432 _http = http;
433 }
434
435 private static Http _http;
436
437 }