package com.udapsoft.waf.system;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import kr.co.hsnc.common.logger.Logger;
import kr.co.hsnc.common.util.Translator;
import kr.co.hsnc.common.util.ValueObject;
import kr.co.hsnc.j2ee.components.multipart.MultipartWrapper;

public class ParameterParser {

	private ValueObject params = null;

	private String as_encoding = "ISO-8859-1";
	private String to_encoding = "UTF-8";

	/**
	 * @param request
	 */
	public ParameterParser(HttpServletRequest request) {
		this(request, false);
	}

	/**
	 * @param request
	 * @param isCaseSensitive
	 */
	public ParameterParser(HttpServletRequest request, boolean isCaseSensitive) {
		super();
//ISO-8859-1
		as_encoding = "ISO-8859-1";
		to_encoding = "UTF-8";

		params = new ValueObject(isCaseSensitive);
		setPrams(request);
	}

	/**
	 * HttpServletRequest에서 추출한 Parameter를 ValueObject타입으로 리턴
	 * 
	 * @return
	 */
	public ValueObject getParams() {
		return this.params;
	}

	/**
	 * HttpServletRequest에서 Parameter를 추출
	 * 
	 * @param request
	 */
	private void setPrams(HttpServletRequest request) {
		String contentType = request.getContentType();
		if(contentType != null && contentType.toUpperCase().indexOf("UTF-8") > 0){
			as_encoding = "UTF-8";
		}else{
			as_encoding = "ISO-8859-1";
		}
		
		
		if (contentType != null && contentType.startsWith("application/xml")) {

		} else if (contentType != null && contentType.startsWith("text/xml")) {
			try {
				String xml = "";
				BufferedReader br = request.getReader();
				while (true) {
					String buf = br.readLine();
					if (buf == null || "".equals(buf) || buf.length() == 0)
						break;
					xml += buf;
				}

			} catch (IOException e) {
				Logger.err.printStackTrace(e);
			}
			
		} else {
			
			for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
				String key = (String) e.nextElement();
				String[] temp = request.getParameterValues(key);
				if (temp != null && temp.length == 1) {
					String value = get(request, key);
					params.set(key, value);
				} else if (temp != null && temp.length > 1) {
					String[] values = gets(request, key);
					params.set(key, values);
				}
			}
		}
	}

	/**
	 * @param request
	 * @param key
	 * @return
	 */
	private String get(HttpServletRequest request, String key) {
		String value = null;
		value = request.getParameter(key);
		boolean isMultipart = (request instanceof MultipartWrapper);
		if (!isMultipart) {
			value = toEncoding(value);
		}
		if (value == null) {
			value = "";
		}
		return value;
	}

	/**
	 * @param request
	 * @param key
	 * @return
	 */
	private String[] gets(HttpServletRequest request, String key) {
		String[] values = null;
		values = request.getParameterValues(key);
		if (values == null) {
			values = new String[1];
			values[0] = "";
		}
		for (int i = 0; i < values.length; i++) {
			values[i] = toEncoding(values[i]);
		}
		return values;
	}

	private String toEncoding(String value) {
		return Translator.translate(as_encoding, to_encoding, value);
	}
}
