/**
 * 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.upload;

import com.liferay.portal.kernel.upload.FileItem;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.upload.UploadServletRequest;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.MimeTypesUtil;
import com.liferay.portal.kernel.util.Validator;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.portlet.PortletRequest;

import javax.servlet.http.HttpServletRequestWrapper;

/**
 * @author Brian Wing Shun Chan
 * @author Harry Mark
 */
public class UploadPortletRequestImpl
	extends HttpServletRequestWrapper implements UploadPortletRequest {

	/**
	 * @deprecated As of 7.0.0, replaced by {@link #UploadPortletRequestImpl(
	 *             UploadServletRequest, PortletRequest, String)}
	 */
	public UploadPortletRequestImpl(
		UploadServletRequest uploadServletRequest, String namespace) {

		super(uploadServletRequest);

		_uploadServletRequest = uploadServletRequest;
		_namespace = namespace;
	}

	public UploadPortletRequestImpl(
		UploadServletRequest uploadServletRequest,
		PortletRequest portletRequest, String namespace) {

		super(uploadServletRequest);

		_uploadServletRequest = uploadServletRequest;
		_portletRequest = portletRequest;
		_namespace = namespace;
	}

	@Override
	public void cleanUp() {
		_uploadServletRequest.cleanUp();
	}

	@Override
	public String getContentType(String name) {
		String contentType = _uploadServletRequest.getContentType(
			_namespace.concat(name));

		if (contentType == null) {
			contentType = _uploadServletRequest.getContentType(name);
		}

		if (Validator.isNull(contentType) ||
			contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {

			contentType = MimeTypesUtil.getContentType(getFile(name));
		}

		return contentType;
	}

	@Override
	public File getFile(String name) {
		return getFile(name, false);
	}

	@Override
	public File getFile(String name, boolean forceCreate) {
		File file = _uploadServletRequest.getFile(
			_namespace.concat(name), forceCreate);

		if (file == null) {
			file = _uploadServletRequest.getFile(name, forceCreate);
		}

		return file;
	}

	@Override
	public InputStream getFileAsStream(String name) throws IOException {
		return getFileAsStream(name, true);
	}

	@Override
	public InputStream getFileAsStream(String name, boolean deleteOnClose)
		throws IOException {

		InputStream inputStream = _uploadServletRequest.getFileAsStream(
			_namespace.concat(name), deleteOnClose);

		if (inputStream == null) {
			inputStream = _uploadServletRequest.getFileAsStream(
				name, deleteOnClose);
		}

		return inputStream;
	}

	@Override
	public String getFileName(String name) {
		String fileName = _uploadServletRequest.getFileName(
			_namespace.concat(name));

		if (fileName == null) {
			fileName = _uploadServletRequest.getFileName(name);
		}

		return fileName;
	}

	@Override
	public String[] getFileNames(String name) {
		String[] fileNames = _uploadServletRequest.getFileNames(
			_namespace.concat(name));

		if (fileNames == null) {
			fileNames = _uploadServletRequest.getFileNames(name);
		}

		return fileNames;
	}

	@Override
	public File[] getFiles(String name) {
		File[] files = _uploadServletRequest.getFiles(_namespace.concat(name));

		if (files == null) {
			files = _uploadServletRequest.getFiles(name);
		}

		return files;
	}

	@Override
	public InputStream[] getFilesAsStream(String name) throws IOException {
		return getFilesAsStream(name, true);
	}

	@Override
	public InputStream[] getFilesAsStream(String name, boolean deleteOnClose)
		throws IOException {

		InputStream[] inputStreams = _uploadServletRequest.getFilesAsStream(
			_namespace.concat(name), deleteOnClose);

		if (inputStreams == null) {
			inputStreams = _uploadServletRequest.getFilesAsStream(
				name, deleteOnClose);
		}

		return inputStreams;
	}

	@Override
	public String getFullFileName(String name) {
		String fullFileName = _uploadServletRequest.getFullFileName(
			_namespace.concat(name));

		if (fullFileName == null) {
			fullFileName = _uploadServletRequest.getFullFileName(name);
		}

		return fullFileName;
	}

	@Override
	public Map<String, FileItem[]> getMultipartParameterMap() {
		if (!(_uploadServletRequest instanceof UploadServletRequestImpl)) {
			return Collections.emptyMap();
		}

		Map<String, FileItem[]> map = new HashMap<String, FileItem[]>();

		UploadServletRequestImpl uploadServletRequestImpl =
			(UploadServletRequestImpl)_uploadServletRequest;

		Map<String, FileItem[]> multipartParameterMap =
			uploadServletRequestImpl.getMultipartParameterMap();

		for (String name : multipartParameterMap.keySet()) {
			if (name.startsWith(_namespace)) {
				map.put(
					name.substring(_namespace.length(), name.length()),
					multipartParameterMap.get(name));
			}
			else {
				map.put(name, multipartParameterMap.get(name));
			}
		}

		return map;
	}

	@Override
	public String getParameter(String name) {
		String parameter = _uploadServletRequest.getParameter(
			_namespace.concat(name));

		if (parameter == null) {
			parameter = _uploadServletRequest.getParameter(name);
		}

		return parameter;
	}

	@Override
	public Map<String, String[]> getParameterMap() {
		Map<String, String[]> map = new HashMap<String, String[]>();

		Enumeration<String> enu = getParameterNames();

		while (enu.hasMoreElements()) {
			String name = enu.nextElement();

			map.put(name, getParameterValues(name));
		}

		return map;
	}

	@Override
	public Enumeration<String> getParameterNames() {
		List<String> parameterNames = new ArrayList<String>();

		Enumeration<String> enu = _uploadServletRequest.getParameterNames();

		while (enu.hasMoreElements()) {
			String name = enu.nextElement();

			if (name.startsWith(_namespace)) {
				parameterNames.add(name.substring(_namespace.length()));
			}
			else {
				parameterNames.add(name);
			}
		}

		return Collections.enumeration(parameterNames);
	}

	@Override
	public String[] getParameterValues(String name) {
		String[] parameterValues = _uploadServletRequest.getParameterValues(
			_namespace.concat(name));

		if (parameterValues == null) {
			parameterValues = _uploadServletRequest.getParameterValues(name);
		}

		return parameterValues;
	}

	@Override
	public PortletRequest getPortletRequest() {
		return _portletRequest;
	}

	@Override
	public Map<String, List<String>> getRegularParameterMap() {
		if (!(_uploadServletRequest instanceof UploadServletRequestImpl)) {
			return Collections.emptyMap();
		}

		Map<String, List<String>> map = new HashMap<String, List<String>>();

		UploadServletRequestImpl uploadServletRequestImpl =
			(UploadServletRequestImpl)_uploadServletRequest;

		Map<String, List<String>> regularParameterMap =
			uploadServletRequestImpl.getRegularParameterMap();

		for (String name : regularParameterMap.keySet()) {
			if (name.startsWith(_namespace)) {
				map.put(
					name.substring(_namespace.length(), name.length()),
					regularParameterMap.get(name));
			}
			else {
				map.put(name, regularParameterMap.get(name));
			}
		}

		return map;
	}

	@Override
	public Long getSize(String name) {
		Long size = _uploadServletRequest.getSize(_namespace.concat(name));

		if (size == null) {
			size = _uploadServletRequest.getSize(name);
		}

		if (size == null) {
			return Long.valueOf(0);
		}

		return size;
	}

	@Override
	public Boolean isFormField(String name) {
		Boolean formField = _uploadServletRequest.isFormField(
			_namespace.concat(name));

		if (formField == null) {
			formField = _uploadServletRequest.isFormField(name);
		}

		if (formField == null) {
			return true;
		}
		else {
			return formField.booleanValue();
		}
	}

	private String _namespace;
	private PortletRequest _portletRequest;
	private UploadServletRequest _uploadServletRequest;

}