package com.qikemi.wechat.api.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qikemi.wechat.api.service.SignService;

/**
 * 
 * Create Date: 2014年10月10日 下午9:42:26 Author XieXianbin<a.b@hotmail.com> Source
 * Repositories Address: <https://github.com/qikemi/open-wechat-sdk>
 */
public class WechatServlet extends HttpServlet {

	private static final long serialVersionUID = 1787849341385711015L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");

		// 随机字符串
		String echostr = req.getParameter("echostr");

		PrintWriter out = null;
		out = resp.getWriter();
		// 通过检验signature对请求进行校验，若校验成功则原样返回echostr，否则接入失败
		if (SignService.checkSignature(req)) {
			out.print(echostr);
		}else{
			out.print("Error Configure.");
		}
		out.close();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		super.doPost(req, resp);
	}

}

@@ -92,16 +92,62 @@
 Writer;%0D
+%0Aimport java.io.UnsupportedEncodingException;%0D
 %0A%0D%0Aimpor
@@ -314,24 +314,59 @@
 esponse;%0D%0A%0D%0A
+import org.apache.log4j.Logger;%0D%0A%0D%0A
 import com.q
@@ -692,16 +692,88 @@
 11015L;%0D
+%0A%09private static Logger logger = Logger.getLogger(WechatServlet.class);%0D
 %0A%0D%0A%09@Ove
@@ -854,49 +854,8 @@
 esp)
-%0D%0A%09%09%09throws ServletException, IOException
  %7B%0D%0A
@@ -901,24 +901,34 @@
 t=utf-8%22);%0D%0A
+%09%09try %7B%0D%0A%09
 %09%09req.setCha
@@ -953,16 +953,116 @@
 f-8%22);%0D%0A
+%09%09%7D catch (UnsupportedEncodingException e1) %7B%0D%0A%09%09%09logger.error(%22req%E7%BC%96%E7%A0%81%E9%94%99%E8%AF%AF%EF%BC%9A%22 + e1.getMessage());%0D%0A%09%09%7D%0D%0A
 %09%09resp.s
@@ -1155,18 +1155,16 @@
 str%22);%0D%0A
-%0D%0A
 %09%09PrintW
@@ -1182,16 +1182,26 @@
  null;%0D%0A
+%09%09try %7B%0D%0A%09
 %09%09out = 
@@ -1219,16 +1219,17 @@
 ter();%0D%0A
+%09
 %09%09// %E9%80%9A%E8%BF%87%E6%A3%80
@@ -1272,16 +1272,17 @@
 %E5%90%A6%E5%88%99%E6%8E%A5%E5%85%A5%E5%A4%B1%E8%B4%A5%0D%0A
+%09
 %09%09if (Si
@@ -1314,24 +1314,25 @@
 req)) %7B%0D%0A%09%09%09
+%09
 out.print(ec
@@ -1346,16 +1346,55 @@
 %0D%0A%09%09
-%7D
+%09%09logger.info(%22doGet() %E9%85%8D%E7%BD%AE%E6%88%90%E5%8A%9F.%22);%0D%0A%09%09%09%7D 
 else
+ 
 %7B%0D%0A
+%09
 %09%09%09o
@@ -1426,19 +1426,21 @@
 .%22);%0D%0A%09%09
+%09
 %7D%0D%0A
+%09
 %09%09out.cl
@@ -1443,24 +1443,211 @@
 t.close();%0D%0A
+%09%09%7D catch (IOException e) %7B%0D%0A%09%09%09logger.error(%22doGet() IOException --%3E%22 + e.getMessage());%0D%0A%09%09%7D catch (Exception e) %7B%0D%0A%09%09%09logger.error(%22doGet() %E5%8F%82%E6%95%B0%E7%BC%BA%E5%A4%B1Exception --%3E%22 + e.getMessage());%0D%0A%09%09%7D%0D%0A
 %09%7D%0D%0A%0D%0A%09@Over

complete doGet method