package com.eactive.eai.agent.web;

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

import com.eactive.eai.common.util.Logger;
import com.eactive.eai.inbound.remote.RemoteTransfer;
import com.eactive.eai.inbound.remote.RemoteTransferLogKeys;

public class FepSendFile extends HttpServlet{

	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

   /**
    * 1. 기능 :  Eai Bat Call
    * 2. 처리 개요 : 요청 메세지를 받아서 처리 후  메세지 리턴.
    * 3. 주의사항
    *
    * @param    request     HttpServletRequest
    * @param    response    HttpServletResponse
    * @return
    * @exception
    **/
    public void service(HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("Servlet starting .......");

            String fileName = request.getParameter("msg");
//            logger.debug("[FepSendFile]msg-->" + fileName);
//            String sysCd = msg.substring(0, 5);
//            String fileName = msg.substring(5);
//            logger.debug("sysCd["+sysCd+"]");
            logger.debug("fileName["+fileName+"]");

            if ( fileName == null || fileName.getBytes().length <= 11 ){
                response.setCharacterEncoding("EUC-KR");
                response.getWriter().print("E:파라메터값(msg)이 전달되지 않았습니다.");
                return;
            }
            
//            String procCode = fileName.substring(0, 5);
//            String instCode = fileName.substring(6, 10);
            RemoteTransfer transfer = new RemoteTransfer();
        	String rtnmsg = transfer.recvFile( fileName, RemoteTransferLogKeys.START_BY_API, RemoteTransferLogKeys.STARTER_SYSTEM);

            
            String outStr = new String(rtnmsg.getBytes(),"EUC-KR");
            logger.debug("outStr["+outStr+"]");
            response.setCharacterEncoding("EUC-KR");
            response.getWriter().print(outStr);
                      
        } catch (Exception ex) {
            logger.debug(ex.getMessage(), ex);
            response.setStatus(500);
            try {
                response.getWriter().println(ex.getMessage());
                throw ex;
            } catch (Exception e) {
            }
        }
    }
}