/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.kernel.util;

import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
import com.liferay.portal.kernel.security.pacl.permission.PortalSocketPermission;

import java.io.IOException;

import java.net.URL;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.RenderRequest;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Brian Wing Shun Chan
 */
public class HttpUtil {

	public static String addParameter(String url, String name, boolean value) {
		return getHttp().addParameter(url, name, value);
	}

	public static String addParameter(String url, String name, double value) {
		return getHttp().addParameter(url, name, value);
	}

	public static String addParameter(String url, String name, int value) {
		return getHttp().addParameter(url, name, value);
	}

	public static String addParameter(String url, String name, long value) {
		return getHttp().addParameter(url, name, value);
	}

	public static String addParameter(String url, String name, short value) {
		return getHttp().addParameter(url, name, value);
	}

	public static String addParameter(String url, String name, String value) {
		return getHttp().addParameter(url, name, value);
	}

	public static String decodePath(String path) {
		return getHttp().decodePath(path);
	}

	public static String decodeURL(String url) {
		return getHttp().decodeURL(url);
	}

	public static String decodeURL(String url, boolean unescapeSpaces) {
		return getHttp().decodeURL(url, unescapeSpaces);
	}

	public static String encodeParameters(String url) {
		return getHttp().encodeParameters(url);
	}

	public static String encodePath(String path) {
		return getHttp().encodePath(path);
	}

	public static String encodeURL(String url) {
		return getHttp().encodeURL(url);
	}

	public static String encodeURL(String url, boolean escapeSpaces) {
		return getHttp().encodeURL(url, escapeSpaces);
	}

	public static String fixPath(String path) {
		return getHttp().fixPath(path);
	}

	public static String fixPath(
		String path, boolean leading, boolean trailing) {

		return getHttp().fixPath(path, leading, trailing);
	}

	public static String getCompleteURL(HttpServletRequest request) {
		return getHttp().getCompleteURL(request);
	}

	public static Cookie[] getCookies() {
		return getHttp().getCookies();
	}

	public static String getDomain(String url) {
		return getHttp().getDomain(url);
	}

	public static Http getHttp() {
		PortalRuntimePermission.checkGetBeanProperty(HttpUtil.class);

		return _http;
	}

	public static String getIpAddress(String url) {
		return getHttp().getIpAddress(url);
	}

	public static String getParameter(String url, String name) {
		return getHttp().getParameter(url, name);
	}

	public static String getParameter(
		String url, String name, boolean escaped) {

		return getHttp().getParameter(url, name, escaped);
	}

	public static Map<String, String[]> getParameterMap(String queryString) {
		return getHttp().getParameterMap(queryString);
	}

	public static String getPath(String url) {
		return getHttp().getPath(url);
	}

	public static String getProtocol(ActionRequest actionRequest) {
		return getHttp().getProtocol(actionRequest);
	}

	public static String getProtocol(boolean secure) {
		return getHttp().getProtocol(secure);
	}

	public static String getProtocol(HttpServletRequest request) {
		return getHttp().getProtocol(request);
	}

	public static String getProtocol(RenderRequest renderRequest) {
		return getHttp().getProtocol(renderRequest);
	}

	public static String getProtocol(String url) {
		return getHttp().getProtocol(url);
	}

	public static String getQueryString(String url) {
		return getHttp().getQueryString(url);
	}

	public static String getRequestURL(HttpServletRequest request) {
		return getHttp().getRequestURL(request);
	}

	public static boolean hasDomain(String url) {
		return getHttp().hasDomain(url);
	}

	public static boolean hasProtocol(String url) {
		return getHttp().hasProtocol(url);
	}

	public static boolean hasProxyConfig() {
		return getHttp().hasProxyConfig();
	}

	public static boolean isNonProxyHost(String host) {
		return getHttp().isNonProxyHost(host);
	}

	public static boolean isProxyHost(String host) {
		return getHttp().isProxyHost(host);
	}

	public static boolean isSecure(String url) {
		return getHttp().isSecure(url);
	}

	public static String normalizePath(String uri) {
		if (Validator.isNull(uri)) {
			return uri;
		}

		uri = removePathParameters(uri);

		for (int i = 0; i < uri.length(); i++) {
			char c = uri.charAt(i);

			if ((c == CharPool.PERCENT) || (c == CharPool.PERIOD) ||
				((c == CharPool.SLASH) && ((i + 1) < uri.length()) &&
				 (uri.charAt(i + 1) == CharPool.SLASH))) {

				break;
			}

			if (i == (uri.length() - 1)) {
				if (c == CharPool.QUESTION) {
					return uri.substring(0, uri.length() - 1);
				}

				return uri;
			}
		}

		String path = null;
		String queryString = null;

		int pos = uri.indexOf('?');

		if (pos != -1) {
			path = uri.substring(0, pos);
			queryString = uri.substring(pos + 1);
		}
		else {
			path = uri;
		}

		String[] uriParts = StringUtil.split(
			path.substring(1), StringPool.SLASH);

		List<String> parts = new ArrayList<String>(uriParts.length);

		for (int i = 0; i < uriParts.length; i++) {
			String curUriPart = URLCodec.decodeURL(uriParts[i]);
			String prevUriPart = null;

			if (i > 0) {
				prevUriPart = URLCodec.decodeURL(uriParts[i - 1]);
			}

			if (curUriPart.equals(StringPool.DOUBLE_PERIOD)) {
				if (!prevUriPart.equals(StringPool.PERIOD)) {
					parts.remove(parts.size() - 1);
				}
			}
			else if ((curUriPart.length() > 0) &&
					 !curUriPart.equals(StringPool.PERIOD)) {

				parts.add(URLCodec.encodeURL(curUriPart));
			}
		}

		if (parts.isEmpty()) {
			return StringPool.SLASH;
		}

		StringBundler sb = new StringBundler(parts.size() * 2 + 2);

		for (String part : parts) {
			sb.append(StringPool.SLASH);
			sb.append(part);
		}

		if (Validator.isNotNull(queryString)) {
			sb.append(StringPool.QUESTION);
			sb.append(queryString);
		}

		return sb.toString();
	}

	public static Map<String, String[]> parameterMapFromString(
		String queryString) {

		return getHttp().parameterMapFromString(queryString);
	}

	public static String parameterMapToString(
		Map<String, String[]> parameterMap) {

		return getHttp().parameterMapToString(parameterMap);
	}

	public static String parameterMapToString(
		Map<String, String[]> parameterMap, boolean addQuestion) {

		return getHttp().parameterMapToString(parameterMap, addQuestion);
	}

	public static String protocolize(String url, ActionRequest actionRequest) {
		return getHttp().protocolize(url, actionRequest);
	}

	public static String protocolize(String url, boolean secure) {
		return getHttp().protocolize(url, secure);
	}

	public static String protocolize(String url, HttpServletRequest request) {
		return getHttp().protocolize(url, request);
	}

	public static String protocolize(String url, int port, boolean secure) {
		return getHttp().protocolize(url, port, secure);
	}

	public static String protocolize(String url, RenderRequest renderRequest) {
		return getHttp().protocolize(url, renderRequest);
	}

	public static String removeDomain(String url) {
		return getHttp().removeDomain(url);
	}

	public static String removeParameter(String url, String name) {
		return getHttp().removeParameter(url, name);
	}

	public static String removePathParameters(String uri) {
		if (Validator.isNull(uri)) {
			return uri;
		}

		int pos = uri.indexOf(StringPool.SEMICOLON);

		if (pos == -1) {
			return uri;
		}

		if (pos == 0) {
			throw new IllegalArgumentException("Unable to handle URI: " + uri);
		}

		String[] uriParts = StringUtil.split(
			uri.substring(1), StringPool.SLASH);

		StringBundler sb = new StringBundler(uriParts.length * 2);

		for (String uriPart : uriParts) {
			pos = uriPart.indexOf(StringPool.SEMICOLON);

			if (pos == -1) {
				sb.append(StringPool.SLASH);
				sb.append(uriPart);
			}
			else if (pos == 0) {
				continue;
			}
			else {
				sb.append(StringPool.SLASH);
				sb.append(uriPart.substring(0, pos));
			}
		}

		if (sb.length() == 0) {
			return StringPool.SLASH;
		}

		return sb.toString();
	}

	public static String removeProtocol(String url) {
		return getHttp().removeProtocol(url);
	}

	public static String sanitizeHeader(String header) {
		return getHttp().sanitizeHeader(header);
	}

	public static String setParameter(String url, String name, boolean value) {
		return getHttp().setParameter(url, name, value);
	}

	public static String setParameter(String url, String name, double value) {
		return getHttp().setParameter(url, name, value);
	}

	public static String setParameter(String url, String name, int value) {
		return getHttp().setParameter(url, name, value);
	}

	public static String setParameter(String url, String name, long value) {
		return getHttp().setParameter(url, name, value);
	}

	public static String setParameter(String url, String name, short value) {
		return getHttp().setParameter(url, name, value);
	}

	public static String setParameter(String url, String name, String value) {
		return getHttp().setParameter(url, name, value);
	}

	public static String shortenURL(String url, int count) {
		return getHttp().shortenURL(url, count);
	}

	public static byte[] URLtoByteArray(Http.Options options)
		throws IOException {

		PortalSocketPermission.checkConnect(options);

		return getHttp().URLtoByteArray(options);
	}

	public static byte[] URLtoByteArray(String location) throws IOException {
		PortalSocketPermission.checkConnect(location);

		return getHttp().URLtoByteArray(location);
	}

	public static byte[] URLtoByteArray(String location, boolean post)
		throws IOException {

		PortalSocketPermission.checkConnect(location);

		return getHttp().URLtoByteArray(location, post);
	}

	public static String URLtoString(Http.Options options) throws IOException {
		PortalSocketPermission.checkConnect(options);

		return getHttp().URLtoString(options);
	}

	public static String URLtoString(String location) throws IOException {
		PortalSocketPermission.checkConnect(location);

		return getHttp().URLtoString(location);
	}

	public static String URLtoString(String location, boolean post)
		throws IOException {

		PortalSocketPermission.checkConnect(location);

		return getHttp().URLtoString(location, post);
	}

	/**
	 * This method only uses the default Commons HttpClient implementation when
	 * the URL object represents a HTTP resource. The URL object could also
	 * represent a file or some JNDI resource. In that case, the default Java
	 * implementation is used.
	 *
	 * @param  url the URL
	 * @return A string representation of the resource referenced by the URL
	 *         object
	 * @throws IOException if an IO Exception occurred
	 */
	public static String URLtoString(URL url) throws IOException {
		PortalSocketPermission.checkConnect(url);

		return getHttp().URLtoString(url);
	}

	public void setHttp(Http http) {
		PortalRuntimePermission.checkSetBeanProperty(getClass());

		_http = http;
	}

	private static Http _http;

}