package com.dev.swms.server.web.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

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

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dev.swms.server.service.interfaceLog.InterfaceManager;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class LoginAppServlet
 */
public class EdiServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected static ApplicationContext ac;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EdiServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void init(ServletConfig sc) throws ServletException {
		super.init(sc);
		ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc.getServletContext());
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}
	 private String reqMailbox(HttpServletRequest request) throws IOException {
	        // 读取请求内容
	        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
	        String line = null;
	        StringBuilder sb = new StringBuilder();
	        while ((line = br.readLine()) != null) {
	            sb.append(line);
	        }
	        // 将资料解码
	        return sb.toString();
	 }
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        // 返回JSON数据
        JSONObject resultObject = new JSONObject();
        // 获取post提交的回复案件的信息
        String postResult = reqMailbox(request);
        String result="";
        if(postResult!=null&&!"".equals(postResult)){
        	//判断类型，根据不同类型进入不同方法
	        InterfaceManager app = (InterfaceManager) ac.getBean("interfaceManager");
	        result=app.readJson(postResult);
	        System.out.println(result);
        }else{
        	resultObject.put("status", -1);
        	resultObject.put("msg","接口信息：空的传值");
        	resultObject.put("data",postResult);
        	result=resultObject.toString();
        }
      //实现逻辑...
        response.getOutputStream().write(result.toString().getBytes("UTF-8"));
	}
}
