package org.fastframework.util;

import com.sun.org.apache.bcel.internal.generic.RET;

import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.map.util.JSONPObject;
import org.fastframework.mvc.annotation.MediaTypes;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by bysocket on 16/8/9.
 */
public class WebUtil {

	/**
	 * 从请求中获取所有参数
	 *
	 * @param request
	 * @return
	 */
	public static List<Object> getRequestParamMap(HttpServletRequest request, Class<?>[] controllerParamTypes) {

		List<Object> requestParamList = new ArrayList<>();
		Enumeration<String> paramNames = request.getParameterNames();
		int i = 0;
		while (paramNames.hasMoreElements()) {
			String paramName = paramNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues != null) {
				if (1 == paramValues.length) {
					// 获取请求参数
					String paramValue = paramValues[0];
					Class<?> paramType = controllerParamTypes[i];
					// 支持四种类型：int/Integer、long/Long、double/Double、String
					if (paramType.equals(int.class) || paramType.equals(Integer.class)) {
						requestParamList.add(Integer.parseInt(paramValue));
					} else if (paramType.equals(long.class) || paramType.equals(Long.class)) {
						requestParamList.add(Long.parseLong(paramValue));
					} else if (paramType.equals(double.class) || paramType.equals(Double.class)) {
						requestParamList.add(Double.parseDouble(paramValue));
					} else if (paramType.equals(String.class)) {
						requestParamList.add(paramValue);
					}
				}
				// TODO 数组
			}
			i++;
		}
		return requestParamList;
	}

	public static Object getRequestBody(HttpServletRequest request, Class<?> clzz) {
		InputStream is = null;
		String tempStr = "";
		Object result = null;
		try {
			is = request.getInputStream();
			tempStr = IOUtils.toString(is, Charset.forName("UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (request.getContentType().equals(MediaTypes.TEXT_PLAIN)) {
			result = tempStr;
		} else if (request.getContentType().equals(MediaTypes.JSON_UTF_8)) {
			result = JSONUtil.toObject(tempStr, clzz);
		}
		return result;
	}

}

@@ -1867,23 +1867,16 @@
 tream is
- = null
 ;%0A%09%09Stri
@@ -1889,13 +1889,8 @@
 pStr
- = %22%22
 ;%0A%09%09
@@ -1960,207 +1960,287 @@
 %0A%09%09%09
-tempStr = IOUtils.toString(is, Charset.forName(%22UTF-8%22));%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09if (request.getContentType().equals(MediaTypes.TEXT_PLAIN)) %7B%0A%09%09%09result = tempStr
+if (request.getContentType().equals(MediaTypes.TEXT_PLAIN)) %7B%0A%09%09%09%09result = IOUtils.toString(is);%0A%09%09%09%7D else if (request.getContentType().equals(MediaTypes.JSON_UTF_8)) %7B%0A%09%09%09%09tempStr = IOUtils.toString(is, Charset.forName(%22UTF-8%22));%0A%09%09%09%09result = JSONUtil.toObject(tempStr, clzz)
 ;%0A
+%09
 %09%09%7D 
@@ -2295,27 +2295,58 @@
 pes.JSON
-_UTF_8
 )) %7B%0A
+%09%09%09%09tempStr = IOUtils.toString(is);%0A%09
 %09%09%09resul
@@ -2383,16 +2383,73 @@
  clzz);%0A
+%09%09%09%7D%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09e.printStackTrace();%0A
 %09%09%7D%0A%09%09re

add request content-type(application/json) support