package com.eactive.eai.rms.onl.adapter.socket2.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

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

import org.apache.commons.httpclient.NameValuePair;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.onl.common.exception.BizException;
import com.eactive.eai.rms.onl.common.vo.HostInfomationForm;


/**
 * <pre>
 * 1. 기능 : 
 * 2. 처리 개요 : 
 *     - 
 * 3. 주의사항 : 
 * </pre>
 * 
 * @author : vs.net
 * @version : $Revision: 1.8 $
 * @since : rts 1.0
 */
@Controller
public class SocketErrorInformation2Controller extends BaseController  {
    private final String AGENT_PAGE = "adapter_socket2_error.jsp";

    /**
     * <pre>
     * 1. 기능 : 어댑터 에러 팝업 정보 
     * 2. 처리 개요 : 
     *     - 
     * 3. 주의사항 : Overriden 
     * </pre>
     * 
     * @author : vs.net
     * @version : $Revision: 1.8 $
     * @since : rts 2.0
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/adapter/socket2/errorInformation2.do")
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String cmd = request.getParameter("cmd");

//        try {
//            if ("SELECT_LIST".equals(cmd)) {
//                getLogList(request, response, model);
//            }
//        } catch (BizException be) {
//            //be.printStackTrace();
//            logger.error("[BIZ ERROR]");
//            ((HttpGauceResponse) response).addException(be);
//        } catch (SysException se) {
//            //se.printStackTrace();
//            logger.error("[SYS ERROR]");
//            ((HttpGauceResponse) response).addException(se);
//        } catch (Exception e) {
//            //e.printStackTrace();
//            logger.error("[SYS ERROR]");
//            ((HttpGauceResponse) response).addException(e);
//        } finally {
//            GauceOutputStream gos = null;
//            gos = ((HttpGauceResponse) response).getGauceOutputStream();
//
//            if (gos != null) {
//                try {
//                    gos.close();
//                } catch (Exception e) {
//                    gos = null;
//                }
//            }
//        }

        return null;
    }

    /**
     * <pre>
     * 1. 기능 : 어댑터 에러 리스트 조회
     * 2. 처리 개요 :
     *     -
     * 3. 주의사항 :
     * </pre>
     * 
     * @author : vs.net
     * @version : $Revision: 1.8 $
     * @since : rts 2.0
     * @param request
     * @param response
     * @param form
     * @throws ServletException
     * @throws IOException
     */
    private void getLogList(HttpServletRequest request, HttpServletResponse response, Map model) throws Exception {
        List serverList = (List) model.get("serverList");

        String url = null;

        String eaiSvrInstNm = request.getParameter("eaiSvrInstNm");
        String adapterGrpNm = request.getParameter("adapterGrpNm");
        String adptNm = request.getParameter("adptNm");

        if(serverList != null){ //취약점소스 수정 2020.08.27
	        for (int i = 0; i < serverList.size(); i++) {
	            Map serverMap = (Map) serverList.get(i);
	            if (serverMap != null && eaiSvrInstNm != null && eaiSvrInstNm.equals((String) serverMap.get("EAISVRINSTNM"))) {
	                url = "http://" + (String) serverMap.get("EAISVRIP") + ":"
	                        + (String) serverMap.get("EAISVRLSNPORT")
	                        + HostInfomationForm.WEB_AGENT_CONTEXT_PATH
	                        + AGENT_PAGE;
	
	                logger.debug(url);
	            }
	        }
        }

        // check url
        if (url == null)
            throw new BizException("인스턴스명과 동일한 URL이 없습니다.");

        // 파라미터에 특수문자가 포함되어 있으므로 Post 방식으로 WebMethod를 실행해야 한다.
        NameValuePair[] postParameters = new NameValuePair[] {
                new NameValuePair("adapterGroupName", adapterGrpNm),
                new NameValuePair("adapterName", adptNm) };

//        GauceInputStream gis = null;
//        GauceOutputStream gos = null;
//
//        try {
//            gis = ((HttpGauceRequest) request).getGauceInputStream();
//            gos = ((HttpGauceResponse) response).getGauceOutputStream();
//            GauceDataSet dataSet = gis.read("ds_01");
//
//            // Agent를 호출한다.
//            CallAgentUtil util = new CallAgentUtil();
//            String result = util.getAgentDataToString(url, postParameters);
//            String arrResult[] = null; //취약점소스 수정 2020.08.24
//            if(result != null){
//            	arrResult = result.split("\n");
//            }
//
//            if (arrResult != null) {
//                for (int i = 0; i < arrResult.length; i++) {
//                    dataSet.put("sline", arrResult[i], 1000,
//                        GauceDataColumn.TB_NORMAL);
//                    dataSet.heap();
//                }
//
//                gos.write(dataSet);
//            } else {
//                logger.debug("ErrorInformation2Controller: result data is null");
//            }
//        } catch (Exception e) {
//            throw e;
//        }
    }
}
