package com.testclient.utils;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

public class HttpServletRequestUtils {
	public static String getHttpServletRequestBody(HttpServletRequest request)
			throws IOException {
		String submitMehtod = request.getMethod();
		String queryString = null;
		String charEncoding = request.getCharacterEncoding();// charset
		if (submitMehtod.equals("GET")) {// GET
			queryString = request.getQueryString();
			if (queryString == null) {
				return "";
			}
			if (StringUtils.isEmpty(charEncoding)) {
				charEncoding = "UTF-8";
			}
			return new String(queryString.getBytes(charEncoding));
		} else {// POST
			return new String(getRequestPostBytes(request), charEncoding);
		}
	}

	/***
	 * Get request query string, form method : post
	 * 
	 * @param request
	 * @return byte[]
	 * @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;
	}

	public static String getValueFromRequest(HttpServletRequest request,
			String text, String key) {
		String val = request.getParameter(key);
		if (null == val)
			val = getValueFromRequestInput(text, key);
		return val;
	}

	public static String getValueFromRequestInput(String text, String key) {
		if (text.indexOf("&lt;") > -1
				&& text.indexOf("&gt;") > text.indexOf("&lt;")) {
			text = text.replaceAll("&lt;", "<").replaceAll("&gt;", ">")
					.replaceAll("&apos;", "'").replaceAll("&quot;", "\"")
					.replaceAll("&amp;", "&");
		}
		if (text.indexOf("<") > -1 && text.indexOf(">") > text.indexOf("<")) {
			int pos = text.toLowerCase().indexOf(key.toLowerCase());
			if (pos != -1) {
				text = text.substring(pos + key.length() + 1);
				if (text.startsWith("\"")) {
					return StringUtils.substringBetween(text, "\"");
				} else {
					return StringUtils.substringBefore(text, "<");
				}
			}
		} else if (text.indexOf("}") > text.indexOf("{")
				&& text.indexOf("{") > -1) {
			int pos = text.toLowerCase().indexOf(
					"\"" + key.toLowerCase() + "\"");
			if (pos != -1) {
				pos += ("\"" + key + "\"").length() + 1;
				text = text.substring(pos);
				if (text.startsWith("\"")) {
					return StringUtils.substringBetween(text, "\"");
				} else {
					String v = StringUtils.substringBefore(text, ",");
					if (v == text)
						v = StringUtils.substringBefore(text, "}");
					return v;
				}
			}
		} else {
			for (String kv : text.split("&")) {
				if (kv.startsWith(key)) {
					return kv.replace(key + "=", "");
				}
			}
		}
		return "";
	}
}

@@ -2280,80 +2280,137 @@
  if 
+(
 (text.
-indexOf(%22%7D%22) %3E
+startsWith(%22%7B%22) &&
  text.
-indexOf(%22%7B%22)%0A%09%09%09%09&& text.indexOf(%22%7B%22) %3E -1) %7B%0A
+endsWith(%22%7D%22))) %7B%0A%09%09%09JSONObject json= new JSONObject(text);%0A%09%09%09return json.get(key).toString();%0A//
 %09%09%09i
@@ -2446,16 +2446,18 @@
 ndexOf(%0A
+//
 %09%09%09%09%09%22%5C%22
@@ -2487,16 +2487,18 @@
  %22%5C%22%22);%0A
+//
 %09%09%09if (p
@@ -2505,24 +2505,26 @@
 os != -1) %7B%0A
+//
 %09%09%09%09pos += (
@@ -2556,16 +2556,18 @@
 () + 1;%0A
+//
 %09%09%09%09text
@@ -2590,16 +2590,18 @@
 g(pos);%0A
+//
 %09%09%09%09if (
@@ -2617,32 +2617,34 @@
 tsWith(%22%5C%22%22)) %7B%0A
+//
 %09%09%09%09%09return Stri
@@ -2673,32 +2673,34 @@
 en(text, %22%5C%22%22);%0A
+//
 %09%09%09%09%7D else %7B%0A%09%09%09
@@ -2692,24 +2692,26 @@
 %09%09%09%7D else %7B%0A
+//
 %09%09%09%09%09String 
@@ -2750,24 +2750,26 @@
 text, %22,%22);%0A
+//
 %09%09%09%09%09if (v =
@@ -2776,16 +2776,18 @@
 = text)%0A
+//
 %09%09%09%09%09%09v 
@@ -2824,24 +2824,26 @@
 text, %22%7D%22);%0A
+//
 %09%09%09%09%09return 
@@ -2843,24 +2843,45 @@
 return v
-;%0A
+.replace(%22%5C%22%22,%22%22);%0A//
 %09%09%09%09%7D%0A
+//
 %09%09%09%7D%0A%09%09%7D

Update HttpServletRequestUtils.java