package com.eactive.eai.outbound;

import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

import com.eactive.eai.adapter.AdapterGroupVO;
import com.eactive.eai.adapter.AdapterManager;
import com.eactive.eai.adapter.AdapterPropManager;
import com.eactive.eai.adapter.AdapterVO;
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceKey;
import com.eactive.eai.common.EAIKeys;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.header.HeaderAction;
import com.eactive.eai.common.header.HeaderActionFactory;
import com.eactive.eai.common.header.HeaderActionKeys;
import com.eactive.eai.common.logger.Keys;
import com.eactive.eai.common.message.EAIMessage;
import com.eactive.eai.common.message.EAIMessageKeys;
import com.eactive.eai.common.message.MessageType;
import com.eactive.eai.common.property.PropManager;
import com.eactive.eai.common.routing.RouteKeys;
import com.eactive.eai.common.server.EAIServerManager;
import com.eactive.eai.common.stdmessage.STDMessageMapper;
import com.eactive.eai.common.util.MessageUtil;
import com.eactive.eai.common.util.StringUtil;
import com.eactive.eai.common.util.TranLogUtil;
import com.eactive.eai.common.worker.Future;
import com.eactive.eai.common.worker.ResponseMap;
import com.eactive.eai.control.LogSender;
import com.eactive.eai.control.Transform;
import com.eactive.eai.control.WebTSender;
import com.eactive.eai.env.ElinkConfig;
import com.eactive.eai.inbound.processor.Processor;
import com.eactive.eai.message.StandardMessage;
import com.eactive.eai.message.StandardMessageUtil;
import com.eactive.eai.message.manager.StandardMessageManager;
import com.eactive.eai.message.parser.StandardReader;
import com.eactive.eai.util.SimHeaderUtil;
import com.ext.eai.common.stdmessage.STDMessageKeys;

import tmax.webt.WebtIOException;
import tmax.webt.WebtServiceException;
import tmax.webt.WebtServiceFailException;

public class WebTProcess extends DefaultProcess {
	static final long serialVersionUID = 1L;
	protected static String prefix = "RECEAIOWP";

	public void outboundCtrlCallServiceAsync() throws Exception {
		// async에서는 인터페이스 값을 설정하지 않고 Adapter의 타임아웃을 쓰기위해서 설정하지 않음
		try {
			this.resObject = WebTSender.callService(this.adapterGroupName, this.outboundProp, this.reqObject, tempProp);
		} catch (WebtIOException e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = prefix + "002";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new RuntimeException(rspErrorCode);
		} catch (WebtServiceFailException e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = prefix + "003";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new RuntimeException(rspErrorCode);
		} catch (WebtServiceException e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = prefix + "004";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new RuntimeException(rspErrorCode);
		} catch (Exception e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = prefix + "001";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new RuntimeException(rspErrorCode);
		} catch (Throwable t) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = prefix + "001";
			String rspErrorMsg = ExceptionUtil.make(t, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new RuntimeException(rspErrorCode);
		}
	}

	public void outboundCtrlCallService() throws Exception {
		// 인터페이스에 셋팅된 타임아웃설정
		int iTimeoutValue = getTimeoutCodeToValue();
		this.timeout = iTimeoutValue * 1000;
		this.tempProp.put(INTERFACE_TIME_OUT, "" + this.timeout);
		try {
			this.resObject = WebTSender.callService(this.adapterGroupName, this.outboundProp, this.reqObject, tempProp);
			// 응답코드에 대한 처리
			this.resEaiMsg = setResponseForOutbound(this.resEaiMsg);
		} catch (WebtIOException e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = prefix + "002";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new RuntimeException(rspErrorCode);
		} catch (WebtServiceFailException e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = prefix + "003";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new RuntimeException(rspErrorCode);
		} catch (WebtServiceException e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = prefix + "004";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new RuntimeException(rspErrorCode);
		} catch (Exception e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = prefix + "001";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new RuntimeException(rspErrorCode);
		} catch (Throwable t) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = prefix + "001";
			String rspErrorMsg = ExceptionUtil.make(t, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new RuntimeException(rspErrorCode);
		}
	}

	public void logSenderCtrlSendRes() throws Exception {
		try {
			LogSender.send(this.logPssSno, this.resEaiMsg, this.prop);
		} catch (Exception e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.resEaiMsg.getEAISvcCd();
			String rspErrorCode = "RECEAIOHP021";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isWarn())
				logger.warn(rspErrorMsg);
		}
	}

	public void logSenderCtrlSendReq() throws Exception {
		try {
			LogSender.send(this.logPssSno, this.reqEaiMsg, this.prop);
		} catch (Exception e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = "RECEAIOHP021";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isWarn())
				logger.warn(rspErrorMsg);
		}
	}

	public void transControlTransformSingleRes() throws Exception {
		try {
			this.srcTranObject = this.resObject;
			this.tgtTranObject = Transform.transformSingle(this.guidLogPrefix, false, this.diURI, this.srcTranObject);
		} catch (Exception e) {
			if (logger.isError())
				logger.error(guidLogPrefix + " 300 변환오류", e);
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = "RECEAIOHP030";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(guidLogPrefix + rspErrorMsg, e);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
		}
	}

	public void transControlTransformSingleReq() throws Exception {
		try {
			this.srcTranObject = StandardMessageUtil.getBizData(this.reqEaiMsg);
			this.diURI = this.reqEaiMsg.getCurrentSvcMsg().getCnvMsgID();
			this.tgtTranObject = Transform.transformSingle(this.guidLogPrefix, true, this.diURI, this.srcTranObject);
		} catch (Exception e) {
			if (logger.isError())
				logger.error(guidLogPrefix + " 200 변환오류", e);
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = "RECEAIOHP020";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(guidLogPrefix + rspErrorMsg, e);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new Exception(rspErrorMsg);
		}
	}

	public void printCallError(Exception e1) throws Exception {
		this.resObject = new byte[0];
		AdapterManager manager = AdapterManager.getInstance();
		AdapterVO vo = manager.getAdapterVO(this.adapterGroupName, this.adapterName);
		if (vo != null) {
			if (!StringUtils.isEmpty(vo.getTestCallClass())) {
				vo.setStatus(false);
			}
		}
	}

	public void setRcvLogInfo() throws Exception {
		this.logPssSno = TranLogUtil.getResLogSeq(this.svcPssTp, this.svcPssSeq, this.isComp); // 300

		this.prop = new Properties();
		if (this.isSuccess) {
			prop.setProperty(Keys.ERROR_MESSAGE, Keys.FALSE);
		} else {
			prop.setProperty(Keys.ERROR_MESSAGE, Keys.TRUE);
		}

		if (MessageUtil.checkRspErrCd(this.resEaiMsg.getRspErrCd())) {
			if (com.eactive.eai.adapter.Keys.IF_STANDARD.equals(this.adptrMsgPtrnCd)) {
				if (logger.isDebug())
					logger.debug(guidLogPrefix + " 내부표준 추출");
				try {
					resEaiMsg = setRcvLogInfo(resEaiMsg, this.adptrMsgType, this.resObject, this.outboundCharset);
					this.resObject = resEaiMsg.getStandardMessage().getBizDataBytes();
				} catch (Exception e) {
					String[] msgArgs = new String[1];
					msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
					String rspErrorCode = "RECEAIOHP002";
					String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
					if (logger.isError())
						logger.error(guidLogPrefix + rspErrorMsg, e);
					this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
				}
			} else if (com.eactive.eai.adapter.Keys.IF_SUBSTANDARD.equals(this.adptrMsgPtrnCd)) {
				if (logger.isDebug())
					logger.debug(guidLogPrefix + "SUB표준 업무데이터 추출");
				try {
					this.resEaiMsg = convertToStandardMessage(resEaiMsg, adptrMsgType, this.resObject,
							this.outboundCharset);
					this.resObject = resEaiMsg.getStandardMessage().getBizDataBytes();
				} catch (Exception e) {
					e.printStackTrace();
					String[] msgArgs = new String[1];
					msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
					String rspErrorCode = "RECEAIOSP031"; // 잘못된 내부표준메시지 수신[{1}]
					String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
					if (logger.isError())
						logger.error(guidLogPrefix + rspErrorMsg, e);
					this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
				}
			}
		} else {
			AdapterManager adapterManager = AdapterManager.getInstance();
			AdapterGroupVO adptGrpVO = adapterManager.getAdapterGroupVO(this.adapterGroupName);
			EAIServerManager eaiServerManager = EAIServerManager.getInstance();
			if (eaiServerManager.isTASEnabledEAIServer()
					&& com.eactive.eai.adapter.Keys.TESTMASTER_TAS.equals(adptGrpVO.getTestMasterYn())) {// 비표준 시뮬레이터
				this.resObject = SimHeaderUtil.getSimData(this.resObject);
			}
		}
	}

	public void setTopicResult() throws Exception {
		try {
			this.jmsRespObject = future.value(this.timeout);
			if (this.jmsRespObject == null) {
				if (logger.isError())
					logger.error(guidLogPrefix + "SYNC-ASYNC - HTTP setTopicResult jmsRespObject is null. timeout="
							+ this.timeout);
				throw new Exception("RECEAIOHP010");
			}
			resEaiMsg = setResponseForOutbound(this.resEaiMsg);
		} catch (Exception e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = "RECEAIOHP010";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(guidLogPrefix + rspErrorMsg, e);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new RuntimeException(rspErrorCode);
		} finally {
			if (jmsRespAction != null) {
				this.jmsRespAction.doEnd();
			}
		}

		if (this.jmsRespObject == null) {
			this.resObject = new byte[0];
		} else if (this.jmsRespObject instanceof EAIMessage) {
			EAIMessage topicEaiMsg = (EAIMessage) this.jmsRespObject;
			this.resObject = topicEaiMsg.getStandardMessage().getBizDataBytes();
		} else if (this.jmsRespObject instanceof byte[]) {
			this.resObject = (byte[]) this.jmsRespObject;
		} else {
			this.resObject = new byte[0];
		}

	}

	public void checkResultMessage() throws Exception {
		if (logger.isDebug()) {
			logger.debug(guidLogPrefix + " SYNC - HTTP RESPONSE MESSAGE : "
					+ MessageUtil.toAdapterTypeString(this.resObject, this.outboundCharset));
		}

	}

	public void checkSendResult() throws Exception {
		if (MessageUtil.checkRspErrCd(this.resEaiMsg.getRspErrCd())) {			
			if (logger.isDebug()) {
				logger.debug(guidLogPrefix + " ASYNC - HTTP RESPONSE MESSAGE : "
						+ MessageUtil.toAdapterTypeString(this.resObject, this.outboundCharset));
			}
		}
	}

	public void init() throws Exception {
		this.prop = null;
		this.adapterName = null;
		this.resEaiMsg = this.reqEaiMsg;
		this.tempProp = new Properties();
		EAIServerManager eaiServerManager = EAIServerManager.getInstance();
		if (eaiServerManager.isTASEnabledEAIServer() && EAIMessageKeys.TRANTYPE_TAS.equals(reqEaiMsg.getTranType())) {
			PropManager manager = PropManager.getInstance();
			Properties prop = manager.getProperties(RouteKeys.SIM);

			if (reqEaiMsg.getCurrentSvcMsg().isSyncItfTp()) {
				this.adapterGroupName = prop.getProperty(RouteKeys.SIM_ADAPTER_SYNC, "_SIM_OU_HTT_SyC");
			} else {
				this.adapterGroupName = prop.getProperty(RouteKeys.SIM_ADAPTER_ASYNC, "_SIM_OU_HTT_AsC");
			}
			this.reqEaiMsg.getSvcMsg(0).setPsvSysItfTp(this.adapterGroupName);
		} else {
			this.adapterGroupName = reqEaiMsg.getCurrentSvcMsg().getPsvSysItfTp();
		}

		this.svcTsmtUsgTp = this.reqEaiMsg.getSvcTsmtUsgTp(); // Inbound 호출방식
		this.psvItfTp = this.reqEaiMsg.getCurrentSvcMsg().getPsvItfTp(); // 호출방식

		// 복합거래의 보상거래여부를 확인
		this.svcPssTp = this.reqEaiMsg.getSvcPssTp();
		this.svcPssSeq = this.reqEaiMsg.getSvcPssSeq();

		if (this.callProp != null) {
			logger.debug(guidLogPrefix + " COMP_VALUE[" + this.callProp.getProperty(EAIKeys.CALL_COMP_KEY) + "]");
			if (EAIKeys.CALL_COMP_VALUE.equals(this.callProp.getProperty(EAIKeys.CALL_COMP_KEY))) {
				this.isComp = true;
				try {
					this.svcPssSeq = Integer.parseInt(this.callProp.getProperty(EAIKeys.CALL_COMP_SVC_SEQ));
					logger.debug(guidLogPrefix + " CALL_COMP_SVC_SEQ[" + this.svcPssSeq + "]");
				} catch (Exception e) {
					logger.warn(guidLogPrefix + " CALL_COMP_SVC_SEQ Get Error");
				}
			} else {
				this.isComp = false;
			}
		} else {
			this.isComp = false;
		}

		String returnType = getReturnType(reqEaiMsg);

		if (STDMessageKeys.SEND_RECV_CD_RECV.equals(returnType)) {
			this.logPssSno = 400;
		} else {
			this.logPssSno = TranLogUtil.getReqLogSeq(this.svcPssTp, this.svcPssSeq, this.isComp);
		}

		// Inbound Adapter mesageType
		AdapterManager adapterManager = AdapterManager.getInstance();
		AdapterGroupVO inAdptGrpVO = adapterManager.getAdapterGroupVO(this.reqEaiMsg.getSngSysItfTp());
		this.inAdapterMsgType = MessageType.ASC;

		if (inAdptGrpVO == null) {
			if (logger.isWarn()) {
				String tasAdapterName = "";
				if (this.callProp != null)
					tasAdapterName = this.callProp.getProperty(Processor.TAS_ADAPTER_GROUP_NAME);
				logger.warn(guidLogPrefix + "Source Adapter is null, TAS Inbound = " + tasAdapterName);
			}
		} else {
			this.inAdapterMsgType = inAdptGrpVO.getMessageType();
		}
	}

	public String getUrlSubString(String url, int length) {
		try {
			int start = 0;
			if (url.startsWith("http://"))
				start = 7;
			if (url.length() > length + start) {
				url = url.substring(start, length + start);
			} else {
				url = url.substring(start);
			}
		} catch (Exception ex) {
			return "";
		}
		return url;
	}

	public void setSendMsg() throws Exception {
		try {
			AdapterGroupVO adptGrpVO = null;
			try {
				AdapterManager adapterManager = AdapterManager.getInstance();
				adptGrpVO = adapterManager.getAdapterGroupVO(this.adapterGroupName);

				if (adptGrpVO == null) {
					throw new Exception("adapter group not found");
				}

				AdapterVO adptVO = adptGrpVO.nextAdapterVO();

				if (adptVO == null) {
					throw new Exception("adapter not found");
				}
				this.adapterName = adptVO.getName();
				
				AdapterPropManager manager = AdapterPropManager.getInstance();

				/**
				 * tempProp로 수정중
				 */
				this.outboundProp = (Properties) manager.getProperties(adptVO.getPropGroupName());// clone 안함으로 변경

				this.tempProp.setProperty(HttpClientAdapterServiceKey.ADAPTER_GROUP_NAME, this.adapterGroupName);
				this.tempProp.setProperty(HttpClientAdapterServiceKey.ADAPTER_NAME, adptVO.getName());

				// MFC_FAILOVER : FAILOVER = Y 일 경우 무조건 2센터로 라우팅하도록 한다. (2020-08-27)
				PropManager pmanager = PropManager.getInstance();
				String mfcFailOver = pmanager.getProperty("MFC_FAILOVER", "FAILOVER");

				if (mfcFailOver != null && "Y".equals(mfcFailOver.toUpperCase())) {
					String url_2grp = this.outboundProp.getProperty(HttpClientAdapterServiceKey.URL_2GRP);
					if (logger.isInfo())
						logger.info(guidLogPrefix + "MFC_FAILOVER URL_2GRP - " + url_2grp);
					if (url_2grp != null && url_2grp.trim().length() > 0) {
						this.tempProp.setProperty(HttpClientAdapterServiceKey.URL, url_2grp);
						this.reqEaiMsg.getCurrentSvcMsg().setPsvSysSvcCd(getUrlSubString(url_2grp, 20));
					}
					if (logger.isInfo())
						logger.info(guidLogPrefix + "MFC_FAILOVER URL - "
								+ this.tempProp.getProperty(HttpClientAdapterServiceKey.URL));
				} else {
					// 센터구분코드(2) 일 경우, URL_2GRP 로 URL 정보를 치환한다. (2020-07-23)
					// DR일 경우에 대한 처리는 동일하게
//                    if(STDMessageKeys.CNTERDSTCD_DR.equals(extmessage.getTargetGroup()) ) {
//                        String url_2grp = this.outboundProp.getProperty(HttpClientAdapterServiceKey.URL_2GRP);
//                        if (logger.isInfo()) logger.info(guidLogPrefix + "CNTERDSTCD URL_2GRP - " + url_2grp );
//                        if(url_2grp != null && url_2grp.trim().length() > 0) {
//                            this.tempProp.setProperty(HttpClientAdapterServiceKey.URL, url_2grp);
//                            this.reqEaiMsg.getCurrentSvcMsg().setPsvSysSvcCd( getUrlSubString(url_2grp, 20) );
//                        }
//                        if (logger.isInfo()) logger.info(guidLogPrefix + "CNTERDSTCD URL - " + this.tempProp.getProperty(HttpClientAdapterServiceKey.URL) );
//                    }
				}

				String svcName = getServiceId(this.reqEaiMsg);
				if (svcName == null) {
					svcName = "";
				} else {
					svcName = svcName.trim();
				}
				this.tempProp.setProperty(HttpClientAdapterServiceKey.TRAN_CODE, svcName);
				if (logger.isInfo())
					logger.info(guidLogPrefix + " HTTP NKESA  svcName : " + svcName);

				// ENVR_INFO_DVSN_CD = L
				// FRST_DMND_SYS_IP_VAL 로 라우팅하도록 함
				String operationEnv = getOperationEnv(this.reqEaiMsg);
				if (com.eactive.eai.common.server.Keys.EAI_LOCAL.equals(operationEnv)
						&& EAIServerManager.getInstance().isMCI()) {
					String firstServiceIp = getFirstRequestSysIp(this.reqEaiMsg);
					this.tempProp.setProperty(HttpClientAdapterServiceKey.LOCAL_CALL,
							HttpClientAdapterServiceKey.LOCAL_CALL_Y);
					this.tempProp.setProperty(HttpClientAdapterServiceKey.LOCAL_IP, firstServiceIp);
					if (logger.isInfo())
						logger.info(guidLogPrefix + " HTTP NKESA  LOCAL_CALL IP : " + firstServiceIp);
				}

				this.adptrMsgPtrnCd = adptGrpVO.getAdptrMsgPtrnCd();
				this.adptrMsgType = adptGrpVO.getMessageType();
			} catch (Exception e) {
				throw new Exception("Target Adapter Fail (" + this.adapterGroupName + ") ");
			}

			// 200 로그를 위한 변환된 메시지부를 설정
			this.reqEaiMsg.getStandardMessage().setBizData(tgtTranObject, this.outboundCharset);

			// 표준메시지 방식일 경우 KESA메시지에 업무메시지를 설정하여 전달한다.
			// migration - boolean 값이 아니라 [S, N, C] 값을 가짐
			if (com.eactive.eai.adapter.Keys.IF_STANDARD.equals(this.adptrMsgPtrnCd)) {
				this.reqObject = makeStandardMessageData(this.reqEaiMsg, this.adptrMsgType, this.reqObject,
						this.outboundCharset);
			} else if (com.eactive.eai.adapter.Keys.IF_SUBSTANDARD.equals(this.adptrMsgPtrnCd)) {
				this.reqObject = makeSubStandardMessageData(this.reqEaiMsg, this.adptrMsgType, this.reqObject,
						this.outboundCharset);
				if (this.reqObject instanceof String) {
					this.reqObject = ((String) this.reqObject).getBytes(this.outboundCharset);
				}
			} else {
				EAIServerManager eaiServerManager = EAIServerManager.getInstance();
				if (eaiServerManager.isTASEnabledEAIServer()
						&& EAIMessageKeys.TRANTYPE_TAS.equals(reqEaiMsg.getTranType())) {
					this.reqObject = SimHeaderUtil.addSimHeader(reqEaiMsg.getEAISvcCd(), getInstCode(reqEaiMsg),
							reqObject);
				}

				if (logger.isDebug())
					logger.debug(guidLogPrefix + " HTTP NONSTANDARD Request Message : "
							+ MessageUtil.toAdapterTypeString(this.reqObject, this.inboundCharset));
			}
		} catch (Exception e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = "RECEAIOHP022";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(guidLogPrefix + rspErrorMsg, e);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new Exception(rspErrorMsg);
		}
	}

	public void getTopicProperty() throws Exception {
		PropManager propManager = PropManager.getInstance();

		this.topicConFactory = propManager.getProperty(RouteKeys.GROUP_NAME,
				RouteKeys.ROUTING_TOPIC_CONNECTION_FACTORY);
		this.topicName = propManager.getProperty(RouteKeys.GROUP_NAME, RouteKeys.ROUTING_TOPIC);
		int iTimeoutValue = getTimeoutCodeToValue();
		this.timeout = iTimeoutValue * 1000;
		this.tracekey = this.reqEaiMsg.getCurrentSvcMsg().getKeyMgtMsgVl();

		if (ElinkConfig.isUseCacheTopic()) {
			this.future = new Future();
			try {
				ResponseMap.getInstance().put(this.tracekey, future);
			} catch (Exception e) {
				logger.error(guidLogPrefix + "Cannot start the Cache Response Future. - " + e.getMessage(), e);
				String[] msgArgs = new String[1];
				msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
				String rspErrorCode = "RECEAIOHP060";
				String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
				this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
				throw new Exception(rspErrorMsg);
			}
		} else {
			jmsRespAction = new com.eactive.eai.common.worker.JMSListenerAction(this.topicConFactory, this.topicName,
					this.timeout);
			jmsRespAction.doInit(this.tracekey);
			this.future = new Future();
			try {
				jmsRespAction.doStart(future.getSlot());
			} catch (Exception e) {
				logger.error(guidLogPrefix + "Cannot start the JMSListenerAction. - " + e.getMessage(), e);
				String[] msgArgs = new String[1];
				msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
				String rspErrorCode = "RECEAIOHP060";
				String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
				this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
				throw new Exception(rspErrorMsg);
			}
		}
	}

	public void performSend() throws Exception {
		try {
			String headerActionName = this.reqEaiMsg.getCurrentSvcMsg().getHdrRefClsName();
			String headerContolType = StringUtil.getNullStr(this.reqEaiMsg.getCurrentSvcMsg().getHdrCtrlDstcd());

			if (logger.isDebug()) {
				logger.debug(guidLogPrefix + " Header 로직을 실행합니다.");
				logger.debug(guidLogPrefix + " headerActionName :  [" + headerActionName + "]");
				logger.debug(guidLogPrefix + " 전송 Type :  [" + HeaderActionKeys.MESSAGE_SEND + "]");
				logger.debug(guidLogPrefix + " Header Control Type :  [" + headerContolType + "]");
			}

			HeaderAction action = HeaderActionFactory.createAction(headerActionName);

			if (this.tgtTranObject instanceof byte[]) {
				this.orgData = new byte[((byte[]) this.tgtTranObject).length];
				System.arraycopy(this.tgtTranObject, 0, this.orgData, 0, ((byte[]) this.tgtTranObject).length);

				this.tgtTranObject = action.excute(this.orgData, (byte[]) this.tgtTranObject,
						HeaderActionKeys.MESSAGE_SEND, headerContolType);

				if (logger.isDebug()) {
					logger.debug(guidLogPrefix + " this.orgMessage"
							+ MessageUtil.toAdapterTypeString((byte[]) this.tgtTranObject, this.inboundCharset));
				}

			} else {
				if (logger.isWarn()) {
					logger.warn(guidLogPrefix + " byte[] 만허용됩니다. 업무메시지타입을 확인하세요" + this.tgtTranObject);
				}
			}
			if (logger.isDebug()) {
				logger.debug(guidLogPrefix + " Header 로직을 종료합니다.");
			}
		} catch (Exception e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = "RECEAIOHP040";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(guidLogPrefix + rspErrorMsg, e);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new Exception(rspErrorMsg);
		}
	}

	public void performRecv() throws Exception {
		try {
			String headerActionName = this.reqEaiMsg.getCurrentSvcMsg().getHdrRefClsName();
			String headerContolType = StringUtil.getNullStr(this.reqEaiMsg.getCurrentSvcMsg().getHdrCtrlDstcd());

			if (logger.isDebug()) {
				logger.debug(guidLogPrefix + " Header 로직을 실행합니다.");
				logger.debug(guidLogPrefix + " headerActionName :  [" + headerActionName + "]");
				logger.debug(guidLogPrefix + " 전송 Type :  [" + HeaderActionKeys.MESSAGE_RECV + "]");
				logger.debug(guidLogPrefix + " Header Control Type :  [" + headerContolType + "]");
			}

			HeaderAction action = HeaderActionFactory.createAction(headerActionName);
			String bizData = this.resEaiMsg.getStandardMessage().getBizData();

			if (bizData != null && bizData.length() > 0) {
				this.tgtTranObject = bizData;
			}

			if (this.tgtTranObject instanceof byte[]) {
				this.tgtTranObject = action.excute(this.orgData, (byte[]) this.tgtTranObject,
						HeaderActionKeys.MESSAGE_RECV, headerContolType);
				this.resEaiMsg.getStandardMessage().setBizData(tgtTranObject, this.inboundCharset);

				if (logger.isDebug()) {
					logger.debug(guidLogPrefix + " this.tgtTranObject"
							+ MessageUtil.toAdapterTypeString((byte[]) this.tgtTranObject, this.outboundCharset));
				}
			} else {
				if (logger.isWarn()) {
					logger.warn(guidLogPrefix + " byte[] 만허용됩니다. 업무메시지타입을 확인하세요" + this.tgtTranObject);
				}
			}
			if (logger.isDebug()) {
				logger.debug(guidLogPrefix + " Header 로직을 종료합니다.");
			}
		} catch (Exception e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = "RECEAIOHP040";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(guidLogPrefix + rspErrorMsg, e);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new Exception(rspErrorMsg);
		}
	}

	public void getBizMessage() throws Exception {

	}

	public void storeMessage() throws Exception {
		// ----------------------------------------------
		// 비동기-비동기 거래 && 수동이 표준이 아닌 경우
		// 원거래복원 여부가 '1' 일 경우에
		// ----------------------------------------------
		String recoverYn = getRecoverYn(reqEaiMsg);
		if (logger.isDebug()) {
			logger.debug(guidLogPrefix + "Message 복원 Flag [" + recoverYn + "]");
		}
		if (EAIMessageKeys.ASYNC_SVC.equals(this.reqEaiMsg.getSvcTsmtUsgTp())
				&& !com.eactive.eai.adapter.Keys.IF_STANDARD.equals(this.adptrMsgPtrnCd)
				&& STDMessageKeys.RECOVER_YN_YES.equals(recoverYn)) {

			try {
				String keyMgtMsgCtnt = this.reqEaiMsg.getCurrentSvcMsg().getKeyMgtMsgVl();
				if (logger.isDebug()) {
					logger.debug(guidLogPrefix + "Message 복원 실행 KEY[" + keyMgtMsgCtnt + "]");
				}
				String extBizCd = this.reqEaiMsg.getExtBizCd();
				STDMessageMapper.storeMessageHeader(keyMgtMsgCtnt, this.reqEaiMsg.getStandardMessage(), extBizCd,
						this.inAdapterMsgType);
			} catch (Exception e) {
				if (logger.isError()) {
					logger.error(guidLogPrefix + "Message 저장 중 에러 발생", e);
				}

				String[] msgArgs = new String[1];
				msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
				String rspErrorCode = "RECEAIOSP011";
				String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
				if (logger.isError())
					logger.error(guidLogPrefix + rspErrorMsg, e);
				this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
				throw new RuntimeException(rspErrorCode);
			}
		}
	}

	public void waitCheckForSync() throws Exception {
		/**
		 * Sync - Sync 거래중 Wait 메시지가 올경우 대비
		 * Async -Sync 는 Flow Control에서 처리 함
		 */
		if (com.eactive.eai.adapter.Keys.IF_STANDARD.equals(this.adptrMsgPtrnCd) && resObject != null) {
			if (EAIMessageKeys.SYNC_SVC.equals(this.reqEaiMsg.getSvcTsmtUsgTp())) {
				StandardMessage standardMessage = null;
				StandardMessageManager standardManager = StandardMessageManager.getInstance();
				StandardReader reader = null;
				try {
					standardMessage = standardManager.getStandardMessage();
					reader = standardManager.getReader(this.adptrMsgType);
					reader.parse(standardMessage, resObject);
					//표준 전문 파싱 후 어댑터의 charset을 설정한다.
					standardMessage.setBizDataCharset(outboundCharset);
				}catch(Exception e) {
					logger.error("standardMessage parsing error " + e.getMessage(),e);
					return ;
				}
				boolean isWait = standardManager.getMessageCoordinator().coordinateIsWaitCheckForSync(standardMessage);
				if (isWait) {
					getTopicProperty();
					try {
						setTopicResult();
					}catch (Exception e) {
						executeTimeout();
					}
				}
			}
		}
	}

	/**
	 * 출력 매체 부 매핑 작업 운영관리 > 환경정보 > 공통코드관리 > 프라퍼티 관리 STDMESSAGE >
	 * DATAHEADER_OTPT_TRANSFORM=true 일 때에만 변환 수행
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void transControlHeaderTrnsformSinglesRes() throws Exception {

	}
}
