/**
 * Copyright &copy; 2013-2017 <a href="http://www.rhcncpa.com/">瑞华会计师事务所</a> All rights reserved.
 */
package com.jeeplus.common.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sf.json.JSONObject;
import javax.servlet.http.HttpServletRequest;


/**
 * request转Map工具类
 * @author jeeplus
 * @version 2013-05-22
 */
public class RequestUtils{
	/**
	 * 将request里面访问的所有参数转成HashMap
	 * 
	 * @param request
	 * @return HashMap<String, String>
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static HashMap<String, String> findRequestMap(HttpServletRequest request) {
		HashMap<String, String> requestMap = new HashMap<String, String>();
		// Map requestParams = request.getParameterMap();
		if (request.getMethod().equals("GET")) {
			Map requestParams = request.getParameterMap();
			for (Iterator iter = requestParams.keySet().iterator(); iter
					.hasNext();) {
				String name = (String) iter.next();
				String[] values = (String[]) requestParams.get(name);
				String valueStr = "";
				for (int i = 0; i < values.length; i++) {
					valueStr = (i == values.length - 1) ? valueStr + values[i]
							: valueStr + values[i] + ",";
				}
				requestMap.put(name, valueStr);
			}
		} else {
			String jsonString = null;
			try {
				jsonString = getRequestJsonString(request);
			} catch (IOException e) {
				e.printStackTrace();
			}

			if (jsonString.contains("&")) {
				String[] strs = jsonString.split("&");
				for (int i = 0; i < strs.length; i++) {
					String json = strs[i];
					String[] strs1 = json.split("=");

					if (strs1.length == 1) {
						requestMap.put(strs1[0].trim(), "");
					} else {
						requestMap.put(strs1[0].trim(), strs1[1].trim());
					}
				}
			} else {
				JSONObject jsonObject = JSONObject.fromObject(jsonString);

				Iterator<String> keys = jsonObject.keys();// 定义迭代器

				String key = null;
				String value = null;

				while (keys.hasNext()) {
					key = keys.next().toString();
					value = jsonObject.get(key).toString();

					requestMap.put(key.trim(), value.trim());
				}

			}
		}
		return requestMap;

	}

	/***
	 * 获取 request 中 json 字符串的内容
	 * 
	 * @param request
	 * @return : <code>byte[]</code>
	 * @throws IOException
	 */
	private static String getRequestJsonString(HttpServletRequest request)
			throws IOException {
		String submitMehtod = request.getMethod();
		System.out.println("submitMehtod ============>" + submitMehtod);
		// GET
		if (submitMehtod.equals("GET")) {
			
			return new String(request.getQueryString().getBytes("iso-8859-1"),
					"utf-8").replaceAll("%22", "\"").replaceAll("\\+", "%2B");
			// POST
		} else {
			return getRequestPostStr(request);
		}
	}

	/**
	 * 描述:获取 post 请求的 byte[] 数组
	 * 
	 * <pre>
	 * 举例：
	 * </pre>
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	private static byte[] getRequestPostBytes(HttpServletRequest request)
			throws IOException {
		int contentLength = request.getContentLength();
		if (contentLength < 0) {
			return null;
		}
		byte buffer[] = new byte[contentLength];
		for (int i = 0; i < contentLength;) {

			int readlen = request.getInputStream().read(buffer, i,
					contentLength - i);
			if (readlen == -1) {
				break;
			}
			i += readlen;
		}
		return buffer;
	}

	/**
	 * 描述:获取 post 请求内容
	 * 
	 * <pre>
	 * 举例：
	 * </pre>
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	private static String getRequestPostStr(HttpServletRequest request)
			throws IOException {
		byte buffer[] = getRequestPostBytes(request);
		String charEncoding = request.getCharacterEncoding();
		if (charEncoding == null) {
			charEncoding = "UTF-8";
		}
		return new String(buffer, charEncoding);
	}
}
