init
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.eactive.eai.adapter.loader.AdapterGroupLoader;
|
||||
import com.eactive.eai.adapter.loader.AdapterLoader;
|
||||
import com.eactive.eai.adapter.mapper.AdapterGroupMapper;
|
||||
import com.eactive.eai.adapter.mapper.AdapterMapper;
|
||||
import com.eactive.eai.common.dao.BaseDAO;
|
||||
import com.eactive.eai.common.dao.DAOException;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.server.EAIServerManager;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.data.entity.onl.adapter.Adapter;
|
||||
import com.eactive.eai.data.entity.onl.adapter.AdapterGroup;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class AdapterDAO extends BaseDAO {
|
||||
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
@Autowired
|
||||
private AdapterGroupLoader adapterGroupLoader;
|
||||
|
||||
@Autowired
|
||||
private AdapterLoader adapterLoader;
|
||||
|
||||
@Autowired
|
||||
private AdapterGroupMapper adapterGroupMapper;
|
||||
|
||||
@Autowired
|
||||
private AdapterMapper adapterMapper;
|
||||
|
||||
public List<AdapterGroupVO> getAllAdapterGroups() throws DAOException {
|
||||
try {
|
||||
List<AdapterGroup> adapterGroups = adapterGroupLoader.selectAllGroups();
|
||||
return adapterGroups.stream().map(adapterGroupMapper::toVo).collect(Collectors.toList());
|
||||
} catch (Exception e) {
|
||||
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAAC001"));
|
||||
}
|
||||
}
|
||||
|
||||
public List<AdapterVO> getAllAdapters(String adapterGroupName) throws DAOException {
|
||||
List<AdapterVO> al = new ArrayList<>();
|
||||
|
||||
try {
|
||||
Iterable<Adapter> adapters = adapterLoader.selectAdaptersByGroup(
|
||||
com.eactive.eai.common.server.Keys.DEFAULT_SERVER, adapterGroupName,
|
||||
EAIServerManager.getInstance().getLocalServerName());
|
||||
|
||||
for (Adapter adapter : adapters) {
|
||||
AdapterVO vo = adapterMapper.toVo(adapter);
|
||||
al.add(vo);
|
||||
}
|
||||
return al;
|
||||
} catch (Exception e) {
|
||||
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAAC002"));
|
||||
}
|
||||
}
|
||||
|
||||
public AdapterGroupVO getAdapterGroup(String adapterGroupName) throws DAOException {
|
||||
try {
|
||||
AdapterGroup adapterGroup = adapterGroupLoader.getById(adapterGroupName);
|
||||
AdapterGroupVO gvo = adapterGroupMapper.toVo(adapterGroup);
|
||||
List<AdapterVO> adapterVos = adapterGroup.getAdapters().stream().map(adapterMapper::toVo)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
adapterVos.forEach(gvo::addAdapterVO);
|
||||
return gvo;
|
||||
} catch (Exception e) {
|
||||
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAAC009"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,569 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.lifecycle.Lifecycle;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleException;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleListener;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleSupport;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class AdapterGroupVO implements Serializable, Lifecycle {
|
||||
private static final long serialVersionUID = -8613631838169562236L;
|
||||
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
private String name; // 어뎁터업무그룹명[TSEAIAD01.ADPTRBZWKGROUPNAME]
|
||||
private String adptrMsgPtrnCd; // 어댑터메시지유형코드[TSEAIAD01.ADPTRMSGPTRNCD] //[K, N, S] 값을 가짐
|
||||
private String eaiBizCode; // EAI업무구분코드[TSEAIAD01.EAIBZWKDSTCD]
|
||||
private String type; // 어댑터코드[TSEAIAD01.ADPTRCD]
|
||||
private String messageType; // 어댑터메시지구분코드[TSEAIAD01.ADPTRMSGDSTCD]
|
||||
private String messageEncode; // 어댑터메시지구분코드[TSEAIAD01.MSGENCODE]
|
||||
private String gubun; // 어뎁터입출력구분코드[TSEAIAD01.ADPTRIODSTCD]
|
||||
private String refClass; // 참고클래스명[TSEAIAD01.REFCLSNAME]
|
||||
private boolean srLogFlag; // 송수신시각로그여부[TSEAIAD01.SNDRCVHMSLOGYN]
|
||||
private boolean usedFlag; // 어댑터사용여부[TSEAIAD01.ADPTRUSEYN]
|
||||
private boolean spcfcLUUseYn; // 특정LU사용여부[TSEAIAD01.SPCFCLUUSEYN] -> 용도변경 : 로그밸런싱 방식으로 사용 RR/LC
|
||||
private String description; // 어댑터업무그룹설명[TSEAIAD01.ADPTRBZWKGROUPDESC]
|
||||
private String osidInstiNo; // 대외기관번호[TSEAIAD01.OSIDINSTINO]
|
||||
// KESA F/W사용여부[TSEAIAD01.KESAUSEYN]
|
||||
// 0 : KESA 미사용, 1: nKESA 사용, 2: uKESA 사용, 3: zKESA 사용
|
||||
private String kesaUseYn;
|
||||
|
||||
// 0 : 기본어댑터, 1: TAS어댑터, 2: TAS참조대표어댑터
|
||||
private String testMasterYn;
|
||||
private String adptrNickName;
|
||||
private Map<String, AdapterVO> adapters;
|
||||
|
||||
private Vector<AdapterVO> actives;
|
||||
private Vector<AdapterVO> disables;
|
||||
|
||||
private List<String> adapterNames;
|
||||
private String companyCodeData; // 업체코드데이터 (업체코드 추출정보 등록)
|
||||
private String adptrInstiCode; // 어댑터업체코드
|
||||
private String bidInterface; // MCI에서 bid 거래시의 대표 인터페이스
|
||||
private AtomicInteger idx = new AtomicInteger(0);
|
||||
private boolean started;
|
||||
private LifecycleSupport lifecycle = new LifecycleSupport(this);
|
||||
|
||||
private int currentTx;
|
||||
private long successTx;
|
||||
private long errorTx;
|
||||
|
||||
private String linkedAdapterGroup;
|
||||
private String sameSessionYn;
|
||||
private String pairAdapterYn;
|
||||
private String clientId;
|
||||
|
||||
private boolean weighted = false;
|
||||
|
||||
|
||||
public AdapterGroupVO() {
|
||||
this("");
|
||||
}
|
||||
|
||||
public AdapterGroupVO(String name) {
|
||||
this.name = name;
|
||||
this.adapters = new ConcurrentHashMap<>();
|
||||
this.actives = new Vector<>();
|
||||
this.disables = new Vector<>();
|
||||
this.adapterNames = new ArrayList<>();
|
||||
}
|
||||
|
||||
public boolean isWeighted() {
|
||||
return weighted;
|
||||
}
|
||||
|
||||
public void setWeighted(boolean weighted) {
|
||||
this.weighted = weighted;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setEaiBizCode(String eaiBizCode) {
|
||||
this.eaiBizCode = eaiBizCode;
|
||||
}
|
||||
|
||||
public String getEaiBizCode() {
|
||||
return this.eaiBizCode;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public void setGubun(String gubun) {
|
||||
this.gubun = gubun;
|
||||
}
|
||||
|
||||
public String getGubun() {
|
||||
return this.gubun;
|
||||
}
|
||||
|
||||
public void setMessageType(String messageType) {
|
||||
this.messageType = messageType;
|
||||
}
|
||||
|
||||
public String getMessageType() {
|
||||
return this.messageType;
|
||||
}
|
||||
|
||||
public String getMessageEncode() {
|
||||
return messageEncode;
|
||||
}
|
||||
|
||||
public void setMessageEncode(String messageEncode) {
|
||||
this.messageEncode = messageEncode;
|
||||
}
|
||||
|
||||
public void setRefClass(String refClass) {
|
||||
this.refClass = refClass;
|
||||
}
|
||||
|
||||
public String getRefClass() {
|
||||
return this.refClass;
|
||||
}
|
||||
|
||||
public void setSrLogFlag(boolean srLogFlag) {
|
||||
this.srLogFlag = srLogFlag;
|
||||
}
|
||||
|
||||
public boolean isSrLogFlag() {
|
||||
return this.srLogFlag;
|
||||
}
|
||||
|
||||
public void setUsedFlag(boolean usedFlag) {
|
||||
this.usedFlag = usedFlag;
|
||||
}
|
||||
|
||||
public boolean isUsedFlag() {
|
||||
return this.usedFlag;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
public void setSpcfcLUUseYn(boolean spcfcLUUseYn) {
|
||||
this.spcfcLUUseYn = spcfcLUUseYn;
|
||||
}
|
||||
|
||||
public boolean isSpcfcLUUseYn() {
|
||||
return this.spcfcLUUseYn;
|
||||
}
|
||||
|
||||
public void setAdptrMsgPtrnCd(String adptrMsgPtrnCd) {
|
||||
this.adptrMsgPtrnCd = adptrMsgPtrnCd;
|
||||
}
|
||||
|
||||
public String getAdptrMsgPtrnCd() {
|
||||
return this.adptrMsgPtrnCd;
|
||||
}
|
||||
|
||||
public String getOsidInstiNo() {
|
||||
return osidInstiNo;
|
||||
}
|
||||
|
||||
public void setOsidInstiNo(String osidInstiNo) {
|
||||
this.osidInstiNo = osidInstiNo;
|
||||
}
|
||||
|
||||
public String getKesaUseYn() {
|
||||
return kesaUseYn;
|
||||
}
|
||||
|
||||
public void setKesaUseYn(String kesaUseYn) {
|
||||
this.kesaUseYn = kesaUseYn;
|
||||
}
|
||||
|
||||
public String getTestMasterYn() {
|
||||
return testMasterYn;
|
||||
}
|
||||
|
||||
public void setTestMasterYn(String testMasterYn) {
|
||||
this.testMasterYn = testMasterYn;
|
||||
}
|
||||
|
||||
public String getAdptrNickName() {
|
||||
return adptrNickName;
|
||||
}
|
||||
|
||||
public void setAdptrNickName(String adptrNickName) {
|
||||
this.adptrNickName = adptrNickName;
|
||||
}
|
||||
|
||||
public String getCompanyCodeData() {
|
||||
return companyCodeData;
|
||||
}
|
||||
|
||||
public void setCompanyCodeData(String companyCodeData) {
|
||||
this.companyCodeData = companyCodeData;
|
||||
}
|
||||
|
||||
public String getAdptrInstiCode() {
|
||||
return adptrInstiCode;
|
||||
}
|
||||
|
||||
public void setAdptrInstiCode(String adptrInstiCode) {
|
||||
this.adptrInstiCode = adptrInstiCode;
|
||||
}
|
||||
|
||||
public String getBidInterface() {
|
||||
return bidInterface;
|
||||
}
|
||||
|
||||
public void setBidInterface(String bidInterface) {
|
||||
this.bidInterface = bidInterface;
|
||||
}
|
||||
|
||||
public String getClientId() { return clientId; }
|
||||
|
||||
public void setClientId(String clientId) {
|
||||
this.clientId = clientId;
|
||||
}
|
||||
|
||||
public synchronized void addAdapterVO(AdapterVO avo) {
|
||||
this.adapters.put(avo.getName(), avo);
|
||||
this.adapterNames.add(avo.getName());
|
||||
}
|
||||
|
||||
public AdapterVO getAdapterVO(String adapterName) {
|
||||
return adapters.get(adapterName);
|
||||
}
|
||||
|
||||
public synchronized void increase() {
|
||||
++currentTx;
|
||||
}
|
||||
|
||||
public synchronized void decrease() {
|
||||
--currentTx;
|
||||
}
|
||||
|
||||
public int currentTx() {
|
||||
return this.currentTx;
|
||||
}
|
||||
|
||||
// GW관련 어댑터 카운터 추가-시작
|
||||
public synchronized void increaseSuccess() {
|
||||
++successTx;
|
||||
}
|
||||
|
||||
public synchronized void increaseError() {
|
||||
++errorTx;
|
||||
}
|
||||
|
||||
public long successTx() {
|
||||
return this.successTx;
|
||||
}
|
||||
|
||||
public long errorTx() {
|
||||
return this.errorTx;
|
||||
}
|
||||
|
||||
public synchronized void resetCount() {
|
||||
this.successTx = 0;
|
||||
this.errorTx = 0;
|
||||
}
|
||||
// GW관련 어댑터 카운터 추가-종료
|
||||
|
||||
public String getLinkedAdapterGroup() {
|
||||
return linkedAdapterGroup;
|
||||
}
|
||||
|
||||
public void setLinkedAdapterGroup(String linkedAdapterGroup) {
|
||||
this.linkedAdapterGroup = linkedAdapterGroup;
|
||||
}
|
||||
|
||||
public String getSameSessionYn() {
|
||||
return sameSessionYn;
|
||||
}
|
||||
|
||||
public void setSameSessionYn(String sameSessionYn) {
|
||||
this.sameSessionYn = sameSessionYn;
|
||||
}
|
||||
|
||||
public String getPairAdapterYn() {
|
||||
return pairAdapterYn;
|
||||
}
|
||||
|
||||
public void setPairAdapterYn(String pairAdapterYn) {
|
||||
this.pairAdapterYn = pairAdapterYn;
|
||||
}
|
||||
|
||||
public void start() throws LifecycleException {
|
||||
if (started)
|
||||
throw new LifecycleException("RECEAIAAC030");
|
||||
|
||||
// Notify our interested LifecycleListeners
|
||||
lifecycle.fireLifecycleEvent(STARTING_EVENT, this);
|
||||
|
||||
if (!this.usedFlag) {
|
||||
String code = "RECEAIAAC040";
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = name;
|
||||
String errText = ExceptionUtil.make(code, msgArgs);
|
||||
if (logger.isError())
|
||||
logger.error(errText);
|
||||
throw new LifecycleException(errText);
|
||||
}
|
||||
|
||||
setWeighted(false);
|
||||
idx.set(0);
|
||||
|
||||
AdapterVO avo = null;
|
||||
Iterator<AdapterVO> it = adapters.values().iterator();
|
||||
while (it.hasNext()) {
|
||||
avo = it.next();
|
||||
if (avo instanceof Lifecycle) {
|
||||
try {
|
||||
LifecycleListener[] listeners = this.lifecycle.findLifecycleListeners();
|
||||
if (listeners != null) {
|
||||
for (int j = 0; j < listeners.length; j++) {
|
||||
avo.addLifecycleListener(listeners[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// 어댑터 프라퍼티에 active_onstartup = false 일 경우
|
||||
// EAI 서버 기동시에 Adapter를 start하지 않는다.
|
||||
boolean activeOnstartup = true;
|
||||
AdapterPropManager manager = AdapterPropManager.getInstance();
|
||||
String activeOnstartupValue = manager.getProperty(avo.getPropGroupName(), "active_onstartup");
|
||||
|
||||
if (activeOnstartupValue != null && "false".equals(activeOnstartupValue.trim())) {
|
||||
activeOnstartup = false;
|
||||
}
|
||||
|
||||
String lbWeight = manager.getProperty(avo.getPropGroupName(), Keys.LB_WEIGHT);
|
||||
if(StringUtils.isNoneEmpty(lbWeight)) {
|
||||
int weight = 1;
|
||||
try {
|
||||
weight = Integer.parseInt(lbWeight);
|
||||
}
|
||||
catch(NumberFormatException ex) {
|
||||
weight = 1;
|
||||
}
|
||||
|
||||
if(weight > 1) setWeighted(true);
|
||||
avo.setWeight(weight);
|
||||
}
|
||||
|
||||
avo.setAdapterGroupVO(this);
|
||||
if (activeOnstartup) {
|
||||
avo.start();
|
||||
} else {
|
||||
disable(avo);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
disable(avo);
|
||||
throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAIAAC031") + " : " + this.name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
started = true;
|
||||
lifecycle.fireLifecycleEvent(STARTED_EVENT, this);
|
||||
}
|
||||
|
||||
public void stop() throws LifecycleException {
|
||||
if (!started)
|
||||
throw new LifecycleException("RECEAIAAC032");
|
||||
|
||||
lifecycle.fireLifecycleEvent(STOPING_EVENT, this);
|
||||
|
||||
AdapterVO avo = null;
|
||||
Iterator<AdapterVO> it = adapters.values().iterator();
|
||||
while (it.hasNext()) {
|
||||
avo = it.next();
|
||||
if (!avo.isStarted())
|
||||
continue;
|
||||
if (avo instanceof Lifecycle) {
|
||||
try {
|
||||
avo.stop();
|
||||
} catch (Exception e) {
|
||||
throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAIAAC033") + " : " + this.name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
started = false;
|
||||
lifecycle.fireLifecycleEvent(STOPPED_EVENT, this);
|
||||
}
|
||||
|
||||
public boolean isStarted() {
|
||||
return this.started;
|
||||
}
|
||||
|
||||
public void active(AdapterVO vo) {
|
||||
if (!this.actives.contains(vo) && vo.isStatus()) {
|
||||
this.actives.add(vo);
|
||||
this.disables.remove(vo);
|
||||
}
|
||||
}
|
||||
|
||||
public void disable(AdapterVO vo) {
|
||||
if (!this.disables.contains(vo) && !vo.isStatus()) {
|
||||
this.disables.add(vo);
|
||||
this.actives.remove(vo);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return !this.actives.isEmpty();
|
||||
}
|
||||
|
||||
public void addLifecycleListener(LifecycleListener listener) {
|
||||
lifecycle.addLifecycleListener(listener);
|
||||
}
|
||||
|
||||
public LifecycleListener[] findLifecycleListeners() {
|
||||
return lifecycle.findLifecycleListeners();
|
||||
}
|
||||
|
||||
public void removeLifecycleListener(LifecycleListener listener) {
|
||||
lifecycle.removeLifecycleListener(listener);
|
||||
}
|
||||
|
||||
public Iterator<AdapterVO> getAdapters() {
|
||||
return this.adapters.values().iterator();
|
||||
}
|
||||
|
||||
public String[] getAdapterNames() {
|
||||
String[] names = new String[this.adapterNames.size()];
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
names[i] = this.adapterNames.get(i);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
public synchronized AdapterVO nextAdapterVO() {
|
||||
if (this.actives.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.actives.size() == 1) {
|
||||
return this.actives.get(0);
|
||||
}
|
||||
|
||||
if(spcfcLUUseYn) {
|
||||
if(logger.isDebug()) logger.debug("nextAdapterVO selectWithLeastConnection");
|
||||
return selectWithLeastConnection();
|
||||
}
|
||||
else {
|
||||
if(isWeighted()) {
|
||||
if(logger.isDebug()) logger.debug("nextAdapterVO selectWithWeightedRoundRobin");
|
||||
return selectWithWeightedRoundRobin();
|
||||
}
|
||||
else {
|
||||
if(logger.isDebug()) logger.debug("nextAdapterVO selectWithRoundRobin");
|
||||
return selectWithRoundRobin();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private AdapterVO selectWithRoundRobin() {
|
||||
try {
|
||||
if (idx.get() >= this.actives.size())
|
||||
idx.set(0);
|
||||
return this.actives.get(idx.getAndIncrement());
|
||||
} catch (Exception e) {
|
||||
if (this.actives.isEmpty())
|
||||
return null;
|
||||
idx.set(0);
|
||||
return this.actives.get(idx.getAndIncrement());
|
||||
}
|
||||
}
|
||||
|
||||
private AdapterVO selectWithWeightedRoundRobin() {
|
||||
try {
|
||||
int totalWeight = actives.stream().mapToInt(AdapterVO::getWeight).sum();
|
||||
int currentIndex = idx.getAndIncrement() % totalWeight;
|
||||
|
||||
for (AdapterVO adapter : actives) {
|
||||
currentIndex -= adapter.getWeight();
|
||||
if (currentIndex < 0) {
|
||||
return adapter;
|
||||
}
|
||||
}
|
||||
return this.actives.get(0);
|
||||
} catch (Exception e) {
|
||||
if (this.actives.isEmpty())
|
||||
return null;
|
||||
idx.set(0);
|
||||
return this.actives.get(idx.getAndIncrement());
|
||||
}
|
||||
}
|
||||
|
||||
private AdapterVO selectWithLeastConnection() {
|
||||
try {
|
||||
AdapterVO selected = this.actives.get(0);
|
||||
for (AdapterVO adapter : this.actives) {
|
||||
if (adapter.getRequestCount() < selected.getRequestCount()) {
|
||||
selected = adapter;
|
||||
}
|
||||
}
|
||||
return selected;
|
||||
} catch (Exception e) {
|
||||
if (this.actives.isEmpty())
|
||||
return null;
|
||||
return this.actives.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void removeAdapterVO(String name) {
|
||||
adapters.remove(name);
|
||||
this.adapterNames.remove(name);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("AdapterGroupVO[ name=").append(name);
|
||||
sb.append(", adptrMsgPtrnCd=").append(adptrMsgPtrnCd);
|
||||
sb.append(", eaiBizCode=").append(eaiBizCode);
|
||||
sb.append(", type=").append(type);
|
||||
sb.append(", messageType=").append(messageType);
|
||||
sb.append(", messageEncode=").append(messageEncode);
|
||||
sb.append(", gubun=").append(gubun);
|
||||
sb.append(", refClass=").append(refClass);
|
||||
sb.append(", srLogFlag=").append(srLogFlag);
|
||||
sb.append(", usedFlag=").append(usedFlag);
|
||||
sb.append(", spcfcLUUseYn=").append(spcfcLUUseYn);
|
||||
sb.append(", description=").append(description);
|
||||
sb.append(", osidInstiNo=").append(osidInstiNo);
|
||||
sb.append(", kesaUseYn=").append(kesaUseYn);
|
||||
sb.append(", testMasterYn=").append(testMasterYn);
|
||||
sb.append(", adptrNickName=").append(adptrNickName);
|
||||
sb.append(", companyCodeData=").append(companyCodeData);
|
||||
sb.append(", adptrInstiCode=").append(adptrInstiCode);
|
||||
sb.append(", bidInterface=").append(bidInterface);
|
||||
sb.append(", actives=").append(this.actives.size());
|
||||
sb.append(", disables=").append(this.disables.size());
|
||||
sb.append(", AdapterList=").append(adapters);
|
||||
sb.append(", clientId=").append(clientId);
|
||||
sb.append(" ]");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.eactive.eai.common.dao.DAOException;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.lifecycle.Lifecycle;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleException;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleListener;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleSupport;
|
||||
import com.eactive.eai.common.util.ApplicationContextProvider;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
@Component
|
||||
public class AdapterManager implements Lifecycle {
|
||||
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
private boolean started;
|
||||
|
||||
private HashMap<String, AdapterGroupVO> adapterGroups = new HashMap<>();
|
||||
|
||||
private LifecycleSupport lifecycle = new LifecycleSupport(this);
|
||||
|
||||
@Autowired
|
||||
private AdapterDAO adapterDAO;
|
||||
|
||||
public static AdapterManager getInstance() {
|
||||
return ApplicationContextProvider.getContext().getBean(AdapterManager.class);
|
||||
}
|
||||
|
||||
public void start() throws LifecycleException {
|
||||
if (started)
|
||||
throw new LifecycleException("RECEAIAAC015");
|
||||
|
||||
lifecycle.fireLifecycleEvent(STARTING_EVENT, this);
|
||||
|
||||
this.adapterGroups.clear();
|
||||
|
||||
List<AdapterGroupVO> groups = null;
|
||||
try {
|
||||
adapterDAO = ApplicationContextProvider.getContext().getBean(AdapterDAO.class);
|
||||
groups = adapterDAO.getAllAdapterGroups();
|
||||
} catch (Exception e) {
|
||||
throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAIAAC016"));
|
||||
}
|
||||
|
||||
for (AdapterGroupVO gvo : groups) {
|
||||
List<AdapterVO> adapterVos = null;
|
||||
try {
|
||||
adapterVos = adapterDAO.getAllAdapters(gvo.getName());
|
||||
for (AdapterVO vo : adapterVos) {
|
||||
gvo.addAdapterVO(vo);
|
||||
}
|
||||
} catch (DAOException e) {
|
||||
if (logger.isError())
|
||||
logger.error(ExceptionUtil.getErrorCode(e, "RECEAIAAC017"));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (logger.isDebug()) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = gvo.toString();
|
||||
logger.debug(ExceptionUtil.make("RDCEAIAAC018", msgArgs));
|
||||
}
|
||||
addAdapterGroupVO(gvo);
|
||||
|
||||
try {
|
||||
LifecycleListener[] listeners = this.lifecycle.findLifecycleListeners();
|
||||
if (listeners != null) {
|
||||
for (int j = 0; j < listeners.length; j++) {
|
||||
gvo.addLifecycleListener(listeners[j]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!gvo.isUsedFlag()) {
|
||||
if (logger.isDebug())
|
||||
logger.debug("Not Used Adapter>> " + gvo);
|
||||
continue;
|
||||
}
|
||||
gvo.start();
|
||||
} catch (Exception e) {
|
||||
if (logger.isError())
|
||||
logger.error(ExceptionUtil.getErrorCode(e, "RECEAIAAC019"));
|
||||
}
|
||||
}
|
||||
started = true;
|
||||
lifecycle.fireLifecycleEvent(STARTED_EVENT, this);
|
||||
}
|
||||
|
||||
public void stop() throws LifecycleException {
|
||||
if (!started)
|
||||
throw new LifecycleException("RECEAIAAC020");
|
||||
|
||||
lifecycle.fireLifecycleEvent(STOPING_EVENT, this);
|
||||
|
||||
logger.warn("Stop Inbound Adapters ...");
|
||||
for (Iterator<AdapterGroupVO> it = this.adapterGroups.values().iterator(); it.hasNext();) {
|
||||
AdapterGroupVO vo = it.next();
|
||||
if ("I".equals(vo.getGubun())) {
|
||||
try {
|
||||
if (!vo.isStarted()) {
|
||||
logger.warn("AdapterGroup not started skip stop. - " + vo);
|
||||
continue;
|
||||
}
|
||||
if (vo instanceof Lifecycle) {
|
||||
vo.stop();
|
||||
}
|
||||
} catch (LifecycleException e) {
|
||||
String[] msgArgs = { vo.toString() };
|
||||
String errText = ExceptionUtil.make("RECEAIAAC021", msgArgs);
|
||||
if (logger.isError())
|
||||
logger.error(errText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.warn("Stop Outbound Adapters ...");
|
||||
for (Iterator<AdapterGroupVO> it = this.adapterGroups.values().iterator(); it.hasNext();) {
|
||||
AdapterGroupVO vo = it.next();
|
||||
if (!"I".equals(vo.getGubun())) {
|
||||
try {
|
||||
if (!vo.isStarted())
|
||||
continue;
|
||||
if (vo instanceof Lifecycle) {
|
||||
vo.stop();
|
||||
}
|
||||
} catch (LifecycleException e) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = vo.toString();
|
||||
String errText = ExceptionUtil.make("RECEAIAAC021", msgArgs);
|
||||
if (logger.isError())
|
||||
logger.error(errText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
started = false;
|
||||
lifecycle.fireLifecycleEvent(STOPPED_EVENT, this);
|
||||
}
|
||||
|
||||
public boolean isStarted() {
|
||||
return this.started;
|
||||
}
|
||||
|
||||
public void addLifecycleListener(LifecycleListener listener) {
|
||||
lifecycle.addLifecycleListener(listener);
|
||||
}
|
||||
|
||||
public LifecycleListener[] findLifecycleListeners() {
|
||||
return lifecycle.findLifecycleListeners();
|
||||
}
|
||||
|
||||
public void removeLifecycleListener(LifecycleListener listener) {
|
||||
lifecycle.removeLifecycleListener(listener);
|
||||
}
|
||||
|
||||
public AdapterVO getAdapterVO(String adapterGroupName, String adapterName) {
|
||||
AdapterGroupVO gvo = adapterGroups.get(adapterGroupName);
|
||||
if (gvo == null)
|
||||
return null;
|
||||
return gvo.getAdapterVO(adapterName);
|
||||
}
|
||||
|
||||
public AdapterGroupVO getAdapterGroupVO(String adapterGroupName) {
|
||||
return adapterGroups.get(adapterGroupName);
|
||||
}
|
||||
|
||||
public AdapterGroupVO getAdapterGroup(String adapterGroupName) throws DAOException {
|
||||
return adapterGroups.get(adapterGroupName);
|
||||
}
|
||||
|
||||
public AdapterGroupVO loadAdapterGroup(String adapterGroupName) throws DAOException {
|
||||
return adapterDAO.getAdapterGroup(adapterGroupName);
|
||||
}
|
||||
|
||||
public void addAdapterGroupVO(AdapterGroupVO vo) {
|
||||
if (vo != null) {
|
||||
adapterGroups.put(vo.getName(), vo);
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<String, AdapterGroupVO> getAllAdapterGroupVO() {
|
||||
return adapterGroups;
|
||||
}
|
||||
|
||||
public List<String> getAllAdapterGroupNames() {
|
||||
ArrayList<String> al = new ArrayList<>();
|
||||
Iterator<String> it = this.adapterGroups.keySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
al.add(it.next());
|
||||
}
|
||||
Collections.sort(al);
|
||||
return al;
|
||||
}
|
||||
|
||||
public void updateAdapterGroupVO(AdapterGroupVO vo) {
|
||||
if (vo != null)
|
||||
adapterGroups.put(vo.getName(), vo);
|
||||
}
|
||||
|
||||
public void removeAdapterGroupVO(String name) {
|
||||
adapterGroups.remove(name);
|
||||
}
|
||||
|
||||
public void print() {
|
||||
if (logger.isDebug())
|
||||
logger.debug("AdapterManager] All Adapter List>> " + this.adapterGroups);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.eactive.eai.adapter.loader.AdapterPropGroupLoader;
|
||||
import com.eactive.eai.common.dao.BaseDAO;
|
||||
import com.eactive.eai.common.dao.DAOException;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.data.entity.onl.adapter.AdapterPropGroup;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class AdapterPropGroupDAO extends BaseDAO {
|
||||
|
||||
private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
@Autowired
|
||||
private AdapterPropGroupLoader adapterPropGroupLoader;
|
||||
|
||||
public Map<String, AdapterPropGroupVO> getAllProperties() throws DAOException {
|
||||
|
||||
Map<String, AdapterPropGroupVO> adapterPropGroupVos = new HashMap<>();
|
||||
try {
|
||||
logger.info("[ @@ Load Adapter Properties Configuration - starting >>>>>>>>>>>>>>>>> ]");
|
||||
|
||||
Iterable<AdapterPropGroup> adapterPropGroups = adapterPropGroupLoader
|
||||
.loadAll()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (AdapterPropGroup adapterPropGroup : adapterPropGroups) {
|
||||
AdapterPropGroupVO adapterPropGroupVO = getProperties(adapterPropGroup);
|
||||
adapterPropGroupVos.put(adapterPropGroup.getPrptygroupname(), adapterPropGroupVO);
|
||||
}
|
||||
|
||||
logger.info("[>>Load Adapter Properties Configuration - ended ]");
|
||||
return adapterPropGroupVos;
|
||||
} catch (Exception e) {
|
||||
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICPM101"));
|
||||
}
|
||||
}
|
||||
|
||||
private AdapterPropGroupVO getProperties(AdapterPropGroup adapterPropGroup) {
|
||||
AdapterPropGroupVO adapterPropGroupVO = new AdapterPropGroupVO(adapterPropGroup.getPrptygroupname());
|
||||
adapterPropGroup.getAdapterProps().forEach(adapterProp -> adapterPropGroupVO
|
||||
.addProperty(adapterProp.getId().getPrptyname(), adapterProp.getPrpty2val()));
|
||||
return adapterPropGroupVO;
|
||||
}
|
||||
|
||||
public AdapterPropGroupVO getProperties(String grpNm) throws DAOException {
|
||||
try {
|
||||
AdapterPropGroup adapterPropGroup = adapterPropGroupLoader.getById(grpNm);
|
||||
return getProperties(adapterPropGroup);
|
||||
} catch (Exception e) {
|
||||
throw new DAOException(ExceptionUtil.make("RECEAICPM103", grpNm));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.eactive.eai.common.util.NullControl;
|
||||
|
||||
/**
|
||||
* 1. 기능 : 어뎁터 프라퍼티 정보를 를 정의한 Java Value Object 클래스
|
||||
* 2. 처리 개요 : 어뎁터 프라퍼티 정보를 정의한다.
|
||||
* * -
|
||||
* 3. 주의사항
|
||||
*
|
||||
* @author :
|
||||
* @version : v 1.0.0
|
||||
* @see : 관련 기능을 참조
|
||||
* @since :
|
||||
* :
|
||||
*/
|
||||
public class AdapterPropGroupVO implements Serializable
|
||||
{
|
||||
private String name; //어뎁터 프라퍼티 그룹명[TSEAIAD14.PrptyGroupName]
|
||||
private String description; //어뎁터 프라퍼티 그룹설명[TSEAIAD14.PrptyGroupDesc]
|
||||
private Properties props; //어뎁터 프라퍼티 명, 프라퍼티2값[TSEAIAD15.PrptyName, TSEAIAD15.Prpty2Val]
|
||||
|
||||
public AdapterPropGroupVO() {
|
||||
this("");
|
||||
}
|
||||
|
||||
public AdapterPropGroupVO(String name) {
|
||||
this(name,"");
|
||||
}
|
||||
|
||||
public AdapterPropGroupVO(String name, String description) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.props = new Properties();
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return NullControl.trimSpace(this.description);
|
||||
}
|
||||
|
||||
public String getProperty(String key) {
|
||||
return (String)props.get(key);
|
||||
}
|
||||
|
||||
public String getProperty(String key, String defaultValue) {
|
||||
String value = (String)props.get(key);
|
||||
return (value==null) ? defaultValue : value;
|
||||
}
|
||||
|
||||
public void addProperty(String key, String value) {
|
||||
props.put(key, NullControl.trimSpace(value));
|
||||
}
|
||||
|
||||
public void setProperty(String key, String value) {
|
||||
props.put(key,value);
|
||||
}
|
||||
|
||||
public void removeProperty(String key) {
|
||||
props.remove(key);
|
||||
}
|
||||
|
||||
public String[] keys() {
|
||||
Iterator it = this.props.keySet().iterator();
|
||||
String[] propKeys = new String[this.props.size()];
|
||||
for(int i = 0; it.hasNext(); i++){
|
||||
propKeys[i] = (String)it.next();
|
||||
}
|
||||
Arrays.sort(propKeys);
|
||||
return propKeys;
|
||||
}
|
||||
|
||||
public Properties getProperties() {
|
||||
return props;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("AdapterPropGroupVO[ name=").append(name);
|
||||
sb.append(", description=").append(description);
|
||||
sb.append(", Properties=").append(props);
|
||||
sb.append(" ]");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void setProperties(Properties prop){
|
||||
this.props = prop;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.lifecycle.Lifecycle;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleException;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleListener;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleSupport;
|
||||
import com.eactive.eai.common.util.ApplicationContextProvider;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
@Component
|
||||
public class AdapterPropManager implements Lifecycle {
|
||||
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
private LifecycleSupport lifecycle = new LifecycleSupport(this);
|
||||
|
||||
private PropertyChangeSupport property = new PropertyChangeSupport(this);
|
||||
|
||||
private Map<String, AdapterPropGroupVO> groups;
|
||||
|
||||
private boolean started;
|
||||
|
||||
@Autowired
|
||||
private AdapterPropGroupDAO adapterPropGroupDAO;
|
||||
|
||||
public static AdapterPropManager getInstance() {
|
||||
return ApplicationContextProvider.getContext().getBean(AdapterPropManager.class);
|
||||
}
|
||||
|
||||
public void addPropertyChangeListener(PropertyChangeListener listener) {
|
||||
property.addPropertyChangeListener(listener);
|
||||
}
|
||||
|
||||
public void removePropertyChangeListener(PropertyChangeListener listener) {
|
||||
property.removePropertyChangeListener(listener);
|
||||
}
|
||||
|
||||
private PropertyChangeEvent createEvent(AdapterPropGroupVO source, String propertyName, Object oldValue,
|
||||
Object newValue) {
|
||||
return new PropertyChangeEvent(source, propertyName, oldValue, newValue);
|
||||
}
|
||||
|
||||
public boolean isStarted() {
|
||||
return this.started;
|
||||
}
|
||||
|
||||
public void start() throws LifecycleException {
|
||||
if (started)
|
||||
throw new LifecycleException("RECEAICPM201");
|
||||
|
||||
lifecycle.fireLifecycleEvent(STARTING_EVENT, this);
|
||||
|
||||
try {
|
||||
init();
|
||||
} catch (Exception e) {
|
||||
throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAICPM201"));
|
||||
}
|
||||
|
||||
started = true;
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("AdapterPropManager] It is started.");
|
||||
}
|
||||
lifecycle.fireLifecycleEvent(STARTED_EVENT, this);
|
||||
}
|
||||
|
||||
private void init() throws Exception {
|
||||
this.groups = adapterPropGroupDAO.getAllProperties();
|
||||
}
|
||||
|
||||
public synchronized void reload() throws Exception {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("AdapterPropManager] reload all Started ...");
|
||||
}
|
||||
init();
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("AdapterPropManager] reload all finished ...");
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void reload(String keyName) throws Exception {
|
||||
AdapterPropGroupVO vo = adapterPropGroupDAO.getProperties(keyName);
|
||||
if (vo != null) {
|
||||
this.groups.put(keyName, vo);
|
||||
} else {
|
||||
throw new Exception("AdapterPropManager not found in Database : key[" + keyName + "]");
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() throws LifecycleException {
|
||||
if (!started)
|
||||
throw new LifecycleException("RECEAICPM203");
|
||||
|
||||
lifecycle.fireLifecycleEvent(STOPING_EVENT, this);
|
||||
|
||||
started = false;
|
||||
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("AdapterPropManager] It is stopped.");
|
||||
}
|
||||
lifecycle.fireLifecycleEvent(STOPPED_EVENT, this);
|
||||
}
|
||||
|
||||
public void addLifecycleListener(LifecycleListener listener) {
|
||||
lifecycle.addLifecycleListener(listener);
|
||||
}
|
||||
|
||||
public LifecycleListener[] findLifecycleListeners() {
|
||||
return lifecycle.findLifecycleListeners();
|
||||
}
|
||||
|
||||
public void removeLifecycleListener(LifecycleListener listener) {
|
||||
lifecycle.removeLifecycleListener(listener);
|
||||
}
|
||||
|
||||
public void addAdapterPropGroupVO(String name, String description) {
|
||||
groups.put(name, new AdapterPropGroupVO(name, description));
|
||||
}
|
||||
|
||||
public AdapterPropGroupVO getAdapterPropGroupVO(String name) {
|
||||
return groups.get(name);
|
||||
}
|
||||
|
||||
public String getProperty(String key) {
|
||||
|
||||
return getProperty(com.eactive.eai.common.server.Keys.DEFAULT_SERVER, key);
|
||||
}
|
||||
|
||||
public String getProperty(String group, String key) {
|
||||
AdapterPropGroupVO vo = groups.get(group);
|
||||
if (vo == null)
|
||||
return null;
|
||||
return vo.getProperty(key);
|
||||
}
|
||||
|
||||
public Properties getProperties(String group) {
|
||||
AdapterPropGroupVO vo = groups.get(group);
|
||||
if (vo == null)
|
||||
throw new RuntimeException("Cannot find the Property Group in AdapterPropManager. - " + group);
|
||||
if (vo.getProperties() == null) {
|
||||
return new Properties();
|
||||
}
|
||||
return (Properties) vo.getProperties().clone();
|
||||
}
|
||||
|
||||
public void setProperty(String group, String key, String value) {
|
||||
AdapterPropGroupVO gvo = groups.get(group);
|
||||
String oldValue = gvo.getProperty(key);
|
||||
gvo.setProperty(key, value);
|
||||
|
||||
if (oldValue != null && !oldValue.equals(value)) {
|
||||
property.firePropertyChange(createEvent(gvo, key, oldValue, value));
|
||||
}
|
||||
}
|
||||
|
||||
public void setAdapterPropGroupVO(String group, AdapterPropGroupVO vo) {
|
||||
groups.put(group, vo);
|
||||
}
|
||||
|
||||
public void removeAdapterPropGroupVO(String name) {
|
||||
groups.remove(name);
|
||||
}
|
||||
|
||||
public String[] getAllPropGroupNames() {
|
||||
Iterator<String> it = this.groups.keySet().iterator();
|
||||
String[] name = new String[this.groups.size()];
|
||||
for (int i = 0; it.hasNext(); i++) {
|
||||
name[i] = it.next();
|
||||
}
|
||||
|
||||
Arrays.sort(name);
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.eactive.eai.adapter.listener.AdapterException;
|
||||
import com.eactive.eai.adapter.listener.AdapterListener;
|
||||
import com.eactive.eai.adapter.listener.ListenerUtil;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.lifecycle.Lifecycle;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleException;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleListener;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleSupport;
|
||||
import com.eactive.eai.common.testcall.TestCallManager;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class AdapterVO implements Serializable, Lifecycle {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
private AdapterGroupVO adapterGroupVO; // 어댑터 그룹
|
||||
|
||||
private String adapterGroupName; // 어뎁터업무그룹명[TSEAIAD02.AdptrBzwkGroupName]
|
||||
private String name; // 어뎁터업무명[TSEAIAD02.AdptrBzwkName]
|
||||
private String eaiSevrInstncName; // 서버인스탄스명[TSEAIAD02.EAISevrInstncName] - migration added.
|
||||
private String listenerClass; // 리스너클래스명[TSEAIAD02.LstnerClsName]
|
||||
private String testCallClass; // 테스터클래스명[TSEAIAD02.TestClsName]
|
||||
private String propGroupName; // 프라퍼티그룹명[TSEAIAD02.PrptyGroupName]
|
||||
private String description; // 어댑터설명[TSEAIAD02.AdptrDesc]
|
||||
private String propGroupDesc; // 프라퍼티그룹설명
|
||||
|
||||
private boolean status;
|
||||
private transient AdapterListener adapterListener;
|
||||
private boolean started;
|
||||
private LifecycleSupport lifecycle = new LifecycleSupport(this);
|
||||
|
||||
private int requestCount;
|
||||
private int weight;
|
||||
|
||||
public int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(int weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public int getRequestCount() {
|
||||
return requestCount;
|
||||
}
|
||||
|
||||
public synchronized void incrementRequestCount() {
|
||||
requestCount++;
|
||||
}
|
||||
|
||||
public synchronized void decrementRequestCount() {
|
||||
requestCount--;
|
||||
}
|
||||
|
||||
public AdapterVO() {
|
||||
this.requestCount = 0;
|
||||
this.weight = 1;
|
||||
}
|
||||
|
||||
public AdapterVO(String name) {
|
||||
this.name = name;
|
||||
this.requestCount = 0;
|
||||
this.weight = 1;
|
||||
}
|
||||
|
||||
public boolean isStarted() {
|
||||
return this.started;
|
||||
}
|
||||
|
||||
public void setAdapterGroupName(String adapterGroupName) {
|
||||
this.adapterGroupName = adapterGroupName;
|
||||
}
|
||||
|
||||
public String getAdapterGroupName() {
|
||||
return this.adapterGroupName;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setListenerClass(String listenerClass) {
|
||||
this.listenerClass = listenerClass;
|
||||
}
|
||||
|
||||
public String getListenerClass() {
|
||||
return this.listenerClass;
|
||||
}
|
||||
|
||||
public void setTestCallClass(String testCallClass) {
|
||||
this.testCallClass = testCallClass;
|
||||
}
|
||||
|
||||
public String getTestCallClass() {
|
||||
return this.testCallClass;
|
||||
}
|
||||
|
||||
public void setPropGroupName(String propGroupName) {
|
||||
this.propGroupName = propGroupName;
|
||||
}
|
||||
|
||||
public String getPropGroupName() {
|
||||
return this.propGroupName;
|
||||
}
|
||||
|
||||
public void setAdapterGroupVO(AdapterGroupVO group) {
|
||||
this.adapterGroupVO = group;
|
||||
}
|
||||
|
||||
public AdapterGroupVO getAdapterGroupVO() {
|
||||
return this.adapterGroupVO;
|
||||
}
|
||||
|
||||
private void setStatus(boolean status, boolean checkTestCall) {
|
||||
this.status = status;
|
||||
|
||||
if (checkTestCall && !this.status && this.testCallClass != null && !this.testCallClass.equals("")) {
|
||||
TestCallManager tmanager = TestCallManager.getInstance();
|
||||
try {
|
||||
tmanager.addTester(this);
|
||||
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = this.toString();
|
||||
if (logger.isInfo())
|
||||
logger.info(ExceptionUtil.make("RICEAIAAC034", msgArgs));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(ExceptionUtil.getErrorCode(e, "RICEAIAAC035"), e);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.status) {
|
||||
this.adapterGroupVO.active(this);
|
||||
} else {
|
||||
this.adapterGroupVO.disable(this);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void setStatus(boolean status) {
|
||||
setStatus(status, true);
|
||||
}
|
||||
|
||||
public synchronized void setStatusForcely(boolean status) {
|
||||
setStatus(status, false);
|
||||
}
|
||||
|
||||
public synchronized boolean isStatus() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
public void start() throws LifecycleException {
|
||||
if (started)
|
||||
throw new LifecycleException("RECEAIAAC036");
|
||||
|
||||
lifecycle.fireLifecycleEvent(STARTING_EVENT, this);
|
||||
|
||||
if (StringUtils.isEmpty(listenerClass)) {
|
||||
listenerClass = ListenerUtil.getDefaultListenerClass(adapterGroupVO.getType(), adapterGroupVO.getGubun());
|
||||
if(logger.isWarn() && StringUtils.isNotEmpty(listenerClass)) {
|
||||
logger.warn("Use DefaultListenerClass {} - {}",adapterGroupVO.getName(), listenerClass);
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.isNotEmpty(listenerClass)) {
|
||||
try {
|
||||
adapterListener = (AdapterListener) Class.forName(listenerClass).newInstance();
|
||||
adapterListener.setAdapterInfo(this);
|
||||
adapterListener.start();
|
||||
} catch (Exception e) {
|
||||
throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAIAAC037"), e);
|
||||
}
|
||||
}
|
||||
|
||||
started = true;
|
||||
// SocketAdapter 기동시에 설정하므로
|
||||
// socket2일 때는 status 를 setting 하지 않음.
|
||||
if (adapterListener == null) {
|
||||
status = true;
|
||||
this.adapterGroupVO.active(this);
|
||||
} else {
|
||||
if (!(adapterListener instanceof com.eactive.eai.adapter.socket2.SocketAdapterListener)) {
|
||||
status = true;
|
||||
} else {
|
||||
this.adapterGroupVO.active(this);
|
||||
}
|
||||
}
|
||||
lifecycle.fireLifecycleEvent(STARTED_EVENT, this);
|
||||
if(logger.isWarn()) {
|
||||
logger.warn("Adapter started {} {} - {}",adapterGroupVO.getName(), getName(), listenerClass);
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() throws LifecycleException {
|
||||
if (!started)
|
||||
throw new LifecycleException("RECEAIAAC038");
|
||||
|
||||
lifecycle.fireLifecycleEvent(STOPING_EVENT, this);
|
||||
|
||||
try {
|
||||
if (adapterListener != null)
|
||||
adapterListener.stop();
|
||||
} catch (AdapterException e) {
|
||||
throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAIAAC039"), e);
|
||||
}
|
||||
|
||||
started = false;
|
||||
status = false;
|
||||
this.adapterGroupVO.disable(this);
|
||||
lifecycle.fireLifecycleEvent(STOPPED_EVENT, this);
|
||||
}
|
||||
|
||||
public void addLifecycleListener(LifecycleListener listener) {
|
||||
lifecycle.addLifecycleListener(listener);
|
||||
}
|
||||
|
||||
public LifecycleListener[] findLifecycleListeners() {
|
||||
return lifecycle.findLifecycleListeners();
|
||||
}
|
||||
|
||||
public void removeLifecycleListener(LifecycleListener listener) {
|
||||
lifecycle.removeLifecycleListener(listener);
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("AdapterVO[ name=").append(name);
|
||||
sb.append(", adapterGroupName=").append(adapterGroupName);
|
||||
sb.append(", eaiSevrInstncName=").append(eaiSevrInstncName);
|
||||
sb.append(", listenerClass=").append(listenerClass);
|
||||
sb.append(", testCallClass=").append(testCallClass);
|
||||
sb.append(", propGroupName=").append(propGroupName);
|
||||
sb.append(", description=").append(description);
|
||||
sb.append(", status=").append(status);
|
||||
sb.append(", requestCount=").append(requestCount);
|
||||
sb.append(" ]");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void setEaiSevrInstncName(String eaiSevrInstncName) {
|
||||
this.eaiSevrInstncName = eaiSevrInstncName;
|
||||
}
|
||||
|
||||
public String getEaiSevrInstncName() {
|
||||
return this.eaiSevrInstncName;
|
||||
}
|
||||
|
||||
public void setPropGroupDesc(String propGroupDesc) {
|
||||
this.propGroupDesc = propGroupDesc;
|
||||
}
|
||||
|
||||
public String getPropGroupDesc() {
|
||||
return this.propGroupDesc;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
public class EAIException extends Exception {
|
||||
private String code;
|
||||
|
||||
public EAIException(String code, String msg) {
|
||||
super(msg);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public EAIException(String msg) {
|
||||
this("", msg);
|
||||
}
|
||||
|
||||
public EAIException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public EAIException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
public String getExceptionCode() {
|
||||
return this.code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
// EAI 엔진에서 어댑터을 common하게 접근하기 위한 Interface
|
||||
public interface ElinkAdapter {
|
||||
public Object callService(String adapterGroupName, Properties prop, Object message, Properties tempProp)
|
||||
throws Exception;
|
||||
|
||||
public Object execute(Properties prop, Object message, Properties tempProp) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.eactive.eai.adapter.amq.AMQSenderAdapter;
|
||||
import com.eactive.eai.adapter.app.BusinessApplicationAdapter;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterMain;
|
||||
import com.eactive.eai.adapter.jtc.JtcClientAdapter;
|
||||
import com.eactive.eai.adapter.socket2.SocketAdapterFacade;
|
||||
import com.eactive.eai.adapter.wca.SNAAdapterClient;
|
||||
import com.eactive.eai.adapter.websocket.WebSocketAdapterFacade;
|
||||
import com.eactive.eai.adapter.webt.WebTClientAdapter;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class ElinkAdapterFactory {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
private static final ElinkAdapterFactory factory = new ElinkAdapterFactory();
|
||||
private ConcurrentHashMap<String, ElinkAdapter> adapterMap;
|
||||
|
||||
private ElinkAdapterFactory() {
|
||||
adapterMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
public static ElinkAdapterFactory newInstance() {
|
||||
return factory;
|
||||
}
|
||||
|
||||
public ElinkAdapter getElinkAdapter(String adapterTypeName) throws Exception {
|
||||
ElinkAdapter adapter = adapterMap.get(adapterTypeName);
|
||||
if (adapter == null) {
|
||||
adapter = createAdapter(adapterTypeName);
|
||||
}
|
||||
if (adapter == null) {
|
||||
throw new RuntimeException("등록된 어댑터 타입을 찾을 수 없습니다. - " + adapterTypeName);
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
|
||||
private ElinkAdapter createAdapter(String adapterTypeName) throws Exception {
|
||||
if (adapterTypeName == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ElinkAdapter adapter = null;
|
||||
|
||||
if (adapterTypeName.equals(Keys.TYPE_HTTP)) {
|
||||
adapter = new HttpClientAdapterMain();
|
||||
adapterMap.put(Keys.TYPE_HTTP, adapter);
|
||||
} else if (adapterTypeName.equals(Keys.TYPE_NET2)) {
|
||||
adapter = SocketAdapterFacade.getInstance();
|
||||
adapterMap.put(Keys.TYPE_NET2, adapter);
|
||||
} else if (adapterTypeName.equals(Keys.TYPE_WSK)) {
|
||||
adapter = WebSocketAdapterFacade.getInstance();
|
||||
adapterMap.put(Keys.TYPE_WSK, adapter);
|
||||
} else if (adapterTypeName.equals(Keys.TYPE_WBT)) {
|
||||
adapter = new WebTClientAdapter();
|
||||
adapterMap.put(Keys.TYPE_WBT, adapter);
|
||||
} else if (adapterTypeName.equals(Keys.TYPE_JTC)) {
|
||||
adapter = new JtcClientAdapter();
|
||||
adapterMap.put(Keys.TYPE_JTC, adapter);
|
||||
} else if (adapterTypeName.equals(Keys.TYPE_APP)) {
|
||||
adapter = new BusinessApplicationAdapter();
|
||||
adapterMap.put(Keys.TYPE_APP, adapter);
|
||||
} else if (adapterTypeName.equals(Keys.TYPE_SNA)) {
|
||||
adapter = new SNAAdapterClient();
|
||||
adapterMap.put(Keys.TYPE_SNA, adapter);
|
||||
} else if (adapterTypeName.equals(Keys.TYPE_AMQ)) {
|
||||
adapter = new AMQSenderAdapter();
|
||||
adapterMap.put(Keys.TYPE_SNA, adapter);
|
||||
} else {
|
||||
logger.warn("Unknown adapter type : " + adapterTypeName);
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class ElinkDefaultAdapter implements ElinkAdapter {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
private AdapterManager manager;
|
||||
|
||||
public ElinkDefaultAdapter() {
|
||||
this.manager = AdapterManager.getInstance();
|
||||
}
|
||||
|
||||
public Object callService(String adapterGroupName, Properties prop, Object message, Properties tempProp)
|
||||
throws Exception {
|
||||
String adapterName = tempProp.getProperty(com.eactive.eai.adapter.Keys.OUT_ADAPTER_NAME);
|
||||
AdapterGroupVO group = this.manager.getAdapterGroupVO(adapterGroupName);
|
||||
if (group == null) {
|
||||
String code = "RECEAIAAC027";
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = adapterGroupName;
|
||||
String errText = ExceptionUtil.make(code, msgArgs);
|
||||
if (logger.isError())
|
||||
logger.error(errText);
|
||||
throw new Exception(errText);
|
||||
}
|
||||
|
||||
AdapterVO adapterVO = null;
|
||||
if(StringUtils.isNotEmpty(adapterName)) {
|
||||
adapterVO = group.getAdapterVO(adapterName);
|
||||
}
|
||||
|
||||
group.increase();
|
||||
|
||||
tempProp.setProperty(com.eactive.eai.adapter.Keys.OUT_ADAPTER_GROUP_NAME, adapterGroupName);
|
||||
// set AdapterGroup charset
|
||||
tempProp.setProperty(com.eactive.eai.adapter.Keys.CHARSET_KEY_OUT,
|
||||
StringUtils.defaultIfBlank(group.getMessageEncode(), Charset.defaultCharset().name()));
|
||||
|
||||
try {
|
||||
if(adapterVO != null) adapterVO.incrementRequestCount();
|
||||
return execute(prop, message, tempProp);
|
||||
} catch (SocketTimeoutException ste) {
|
||||
throw ste;
|
||||
} catch (HttpStatusException hse) {
|
||||
throw hse;
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
} finally {
|
||||
group.decrease();
|
||||
if(adapterVO != null) adapterVO.decrementRequestCount();
|
||||
}
|
||||
}
|
||||
|
||||
public Object execute(Properties prop, Object message, Properties tempProp) throws Exception {
|
||||
throw new java.lang.UnsupportedOperationException("RECEAIAAC029");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
// adapter 패키지에서 사용하는 상수를 정의한 Interface
|
||||
public interface Keys {
|
||||
public static final String OUT_ADAPTER_GROUP_NAME = "OUT_ADAPTER_GROUP_NAME";
|
||||
public static final String OUT_ADAPTER_NAME = "OUT_ADAPTER_NAME";
|
||||
|
||||
public static final String GUBUN_IN = "INBOUND";
|
||||
public static final String GUBUN_OUT = "OUTBOUND";
|
||||
|
||||
public static final String LB_WEIGHT = "LB_WEIGHT";
|
||||
|
||||
// inbound adapter charset
|
||||
public static final String CHARSET_KEY_IN = "CHARSET_IN";
|
||||
// outbound adapter charset
|
||||
public static final String CHARSET_KEY_OUT = "CHARSET_OUT";
|
||||
|
||||
public static final String TYPE_HTTP = "HTT";
|
||||
//HTTP custom port - undertow
|
||||
public static final String TYPE_HTTP_CUSTOM = "HTC";
|
||||
public static final String TYPE_REST = "RST";
|
||||
public static final String TYPE_NET2 = "NET";
|
||||
public static final String TYPE_SNA = "SNA";
|
||||
public static final String TYPE_WSK = "WSK";
|
||||
public static final String TYPE_WBT = "WBT";
|
||||
public static final String TYPE_JTC = "JTC";
|
||||
public static final String TYPE_JCO = "JCO";
|
||||
public static final String TYPE_APP = "APP";
|
||||
public static final String TYPE_AMQ = "AMQ";
|
||||
|
||||
public static final String IF_NONSTANDARD = "N";
|
||||
public static final String IF_STANDARD = "K";
|
||||
public static final String IF_SUBSTANDARD = "B";
|
||||
|
||||
public static final String TESTMASTER_DEFAULT = "0";
|
||||
public static final String TESTMASTER_TAS = "1";
|
||||
public static final String TESTMASTER_TAS_INFO = "2";
|
||||
|
||||
public static final String ADAPTRER_IN = "I";
|
||||
public static final String ADAPTER_OUT = "O";
|
||||
|
||||
public static final String NO_KESA = "0";
|
||||
public static final String N_KESA = "1";
|
||||
public static final String U_KESA = "2";
|
||||
public static final String Z_KESA = "3";
|
||||
public static final String CARD_KESA = "C";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package com.eactive.eai.adapter;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.message.MessageType;
|
||||
import com.eactive.eai.common.server.EAIServerManager;
|
||||
import com.eactive.eai.common.state.ElinkState;
|
||||
import com.eactive.eai.common.state.ElinkStateException;
|
||||
import com.eactive.eai.common.state.ElinkStateManager;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.inbound.processor.Processor;
|
||||
import com.eactive.eai.inbound.processor.ProcessorFactory;
|
||||
|
||||
public class RequestDispatcher {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
private static boolean isTASEnabledEAIServer = true;
|
||||
static {
|
||||
EAIServerManager eaiServerManager = EAIServerManager.getInstance();
|
||||
isTASEnabledEAIServer = eaiServerManager.isTASEnabledEAIServer();
|
||||
}
|
||||
|
||||
private static ConcurrentHashMap<String, RequestDispatcher> dispatchers = new ConcurrentHashMap<>();
|
||||
|
||||
private String adapterGroupName;
|
||||
private Processor processor;
|
||||
|
||||
private RequestDispatcher(String adapterGroupName) {
|
||||
this.adapterGroupName = adapterGroupName;
|
||||
processor = ProcessorFactory.newInstance();
|
||||
}
|
||||
|
||||
public static RequestDispatcher getRequestDispatcher(String adapterGroupName) {
|
||||
return dispatchers.computeIfAbsent(adapterGroupName, k -> new RequestDispatcher(adapterGroupName));
|
||||
}
|
||||
|
||||
public Object handle(String adapterName, Object message, Properties prop) throws Exception {
|
||||
AdapterManager manager = AdapterManager.getInstance();
|
||||
|
||||
String inAdapterGroupName = this.adapterGroupName;
|
||||
String actionClass = "";
|
||||
String adptrMsgPtrnCd = "";
|
||||
String messageType = MessageType.ASC;
|
||||
|
||||
AdapterGroupVO group = manager.getAdapterGroupVO(inAdapterGroupName);
|
||||
if (group == null) {
|
||||
String code = "RECEAIAAC042";
|
||||
String[] msgArgs = new String[2];
|
||||
msgArgs[0] = adapterGroupName;
|
||||
msgArgs[1] = adapterName;
|
||||
String errText = ExceptionUtil.make(code, msgArgs);
|
||||
if (logger.isError()) {
|
||||
logger.error(errText);
|
||||
}
|
||||
throw new Exception(errText);
|
||||
}
|
||||
|
||||
AdapterVO adapter = group.getAdapterVO(adapterName);
|
||||
if (adapter == null) {
|
||||
String code = "RECEAIAAC022";
|
||||
String[] msgArgs = { adapterGroupName, adapterName, group.toString() };
|
||||
String errText = ExceptionUtil.make(code, msgArgs);
|
||||
if (logger.isError()) {
|
||||
logger.error(errText);
|
||||
}
|
||||
throw new Exception(errText);
|
||||
}
|
||||
|
||||
if (prop == null) {
|
||||
prop = new Properties();
|
||||
}
|
||||
|
||||
actionClass = group.getRefClass();
|
||||
adptrMsgPtrnCd = group.getAdptrMsgPtrnCd();
|
||||
messageType = group.getMessageType();
|
||||
try {
|
||||
prop.put(Processor.ADAPTER_GROUP_NAME, inAdapterGroupName);
|
||||
prop.put(Processor.ADAPTER_NAME, adapterName);
|
||||
prop.put(Processor.STD_MESSAGE, adptrMsgPtrnCd);
|
||||
prop.put(Processor.REQUEST_ACTION, actionClass);
|
||||
prop.put(Processor.MESSAGE_TYPE, messageType);
|
||||
|
||||
// set Inbound AdapterGroup charset
|
||||
prop.setProperty(com.eactive.eai.adapter.Keys.CHARSET_KEY_IN,
|
||||
StringUtils.defaultIfBlank(group.getMessageEncode(), Charset.defaultCharset().name()));
|
||||
} catch (Exception e) {
|
||||
throw new Exception(ExceptionUtil.getErrorCode(e, "RECEAIAAC024"));
|
||||
}
|
||||
|
||||
Object obj = null;
|
||||
|
||||
// Check Elink lifecycle is STARTED
|
||||
// otherwise reject this request
|
||||
if(ElinkStateManager.getInstance().getState() != ElinkState.STARTED) {
|
||||
logger.error("Elink STATE FAIL - " + ElinkStateManager.getInstance().getState());
|
||||
throw new ElinkStateException("Elink STATE FAIL - " + ElinkStateManager.getInstance().getState());
|
||||
}
|
||||
|
||||
try {
|
||||
ElinkStateManager.getInstance().startRequest();
|
||||
group.increase();
|
||||
adapter.incrementRequestCount();
|
||||
obj = processor.execute(message, prop);
|
||||
group.increaseSuccess();
|
||||
} catch (HttpStatusException e) {
|
||||
group.increaseError();
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
group.increaseError();
|
||||
throw new Exception(ExceptionUtil.getErrorCode(e, "RECEAIAAC025"));
|
||||
} finally {
|
||||
group.decrease();
|
||||
adapter.decrementRequestCount();
|
||||
ElinkStateManager.getInstance().completeRequest();
|
||||
}
|
||||
|
||||
if (obj != null) {
|
||||
return obj;
|
||||
}
|
||||
else {
|
||||
if (message instanceof byte[]) {
|
||||
return new byte[0];
|
||||
} else if (message instanceof String) {
|
||||
return "";
|
||||
} else {
|
||||
return new Object();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.eactive.eai.adapter.amq;
|
||||
|
||||
public class AMQAdapterConstants {
|
||||
|
||||
// adapter properties
|
||||
public static final String PROP_BROKER_URL = "BROKER_URL";
|
||||
public static final String PROP_USER_NAME = "BROKER_USER_NAME";
|
||||
public static final String PROP_PASSWORD = "BROKER_PASSWORD";
|
||||
public static final String PROP_POOL_MAX_CONNECTIONS = "POOL_MAX_CONNECTIONS";
|
||||
public static final String PROP_POOL_IDLE_TIMEOUT = "POOL_IDLE_TIMEOUT";
|
||||
public static final String PROP_POOL_EXPIRY_TIMEOUT = "POOL_EXPIRY_TIMEOUT";
|
||||
public static final String PROP_REQUEST_QUEUE_NAME = "REQUEST_QUEUE_NAME";
|
||||
public static final String PROP_REPLY_QUEUE_NAME = "REPLY_QUEUE_NAME";
|
||||
public static final String PROP_MESSAGE_TIME_TO_LIVE = "MESSAGE_TIME_TO_LIVE";
|
||||
public static final String PROP_MESSAGE_RECEIVE_TIMEOUT = "REPLY_MESSAGE_RECEIVE_TIMEOUT";
|
||||
public static final String PROP_RECEIVE_QUEUE_NAME = "RECEIVE_QUEUE_NAME";
|
||||
public static final String PROP_RECEIVE_MAX_CONCURRENT_CONSUMERS = "RECEIVE_MAX_CONCURRENT_CONSUMERS";
|
||||
public static final String PROP_REPLY_POOL_MAX_CONNECTIONS = "REPLY_POOL_MAX_CONNECTIONS";
|
||||
public static final String PROP_REPLY_POOL_IDLE_TIMEOUT = "REPLY_POOL_IDLE_TIMEOUT";
|
||||
public static final String PROP_REPLY_POOL_EXPIRY_TIMEOUT = "REPLY_POOL_EXPIRY_TIMEOUT";
|
||||
public static final String PROP_RELAY_PROPERTY_GROUP = "RELAY_PROPERTY_GROUP";
|
||||
public static final String PROP_RELAY_REQUEST_PROPERTY_KEYS = "RELAY_REQUEST_PROPERTY_KEYS";
|
||||
public static final String PROP_RELAY_RESPONSE_PROPERTY_KEYS = "RELAY_RESPONSE_PROPERTY_KEYS";
|
||||
|
||||
public static final String TEMP_INTERFACE_RESPONSE_TYPE = "INTERFACE_RESPONSE_TYPE";
|
||||
public static final String TEMP_MESSAGE_TYPE = "MESSAGE_TYPE";
|
||||
|
||||
public static final int DEFAULT_POOL_MAX_CONNECTIONS = 8;
|
||||
public static final long DEFAULT_MESSAGE_RECEIVE_TIMEOUT = 30;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.eactive.eai.adapter.amq;
|
||||
|
||||
import com.eactive.eai.adapter.listener.AdapterException;
|
||||
import com.eactive.eai.adapter.listener.AdapterListenerSupport;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
/**
|
||||
* @formatter:off
|
||||
* ActiveMQ 어댑터 LISTENER 클래스
|
||||
* ActiveMQ 어댑터를 기동/중지한다
|
||||
* @formatter:on
|
||||
*/
|
||||
public class AMQAdapterListener extends AdapterListenerSupport {
|
||||
public static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
@Override
|
||||
public void start() throws AdapterException {
|
||||
if (logger.isInfo()) {
|
||||
logger.info("= AMQAdapterListener start.........");
|
||||
}
|
||||
|
||||
try {
|
||||
if (info == null) {
|
||||
throw new Exception("AMQAdapterListener | AdapterVO가 NULL입니다.");
|
||||
}
|
||||
|
||||
AMQAdapterManager manager = AMQAdapterManager.getInstance();
|
||||
info.setStatus(true);
|
||||
manager.start(info);
|
||||
} catch (Throwable e) {
|
||||
logger.error("[" + info.getName() + "] 구동 실패", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws AdapterException {
|
||||
if (logger.isInfo()) {
|
||||
logger.info("= AMQAdapterListener stop.........");
|
||||
}
|
||||
|
||||
try {
|
||||
if (info == null) {
|
||||
throw new Exception("AMQAdapterListener | AdapterVO가 NULL입니다.");
|
||||
}
|
||||
|
||||
AMQAdapterManager manager = AMQAdapterManager.getInstance();
|
||||
manager.stop(info);
|
||||
info.setStatus(false);
|
||||
} catch (Throwable e) {
|
||||
logger.error("[" + info.getName() + "] 중지 실패", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.eactive.eai.adapter.amq;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
import org.apache.activemq.jms.pool.PooledConnectionFactory;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterGroupVO;
|
||||
import com.eactive.eai.adapter.AdapterPropManager;
|
||||
import com.eactive.eai.adapter.AdapterVO;
|
||||
import com.eactive.eai.agent.command.CommandException;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class AMQAdapterManager {
|
||||
public static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
private static AMQAdapterManager instance = new AMQAdapterManager();
|
||||
|
||||
// adapterName - Outbound Queue PooledConnectionFactory
|
||||
private ConcurrentHashMap<String, PooledConnectionFactory> adapterConFacMap = new ConcurrentHashMap<>();
|
||||
|
||||
// adapterName - Inbound Queue Listener
|
||||
private ConcurrentHashMap<String, AMQReceiverAdapter> receiverAdapterMap = new ConcurrentHashMap<>();
|
||||
|
||||
public static AMQAdapterManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public PooledConnectionFactory getOutboundPooledConnectionFactory(String adapterName) {
|
||||
return adapterConFacMap.get(adapterName);
|
||||
}
|
||||
|
||||
public void start(AdapterVO adapterVO) throws Exception {
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("= AMQAdapterManager.start...");
|
||||
}
|
||||
|
||||
AdapterGroupVO adapterGroupVO = adapterVO.getAdapterGroupVO();
|
||||
AdapterPropManager propManager = AdapterPropManager.getInstance();
|
||||
Properties queueProp = propManager.getProperties(adapterVO.getPropGroupName());
|
||||
String brokerUrl = queueProp.getProperty(AMQAdapterConstants.PROP_BROKER_URL);
|
||||
if (StringUtils.isBlank(brokerUrl)) {
|
||||
throw new CommandException(AMQAdapterConstants.PROP_BROKER_URL + " is a required.");
|
||||
}
|
||||
|
||||
if (StringUtils.equals(adapterGroupVO.getGubun(), "I")) {
|
||||
AMQReceiverAdapter receiverAdapter = new AMQReceiverAdapter(adapterVO);
|
||||
receiverAdapter.start();
|
||||
receiverAdapterMap.put(adapterVO.getName(), receiverAdapter);
|
||||
|
||||
// Sync 거래일 경우 응답 Queue 연결
|
||||
String replyQueue = queueProp.getProperty(AMQAdapterConstants.PROP_REPLY_QUEUE_NAME);
|
||||
if (StringUtils.isNotBlank(replyQueue)) {
|
||||
String userName = queueProp.getProperty(AMQAdapterConstants.PROP_USER_NAME);
|
||||
String password = queueProp.getProperty(AMQAdapterConstants.PROP_PASSWORD);
|
||||
String maxConnections = queueProp.getProperty(AMQAdapterConstants.PROP_REPLY_POOL_MAX_CONNECTIONS);
|
||||
String idleTimeout = queueProp.getProperty(AMQAdapterConstants.PROP_REPLY_POOL_IDLE_TIMEOUT);
|
||||
String expiryTimeout = queueProp.getProperty(AMQAdapterConstants.PROP_REPLY_POOL_EXPIRY_TIMEOUT);
|
||||
adapterConFacMap.put(adapterVO.getName(), getPooledConnectionFactory(brokerUrl, userName, password,
|
||||
maxConnections, idleTimeout, expiryTimeout));
|
||||
}
|
||||
} else {
|
||||
String userName = queueProp.getProperty(AMQAdapterConstants.PROP_USER_NAME);
|
||||
String password = queueProp.getProperty(AMQAdapterConstants.PROP_PASSWORD);
|
||||
String maxConnections = queueProp.getProperty(AMQAdapterConstants.PROP_POOL_MAX_CONNECTIONS);
|
||||
String idleTimeout = queueProp.getProperty(AMQAdapterConstants.PROP_POOL_IDLE_TIMEOUT);
|
||||
String expiryTimeout = queueProp.getProperty(AMQAdapterConstants.PROP_POOL_EXPIRY_TIMEOUT);
|
||||
adapterConFacMap.put(adapterVO.getName(), getPooledConnectionFactory(brokerUrl, userName, password,
|
||||
maxConnections, idleTimeout, expiryTimeout));
|
||||
}
|
||||
}
|
||||
|
||||
public void stop(AdapterVO adapterVO) throws Exception {
|
||||
AdapterGroupVO adapterGroupVO = adapterVO.getAdapterGroupVO();
|
||||
|
||||
if (StringUtils.equals(adapterGroupVO.getGubun(), "I")) {
|
||||
AMQReceiverAdapter receiverAdapter = receiverAdapterMap.get(adapterVO.getName());
|
||||
if (receiverAdapter != null) {
|
||||
receiverAdapterMap.remove(adapterVO.getName());
|
||||
receiverAdapter.stop();
|
||||
}
|
||||
|
||||
// Sync 거래일 경우 응답 Queue 연결 해제
|
||||
PooledConnectionFactory connectionFactory = adapterConFacMap.get(adapterVO.getName());
|
||||
if (connectionFactory != null) {
|
||||
adapterConFacMap.remove(adapterVO.getName());
|
||||
connectionFactory.clear();
|
||||
}
|
||||
} else {
|
||||
PooledConnectionFactory connectionFactory = adapterConFacMap.get(adapterVO.getName());
|
||||
if (connectionFactory != null) {
|
||||
adapterConFacMap.remove(adapterVO.getName());
|
||||
connectionFactory.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("= AMQAdapterManager.stoped...");
|
||||
}
|
||||
}
|
||||
|
||||
private PooledConnectionFactory getPooledConnectionFactory(String brokerUrl, String userName, String password,
|
||||
String maxConnections, String idleTimeout, String expiryTimeout) throws Exception {
|
||||
// Queue Connection pool 생성
|
||||
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(brokerUrl);
|
||||
if (StringUtils.isNotBlank(userName)) {
|
||||
activeMQConnectionFactory.setUserName(userName);
|
||||
activeMQConnectionFactory.setPassword(StringUtils.defaultString(password));
|
||||
}
|
||||
activeMQConnectionFactory.setTrustAllPackages(true);
|
||||
|
||||
PooledConnectionFactory connectionFactory = new PooledConnectionFactory();
|
||||
connectionFactory.setConnectionFactory(activeMQConnectionFactory);
|
||||
connectionFactory
|
||||
.setMaxConnections(NumberUtils.toInt(maxConnections, AMQAdapterConstants.DEFAULT_POOL_MAX_CONNECTIONS));
|
||||
if (NumberUtils.isParsable(idleTimeout)) {
|
||||
connectionFactory.setIdleTimeout(NumberUtils.toInt(idleTimeout) * 1000);
|
||||
}
|
||||
if (NumberUtils.isParsable(expiryTimeout)) {
|
||||
connectionFactory.setExpiryTimeout(NumberUtils.toLong(expiryTimeout) * 1000);
|
||||
}
|
||||
|
||||
return connectionFactory;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
package com.eactive.eai.adapter.amq;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageListener;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
import org.apache.activemq.jms.pool.PooledConnectionFactory;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.jms.core.MessageCreator;
|
||||
import org.springframework.jms.listener.DefaultMessageListenerContainer;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterPropManager;
|
||||
import com.eactive.eai.adapter.AdapterVO;
|
||||
import com.eactive.eai.adapter.RequestDispatcher;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceKey;
|
||||
import com.eactive.eai.agent.command.CommandException;
|
||||
import com.eactive.eai.common.message.MessageType;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class AMQReceiverAdapter {
|
||||
private static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
private AdapterVO adapterVo;
|
||||
DefaultMessageListenerContainer listenerContainer = null;
|
||||
|
||||
public AMQReceiverAdapter(AdapterVO adapterVo) {
|
||||
this.adapterVo = adapterVo;
|
||||
}
|
||||
|
||||
public void start() throws Exception {
|
||||
AdapterPropManager propManager = AdapterPropManager.getInstance();
|
||||
Properties queueProp = propManager.getProperties(adapterVo.getPropGroupName());
|
||||
String brokerUrl = queueProp.getProperty(AMQAdapterConstants.PROP_BROKER_URL);
|
||||
if (StringUtils.isBlank(brokerUrl)) {
|
||||
throw new CommandException(AMQAdapterConstants.PROP_BROKER_URL + " is a required.");
|
||||
}
|
||||
String userName = queueProp.getProperty(AMQAdapterConstants.PROP_USER_NAME);
|
||||
String password = queueProp.getProperty(AMQAdapterConstants.PROP_PASSWORD);
|
||||
String receiveQueue = queueProp.getProperty(AMQAdapterConstants.PROP_RECEIVE_QUEUE_NAME);
|
||||
if (StringUtils.isBlank(receiveQueue)) {
|
||||
throw new Exception("No have receive Queue name");
|
||||
}
|
||||
String concurrency = queueProp.getProperty(AMQAdapterConstants.PROP_RECEIVE_MAX_CONCURRENT_CONSUMERS);
|
||||
String replyQueue = queueProp.getProperty(AMQAdapterConstants.PROP_REPLY_QUEUE_NAME);
|
||||
String relayPropertyGroup = queueProp.getProperty(AMQAdapterConstants.PROP_RELAY_PROPERTY_GROUP);
|
||||
String relayRequestPropertyKeys = queueProp.getProperty(AMQAdapterConstants.PROP_RELAY_REQUEST_PROPERTY_KEYS);
|
||||
|
||||
// Queue Connection pool 생성
|
||||
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
|
||||
if (StringUtils.isNotBlank(userName)) {
|
||||
connectionFactory.setUserName(userName);
|
||||
connectionFactory.setPassword(StringUtils.defaultString(password));
|
||||
}
|
||||
connectionFactory.setTrustAllPackages(true);
|
||||
|
||||
listenerContainer = new DefaultMessageListenerContainer();
|
||||
listenerContainer.setConnectionFactory(connectionFactory);
|
||||
listenerContainer.setDestinationName(receiveQueue);
|
||||
if (NumberUtils.isParsable(concurrency)) {
|
||||
listenerContainer.setConcurrency(concurrency);
|
||||
}
|
||||
listenerContainer.setMessageListener(new MessageListener() {
|
||||
@Override
|
||||
public void onMessage(Message message) {
|
||||
try {
|
||||
if (message instanceof TextMessage) {
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("Received from " + receiveQueue + ": " + ((TextMessage) message).getText());
|
||||
}
|
||||
|
||||
String receivedMsg = convertMessageToString(message);
|
||||
|
||||
Properties prop = new Properties();
|
||||
// RestDataAdapterGroupParseRequestAction 오류 방지 위해 set
|
||||
prop.setProperty(HttpAdapterServiceKey.INBOUND_METHOD, "QUEUE");
|
||||
|
||||
Object returnObject = RequestDispatcher.getRequestDispatcher(adapterVo.getAdapterGroupName())
|
||||
.handle(adapterVo.getName(), receivedMsg, prop);
|
||||
|
||||
// reply queue가 설정되어 있으면(Sync) 전송
|
||||
if (StringUtils.isNotBlank(replyQueue)) {
|
||||
replyMessage((String) returnObject, replyQueue, relayPropertyGroup);
|
||||
}
|
||||
} else {
|
||||
logger.error("Only supports TextMessage.");
|
||||
throw new IllegalArgumentException(
|
||||
"Only supports TextMessage: " + message.getClass().getName());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "deprecation", "unchecked" })
|
||||
private String convertMessageToString(Message message) throws Exception {
|
||||
String strMsg = ((TextMessage) message).getText();
|
||||
|
||||
// Queue property relay
|
||||
if (MessageType.JSON.equals(adapterVo.getAdapterGroupVO().getMessageType())
|
||||
&& StringUtils.isNotBlank(relayPropertyGroup)
|
||||
&& StringUtils.isNotBlank(relayRequestPropertyKeys)) {
|
||||
JSONObject jsonMessage = (JSONObject) JSONValue.parse(StringUtils.defaultString(strMsg));
|
||||
JSONObject propertyJson = new JSONObject();
|
||||
if (StringUtils.equalsIgnoreCase(relayRequestPropertyKeys, "ALL")) {
|
||||
Enumeration<String> propertyNames = message.getPropertyNames();
|
||||
while (propertyNames.hasMoreElements()) {
|
||||
try {
|
||||
String propertyName = propertyNames.nextElement();
|
||||
propertyJson.put(propertyName, message.getStringProperty(propertyName));
|
||||
} catch (Exception e) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
String[] relayKeyArr = org.springframework.util.StringUtils
|
||||
.tokenizeToStringArray(relayRequestPropertyKeys, ",");
|
||||
for (String key : relayKeyArr) {
|
||||
try {
|
||||
String propertyValue = message.getStringProperty(key);
|
||||
if (StringUtils.isNotBlank(propertyValue)) {
|
||||
propertyJson.put(key, propertyValue);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (propertyJson.size() > 0) {
|
||||
jsonMessage.put(relayPropertyGroup, propertyJson);
|
||||
strMsg = jsonMessage.toJSONString();
|
||||
}
|
||||
}
|
||||
|
||||
return strMsg;
|
||||
}
|
||||
|
||||
private void replyMessage(String replyMessage, String replyQueue, String relayPropertyGroup)
|
||||
throws Exception {
|
||||
PooledConnectionFactory connectionFactory = AMQAdapterManager.getInstance()
|
||||
.getOutboundPooledConnectionFactory(adapterVo.getName());
|
||||
|
||||
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
|
||||
jmsTemplate.setExplicitQosEnabled(true);
|
||||
jmsTemplate.setDeliveryPersistent(false);
|
||||
|
||||
jmsTemplate.send(replyQueue, new MessageCreator() {
|
||||
@SuppressWarnings("deprecation")
|
||||
public Message createMessage(Session session) throws JMSException {
|
||||
String strMsg = replyMessage;
|
||||
|
||||
// relay to Queue property
|
||||
HashMap<String, String> properties = new HashMap<>();
|
||||
if (MessageType.JSON.equals(adapterVo.getAdapterGroupVO().getMessageType())
|
||||
&& StringUtils.isNotBlank(relayPropertyGroup)
|
||||
&& StringUtils.contains(replyMessage, relayPropertyGroup)) {
|
||||
try {
|
||||
JSONObject dataObject = (JSONObject) JSONValue.parse(replyMessage);
|
||||
JSONObject propertyObject = (JSONObject) dataObject.get(relayPropertyGroup);
|
||||
if (propertyObject != null) {
|
||||
for (Object key : propertyObject.keySet()) {
|
||||
|
||||
Object obj = propertyObject.get(key);
|
||||
if ((obj instanceof JSONObject) || (obj instanceof JSONArray)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
properties.put((String) key, (String) obj);
|
||||
}
|
||||
|
||||
dataObject.remove(relayPropertyGroup);
|
||||
strMsg = dataObject.toJSONString();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Message ms = session.createTextMessage(strMsg);
|
||||
for (Map.Entry<String, String> entry : properties.entrySet()) {
|
||||
ms.setStringProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
return ms;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
listenerContainer.afterPropertiesSet();
|
||||
listenerContainer.start();
|
||||
}
|
||||
|
||||
public void stop() throws Exception {
|
||||
if (listenerContainer != null) {
|
||||
listenerContainer.stop();
|
||||
listenerContainer.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,358 @@
|
||||
package com.eactive.eai.adapter.amq;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageConsumer;
|
||||
import javax.jms.MessageProducer;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import org.apache.activemq.jms.pool.PooledConnectionFactory;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.jms.core.MessageCreator;
|
||||
import org.springframework.jms.core.SessionCallback;
|
||||
import org.springframework.jms.support.JmsUtils;
|
||||
import org.springframework.jms.support.destination.DestinationResolver;
|
||||
|
||||
import com.eactive.eai.adapter.ElinkDefaultAdapter;
|
||||
import com.eactive.eai.adapter.Keys;
|
||||
import com.eactive.eai.common.message.EAIMessageKeys;
|
||||
import com.eactive.eai.common.message.MessageType;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.env.ElinkConfig;
|
||||
|
||||
public class AMQSenderAdapter extends ElinkDefaultAdapter {
|
||||
public static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
public static final String ADAPTER_NAME = "ADAPTER_NAME";
|
||||
|
||||
private static final class ProducerConsumer implements SessionCallback<Message> {
|
||||
private final String msg;
|
||||
private final DestinationResolver destinationResolver;
|
||||
private final String queue;
|
||||
private final String receiveQueue;
|
||||
private final Long receiveTimeoutSeconds;
|
||||
private final Long timeToLiveSeconds;
|
||||
private final String messageType;
|
||||
private final String relayPropertyGroup;
|
||||
|
||||
public ProducerConsumer(final String msg, String queue, String receiveQueue,
|
||||
final DestinationResolver destinationResolver, Long receiveTimeoutSeconds, Long timeToLiveSeconds,
|
||||
String messageType, String relayPropertyGroup) {
|
||||
this.msg = msg;
|
||||
this.queue = queue;
|
||||
this.receiveQueue = receiveQueue;
|
||||
this.destinationResolver = destinationResolver;
|
||||
this.receiveTimeoutSeconds = receiveTimeoutSeconds;
|
||||
this.timeToLiveSeconds = timeToLiveSeconds;
|
||||
this.messageType = messageType;
|
||||
this.relayPropertyGroup = relayPropertyGroup;
|
||||
}
|
||||
|
||||
public Message doInJms(final Session session) throws JMSException {
|
||||
MessageConsumer consumer = null;
|
||||
MessageProducer producer = null;
|
||||
try {
|
||||
final String correlationId = UUID.randomUUID().toString();
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("correlationId=====" + correlationId);
|
||||
}
|
||||
final Destination requestQueue = destinationResolver.resolveDestinationName(session, queue, false);
|
||||
final Destination replyQueue = destinationResolver.resolveDestinationName(session, receiveQueue, false);
|
||||
|
||||
// Create the consumer first!
|
||||
consumer = session.createConsumer(replyQueue, "JMSCorrelationID = '" + correlationId + "'");
|
||||
|
||||
JSONObject propObject = new JSONObject();
|
||||
String strMsg = AMQSenderAdapter.extractRequestProperties(propObject, msg, messageType,
|
||||
relayPropertyGroup);
|
||||
final Message textMessage = session.createTextMessage(strMsg);
|
||||
textMessage.setJMSCorrelationID(correlationId);
|
||||
textMessage.setJMSReplyTo(replyQueue);
|
||||
AMQSenderAdapter.assignRequestProperties(textMessage, propObject);
|
||||
|
||||
// Send the request second!
|
||||
producer = session.createProducer(requestQueue);
|
||||
if (timeToLiveSeconds != null) {
|
||||
producer.setTimeToLive(timeToLiveSeconds.longValue() * 1000);
|
||||
}
|
||||
producer.send(requestQueue, textMessage);
|
||||
|
||||
// Block on receiving the response with a timeout
|
||||
if (receiveTimeoutSeconds != null && receiveTimeoutSeconds.longValue() > 0) {
|
||||
return consumer.receive(receiveTimeoutSeconds.longValue() * 1000);
|
||||
} else {
|
||||
return consumer.receive(AMQAdapterConstants.DEFAULT_MESSAGE_RECEIVE_TIMEOUT * 1000);
|
||||
}
|
||||
} catch (JMSException jmse) {
|
||||
throw jmse;
|
||||
} catch (Exception e) {
|
||||
throw new JMSException(e.getMessage());
|
||||
} finally {
|
||||
JmsUtils.closeMessageConsumer(consumer);
|
||||
JmsUtils.closeMessageProducer(producer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(Properties prop, Object message, Properties tempProp) throws Exception {
|
||||
String syncAsync = tempProp.getProperty(AMQAdapterConstants.TEMP_INTERFACE_RESPONSE_TYPE);
|
||||
if (StringUtils.equals(syncAsync, EAIMessageKeys.ASYNC_SVC)) {
|
||||
return executeAsync(prop, message, tempProp);
|
||||
}
|
||||
|
||||
String sendQueue = prop.getProperty(AMQAdapterConstants.PROP_REQUEST_QUEUE_NAME);
|
||||
if (StringUtils.isBlank(sendQueue)) {
|
||||
throw new Exception("No have send Queue name");
|
||||
}
|
||||
|
||||
String replyQueue = prop.getProperty(AMQAdapterConstants.PROP_REPLY_QUEUE_NAME);
|
||||
if (StringUtils.isBlank(replyQueue)) {
|
||||
throw new Exception("No have reply Queue name");
|
||||
}
|
||||
|
||||
String timeToLive = prop.getProperty(AMQAdapterConstants.PROP_MESSAGE_TIME_TO_LIVE);
|
||||
Long timeToLiveSeconds = null;
|
||||
if (NumberUtils.isParsable(timeToLive)) {
|
||||
timeToLiveSeconds = new Long(timeToLive);
|
||||
}
|
||||
|
||||
String receiveTimeout = prop.getProperty(AMQAdapterConstants.PROP_MESSAGE_RECEIVE_TIMEOUT);
|
||||
Long receiveTimeoutSeconds = AMQAdapterConstants.DEFAULT_MESSAGE_RECEIVE_TIMEOUT;
|
||||
if (NumberUtils.isParsable(receiveTimeout)) {
|
||||
receiveTimeoutSeconds = new Long(receiveTimeout);
|
||||
}
|
||||
|
||||
// 인터페이스 정보에 설정된 타임아웃이 우선
|
||||
String interfaceTimeout = tempProp.getProperty(com.eactive.eai.common.routing.Process.INTERFACE_TIME_OUT);
|
||||
if (NumberUtils.isParsable(interfaceTimeout)) {
|
||||
long miliseconds = NumberUtils.toLong(interfaceTimeout);
|
||||
if (miliseconds >= 1000) {
|
||||
receiveTimeoutSeconds = new Long(miliseconds / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
String encode = tempProp.getProperty(Keys.CHARSET_KEY_OUT, Charset.defaultCharset().name());
|
||||
String sendMsg = "";
|
||||
if (message instanceof String) {
|
||||
sendMsg = (String) message;
|
||||
} else if (message instanceof byte[]) {
|
||||
sendMsg = new String((byte[]) message, encode);
|
||||
}
|
||||
|
||||
String relayPropertyGroup = prop.getProperty(AMQAdapterConstants.PROP_RELAY_PROPERTY_GROUP);
|
||||
String relayResponsePropertyKeys = prop.getProperty(AMQAdapterConstants.PROP_RELAY_RESPONSE_PROPERTY_KEYS);
|
||||
String messageType = tempProp.getProperty(AMQAdapterConstants.TEMP_MESSAGE_TYPE);
|
||||
|
||||
String adapterName = tempProp.getProperty(ADAPTER_NAME);
|
||||
|
||||
try {
|
||||
PooledConnectionFactory connectionFactory = AMQAdapterManager.getInstance()
|
||||
.getOutboundPooledConnectionFactory(adapterName);
|
||||
|
||||
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
|
||||
jmsTemplate.setExplicitQosEnabled(true);
|
||||
jmsTemplate.setDeliveryPersistent(false);
|
||||
jmsTemplate.setReceiveTimeout(receiveTimeoutSeconds.longValue() * 1000);
|
||||
if (timeToLiveSeconds != null) {
|
||||
jmsTemplate.setTimeToLive(timeToLiveSeconds.longValue() * 1000);
|
||||
}
|
||||
|
||||
Message receiveMessage = jmsTemplate
|
||||
.execute(new ProducerConsumer(sendMsg, sendQueue, replyQueue, jmsTemplate.getDestinationResolver(),
|
||||
receiveTimeoutSeconds, timeToLiveSeconds, messageType, relayPropertyGroup), true);
|
||||
|
||||
String returnMsg = "";
|
||||
if (receiveMessage != null) {
|
||||
returnMsg = assignResponseProperties(receiveMessage, messageType, relayPropertyGroup,
|
||||
relayResponsePropertyKeys);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("AMQSenderAdapter] Received message: {}", returnMsg);
|
||||
}
|
||||
} else {
|
||||
throw new JMSException(
|
||||
"Received NULL(The timeout expires or this message consumer is concurrently closed)");
|
||||
}
|
||||
|
||||
return returnMsg;
|
||||
} catch (Exception e) {
|
||||
logger.error("AMQSenderAdapter] Exception (" + adapterName + ") : " + e.getMessage(), e);
|
||||
throw e;
|
||||
} catch (Throwable t) {
|
||||
logger.error("AMQSenderAdapter] Throwable (" + adapterName + ") : " + t.getMessage(), t);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "deprecation" })
|
||||
private String assignResponseProperties(Message receiveMessage, String messageType, String relayPropertyGroup,
|
||||
String relayResponsePropertyKeys) throws Exception {
|
||||
if (!(receiveMessage instanceof TextMessage)) {
|
||||
return receiveMessage.toString();
|
||||
}
|
||||
|
||||
String returnMsg = ((TextMessage) receiveMessage).getText();
|
||||
|
||||
// Queue property to JSON
|
||||
try {
|
||||
if (MessageType.JSON.equals(messageType) && StringUtils.isNotBlank(relayPropertyGroup)
|
||||
&& StringUtils.isNotBlank(relayResponsePropertyKeys)) {
|
||||
String[] relayKeyArr = org.springframework.util.StringUtils
|
||||
.tokenizeToStringArray(relayResponsePropertyKeys, ",");
|
||||
JSONObject propertyJson = new JSONObject();
|
||||
if (StringUtils.equalsIgnoreCase(relayResponsePropertyKeys, "ALL")) {
|
||||
Enumeration<String> propertyNames = receiveMessage.getPropertyNames();
|
||||
while (propertyNames.hasMoreElements()) {
|
||||
String propertyName = propertyNames.nextElement();
|
||||
propertyJson.put(propertyName, receiveMessage.getStringProperty(propertyName));
|
||||
}
|
||||
} else {
|
||||
for (String key : relayKeyArr) {
|
||||
String value = receiveMessage.getStringProperty(key);
|
||||
if (value != null) {
|
||||
propertyJson.put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (propertyJson.size() <= 0) {
|
||||
return returnMsg;
|
||||
}
|
||||
|
||||
JSONObject bodyJson = null;
|
||||
if (StringUtils.isBlank(returnMsg)) {
|
||||
bodyJson = new JSONObject();
|
||||
} else {
|
||||
bodyJson = (JSONObject) JSONValue.parse(returnMsg);
|
||||
}
|
||||
|
||||
// malformed json message
|
||||
if (bodyJson == null) {
|
||||
return returnMsg;
|
||||
}
|
||||
|
||||
bodyJson.put(relayPropertyGroup, propertyJson);
|
||||
return bodyJson.toJSONString();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return returnMsg;
|
||||
}
|
||||
|
||||
private Object executeAsync(Properties prop, Object message, Properties tempProp) throws Exception {
|
||||
String sendQueue = prop.getProperty(AMQAdapterConstants.PROP_REQUEST_QUEUE_NAME);
|
||||
if (StringUtils.isBlank(sendQueue)) {
|
||||
throw new Exception("No have send Queue name");
|
||||
}
|
||||
|
||||
String relayPropertyGroup = prop.getProperty(AMQAdapterConstants.PROP_RELAY_PROPERTY_GROUP);
|
||||
String messageType = tempProp.getProperty(AMQAdapterConstants.TEMP_MESSAGE_TYPE);
|
||||
String timeToLive = prop.getProperty(AMQAdapterConstants.PROP_MESSAGE_TIME_TO_LIVE);
|
||||
|
||||
String encode = tempProp.getProperty(Keys.CHARSET_KEY_OUT, Charset.defaultCharset().name());
|
||||
final String sendMsg;
|
||||
if (message instanceof String) {
|
||||
sendMsg = (String) message;
|
||||
} else if (message instanceof byte[]) {
|
||||
sendMsg = new String((byte[]) message, encode);
|
||||
} else {
|
||||
sendMsg = "";
|
||||
}
|
||||
|
||||
String adapterName = tempProp.getProperty(ADAPTER_NAME);
|
||||
|
||||
try {
|
||||
PooledConnectionFactory connectionFactory = AMQAdapterManager.getInstance()
|
||||
.getOutboundPooledConnectionFactory(adapterName);
|
||||
|
||||
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
|
||||
jmsTemplate.setExplicitQosEnabled(true);
|
||||
jmsTemplate.setDeliveryPersistent(false);
|
||||
if (NumberUtils.isParsable(timeToLive)) {
|
||||
jmsTemplate.setTimeToLive(NumberUtils.toLong(timeToLive) * 1000);
|
||||
}
|
||||
|
||||
jmsTemplate.send(sendQueue, new MessageCreator() {
|
||||
public Message createMessage(Session session) throws JMSException {
|
||||
JSONObject propObject = new JSONObject();
|
||||
String strMsg = AMQSenderAdapter.extractRequestProperties(propObject, sendMsg, messageType,
|
||||
relayPropertyGroup);
|
||||
Message ms = session.createTextMessage(strMsg);
|
||||
AMQSenderAdapter.assignRequestProperties(ms, propObject);
|
||||
|
||||
return ms;
|
||||
}
|
||||
});
|
||||
|
||||
return ElinkConfig.getAsyncDummyData(null);
|
||||
} catch (Exception e) {
|
||||
logger.error("AMQSenderAdapter Async] Exception (" + adapterName + ") : " + e.getMessage(), e);
|
||||
throw e;
|
||||
} catch (Throwable t) {
|
||||
logger.error("AMQSenderAdapter Async] Throwable (" + adapterName + ") : " + t.getMessage(), t);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "deprecation", "unchecked" })
|
||||
protected static String extractRequestProperties(JSONObject propObject, String sendMsg, String messageType,
|
||||
String relayPropertyGroup) {
|
||||
String strMsg = sendMsg;
|
||||
if (MessageType.JSON.equals(messageType) && StringUtils.isNotBlank(relayPropertyGroup)
|
||||
&& StringUtils.contains(strMsg, relayPropertyGroup)) {
|
||||
try {
|
||||
JSONObject dataObject = (JSONObject) JSONValue.parse(strMsg);
|
||||
JSONObject tmpPropObject = (JSONObject) dataObject.get(relayPropertyGroup);
|
||||
if (tmpPropObject != null) {
|
||||
propObject.putAll(tmpPropObject);
|
||||
dataObject.remove(relayPropertyGroup);
|
||||
strMsg = dataObject.toJSONString();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return strMsg;
|
||||
}
|
||||
|
||||
protected static void assignRequestProperties(Message ms, JSONObject propObject) {
|
||||
if (propObject == null || propObject.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Object key : propObject.keySet()) {
|
||||
Object obj = propObject.get(key);
|
||||
if ((obj instanceof JSONObject) || (obj instanceof JSONArray)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
ms.setStringProperty((String) key, (String) obj);
|
||||
} catch (Exception e) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.eactive.eai.adapter.app;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public interface BusinessApplication {
|
||||
public static final String EAI_SVC_NAME = "eaiSvcName";
|
||||
|
||||
public Object executeApplication(Properties prop, Object message, Properties tempProp) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.eactive.eai.adapter.app;
|
||||
|
||||
import com.eactive.eai.adapter.ElinkDefaultAdapter;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceKey;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public class BusinessApplicationAdapter extends ElinkDefaultAdapter {
|
||||
public static final String ADAPTER_PROP_CLASS_NAME = "BUSINESS_CLASS_NAME";
|
||||
public static final String REST_OPTION_CLASS_NAME = "businessClassName";
|
||||
|
||||
@Override
|
||||
public Object execute(Properties prop, Object message, Properties tempProp) throws Exception {
|
||||
String restOption = prop.getProperty(HttpClientAdapterServiceKey.REST_OPTION);
|
||||
JSONObject restOptionObject = parseJson(restOption);
|
||||
|
||||
String businessClassName = null;
|
||||
if (restOptionObject != null) {
|
||||
businessClassName = (String) restOptionObject.get(REST_OPTION_CLASS_NAME);
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(businessClassName)) {
|
||||
businessClassName = prop.getProperty(ADAPTER_PROP_CLASS_NAME);
|
||||
if (StringUtils.isBlank(businessClassName)) {
|
||||
throw new Exception(ADAPTER_PROP_CLASS_NAME + " is not set.");
|
||||
}
|
||||
}
|
||||
|
||||
BusinessApplication app = BusinessApplicationFactory.createApplication(businessClassName);
|
||||
return app.executeApplication(prop, message, tempProp);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private JSONObject parseJson(String message) {
|
||||
if (StringUtils.isBlank(message)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (JSONObject) JSONValue.parse(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.eactive.eai.adapter.app;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class BusinessApplicationFactory {
|
||||
static private ConcurrentHashMap<String, BusinessApplication> h = new ConcurrentHashMap<String, BusinessApplication>();
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static BusinessApplication createApplication(String className) throws Exception {
|
||||
if (h.containsKey(className)) {
|
||||
return (BusinessApplication) h.get(className);
|
||||
}
|
||||
|
||||
BusinessApplication app = null;
|
||||
try {
|
||||
Class cl = Class.forName(className);
|
||||
app = (BusinessApplication) cl.newInstance();
|
||||
if (app != null) {
|
||||
h.put(className, app);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new Exception("Cannot create a BusinessApplication. - " + className);
|
||||
}
|
||||
return app;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.eactive.eai.adapter.app.impl;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.eactive.eai.adapter.app.BusinessApplication;
|
||||
import com.eactive.eai.common.util.DatetimeUtil;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.common.util.StringUtil;
|
||||
|
||||
public class OnlineToFileWriter implements BusinessApplication {
|
||||
private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
public static final String CLASS_PROPS = "BUSINESS_CLASS_PROPS";
|
||||
public static final String CLASS_PROPS_BASE_FILE_PATH = "baseFilePath";
|
||||
public static final String CLASS_PROPS_ENCODE = "encode";
|
||||
public static final String CLASS_PROPS_FILE_PATH_EXT = "fileExt";
|
||||
|
||||
/**
|
||||
* @formatter:off
|
||||
* Adapter property ¼³Á¤: BUSINESS_CLASS_PROPS
|
||||
* - ex) baseFilePath=/data/onlineToJson, fileExt=json
|
||||
* @formatter:on
|
||||
*/
|
||||
@Override
|
||||
public Object executeApplication(Properties prop, Object message, Properties tempProp) throws Exception {
|
||||
String classPropsStr = prop.getProperty(CLASS_PROPS);
|
||||
Properties classProp = StringUtil.getProperties(classPropsStr);
|
||||
String baseFilePath = classProp.getProperty(CLASS_PROPS_BASE_FILE_PATH);
|
||||
if (StringUtils.isBlank(baseFilePath)) {
|
||||
throw new Exception(String.format("Property(%s) is not setted", CLASS_PROPS_BASE_FILE_PATH));
|
||||
}
|
||||
|
||||
String encode = classProp.getProperty(CLASS_PROPS_ENCODE, "UTF-8");
|
||||
String fileExt = classProp.getProperty(CLASS_PROPS_FILE_PATH_EXT, "txt");
|
||||
String eaiSvcName = tempProp.getProperty(BusinessApplication.EAI_SVC_NAME);
|
||||
if (StringUtils.isBlank(eaiSvcName)) {
|
||||
throw new Exception("Can not find serviceId");
|
||||
}
|
||||
String dirStr = StringUtils.removeEnd(baseFilePath, "/") + "/" + eaiSvcName;
|
||||
File dir = new File(dirStr);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
|
||||
String fileName = DatetimeUtil.getFormattedDate("yyyyMMdd-HHmmssSSS") + "." + fileExt;
|
||||
File file = new File(dir, fileName);
|
||||
|
||||
Map<String, String> resultMap = new HashMap<>();
|
||||
|
||||
try (
|
||||
FileWriter fw = new FileWriter(file);
|
||||
BufferedWriter bw = new BufferedWriter(fw);
|
||||
){
|
||||
if (message == null) {
|
||||
logger.warn("message is null");
|
||||
} else if (message instanceof byte[]) {
|
||||
byte[] buff = (byte[]) message;
|
||||
bw.write(new String(buff, encode));
|
||||
} else {
|
||||
bw.write(message.toString());
|
||||
}
|
||||
bw.flush();
|
||||
|
||||
resultMap.put("RESULTCD", "000");
|
||||
resultMap.put("RESULTMSG", "Success");
|
||||
resultMap.put("FILENAME", fileName);
|
||||
} catch (Exception e) {
|
||||
logger.error("OnlineToFileWriter error", e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
JSONObject resultJsonObject = new JSONObject();
|
||||
resultJsonObject.putAll(resultMap);
|
||||
return resultJsonObject.toJSONString().getBytes();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.eactive.eai.adapter.db;
|
||||
|
||||
public class DBAdapterKeys {
|
||||
public static final String ADAPTER_GROUPNAME = "adapterGroupName";
|
||||
public static final String ADAPTER_NAME = "adapterName";
|
||||
public static final String ADAPTER_PROPGN = "propGroupName";
|
||||
|
||||
// jdbc common
|
||||
public static final String DRIVER = "jdbc.driver";
|
||||
public static final String URL = "jdbc.url";
|
||||
public static final String USER = "jdbc.user";
|
||||
public static final String PVAL = "jdbc.pval";
|
||||
public static final String DATASOURCE = "jdbc.datasource"; // optional
|
||||
public static final String ENCODE = "encode"; // optional
|
||||
|
||||
// inbound query
|
||||
public static final String SELECT = "jdbc.select"; // select query
|
||||
public static final String UPDATE = "jdbc.update"; // update query
|
||||
// inbound scheduler
|
||||
public static final String RUN_CRONEXP = "run.cronexp"; // Quartz [ 0/10 * * * * ? ]
|
||||
public static final String RUN_INTVL = "run.interval"; // 0...N
|
||||
public static final String RUN_RETRY = "run.retry"; // Y|N
|
||||
public static final String RUN_RETRYCNT = "run.retrycnt"; // 0...N
|
||||
public static final String RUN_RETRYINTVL = "run.retryinterval"; //ms
|
||||
public static final String RUN_MSGTYPE = "run.msgtype"; // XML | JSON
|
||||
|
||||
// Outbound
|
||||
public static final String QUERY = "query";
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.eactive.eai.adapter.db.sql;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterPropManager;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Properties;
|
||||
|
||||
public class AdapterUtils {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String updateSql = "update EAIINF01 set RFLAG = 'C' where UNID = '%UNID%'";
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
map.put("Unid", "ID00000001");
|
||||
|
||||
String prefix = "%", surfix = "%";
|
||||
System.out.println(updateSql);
|
||||
System.out.println(getUpdateQuery(updateSql, map, prefix, surfix));
|
||||
}
|
||||
|
||||
public static Properties getAdapterProerties(String propGroupName) {
|
||||
AdapterPropManager pmanager = AdapterPropManager.getInstance();
|
||||
Properties prop = null;
|
||||
if(pmanager == null) {
|
||||
prop = new Properties();
|
||||
}
|
||||
else {
|
||||
prop = pmanager.getProperties(propGroupName);
|
||||
}
|
||||
return prop;
|
||||
}
|
||||
|
||||
public static String getUpdateQuery(String updateSql, HashMap<String, Object> map) {
|
||||
return getUpdateQuery(updateSql, map, "%", "%");
|
||||
}
|
||||
|
||||
public static String getUpdateQuery(String updateSql, HashMap<String, Object> map, String prefix, String surfix) {
|
||||
String convertedSql = updateSql;
|
||||
|
||||
String paramName = null;
|
||||
for(String colName : map.keySet()) {
|
||||
paramName = prefix+colName.toUpperCase()+surfix;
|
||||
if(updateSql.indexOf(paramName) > -1) {
|
||||
convertedSql = convertedSql.replaceAll(paramName, (String)map.get(colName));
|
||||
}
|
||||
}
|
||||
return convertedSql;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
package com.eactive.eai.adapter.db.sql;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterVO;
|
||||
import com.eactive.eai.adapter.RequestDispatcher;
|
||||
import com.eactive.eai.adapter.db.DBAdapterKeys;
|
||||
import com.eactive.eai.adapter.wca.jdbc.JDBCException;
|
||||
import com.eactive.eai.common.message.EAIMessageKeys;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.common.util.ServiceLocator;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.*;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Properties;
|
||||
|
||||
public class DBInboudSqlAdapter {
|
||||
public static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
private boolean isLocalTest = false;
|
||||
|
||||
|
||||
private String logPrefix = "";
|
||||
private boolean isRunning = false;
|
||||
private boolean stopRunning = false;
|
||||
|
||||
private AdapterVO vo;
|
||||
private Properties configProperties = null;
|
||||
|
||||
public boolean isRunningState() {
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
public void setAdapterVO(AdapterVO vo) {
|
||||
this.vo = vo;
|
||||
}
|
||||
|
||||
public void setProperties(Properties p) {
|
||||
this.configProperties = p;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
stopRunning = true;
|
||||
}
|
||||
|
||||
public DBInboudSqlAdapter(AdapterVO vo, Properties configProperties) {
|
||||
super();
|
||||
this.vo = vo;
|
||||
this.configProperties = configProperties;
|
||||
if(vo == null) {
|
||||
logPrefix = this.getClass().getSimpleName() +"] ";
|
||||
}
|
||||
else {
|
||||
logPrefix = vo.getName() +"] ";
|
||||
}
|
||||
String isLocal = System.getProperty("isLocal");
|
||||
if(isLocal != null && "true".equalsIgnoreCase(isLocal)) {
|
||||
isLocalTest = true;
|
||||
}
|
||||
System.out.println("$$$ DBInboudSqlAdapter] isLocalTest = " + isLocalTest);
|
||||
}
|
||||
|
||||
public boolean execute() {
|
||||
isRunning = true;
|
||||
int chunkSize = 15;
|
||||
Connection con = null;
|
||||
PreparedStatement pstmt = null;
|
||||
PreparedStatement pstmtUpdate = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
String datasoureName = configProperties.getProperty(DBAdapterKeys.DATASOURCE) ;
|
||||
String selectQuery = configProperties.getProperty(DBAdapterKeys.SELECT) ;
|
||||
String updateQuery = configProperties.getProperty(DBAdapterKeys.UPDATE) ;
|
||||
|
||||
int runInterval = 0;
|
||||
String runIntervalStr = configProperties.getProperty(DBAdapterKeys.RUN_INTVL) ;
|
||||
try {
|
||||
runInterval = Integer.parseInt(runIntervalStr);
|
||||
}
|
||||
catch(Exception e) {
|
||||
runInterval = 0;
|
||||
}
|
||||
|
||||
String runRetry = configProperties.getProperty(DBAdapterKeys.RUN_RETRY, "N") ; // Y | N
|
||||
|
||||
int runRetryCnt = 0;
|
||||
String runRetryCntStr = configProperties.getProperty(DBAdapterKeys.RUN_RETRYCNT, "0") ;
|
||||
try {
|
||||
runRetryCnt = Integer.parseInt(runRetryCntStr);
|
||||
}
|
||||
catch(Exception e) {
|
||||
runRetryCnt = 0;
|
||||
}
|
||||
|
||||
int runRetryInterval = 0;
|
||||
String runRetryIntervalStr = configProperties.getProperty(DBAdapterKeys.RUN_RETRYINTVL, "0") ;
|
||||
try {
|
||||
runRetryInterval = Integer.parseInt(runRetryIntervalStr);
|
||||
}
|
||||
catch(Exception e) {
|
||||
runRetryInterval = 0;
|
||||
}
|
||||
|
||||
int rowIndex = 0;
|
||||
int colCount = 0;
|
||||
try {
|
||||
if(datasoureName != null && datasoureName.trim().length() > 0) {
|
||||
con = getDataSourceConnection(datasoureName);
|
||||
}
|
||||
else {
|
||||
con = getJDBCConnection(configProperties);
|
||||
}
|
||||
// default : row unit commit
|
||||
con.setAutoCommit(true);
|
||||
|
||||
// select chunk record
|
||||
pstmt = con.prepareStatement(selectQuery);
|
||||
rs = pstmt.executeQuery();
|
||||
|
||||
rowIndex = 0;
|
||||
colCount = rs.getMetaData().getColumnCount();
|
||||
debug(String.format("$$$ RUNNING Query [%s] - colCount = %s", selectQuery, colCount) );
|
||||
|
||||
String rowString = null;
|
||||
|
||||
// LOOP - Inbound Call
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
boolean isSuccess = false;
|
||||
int retryExecCounter = 0;
|
||||
boolean retryFailed = false;
|
||||
while(rs.next() && !retryFailed){
|
||||
debug(String.format("==> run [%s]", (rowIndex+1)) );
|
||||
if(stopRunning) {
|
||||
debug(String.format("$$$ STOP RUNNING stopRunning[%s]", stopRunning) );
|
||||
break;
|
||||
}
|
||||
|
||||
for(int i = 1; i <= colCount; i++){
|
||||
String key = rs.getMetaData().getColumnName(i);
|
||||
String value = rs.getString(i);
|
||||
if(value == null){
|
||||
value = "";
|
||||
}
|
||||
map.put(key, value);
|
||||
}
|
||||
|
||||
if("JSON".equals(configProperties.getProperty(DBAdapterKeys.RUN_MSGTYPE))) {
|
||||
rowString = ResultParser.toJsonString(map);
|
||||
}
|
||||
if("XML".equals(configProperties.getProperty(DBAdapterKeys.RUN_MSGTYPE))) {
|
||||
rowString = ResultParser.toXmlString(map);
|
||||
}
|
||||
|
||||
debug(String.format("==> run [%s] : ", (rowIndex+1)) + rowString);
|
||||
//-- start sudo test code
|
||||
// debug(String.format("Sleep 1 secs - run [%s/%s]", (rowIndex+1), chunkSize) );
|
||||
|
||||
//-- end sudo test code
|
||||
|
||||
byte[] request = rowString.getBytes();
|
||||
byte[] response = null;
|
||||
String elinkResponseCode = null;
|
||||
while(true) {
|
||||
try {
|
||||
isSuccess = false;
|
||||
RequestDispatcher dispatcher = RequestDispatcher.getRequestDispatcher(vo.getAdapterGroupName());
|
||||
response = (byte[]) dispatcher.handle(vo.getName(), request, this.configProperties);
|
||||
|
||||
elinkResponseCode = configProperties.getProperty(EAIMessageKeys.ELINK_RET_CODE);
|
||||
debug("RequestProcessor.ELINK_RET_CODE ["+ elinkResponseCode +"]" );
|
||||
|
||||
// Sunncess check with elink F/W response code ESBEAI111111
|
||||
if(EAIMessageKeys.EAI_SUCCESS_CODE.equals(elinkResponseCode)
|
||||
|| EAIMessageKeys.EAI_DEFAULT_CODE.equals(elinkResponseCode)) {
|
||||
isSuccess = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("RequestDispatcher call error",e);
|
||||
}
|
||||
|
||||
if(isSuccess) {
|
||||
// check response
|
||||
if(response != null) debug("Response Message : " + new String(response));
|
||||
|
||||
// if success, update CHK column
|
||||
String mappedUpdate = AdapterUtils.getUpdateQuery(updateQuery, map);
|
||||
debug("mappedUpdate - "+ mappedUpdate);
|
||||
pstmtUpdate = con.prepareStatement(mappedUpdate);
|
||||
int cnt = pstmtUpdate.executeUpdate();
|
||||
debug("mappedUpdate result - "+ cnt);
|
||||
// if success, skip retry
|
||||
break;
|
||||
}
|
||||
else {
|
||||
// if failed, retry or break, check retry options
|
||||
if("Y".equals(runRetry)) {
|
||||
debug(String.format(">> RETRY [%s / %s]", retryExecCounter, runRetryCnt) );
|
||||
|
||||
if(retryExecCounter < runRetryCnt) {
|
||||
debug(String.format(">> RETRY runRetry=%s", runRetry) );
|
||||
++retryExecCounter;
|
||||
if(runRetryInterval > 0) {
|
||||
debug("Sleep run.retryinterval - " +runRetryInterval);
|
||||
Thread.sleep(runRetryInterval);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
debug(String.format(">> RETRY COMPLETED [%s / %s], STOP JOB", retryExecCounter, runRetryCnt) );
|
||||
retryFailed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
debug(String.format(">> RETRY Disabled, runRetry=%s", runRetry) );
|
||||
retryFailed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} // end retry LOOP
|
||||
rowIndex++;
|
||||
if(runInterval > 0) {
|
||||
debug("Sleep run.interval - " +runInterval);
|
||||
Thread.sleep(runInterval);
|
||||
}
|
||||
}
|
||||
// end row LOOP
|
||||
}
|
||||
catch(Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
isRunning = false;
|
||||
debug(String.format("JOB Completed. isRunning=%s, executed ROWCount=%s", isRunning, rowIndex) );
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
private Connection getJDBCConnection(Properties p) throws SQLException {
|
||||
try {
|
||||
Class.forName(p.getProperty(DBAdapterKeys.DRIVER));
|
||||
Connection con = DriverManager.getConnection(p.getProperty(DBAdapterKeys.URL),
|
||||
p.getProperty(DBAdapterKeys.USER), p.getProperty(DBAdapterKeys.PVAL));
|
||||
return con;
|
||||
} catch (Exception e) {
|
||||
throw new SQLException("DBAdapter] Cannot create a JDBC Connection. - " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getDataSourceConnection(String dataSourceName) throws JDBCException
|
||||
{
|
||||
DataSource dataSource = null;
|
||||
try {
|
||||
ServiceLocator sl = ServiceLocator.getInstance();
|
||||
dataSource = sl.getDataSource(dataSourceName);
|
||||
return dataSource.getConnection();
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new JDBCException("["+dataSourceName+"]을 얻을 수 없습니다.");
|
||||
}
|
||||
}
|
||||
|
||||
private java.sql.Date toDate(String sDate) throws Exception {
|
||||
java.sql.Date s = null;
|
||||
if(sDate.length() == 10 && sDate.indexOf("-") > 0) {
|
||||
s = java.sql.Date.valueOf(sDate);
|
||||
}
|
||||
else if(sDate.length() == 8) {
|
||||
java.util.Date d = new java.text.SimpleDateFormat("yyyyMMdd").parse(sDate);
|
||||
s = new java.sql.Date(d.getTime());
|
||||
}
|
||||
else {
|
||||
throw new Exception("Invalid Date String - "+sDate);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
private void debug(String msg) {
|
||||
if(isLocalTest) {
|
||||
System.out.println (logPrefix + msg);
|
||||
}
|
||||
else {
|
||||
logger.info(logPrefix + msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.eactive.eai.adapter.db.sql;
|
||||
|
||||
import com.eactive.eai.adapter.db.DBAdapterKeys;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.quartz.utils.Key;
|
||||
|
||||
public class DBInboudSqlAdapterJob implements Job {
|
||||
|
||||
public static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
private static boolean isLocalTest = false;
|
||||
|
||||
static {
|
||||
String isLocal = System.getProperty("isLocalTest");
|
||||
if(isLocal != null && "true".equalsIgnoreCase(isLocal)) {
|
||||
isLocalTest = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void debug(String msg) {
|
||||
if(isLocalTest) {
|
||||
System.out.println (msg);
|
||||
}
|
||||
else {
|
||||
logger.info(msg);
|
||||
}
|
||||
}
|
||||
|
||||
// private DBInboudSqlAdapter adapter = null;
|
||||
public void execute(JobExecutionContext context) throws JobExecutionException {
|
||||
// JobKey jobKey = context.getJobDetail().getKey();
|
||||
Key jobKey = context.getJobDetail().getKey();
|
||||
String data = "";
|
||||
for (String key : context.getJobDetail().getJobDataMap().getKeys()) {
|
||||
data = data + key + "=" + context.getJobDetail().getJobDataMap().getString(key) + ",";
|
||||
}
|
||||
debug( jobKey + " start data(" + data + ")");
|
||||
logger.warn(">> " + jobKey + " start data(" + data + ")");
|
||||
String jobKeyName = jobKey.getName();
|
||||
|
||||
String adapterName = context.getJobDetail().getJobDataMap().getString(DBAdapterKeys.ADAPTER_NAME);
|
||||
String propGroupName = context.getJobDetail().getJobDataMap().getString(DBAdapterKeys.ADAPTER_PROPGN);
|
||||
debug("adapterName=" + adapterName + ", propGroupName"+ propGroupName );
|
||||
|
||||
|
||||
DBInboudSqlAdapter adapter = DBInboudSqlAdapterManager.getInstance().get(adapterName);
|
||||
if(adapter == null) {
|
||||
debug(jobKey + " : @@@@ Adapter not found in JobDataMap");
|
||||
}
|
||||
else {
|
||||
if(adapter.isRunningState()) {
|
||||
debug(jobKey + " : Adapter already RUNNING with previous scledule");
|
||||
}
|
||||
else {
|
||||
debug(jobKey + " : Adapter Called.");
|
||||
adapter.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
package com.eactive.eai.adapter.db.sql;
|
||||
|
||||
import com.eactive.eai.adapter.db.DBAdapterKeys;
|
||||
import com.eactive.eai.adapter.listener.AdapterException;
|
||||
import com.eactive.eai.adapter.listener.AdapterListenerSupport;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import org.quartz.*;
|
||||
import org.quartz.impl.StdSchedulerFactory;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.quartz.CronScheduleBuilder.cronSchedule;
|
||||
import static org.quartz.JobBuilder.newJob;
|
||||
import static org.quartz.TriggerBuilder.newTrigger;
|
||||
|
||||
public class DBInboudSqlAdapterListener extends AdapterListenerSupport {
|
||||
|
||||
String className = this.getClass().getName();
|
||||
Scheduler scheduler = null;
|
||||
private static boolean isLocalTest = false;
|
||||
private String SCHE_GROUP_NAME = "DBQ_GROUP";
|
||||
|
||||
public boolean startScheduler(Properties prop) {
|
||||
boolean isStart = false;
|
||||
try {
|
||||
debug(className + " Scheduler start");
|
||||
logger.warn(className + " Scheduler start");
|
||||
if(scheduler != null) {
|
||||
logger.warn("Scheduler shutdown previous RUN !");
|
||||
scheduler.shutdown();
|
||||
}
|
||||
String adapterName = prop.getProperty("adapterName");
|
||||
scheduler = new StdSchedulerFactory().getScheduler();
|
||||
String jobName = "DBInboudSqlAdapterJob";
|
||||
String jobClassName = "com.eactive.eai.adapter.db.sql.DBInboudSqlAdapterJob";
|
||||
String cronExp = prop.getProperty( DBAdapterKeys.RUN_CRONEXP ); // "0/10 * * * * ?";
|
||||
registJob(jobName, jobClassName, cronExp, prop);
|
||||
scheduler.start();
|
||||
isStart = true;
|
||||
}
|
||||
catch(SchedulerException se) {
|
||||
logger.error("Scheduler start failed", se);
|
||||
}
|
||||
catch(Exception e) {
|
||||
logger.error("Scheduler start failed", e);
|
||||
}
|
||||
return isStart;
|
||||
}
|
||||
|
||||
private String getTriggerName(String jobName) {
|
||||
return jobName + "-trigger";
|
||||
}
|
||||
|
||||
public boolean registJob(String jobName, String jobClassName, String cronExp, Properties p) {
|
||||
try {
|
||||
//configure the scheduler time
|
||||
Class jobClass = Class.forName(jobClassName);
|
||||
|
||||
JobBuilder jobBuilder = newJob(jobClass);
|
||||
|
||||
jobBuilder = jobBuilder.withIdentity(jobName, Scheduler.DEFAULT_GROUP);
|
||||
|
||||
// pass adapter name : not used this time
|
||||
jobBuilder.usingJobData(DBAdapterKeys.ADAPTER_NAME, p.getProperty(DBAdapterKeys.ADAPTER_NAME));
|
||||
jobBuilder.usingJobData(DBAdapterKeys.ADAPTER_PROPGN, p.getProperty(DBAdapterKeys.ADAPTER_PROPGN));
|
||||
|
||||
JobDetail job = jobBuilder.build();
|
||||
|
||||
CronTrigger trigger = newTrigger().withIdentity( getTriggerName(jobName), SCHE_GROUP_NAME )
|
||||
.withSchedule(cronSchedule(cronExp))
|
||||
.build();
|
||||
|
||||
Date ft = scheduler.scheduleJob(job, trigger);
|
||||
logger.warn(String.format("%s has been scheduled to run at: %s and repeat based on expression: %s", job.getKey(), ft,
|
||||
trigger.getCronExpression()) );
|
||||
|
||||
return true;
|
||||
}
|
||||
catch(Exception e) {
|
||||
logger.warn("registJob failed", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void printProperties(Properties prop) throws Exception {
|
||||
if(prop == null) {
|
||||
debug("printProperties - Properties is null");
|
||||
return;
|
||||
}
|
||||
for (Object key: prop.keySet()) {
|
||||
debug(key + ": " + prop.getProperty(key.toString()));
|
||||
}
|
||||
// prop.forEach((key, value) -> debug("Key : " + key + ", Value : " + value));
|
||||
}
|
||||
|
||||
private void init(Properties prop) throws Exception {
|
||||
debug("Start init");
|
||||
printProperties(prop);
|
||||
startScheduler(prop);
|
||||
}
|
||||
|
||||
public void start() throws AdapterException {
|
||||
Properties prop = null;
|
||||
String adapterName = null;
|
||||
if(info != null) {
|
||||
String propGroupName = info.getPropGroupName();
|
||||
prop = AdapterUtils.getAdapterProerties(propGroupName);
|
||||
adapterName = info.getName();
|
||||
prop.setProperty(DBAdapterKeys.ADAPTER_PROPGN, propGroupName);
|
||||
}
|
||||
// TODO : Application Test Code.
|
||||
// in real system, remove this block
|
||||
else {
|
||||
prop = new Properties();
|
||||
|
||||
prop.setProperty(DBAdapterKeys.RUN_CRONEXP, "0/10 * * * * ?");
|
||||
prop.setProperty(DBAdapterKeys.RUN_INTVL, "1000");
|
||||
prop.setProperty(DBAdapterKeys.RUN_RETRY, "N");
|
||||
prop.setProperty(DBAdapterKeys.RUN_RETRYCNT, "2");
|
||||
prop.setProperty(DBAdapterKeys.RUN_RETRYINTVL, "5000");
|
||||
prop.setProperty(DBAdapterKeys.RUN_MSGTYPE, "JSON");
|
||||
|
||||
prop.setProperty(DBAdapterKeys.DRIVER, "oracle.jdbc.driver.OracleDriver");
|
||||
prop.setProperty(DBAdapterKeys.URL, "jdbc:oracle:thin:@localhost:1521:XE");
|
||||
prop.setProperty(DBAdapterKeys.USER, "eai");
|
||||
prop.setProperty(DBAdapterKeys.PVAL, "eaiadmin");
|
||||
// prop.setProperty(DBAdapterKeys.SELECT, "select * from APIJOBSCHE");
|
||||
prop.setProperty(DBAdapterKeys.SELECT, "select * from EAIINF01 where RFLAG IS null ORDER BY UNID");
|
||||
prop.setProperty(DBAdapterKeys.UPDATE, "update EAIINF01 set RFLAG = 'C' where UNID = '%UNID%' ");
|
||||
|
||||
debug("Start Scheduler");
|
||||
adapterName = "_EAI_IN_DBQ_SyC";
|
||||
}
|
||||
// TODO : Application Test Code End.
|
||||
|
||||
prop.setProperty(DBAdapterKeys.ADAPTER_NAME, adapterName);
|
||||
// Start Scheduler
|
||||
try{
|
||||
logger.warn(className + " STARTING");
|
||||
init(prop);
|
||||
DBInboudSqlAdapterManager maanger = DBInboudSqlAdapterManager.getInstance();
|
||||
// init adapter configuration values
|
||||
DBInboudSqlAdapter adapter = new DBInboudSqlAdapter(info, prop);
|
||||
maanger.put(adapterName, adapter);
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() throws AdapterException {
|
||||
try {
|
||||
String adapterName = "";
|
||||
if (info == null) {
|
||||
adapterName = "_EAI_IN_DBQ_SyC";
|
||||
// throw new Exception(this.getClass().getName() +" | AdapterVO°¡ NULLÀÔ´Ï´Ù.");
|
||||
}
|
||||
else {
|
||||
adapterName = info.getName();
|
||||
}
|
||||
if(scheduler != null) {
|
||||
try {
|
||||
DBInboudSqlAdapterManager manager = DBInboudSqlAdapterManager.getInstance();
|
||||
DBInboudSqlAdapter adapter = manager.get(adapterName);
|
||||
logger.warn("stop adapter - " + adapterName);
|
||||
adapter.stop();
|
||||
// Stop Scheduler
|
||||
logger.warn("stop scheduler - " + scheduler.getSchedulerName());
|
||||
JobKey key = new JobKey(adapterName, SCHE_GROUP_NAME );
|
||||
scheduler.deleteJob(key);
|
||||
scheduler.shutdown();
|
||||
}
|
||||
catch(SchedulerException se) {
|
||||
logger.error("stop scheduler failed", se);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String errCode = ExceptionUtil.getErrorCode(e, "RECEAIOIA009");
|
||||
throw new AdapterException(errCode);
|
||||
}
|
||||
}
|
||||
|
||||
private void debug(String msg) {
|
||||
if(isLocalTest) {
|
||||
System.out.println (msg);
|
||||
}
|
||||
else {
|
||||
logger.info(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
isLocalTest = true;
|
||||
System.setProperty("isLocal", "true");
|
||||
DBInboudSqlAdapterListener listener = new DBInboudSqlAdapterListener();
|
||||
int testSecs = 60;
|
||||
System.out.println (String.format(">> Test while %s secs ...", testSecs));
|
||||
listener.start();
|
||||
Thread.sleep(testSecs * 1000);
|
||||
System.out.println ("<< Test end.");
|
||||
listener.stop();
|
||||
}
|
||||
catch(Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.eactive.eai.adapter.db.sql;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class DBInboudSqlAdapterManager {
|
||||
private static DBInboudSqlAdapterManager instance = new DBInboudSqlAdapterManager();
|
||||
|
||||
private static ConcurrentHashMap<String, DBInboudSqlAdapter> adapters = new ConcurrentHashMap<String, DBInboudSqlAdapter>();
|
||||
|
||||
public static DBInboudSqlAdapterManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public synchronized void put(String adapterName, DBInboudSqlAdapter adapter) {
|
||||
adapters.put(adapterName, adapter);
|
||||
}
|
||||
|
||||
public synchronized DBInboudSqlAdapter get(String adapterName) {
|
||||
return adapters.get(adapterName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.eactive.eai.adapter.db.sql;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public class ResultParser {
|
||||
public static String toJsonString(LinkedHashMap<String, Object> map) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
for (String key : map.keySet()) {
|
||||
jsonObject.put(key, map.get(key));
|
||||
}
|
||||
return jsonObject.toJSONString();
|
||||
}
|
||||
|
||||
public static String toXmlString(LinkedHashMap<String, Object> map) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<row>");
|
||||
for (String key : map.keySet()) {
|
||||
sb.append("<").append(key).append(">");
|
||||
sb.append(map.get(key));
|
||||
sb.append("</").append(key).append(">");
|
||||
}
|
||||
sb.append("</row>");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.eactive.eai.adapter.handler;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import com.eactive.eai.message.StandardMessage;
|
||||
|
||||
public interface AdapterErrorMessageHandler {
|
||||
public Object generateNonStandardErrorResponseMessage(String inboudnAdapterGroupName, String inboudnAdapterName, Properties callProp, Object outboundRequestData, StandardMessage resStandardMessage) throws Exception;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.eactive.eai.adapter.handler;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class AdapterErrorMessageHandlerFactory {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
static private ConcurrentHashMap<String, AdapterErrorMessageHandler> h = new ConcurrentHashMap<String, AdapterErrorMessageHandler>();
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static AdapterErrorMessageHandler createHandler(String handlerClassName) {
|
||||
if (h.containsKey(handlerClassName)) {
|
||||
return (AdapterErrorMessageHandler) h.get(handlerClassName);
|
||||
}
|
||||
|
||||
AdapterErrorMessageHandler handler = null;
|
||||
try {
|
||||
Class cl = Class.forName(handlerClassName);
|
||||
handler = (AdapterErrorMessageHandler) cl.newInstance();
|
||||
} catch (Exception e) {
|
||||
logger.error("Cannot create a AdapterErrorMessageHandler. - {}", handlerClassName);
|
||||
}
|
||||
if (handler != null) {
|
||||
h.put(handlerClassName, handler);
|
||||
return handler;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.eactive.eai.adapter.handler;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import com.eactive.eai.message.StandardMessage;
|
||||
|
||||
public class DefaultAdapterErrorMessageHandler implements AdapterErrorMessageHandler {
|
||||
|
||||
@Override
|
||||
public Object generateNonStandardErrorResponseMessage(String inboudnAdapterGroupName, String inboudnAdapterName,
|
||||
Properties callProp, Object outboundRequestData, StandardMessage resStandardMessage) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.eactive.eai.adapter.http;
|
||||
|
||||
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
|
||||
|
||||
public class EntityEnclosingDeleteMethod extends EntityEnclosingMethod {
|
||||
|
||||
public EntityEnclosingDeleteMethod() {
|
||||
}
|
||||
|
||||
public EntityEnclosingDeleteMethod(String uri) {
|
||||
super(uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "DELETE";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.eactive.eai.adapter.http;
|
||||
|
||||
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
|
||||
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class EntityEnclosingGetMethod extends EntityEnclosingMethod {
|
||||
public static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
public EntityEnclosingGetMethod() {
|
||||
setFollowRedirects(true);
|
||||
}
|
||||
|
||||
public EntityEnclosingGetMethod(String uri) {
|
||||
super(uri);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("enter GetMethod(String)");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "GET";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
//package com.eactive.eai.adapter.http;
|
||||
//
|
||||
//import java.net.URLDecoder;
|
||||
//import java.net.URLEncoder;
|
||||
//import java.util.Properties;
|
||||
//import javax.naming.Context;
|
||||
//import javax.naming.InitialContext;
|
||||
//import javax.servlet.ServletException;
|
||||
//import javax.servlet.http.HttpServlet;
|
||||
//import javax.servlet.http.HttpServletRequest;
|
||||
//import javax.servlet.http.HttpServletResponse;
|
||||
//
|
||||
//import com.eactive.eai.adapter.RequestDispatcher;
|
||||
//import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
//import com.eactive.eai.common.util.LogKeys;
|
||||
//import com.eactive.eai.common.util.Logger;
|
||||
//import com.eactive.eai.common.util.StringUtil;
|
||||
//
|
||||
///**
|
||||
//* 1. 기능 : HTTP 웹 요청을 수신해 EAI 프레임웍에 전달하는 HTTP 어댑터
|
||||
//* 2. 처리 개요 :
|
||||
//* * - 약속된 URL 파라미터(“_message_”)의 값을 얻어 RequestDispatcher의 handle() 메서드를 호출한다.
|
||||
//* 3. 주의사항
|
||||
//*
|
||||
//* @author :
|
||||
//* @version : v 1.0.0
|
||||
//* @see : EAIHttpAdapter.java, RequestDispatcher.java
|
||||
//* @since :
|
||||
//* :
|
||||
//*/
|
||||
//public class HttpAdapter extends HttpServlet
|
||||
//{
|
||||
// /*
|
||||
// * URL 파리미터 이름
|
||||
// */
|
||||
// static final String PARAMETER_MESSAGE = "_message_";
|
||||
// /*
|
||||
// * Http Adapter 이름
|
||||
// */
|
||||
// static final String HTTP_ADAPTER_NAME = "HTTP_ADAPTER_NAME";
|
||||
// /*
|
||||
// * Http Adapter Group 이름
|
||||
// */
|
||||
// static final String HTTP_ADAPTER_GROUP_NAME = "HTTP_ADAPTER_GROUP_NAME";
|
||||
//
|
||||
// private String adapterGroupName;
|
||||
// private String adapterName;
|
||||
// /*
|
||||
// * Adapter Logger
|
||||
// */
|
||||
// static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : Http Adapter의 property 속성을 설정한다.
|
||||
// * 2. 처리 개요 :
|
||||
// * -
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// **/
|
||||
// public void init() {
|
||||
// //-------------------------------------------------------
|
||||
// // Edited 2007.09.12 : DHLEE
|
||||
// //-------------------------------------------------------
|
||||
// this.adapterName = this.getInitParameter(HTTP_ADAPTER_NAME);
|
||||
// this.adapterGroupName = this.getInitParameter(HTTP_ADAPTER_GROUP_NAME);
|
||||
// //-------------------------------------------------------
|
||||
//
|
||||
// if(this.adapterName == null) this.adapterName = "IN_HTTP_STD_Sync01";
|
||||
// if(this.adapterGroupName == null) this.adapterGroupName = "HttpAdapter{STD}";
|
||||
//
|
||||
// String[] msgArgs = new String[2];
|
||||
// msgArgs[0] = this.adapterGroupName;
|
||||
// msgArgs[1] = this.adapterName;
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA004", msgArgs);
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpAdapter] " + resMsg);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 로컬 서비스 호출하는 비지니스 로직
|
||||
// * 2. 처리 개요 :
|
||||
// * - 로컬 서비스를 호출 하여 업무를 처리하고 결과를 리턴 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param message 요청 메시지
|
||||
// * @return prop Http Adapter 속성 정보
|
||||
// * @exception Exception 리턴 Object의 타입이 틀릴 경우
|
||||
// **/
|
||||
// protected byte[] service(byte[] message, Properties prop) throws Exception {
|
||||
// Object obj = RequestDispatcher.getRequestDispatcher(this.adapterGroupName).handle(adapterName,message,prop);
|
||||
// if(obj == null) return null;
|
||||
// if(obj instanceof byte[]) {
|
||||
// return (byte[])obj;
|
||||
// } else if(obj instanceof String) {
|
||||
// return ((String)obj).getBytes();
|
||||
// } else {
|
||||
// throw new Exception("RECEAIAHA001");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : Http Servlet service 구현 메소드
|
||||
// * 2. 처리 개요 :
|
||||
// * - 로컬 서비스 호출
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param request Http Servlet Request
|
||||
// * @return response Http Servlet Response
|
||||
// * @exception Exception PARAMETER_MESSAGE가 null 이거나 로컬 서비스 호출 중 오류가 발생할 경우
|
||||
// **/
|
||||
// public void service(HttpServletRequest request, HttpServletResponse response) {
|
||||
// try {
|
||||
//
|
||||
// String paramValue = request.getParameter(PARAMETER_MESSAGE);
|
||||
// if(paramValue==null)
|
||||
// throw new ServletException("RECEAIAHA002");
|
||||
//
|
||||
// //byte[] message = StringUtil.hex2Bytes(hexMessage);
|
||||
// String message = URLDecoder.decode(paramValue);
|
||||
// //logger.info("HttpAdapter] "+this.adapterGroupName+" (RCEV) - "+message);
|
||||
// String[] msgArgs = new String[2];
|
||||
// msgArgs[0] = this.adapterGroupName;
|
||||
// msgArgs[1] = message;
|
||||
// String resMsg = ExceptionUtil.make("RICEAIAHA005", msgArgs);
|
||||
//
|
||||
// if(logger.isInfo()){
|
||||
// logger.info("HttpAdapter] " + resMsg);
|
||||
// }
|
||||
//
|
||||
// Properties prop = new Properties();
|
||||
// // 로컬 서비스 호출
|
||||
// byte[] result = service(message.getBytes(),prop);
|
||||
// if(message != null) {
|
||||
// //response.getWriter().print(message);
|
||||
// //hexMessage = StringUtil.bytes2Hex(message);
|
||||
// paramValue = URLEncoder.encode(new String(result));
|
||||
// response.getWriter().print(paramValue);
|
||||
// }
|
||||
//
|
||||
// } catch (Exception e) {
|
||||
// logger.error("HttpAdapter] "+adapterGroupName+"-"+adapterName+">>"+e.getMessage(),e);
|
||||
// response.setStatus(500);
|
||||
// try {
|
||||
// response.getWriter().println(e.getMessage());
|
||||
// String errCode = ExceptionUtil.getErrorCode(e, "RECEAIAHA003");
|
||||
// throw new Exception(errCode);
|
||||
// } catch (Exception ex) {
|
||||
// // IGNORE
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.eactive.eai.adapter.http;
|
||||
|
||||
import com.eactive.eai.adapter.*;
|
||||
import com.eactive.eai.common.property.PropManager;
|
||||
import com.eactive.eai.common.testcall.TestCallException;
|
||||
import com.eactive.eai.common.testcall.TestCallManager;
|
||||
import com.eactive.eai.common.testcall.TestCallSupport;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public class HttpAdapterTestCall extends TestCallSupport
|
||||
{
|
||||
public static final String MESSAGE = "EAI TestCall Message";
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
public boolean isAlive() throws TestCallException
|
||||
{
|
||||
// int nConnected = 0 ;
|
||||
try {
|
||||
ElinkAdapterFactory factory = ElinkAdapterFactory.newInstance();
|
||||
ElinkAdapter adapter = factory.getElinkAdapter(Keys.TYPE_HTTP);
|
||||
AdapterVO vo = this.getAdapterInfo();
|
||||
String pgoName = vo.getPropGroupName();
|
||||
//PropManager manager = PropManager.getInstance();
|
||||
AdapterPropManager manager = AdapterPropManager.getInstance();
|
||||
Properties prop = (Properties)(manager.getProperties(pgoName)).clone();
|
||||
|
||||
String testCallUrl = (String)prop.get("TEST_CALL_URL");
|
||||
|
||||
// MFC_FAILOVER : FAILOVER = Y 일 경우 무조건 2센터로 테스트콜을 처리한다 (2020-10-16)
|
||||
PropManager pmanager = PropManager.getInstance();
|
||||
String mfcFailOver = pmanager.getProperty("MFC_FAILOVER", "FAILOVER");
|
||||
|
||||
if(mfcFailOver != null && "Y".equals(mfcFailOver.toUpperCase())) {
|
||||
String testCallUrl2Grp = (String)prop.get("TEST_CALL_URL_2GRP");
|
||||
if (testCallUrl2Grp != null && testCallUrl2Grp.trim().length() > 0) {
|
||||
testCallUrl = testCallUrl2Grp;
|
||||
if (logger.isWarn()) logger.warn(pgoName + " - USE TEST_CALL_URL_2GRP - " + testCallUrl );
|
||||
}
|
||||
}
|
||||
// MFC_FAILOVER : FAILOVER = Y 일 경우 무조건 2센터로 테스트콜을 처리한다 (2020-10-16)
|
||||
|
||||
if (testCallUrl != null && testCallUrl.trim().length() > 0) {
|
||||
prop.put("URL", testCallUrl);
|
||||
}
|
||||
|
||||
Properties testCallProp = PropManager.getInstance().getProperties(TestCallManager.PROP_GROUP);
|
||||
String responseType = testCallProp.getProperty(TestCallManager.RESPONSE_TYPE, "SYNC");
|
||||
String httpTimeout = testCallProp.getProperty(TestCallManager.HTTP_TIME_OUT, "5000");
|
||||
String connectionTimeout = testCallProp.getProperty(TestCallManager.CONNECTION_TIMEOUT, "3000");
|
||||
|
||||
prop.setProperty(TestCallManager.RESPONSE_TYPE,responseType);
|
||||
prop.setProperty(TestCallManager.HTTP_TIME_OUT, httpTimeout);
|
||||
prop.setProperty(TestCallManager.CONNECTION_TIMEOUT, connectionTimeout);
|
||||
Properties tempProp = new Properties();
|
||||
tempProp.setProperty(TestCallManager.TESTCALL_YN, "Y");
|
||||
|
||||
if(logger.isWarn()) {
|
||||
logger.warn("HttpAdapterTestCall] TestCall ["+vo.getAdapterGroupName()+"] URL = " + prop.getProperty("URL") );
|
||||
logger.warn("HttpAdapterTestCall] TestCall ["+vo.getAdapterGroupName()+"] " + TestCallManager.RESPONSE_TYPE +" = " + prop.getProperty(TestCallManager.RESPONSE_TYPE) );
|
||||
logger.warn("HttpAdapterTestCall] TestCall ["+vo.getAdapterGroupName()+"] " + TestCallManager.HTTP_TIME_OUT +" = " + prop.getProperty(TestCallManager.HTTP_TIME_OUT) );
|
||||
logger.warn("HttpAdapterTestCall] TestCall ["+vo.getAdapterGroupName()+"] " + TestCallManager.CONNECTION_TIMEOUT +" = " + prop.getProperty(TestCallManager.CONNECTION_TIMEOUT) );
|
||||
logger.warn("HttpAdapterTestCall] TestCall ["+vo.getAdapterGroupName()+"] " + TestCallManager.TESTCALL_YN +" = " + tempProp.getProperty(TestCallManager.TESTCALL_YN) );
|
||||
|
||||
}
|
||||
|
||||
Object result = adapter.callService(vo.getAdapterGroupName(),prop,MESSAGE.getBytes(), tempProp);
|
||||
if(logger.isWarn()) {
|
||||
logger.warn("HttpAdapterTestCall] TestCall["+vo.getAdapterGroupName()+"], result ="+ result );
|
||||
}
|
||||
return true;
|
||||
} catch(HttpStatusException hse) {
|
||||
logger.warn("HttpAdapterTestCall] Error - "+hse.getClass().getName()+" : "+hse.getMessage());
|
||||
return true;
|
||||
} catch(Exception e) {
|
||||
//e.printStackTrace();
|
||||
logger.warn("HttpAdapterTestCall] Error - "+e.getClass().getName()+" : "+e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,915 @@
|
||||
//
|
||||
//package com.eactive.eai.adapter.http;
|
||||
//
|
||||
//import java.net.ConnectException;
|
||||
//import java.net.SocketTimeoutException;
|
||||
//import java.util.Properties;
|
||||
//import org.apache.commons.httpclient.HttpClient;
|
||||
//import org.apache.commons.httpclient.HttpMethod;
|
||||
//import org.apache.commons.httpclient.HttpMethodRetryHandler;
|
||||
//import org.apache.commons.httpclient.HttpStatus;
|
||||
//import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
||||
//import org.apache.commons.httpclient.NoHttpResponseException;
|
||||
//import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
|
||||
//import org.apache.commons.httpclient.methods.GetMethod;
|
||||
//import org.apache.commons.httpclient.methods.PostMethod;
|
||||
//import org.apache.commons.httpclient.params.HttpClientParams;
|
||||
//import org.apache.commons.httpclient.params.HttpMethodParams;
|
||||
//
|
||||
//import com.eactive.eai.adapter.WLIDefaultAdapter;
|
||||
//import com.eactive.eai.common.errorcode.ErrorCodeHandler;
|
||||
//import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
//import com.eactive.eai.common.property.PropManager;
|
||||
//import com.eactive.eai.common.util.Logger;
|
||||
//
|
||||
//
|
||||
///**
|
||||
//* 1. 기능 : EAI HTTP OutBound 용 어댑터로 수동 시스템의 HTTP 웹 컴포넌트를
|
||||
//* GET/POST 방식으로 호출할 수 있는 기능을 제공한다.
|
||||
//* 2. 처리 개요 :
|
||||
//* * - 2009.11.24 retry 로직 제거 : 강길수차장요청
|
||||
//* 3. 주의사항
|
||||
//*
|
||||
//* @author :
|
||||
//* @version : v 1.0.0
|
||||
//* @see : WLIAdapterFactory.java, WLIAdapter.java, WLIDefaultAdapter.java
|
||||
//* @since :
|
||||
//
|
||||
//*/
|
||||
//public class HttpClientAdapter extends WLIDefaultAdapter
|
||||
//{
|
||||
// public static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
//
|
||||
// public static final String URL = "URL";
|
||||
// public static final String METHOD = "METHOD";
|
||||
// public static final String PARAMETER_NAME = "PARAMETER_NAME";
|
||||
//
|
||||
// // encode 추가 : 20071207
|
||||
// public static final String ENCODE = "ENCODE";
|
||||
//
|
||||
// public static final String METHOD_GET = "GET";
|
||||
// public static final String METHOD_POST = "POST";
|
||||
//
|
||||
// //-------------------------------------------------
|
||||
// // 전자무역 상태조회 :
|
||||
// // 2009.01.09
|
||||
// public static final String METHOD_SOAP = "SOAP";
|
||||
// public static final String METHOD_SOAP_ACTION = "SOAPAction";
|
||||
// //-------------------------------------------------
|
||||
//
|
||||
// public static final String HTTP_CONNECTION_STALE_CHECKING_ENABLED = "HTTP_CONNECTION_STALE_CHECKING_ENABLED";
|
||||
// /*
|
||||
// * 호스트 마다 최대 연결 수
|
||||
// */
|
||||
// public static final String HTTP_MAX_CONNECTION_PER_HOST = "HTTP_MAX_CONNECTION_PER_HOST";
|
||||
// /*
|
||||
// * 최대 HTTP 연결 수
|
||||
// */
|
||||
// public static final String HTTP_MAX_TOTAL_CONNECTIONS = "HTTP_MAX_TOTAL_CONNECTIONS";
|
||||
//
|
||||
// static final String PARAMETER_MESSAGE = "_message_";
|
||||
//
|
||||
// private HttpClient client;
|
||||
//
|
||||
// // 2008.01.21 이동훈
|
||||
// // HttpClient 2.0 Timeout Setting
|
||||
// public static final String HTTP_TIME_OUT = "HTTP_TIME_OUT";
|
||||
// public static final int DEFAULT_TIME_OUT = 30 *1000;
|
||||
// // HttpClient 2.0 Timeout Setting
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : HttpClientAdapter default 생성자
|
||||
// * 2. 처리 개요 :
|
||||
// * - property 정보를 가져와 MultiThreadedHttpConnectionManager 속성 설정
|
||||
// * - HttpClient의 instance 생성
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// **/
|
||||
// public HttpClientAdapter() {
|
||||
// MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager();
|
||||
// PropManager manager = PropManager.getInstance();
|
||||
// String enabled = manager.getProperty(HTTP_CONNECTION_STALE_CHECKING_ENABLED);
|
||||
//
|
||||
// if(enabled==null) {
|
||||
//
|
||||
// enabled="true";
|
||||
//
|
||||
// String code = "RWCEAIAHA006";
|
||||
// String msg = ErrorCodeHandler.getMessage(code);
|
||||
// if(logger.isWarn()){
|
||||
// logger.warn(ExceptionUtil.make(code, msg));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// try {
|
||||
//
|
||||
// cm.getParams().setStaleCheckingEnabled(new Boolean(enabled).booleanValue());
|
||||
//
|
||||
// } catch(Exception e) {
|
||||
// e.printStackTrace();
|
||||
// String errCode = ExceptionUtil.getErrorCode(e, "RECEAIAHA007");
|
||||
// throw new RuntimeException(errCode);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// String connPerHost = manager.getProperty(HTTP_MAX_CONNECTION_PER_HOST);
|
||||
//
|
||||
// if(connPerHost==null) {
|
||||
// connPerHost = "10";
|
||||
// String code = "RWCEAIAHA008";
|
||||
// String msg = ErrorCodeHandler.getMessage(code);
|
||||
// if(logger.isWarn()){
|
||||
// logger.warn(ExceptionUtil.make(code, msg));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// try {
|
||||
// //cm.setMaxConnectionsPerHost(new Integer(connPerHost).intValue());
|
||||
// cm.getParams().setDefaultMaxConnectionsPerHost(new Integer(connPerHost).intValue());
|
||||
// } catch(Exception e) {
|
||||
// String errCode = ExceptionUtil.getErrorCode(e, "RECEAIAHA009");
|
||||
// throw new RuntimeException(errCode);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// String totalConnections = manager.getProperty(HTTP_MAX_TOTAL_CONNECTIONS);
|
||||
// if(totalConnections==null) {
|
||||
// totalConnections = "50";
|
||||
// String code = "RWCEAIAHA010";
|
||||
// String msg = ErrorCodeHandler.getMessage(code);
|
||||
// if(logger.isWarn()){
|
||||
// logger.warn(ExceptionUtil.make(code, msg));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// try {
|
||||
// //cm.setMaxTotalConnections(new Integer(totalConnections).intValue());
|
||||
// cm.getParams().setMaxTotalConnections(new Integer(totalConnections).intValue());
|
||||
// } catch(Exception e) {
|
||||
// String errCode = ExceptionUtil.getErrorCode(e, "RECEAIAHA011");
|
||||
// throw new RuntimeException(errCode);
|
||||
// }
|
||||
//
|
||||
// this.client = new HttpClient(cm);
|
||||
//
|
||||
//
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 현재 사용중인 Connection 수
|
||||
// * 2. 처리 개요 :
|
||||
// * -
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @return int 사용중인 Connection 수
|
||||
// **/
|
||||
// public int getConnectionsInPool() {
|
||||
// MultiThreadedHttpConnectionManager mgr = (MultiThreadedHttpConnectionManager)this.client.getHttpConnectionManager();
|
||||
// return mgr.getConnectionsInPool();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 생성된 총 Connection 수
|
||||
// * 2. 처리 개요 :
|
||||
// * -
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @return int 총 Connection 수
|
||||
// **/
|
||||
// public int getMaxTotalConnections() {
|
||||
// MultiThreadedHttpConnectionManager mgr = (MultiThreadedHttpConnectionManager)this.client.getHttpConnectionManager();
|
||||
// return mgr.getParams().getMaxTotalConnections();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 호스트 당 할당된 총 Connection 수
|
||||
// * 2. 처리 개요 :
|
||||
// * -
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @return int 총 Connection 수
|
||||
// **/
|
||||
// public int getMaxConnectionsPerHost() {
|
||||
// MultiThreadedHttpConnectionManager mgr = (MultiThreadedHttpConnectionManager)this.client.getHttpConnectionManager();
|
||||
// return mgr.getParams().getDefaultMaxConnectionsPerHost();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 전문과 같은 Raw Data 통신에 사용
|
||||
// * 2. 처리 개요 :
|
||||
// * - 속성 정보를 설정 하고 수동 시스템 서비스를 호출 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param prop Http Adapter 속성 정보
|
||||
// * @param message 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// public Object execute(Properties prop, Object message) throws Exception
|
||||
// {
|
||||
//
|
||||
// byte[] data = null;
|
||||
// if(message instanceof byte[]) {
|
||||
// data = (byte[])message;
|
||||
// } else if( message instanceof String) {
|
||||
// data = ((String)message).getBytes();
|
||||
// } else {
|
||||
// throw new Exception("RECEAIAHA011");
|
||||
// }
|
||||
// String url = prop.getProperty(URL);
|
||||
// String parameterName = prop.getProperty(PARAMETER_NAME);
|
||||
// String httpMethod = prop.getProperty(METHOD);
|
||||
// String encode = prop.getProperty(ENCODE);
|
||||
// String httpTimeOut = prop.getProperty(HTTP_TIME_OUT);
|
||||
//
|
||||
// String soapAction = prop.getProperty(METHOD_SOAP_ACTION);
|
||||
//
|
||||
// //-------------------------------------------
|
||||
// // trim 처리 : 2009.03.27
|
||||
// //-------------------------------------------
|
||||
// if(url != null) {
|
||||
// url = url.trim();
|
||||
// }
|
||||
//
|
||||
// if(parameterName != null) {
|
||||
// parameterName = parameterName.trim();
|
||||
// }
|
||||
//
|
||||
// if(httpMethod != null) {
|
||||
// httpMethod = httpMethod.trim();
|
||||
// }
|
||||
//
|
||||
// if(encode != null) {
|
||||
// encode = encode.trim();
|
||||
// }
|
||||
//
|
||||
// if(soapAction != null) {
|
||||
// soapAction = soapAction.trim();
|
||||
// }
|
||||
//
|
||||
// //----------------------------------
|
||||
// // 2008.02.15 이동훈
|
||||
// // 설정값이 없을 경우 무한대로 기다리도록
|
||||
// // 0 으로 설정하도록 함
|
||||
// //----------------------------------
|
||||
// if(httpTimeOut == null) {
|
||||
// httpTimeOut = "0";
|
||||
// }
|
||||
// else {
|
||||
// httpTimeOut = httpTimeOut.trim();
|
||||
// }
|
||||
//
|
||||
// int timeout = -1;
|
||||
// try {
|
||||
// timeout = new Integer(httpTimeOut).intValue();
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] HTTP TIMEOUT SET ["+httpTimeOut+"]");
|
||||
// }
|
||||
// } catch(Exception e) {
|
||||
// if(logger.isWarn()){
|
||||
// logger.warn("HttpClientAdapter] HTTP TIMEOUT 설정값을 확인하세요. ["+httpTimeOut+"]");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] Send("+httpMethod+") Data ["+new String(data)+"]");
|
||||
// }
|
||||
//
|
||||
// if(httpMethod!=null && httpMethod.equals(METHOD_POST)) {
|
||||
// return callPostService(url, parameterName, data, encode, timeout);
|
||||
// }
|
||||
// else if(httpMethod!=null && httpMethod.equals(METHOD_SOAP)) {
|
||||
// return callSoapService(url, soapAction, data, encode, timeout);
|
||||
// }
|
||||
// else if(httpMethod!=null && httpMethod.equals("MEMO")) {
|
||||
// return callMemoService(url, data, encode, timeout);
|
||||
// }
|
||||
// else if(httpMethod!=null && httpMethod.equals("ANN")) {
|
||||
// return callAnnounceService(url, data, encode, timeout);
|
||||
// }
|
||||
// else if(httpMethod!=null && httpMethod.equals("POPUP")) {
|
||||
// return callPopupService(url, data, encode, timeout);
|
||||
// }
|
||||
// else if(httpMethod!=null && httpMethod.equals("CDSVC")) {
|
||||
// return callChangeDateService(url, data, encode, timeout);
|
||||
// }
|
||||
// else {
|
||||
// return callGetService(url, parameterName, data, encode, timeout);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 수동 시스템에 Post 방식으로 서비스 호출
|
||||
// * 2. 처리 개요 :
|
||||
// * - 수동 시스템에 서비스 요청 하고 결과를 리턴 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param url 수동 시스템 웹 컴포넌트 URL
|
||||
// * @param parameterName URL 파리미터 이름
|
||||
// * @param data 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// private byte[] callPostService(String url, String parameterName, byte[] data, String encode, int timeout)
|
||||
// throws Exception {
|
||||
// PostMethod method = new PostMethod(url);
|
||||
//
|
||||
// //2008.02.15 : Timeout 처리로직 변경 (0 : 무한대 설정가능하게 함)
|
||||
// if(timeout > -1) {
|
||||
// method.getParams().setSoTimeout(timeout);
|
||||
// }
|
||||
//
|
||||
// //method.setRequestHeader("Content-Type","text/html; charset=euc-kr");
|
||||
//
|
||||
// String sendData = null;
|
||||
// HttpClient mclient = new HttpClient(new MultiThreadedHttpConnectionManager());
|
||||
//
|
||||
// if(encode != null && encode.length() > 0) {
|
||||
//
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] ENCODE("+encode+") ");
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// mclient.getParams().setContentCharset(encode);
|
||||
// sendData = new String(data,encode);
|
||||
// } catch(Exception e) {
|
||||
// if(logger.isError()){
|
||||
// logger.error("HttpClientAdapter] ENCODE("+encode+") ERROR : "+ e.toString());
|
||||
// logger.error("HttpClientAdapter] ENCODE("+encode+") SKIP ");
|
||||
// }
|
||||
// sendData = new String(data);
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// //신용보증기금에 필요 분기 - 조현덕 추가(2008.11.04)
|
||||
// if(parameterName.equals("SHINBO")){
|
||||
// ;
|
||||
// }else{
|
||||
// sendData = java.net.URLEncoder.encode(new String(data));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// if(parameterName==null || parameterName.equals("")) {
|
||||
// method.addParameter(PARAMETER_MESSAGE, sendData);
|
||||
//
|
||||
// //신용보증기금에 필요 분기 - 조현덕 추가(2008.05.06)
|
||||
// } else if(parameterName.equals("SHINBO")){
|
||||
// method.setRequestBody(sendData);
|
||||
// } else {
|
||||
// method.addParameter(parameterName, sendData);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// int status = -1;
|
||||
//
|
||||
//
|
||||
// try {
|
||||
// status = mclient.executeMethod(method);
|
||||
// //----------------------------------------------------------
|
||||
// // 2008.01.23 : 응답이 깨지는 것 수정
|
||||
// //----------------------------------------------------------
|
||||
// //String responseMessage = method.getResponseBodyAsString();
|
||||
// String responseMessage = new String(method.getResponseBody());
|
||||
// //----------------------------------------------------------
|
||||
//
|
||||
// return responseMessage.getBytes();
|
||||
//
|
||||
// } catch(SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
// ste.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] SocketTimeoutException : " + ste.toString() );
|
||||
// throw ste;
|
||||
// } catch(ConnectException ce) {
|
||||
// ce.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] Connection Exception : " + ce.toString() );
|
||||
// throw ce;
|
||||
// } catch(Exception e) {
|
||||
// e.printStackTrace();
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if(status != HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 수동 시스템에 Get 방식으로 서비스 호출
|
||||
// * 2. 처리 개요 :
|
||||
// * - 수동 시스템에 서비스 요청 하고 결과를 리턴 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param url 수동 시스템 웹 컴포넌트 URL
|
||||
// * @param parameterName URL 파리미터 이름
|
||||
// * @param data 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// private byte[] callGetService(String url, String parameterName, byte[] data, String encode, int timeout)
|
||||
// throws Exception {
|
||||
// GetMethod method = new GetMethod(url);
|
||||
//
|
||||
// //2008.02.15 : Timeout 처리로직 변경 (0 : 무한대 설정가능하게 함)
|
||||
// if(timeout > -1) {
|
||||
// method.getParams().setSoTimeout(timeout);
|
||||
// }
|
||||
//
|
||||
// String sendData = null;
|
||||
//
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] GET Method URLEncoder");
|
||||
// }
|
||||
// sendData = java.net.URLEncoder.encode(new String(data));
|
||||
//
|
||||
// /*
|
||||
// if(encode != null && encode.length() > 0) {
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] ENCODE("+encode+") ");
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// sendData = new String(data, encode);
|
||||
// } catch(Exception e) {
|
||||
// if(logger.isError()){
|
||||
// logger.error("HttpClientAdapter] ENCODE("+encode+") ERROR : "+ e.toString());
|
||||
// logger.error("HttpClientAdapter] ENCODE("+encode+") SKIP ");
|
||||
// }
|
||||
// sendData = new String(data);
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// sendData = new String(data);
|
||||
//
|
||||
// }
|
||||
// */
|
||||
//
|
||||
// method.setQueryString(parameterName+"=" + sendData);
|
||||
//
|
||||
// int status = -1;
|
||||
// try {
|
||||
// status = client.executeMethod(method);
|
||||
// //----------------------------------------------------------
|
||||
// // 2008.01.23 : 응답이 깨지는 것 수정
|
||||
// //----------------------------------------------------------
|
||||
// //String responseMessage = method.getResponseBodyAsString();
|
||||
// String responseMessage = new String(method.getResponseBody());
|
||||
// //----------------------------------------------------------
|
||||
// return responseMessage.getBytes();
|
||||
// } catch(SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
// ste.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] SocketTimeoutException : " + ste.toString() );
|
||||
// throw ste;
|
||||
// } catch(ConnectException ce) {
|
||||
// ce.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] Connection Exception : " + ce.toString() );
|
||||
// throw ce;
|
||||
// } catch(Exception e) {
|
||||
// e.printStackTrace();
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if(status!=HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA014", msgArgs);
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 수동 시스템에 SOAP 방식으로 서비스 호출
|
||||
// * 2. 처리 개요 :
|
||||
// * - 수동 시스템에 서비스 요청 하고 결과를 리턴 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param url 수동 시스템 웹 컴포넌트 URL
|
||||
// * @param soapAction soapAction 이름
|
||||
// * @param data 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// private byte[] callSoapService(String url, String soapAction, byte[] data, String encode, int timeout)
|
||||
// throws Exception {
|
||||
// PostMethod method = new PostMethod(url);
|
||||
//
|
||||
// if(timeout > -1) {
|
||||
// method.getParams().setSoTimeout(timeout);
|
||||
// }
|
||||
//
|
||||
// ByteArrayRequestEntity entity = new ByteArrayRequestEntity(data);
|
||||
//
|
||||
// method.setRequestEntity(entity);
|
||||
// method.setRequestHeader("SOAPAction", soapAction);
|
||||
//
|
||||
// int status = -1;
|
||||
//
|
||||
//
|
||||
// try {
|
||||
// status = client.executeMethod(method);
|
||||
// //----------------------------------------------------------
|
||||
// // 2008.01.23 : 응답이 깨지는 것 수정
|
||||
// //----------------------------------------------------------
|
||||
// //String responseMessage = method.getResponseBodyAsString();
|
||||
// String responseMessage = new String(method.getResponseBody());
|
||||
// //----------------------------------------------------------
|
||||
//
|
||||
// return responseMessage.getBytes();
|
||||
//
|
||||
// } catch(SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
// ste.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] SocketTimeoutException : " + ste.toString() );
|
||||
// throw ste;
|
||||
// } catch(ConnectException ce) {
|
||||
// ce.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] Connection Exception : " + ce.toString() );
|
||||
// throw ce;
|
||||
// } catch(Exception e) {
|
||||
// e.printStackTrace();
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if(status != HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 수동 시스템에 Post 방식으로 메신저 연계서비스 호출
|
||||
// * 2. 처리 개요 :
|
||||
// * - 수동 시스템에 서비스 요청 하고 결과를 리턴 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param url 수동 시스템 웹 컴포넌트 URL
|
||||
// * @param data 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// private byte[] callMemoService(String url, byte[] data, String encode, int timeout)
|
||||
// throws Exception {
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] callMemoService called ");
|
||||
// }
|
||||
//
|
||||
// PostMethod method = new PostMethod(url);
|
||||
//
|
||||
// //2008.02.15 : Timeout 처리로직 변경 (0 : 무한대 설정가능하게 함)
|
||||
// if(timeout > -1) {
|
||||
// method.getParams().setSoTimeout(timeout);
|
||||
// }
|
||||
//
|
||||
// // 2009.08.24 강길수차장 요청
|
||||
// // MEMO Data Size : 161 Bytes -> 587 (항목 및 길이가 변경됨)
|
||||
// int memoMinSize = 587;
|
||||
//
|
||||
// if(data.length >= memoMinSize) {
|
||||
// if(encode != null && encode.length() > 0) {
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] callMemoService ENCODE("+encode+") ");
|
||||
// }
|
||||
// method.addParameter("SRV_CODE", new String(data, 0, 6, encode) );
|
||||
// method.addParameter("SER_TYPE", new String(data, 6, 1, encode) );
|
||||
// method.addParameter("RECIPIENT", new String(data, 7, 7, encode) );
|
||||
// method.addParameter("SEND", new String(data, 14, 7, encode) );
|
||||
// method.addParameter("TITLE", new String(data, 21, 40, encode) );
|
||||
// method.addParameter("BODY", new String(data, 61,500, encode) );
|
||||
// method.addParameter("URGENT", new String(data,561, 1, encode) );
|
||||
// method.addParameter("SAVEOPTION", new String(data,562, 1, encode) );
|
||||
// method.addParameter("RCV_IP", new String(data,563, 24, encode) );
|
||||
// }
|
||||
// else {
|
||||
// method.addParameter("SRV_CODE", new String(data, 0, 6) );
|
||||
// method.addParameter("SER_TYPE", new String(data, 6, 1) );
|
||||
// method.addParameter("RECIPIENT", new String(data, 7, 7) );
|
||||
// method.addParameter("SEND", new String(data, 14, 7) );
|
||||
// method.addParameter("TITLE", new String(data, 21, 40) );
|
||||
// method.addParameter("BODY", new String(data, 61,500) );
|
||||
// method.addParameter("URGENT", new String(data,561, 1) );
|
||||
// method.addParameter("SAVEOPTION", new String(data,562, 1) );
|
||||
// method.addParameter("RCV_IP", new String(data,563, 24) );
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// if(logger.isError()){
|
||||
// logger.error("HttpClientAdapter] callMemoService Invalid Data Length (" + data.length + ") < "+memoMinSize);
|
||||
// }
|
||||
// throw new Exception("HttpClientAdapter.callMemoService Error : MessageSize("+(data.length)+" Bytes) too small < "+memoMinSize);
|
||||
// }
|
||||
//
|
||||
// int status = -1;
|
||||
//
|
||||
//
|
||||
// try {
|
||||
// HttpClient mclient = new HttpClient(new MultiThreadedHttpConnectionManager());
|
||||
// mclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
|
||||
// mclient.getParams().setContentCharset("EUC_KR");
|
||||
// status = mclient.executeMethod(method);
|
||||
// String responseMessage = new String(method.getResponseBody());
|
||||
//
|
||||
// return responseMessage.getBytes();
|
||||
//
|
||||
// } catch(SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
// ste.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] SocketTimeoutException : " + ste.toString() );
|
||||
// throw ste;
|
||||
// } catch(ConnectException ce) {
|
||||
// ce.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] Connection Exception : " + ce.toString() );
|
||||
// throw ce;
|
||||
// } catch(Exception e) {
|
||||
// e.printStackTrace();
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if(status != HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 수동 시스템에 Post 방식으로 메신저 연계서비스 호출
|
||||
// * 2. 처리 개요 :
|
||||
// * - 수동 시스템에 서비스 요청 하고 결과를 리턴 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param url 수동 시스템 웹 컴포넌트 URL
|
||||
// * @param data 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// private byte[] callAnnounceService(String url, byte[] data, String encode, int timeout)
|
||||
// throws Exception {
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] callAnnounceService called ");
|
||||
// }
|
||||
//
|
||||
// PostMethod method = new PostMethod(url);
|
||||
//
|
||||
// //2008.02.15 : Timeout 처리로직 변경 (0 : 무한대 설정가능하게 함)
|
||||
// if(timeout > -1) {
|
||||
// method.getParams().setSoTimeout(timeout);
|
||||
// }
|
||||
//
|
||||
// // Announce Data Size : 113 Bytes
|
||||
// int annMinSize = 113;
|
||||
//
|
||||
// if(data.length >= annMinSize) {
|
||||
// if(encode != null && encode.length() > 0) {
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] callAnnounceService ENCODE("+encode+") ");
|
||||
// }
|
||||
// method.addParameter("SRV_CODE", new String(data, 0, 6, encode) );
|
||||
// method.addParameter("RECIPIENT", new String(data, 6, 7, encode) );
|
||||
// method.addParameter("BODY", new String(data, 13,100, encode) );
|
||||
// if(data.length > 153) {
|
||||
// method.addParameter("URL", new String(data, 113, data.length-annMinSize,encode) );
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// method.addParameter("SRV_CODE", new String(data, 0, 6) );
|
||||
// method.addParameter("RECIPIENT", new String(data, 6, 7) );
|
||||
// method.addParameter("BODY", new String(data, 13,100) );
|
||||
// if(data.length > 153) {
|
||||
// method.addParameter("URL", new String(data,113, data.length-annMinSize) );
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// if(logger.isError()){
|
||||
// logger.error("HttpClientAdapter] callAnnounceService Invalid Data Length (" + data.length + ") < "+annMinSize);
|
||||
// }
|
||||
// throw new Exception("HttpClientAdapter.callAnnounceService Error : MessageSize("+(data.length)+" Bytes) too small < "+annMinSize);
|
||||
// }
|
||||
//
|
||||
// int status = -1;
|
||||
//
|
||||
//
|
||||
// try {
|
||||
// HttpClient mclient = new HttpClient(new MultiThreadedHttpConnectionManager());
|
||||
// mclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
|
||||
// mclient.getParams().setContentCharset("EUC_KR");
|
||||
// status = mclient.executeMethod(method);
|
||||
// String responseMessage = new String(method.getResponseBody());
|
||||
//
|
||||
// return responseMessage.getBytes();
|
||||
//
|
||||
// } catch(SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
// ste.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] SocketTimeoutException : " + ste.toString() );
|
||||
// throw ste;
|
||||
// } catch(ConnectException ce) {
|
||||
// ce.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] Connection Exception : " + ce.toString() );
|
||||
// throw ce;
|
||||
// } catch(Exception e) {
|
||||
// e.printStackTrace();
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if(status != HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 수동 시스템에 Post 방식으로 메신저 연계서비스 호출
|
||||
// * 2. 처리 개요 :
|
||||
// * - 수동 시스템에 서비스 요청 하고 결과를 리턴 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param url 수동 시스템 웹 컴포넌트 URL
|
||||
// * @param data 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// private byte[] callPopupService(String url, byte[] data, String encode, int timeout)
|
||||
// throws Exception {
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] callPopupService called ");
|
||||
// }
|
||||
//
|
||||
// PostMethod method = new PostMethod(url);
|
||||
//
|
||||
//
|
||||
// //2008.02.15 : Timeout 처리로직 변경 (0 : 무한대 설정가능하게 함)
|
||||
// if(timeout > -1) {
|
||||
// method.getParams().setSoTimeout(timeout);
|
||||
// }
|
||||
//
|
||||
// // Popup Data Size : 153 Bytes
|
||||
// int popupMinSize = 153;
|
||||
//
|
||||
// if(data.length >= popupMinSize) {
|
||||
// if(encode != null && encode.length() > 0) {
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] callPopupService ENCODE("+encode+") ");
|
||||
// }
|
||||
// method.addParameter("SRV_CODE", new String(data, 0, 6, encode) );
|
||||
// method.addParameter("RECIPIENT", new String(data, 6, 7, encode) );
|
||||
// method.addParameter("TITLE", new String(data, 13, 40, encode) );
|
||||
// method.addParameter("BODY", new String(data, 53,100, encode) );
|
||||
// }
|
||||
// else {
|
||||
// method.addParameter("SRV_CODE", new String(data, 0, 6) );
|
||||
// method.addParameter("RECIPIENT", new String(data, 6, 7) );
|
||||
// method.addParameter("TITLE", new String(data, 13, 40) );
|
||||
// method.addParameter("BODY", new String(data, 53,100) );
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// if(logger.isError()){
|
||||
// logger.error("HttpClientAdapter] callPopupService Invalid Data Length (" + data.length + ") < "+popupMinSize);
|
||||
// }
|
||||
// throw new Exception("HttpClientAdapter.callPopupService Error : MessageSize("+(data.length)+" Bytes) too small < "+popupMinSize);
|
||||
// }
|
||||
//
|
||||
// int status = -1;
|
||||
//
|
||||
//
|
||||
// try {
|
||||
// HttpClient mclient = new HttpClient(new MultiThreadedHttpConnectionManager());
|
||||
// mclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
|
||||
// mclient.getParams().setContentCharset("EUC_KR");
|
||||
// status = mclient.executeMethod(method);
|
||||
// String responseMessage = new String(method.getResponseBody());
|
||||
//
|
||||
// return responseMessage.getBytes();
|
||||
//
|
||||
// } catch(SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
// ste.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] SocketTimeoutException : " + ste.toString() );
|
||||
// throw ste;
|
||||
// } catch(ConnectException ce) {
|
||||
// ce.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] Connection Exception : " + ce.toString() );
|
||||
// throw ce;
|
||||
// } catch(Exception e) {
|
||||
// e.printStackTrace();
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if(status != HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 수동 시스템에 Post 방식으로 일자전환이벤트 호출
|
||||
// * 2. 처리 개요 :
|
||||
// * - 수동 시스템에 서비스 요청 하고 결과를 리턴 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param url 수동 시스템 웹 컴포넌트 URL
|
||||
// * @param data 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// private byte[] callChangeDateService(String url, byte[] data, String encode, int timeout)
|
||||
// throws Exception {
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] callChangeDateService called ");
|
||||
// }
|
||||
//
|
||||
// PostMethod method = new PostMethod(url);
|
||||
//
|
||||
// //2008.02.15 : Timeout 처리로직 변경 (0 : 무한대 설정가능하게 함)
|
||||
// if(timeout > -1) {
|
||||
// method.getParams().setSoTimeout(timeout);
|
||||
// }
|
||||
//
|
||||
// // Data Size : 15 Bytes
|
||||
// int popupMinSize = 15;
|
||||
//
|
||||
// if(data.length >= popupMinSize) {
|
||||
// if(encode != null && encode.length() > 0) {
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapter] callChangeDateService ENCODE("+encode+") ");
|
||||
// }
|
||||
// method.addParameter("req", new String(data, 0, 7, encode).trim() );
|
||||
// method.addParameter("date", new String(data, 7, 8, encode) );
|
||||
// }
|
||||
// else {
|
||||
// method.addParameter("req", new String(data, 0, 7).trim() );
|
||||
// method.addParameter("date", new String(data, 7, 8) );
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// if(logger.isError()){
|
||||
// logger.error("HttpClientAdapter] callChangeDateService Invalid Data Length (" + data.length + ") < "+popupMinSize);
|
||||
// }
|
||||
// throw new Exception("HttpClientAdapter.callChangeDateService Error : MessageSize("+(data.length)+" Bytes) too small < "+popupMinSize);
|
||||
// }
|
||||
//
|
||||
// int status = -1;
|
||||
//
|
||||
//
|
||||
// try {
|
||||
// HttpClient mclient = new HttpClient(new MultiThreadedHttpConnectionManager());
|
||||
// mclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
|
||||
// mclient.getParams().setContentCharset("EUC_KR");
|
||||
// status = mclient.executeMethod(method);
|
||||
// String responseMessage = new String(method.getResponseBody());
|
||||
//
|
||||
// return responseMessage.getBytes();
|
||||
//
|
||||
// } catch(SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
// ste.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] SocketTimeoutException : " + ste.toString() );
|
||||
// throw ste;
|
||||
// } catch(ConnectException ce) {
|
||||
// ce.printStackTrace();
|
||||
// System.out.println("HttpClientAdapter] Connection Exception : " + ce.toString() );
|
||||
// throw ce;
|
||||
// } catch(Exception e) {
|
||||
// e.printStackTrace();
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if(status != HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.eactive.eai.adapter.http;
|
||||
//import org.apache.commons.httpclient.params.HttpClientParams;
|
||||
|
||||
|
||||
public class HttpClientTest
|
||||
{
|
||||
// public static void main(String args[]) throws Exception {
|
||||
// MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager();
|
||||
//
|
||||
//
|
||||
// cm.setConnectionStaleCheckingEnabled(false);
|
||||
// cm.setMaxConnectionsPerHost(10);
|
||||
// cm.setMaxTotalConnections(1);
|
||||
//
|
||||
// // 2.0
|
||||
// HttpClient client = new HttpClient(cm);
|
||||
//
|
||||
// //---------------------------------------------
|
||||
// // 3.1
|
||||
// //---------------------------------------------
|
||||
// /*
|
||||
// HttpClientParams params = new HttpClientParams();
|
||||
// params.setSoTimeout(10 * 1000);
|
||||
// HttpClient client = new HttpClient(params, cm);
|
||||
// */
|
||||
// //---------------------------------------------
|
||||
//
|
||||
// // Client Timeout 2.0
|
||||
// //client.setTimeout(1000);
|
||||
// String url = "http://172.29.65.69:7001/servlets/knova/svc/servlet/KBServlet";
|
||||
// //String url = "http://search.empas.com/error.html";
|
||||
// //String url = "http://172.17.33.237:7001/servlets/knova/svc/servlet/KBServlet";
|
||||
//
|
||||
// PostMethod method = new PostMethod(url);
|
||||
// DefaultMethodRetryHandler retryhandler = new DefaultMethodRetryHandler();
|
||||
// retryhandler.setRequestSentRetryEnabled(false);
|
||||
// retryhandler.setRetryCount(3);
|
||||
// method.setMethodRetryHandler(retryhandler);
|
||||
//
|
||||
// method.getParams().setSoTimeout(5000);
|
||||
//
|
||||
// System.out.println("HttpClientAdapter] getContentCharset("+client.getParams().getContentCharset()+") ");
|
||||
// //client.getParams().setContentCharset("KSC5601");
|
||||
// System.out.println("HttpClientAdapter] getContentCharset("+client.getParams().getContentCharset()+") ");
|
||||
//
|
||||
// String encode = "ISO8859_1";
|
||||
// byte[] data = "000924KCO.11YE#0803249920000026 0200NOV01500001 20080324175117 HOSTDOM NOVDOM BB2B YE#0803249920000026 4001 000306604151345551013205062155211 이기옥 EL08022500020000000000000000056020080324200902261 ? 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 "
|
||||
// .getBytes();
|
||||
// String sendData = null;
|
||||
// if(encode != null && encode.length() > 0) {
|
||||
// System.out.println("HttpClientAdapter] ENCODE("+encode+") ");
|
||||
//
|
||||
// try {
|
||||
// sendData = new String(data,encode);
|
||||
// } catch(Exception e) {
|
||||
// System.out.println("HttpClientAdapter] ENCODE("+encode+") ERROR : "+ e.toString());
|
||||
// System.out.println("HttpClientAdapter] ENCODE("+encode+") SKIP ");
|
||||
// sendData = new String(data);
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// sendData = java.net.URLEncoder.encode(new String(data));
|
||||
// }
|
||||
//
|
||||
// method.addParameter("_ELD_", sendData);
|
||||
//
|
||||
// int status = -1;
|
||||
// try {
|
||||
// status = client.executeMethod(method);
|
||||
// //String responseMessage = method.getResponseBodyAsString();
|
||||
// byte[] responseBytes = method.getResponseBody();
|
||||
// System.out.println("method.getRequestCharSet() [" + method.getRequestCharSet() +"]");
|
||||
// System.out.println("method.getResponseCharSet() [" + method.getResponseCharSet() +"]");
|
||||
// System.out.println("responseMessage [" +new String(responseBytes) +"]");
|
||||
// System.out.println("ISO8859_1 [" + new String(responseBytes, "ISO8859_1") +"]");
|
||||
// System.out.println("EUC-KR [" + new String(responseBytes, "EUC-KR") +"]");
|
||||
// } catch(ConnectException ce) {
|
||||
// System.out.println("Connection Exception : " + ce.toString() );
|
||||
// } catch(HttpRecoverableException hre) { // HttpClient 2.0일 경우에 발생
|
||||
// System.out.println("HttpRecoverableException : " + hre.toString() );
|
||||
// } catch(SocketTimeoutException ste) { // HttpClient 3.1일 경우에 발생
|
||||
// System.out.println("SocketTimeoutException : " + ste.toString() );
|
||||
// } catch(Exception e) {
|
||||
// e.printStackTrace();
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if(status != HttpStatus.SC_OK) {
|
||||
// System.out.println("HttpStatus ["+status+"]");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.eactive.eai.adapter.http;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterPropManager;
|
||||
import com.eactive.eai.adapter.AdapterVO;
|
||||
import com.eactive.eai.common.testcall.TestCallException;
|
||||
import com.eactive.eai.common.testcall.TestCallSupport;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
|
||||
//HttpQuicsClientAdapter의 통신상태를 체크하는 클래스
|
||||
public class HttpExtOrgClientAdapterTestCall extends TestCallSupport
|
||||
{
|
||||
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
public boolean isAlive() throws TestCallException
|
||||
{
|
||||
logger.debug("HttpExtOrgClientAdapterTestCall started");
|
||||
|
||||
try {
|
||||
AdapterVO vo = this.getAdapterInfo();
|
||||
String pgoName = vo.getPropGroupName();
|
||||
//PropManager manager = PropManager.getInstance();
|
||||
AdapterPropManager manager = AdapterPropManager.getInstance();
|
||||
Properties prop = manager.getProperties(pgoName);
|
||||
|
||||
|
||||
String url = prop.getProperty("URL");
|
||||
//String url = "101.88.50.120:8285";
|
||||
logger.debug("HttpExtOrgClientAdapterTestCall URL]["+url+"s]");
|
||||
|
||||
URL u = null;
|
||||
try {
|
||||
logger.debug("HttpExtOrgClientAdapterTestCall] url - ["+url+"]");
|
||||
u = new URL(url);
|
||||
} catch (MalformedURLException e) {
|
||||
logger.warn("HttpExtOrgClientAdapterTestCall] Error at new URL - "+e.getClass().getName()+" : "+e.getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
HttpURLConnection conn = null;
|
||||
|
||||
try {
|
||||
conn = (HttpURLConnection) u.openConnection();
|
||||
} catch (Exception e) {
|
||||
logger.warn("HttpExtOrgClientAdapterTestCall] Error at openConnection - "+e.getClass().getName()+" : "+e.getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
if(conn == null){
|
||||
logger.warn("HttpExtOrgClientAdapterTestCall] Error at HttpURLConnection is null ");
|
||||
return false;
|
||||
}
|
||||
logger.debug("HttpExtOrgClientAdapterTestCall ] try HttpURLConnection connect");
|
||||
conn.connect();
|
||||
logger.debug("HttpExtOrgClientAdapterTestCall end");
|
||||
return true;
|
||||
|
||||
} catch(Exception e) {
|
||||
//e.printStackTrace();
|
||||
logger.warn("HttpExtOrgClientAdapterTestCall] Error - "+e.getClass().getName()+" : "+e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// public static void main(String args[]) throws Exception {
|
||||
// test();
|
||||
//}
|
||||
|
||||
// private static void test() throws Exception {
|
||||
// HttpExtOrgClientAdapterTestCall test = new HttpExtOrgClientAdapterTestCall();
|
||||
// System.out.println("HttpExtOrgClientAdapterTestCall ] isAlive - ["+test.isAlive()+"]");
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.eactive.eai.adapter.http;
|
||||
|
||||
import com.eactive.eai.transformer.util.CircularQueue;
|
||||
import com.eactive.eai.transformer.util.ExceptionStackTrace;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
|
||||
public class HttpMemoryLogger {
|
||||
final static int MAX_LOG_MESSAGE_SIZE = 500000;
|
||||
|
||||
static LinkedHashMap<String,CircularQueue> txQueues = new LinkedHashMap<String,CircularQueue>();
|
||||
static LinkedHashMap<String,CircularQueue> errorQueues = new LinkedHashMap<String,CircularQueue>();
|
||||
static LinkedHashMap<String,Long> errorCounts = new LinkedHashMap<String,Long>();
|
||||
static LinkedHashMap<String,Long> txCounts = new LinkedHashMap<String,Long>();
|
||||
|
||||
static private void checkTxQueue(String adapterGroupName){
|
||||
if (txQueues.get(adapterGroupName) == null){
|
||||
txQueues.put(adapterGroupName,new CircularQueue(20));
|
||||
}
|
||||
}
|
||||
static private void checkErrorQueue(String adapterGroupName){
|
||||
if (errorQueues.get(adapterGroupName) == null){
|
||||
errorQueues.put(adapterGroupName,new CircularQueue(20));
|
||||
}
|
||||
}
|
||||
static private void checkErrorCount(String adapterGroupName){
|
||||
if (errorCounts.get(adapterGroupName) == null){
|
||||
errorCounts.put(adapterGroupName,new Long(0));
|
||||
}
|
||||
}
|
||||
static private void checkTxCount(String adapterGroupName){
|
||||
if (txCounts.get(adapterGroupName) == null){
|
||||
txCounts.put(adapterGroupName,new Long(0));
|
||||
}
|
||||
}
|
||||
static public long getTransactionCount(String adapterGroupName) {
|
||||
checkTxCount(adapterGroupName);
|
||||
if (txCounts.get(adapterGroupName) == null)
|
||||
return 0;
|
||||
return txCounts.get(adapterGroupName);
|
||||
}
|
||||
|
||||
static public String[] getTransactions(String adapterGroupName) {
|
||||
checkTxQueue(adapterGroupName);
|
||||
return txQueues.get(adapterGroupName).getRecords();
|
||||
}
|
||||
|
||||
|
||||
// static public void txlog(String adapterGroupName, String message) {
|
||||
// if (message == null)
|
||||
// return;
|
||||
// if (message.length() > MAX_LOG_MESSAGE_SIZE)
|
||||
// message = message.substring(0, MAX_LOG_MESSAGE_SIZE);
|
||||
// checkTxQueue(adapterGroupName);
|
||||
// txQueues.get(adapterGroupName).addRecord(message);
|
||||
// }
|
||||
|
||||
static public void txlog(String adapterGroupName, String message) {
|
||||
if (message == null)
|
||||
return;
|
||||
if (message.length() > MAX_LOG_MESSAGE_SIZE)
|
||||
message = message.substring(0, MAX_LOG_MESSAGE_SIZE);
|
||||
synchronized(HttpMemoryLogger.class) {
|
||||
checkTxCount(adapterGroupName);
|
||||
checkTxQueue(adapterGroupName);
|
||||
Long l = txCounts.get(adapterGroupName);
|
||||
txCounts.put(adapterGroupName,++l);
|
||||
}
|
||||
txQueues.get(adapterGroupName).addRecord(message);
|
||||
}
|
||||
|
||||
|
||||
static public long getErrorCount(String adapterGroupName) {
|
||||
checkErrorCount(adapterGroupName);
|
||||
if (errorCounts.get(adapterGroupName) == null)
|
||||
return 0;
|
||||
return errorCounts.get(adapterGroupName);
|
||||
}
|
||||
|
||||
static public String[] getErrors(String adapterGroupName) {
|
||||
checkErrorQueue(adapterGroupName);
|
||||
return errorQueues.get(adapterGroupName).getRecords();
|
||||
}
|
||||
|
||||
static public void error(String adapterGroupName, String message) {
|
||||
if (message == null)
|
||||
return;
|
||||
if (message.length() > MAX_LOG_MESSAGE_SIZE)
|
||||
message = message.substring(0, MAX_LOG_MESSAGE_SIZE);
|
||||
|
||||
synchronized(HttpMemoryLogger.class) {
|
||||
checkErrorCount(adapterGroupName);
|
||||
checkErrorQueue(adapterGroupName);
|
||||
Long l = errorCounts.get(adapterGroupName);
|
||||
errorCounts.put(adapterGroupName,++l);
|
||||
}
|
||||
errorQueues.get(adapterGroupName).addRecord(message);
|
||||
//txQueues.get(adapterGroupName).addRecord("<font color='blue'>" + message + "</font>");
|
||||
}
|
||||
|
||||
static public void error(String adapterGroupName,String message, Throwable t) {
|
||||
if (message == null)
|
||||
return;
|
||||
if (message.length() > MAX_LOG_MESSAGE_SIZE)
|
||||
message = message.substring(0, MAX_LOG_MESSAGE_SIZE);
|
||||
|
||||
synchronized(HttpMemoryLogger.class) {
|
||||
checkErrorCount(adapterGroupName);
|
||||
checkErrorQueue(adapterGroupName);
|
||||
Long l = errorCounts.get(adapterGroupName);
|
||||
errorCounts.put(adapterGroupName,++l);
|
||||
}
|
||||
errorQueues.get(adapterGroupName).addRecord(message + " | " + ExceptionStackTrace.dump(t));
|
||||
//txQueues.get(adapterGroupName).addRecord("<font color='blue'>" + message + "\n" + ExceptionStackTrace.dump(t) + "</font>");
|
||||
}
|
||||
static public boolean isMemoryLogEnabled(String adapterGroupName) {
|
||||
return true;
|
||||
// PropGroupVO propGroupVO = PropManager.getInstance().getPropGroupVO("Transformer");
|
||||
// if (propGroupVO == null)
|
||||
// return false;
|
||||
// String value = propGroupVO.getProperty("memory.log");
|
||||
// if ("1".equals(value))
|
||||
// return true;
|
||||
// return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.eactive.eai.adapter.http;
|
||||
|
||||
public enum HttpMethodType {
|
||||
POST,
|
||||
PUT,
|
||||
DELETE,
|
||||
GET,
|
||||
UNKNOWN;
|
||||
public static HttpMethodType getValue(String value) {
|
||||
try {
|
||||
return valueOf(value.toUpperCase());
|
||||
} catch (Exception e) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
// public static void main(String[] args) throws Exception {
|
||||
//
|
||||
// if ("DELETE".equals(HttpMethodType.DELETE.toString())){
|
||||
// System.out.println("delete");
|
||||
// }else{
|
||||
//
|
||||
// System.out.println("not delete");
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.eactive.eai.adapter.http;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class HttpStatusException extends Exception {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private int status = 200;
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 1. 기능 : Default Constructor
|
||||
* 2. 처리 개요 : Default Constructor
|
||||
* 3. 주의사항
|
||||
*
|
||||
**/
|
||||
public HttpStatusException() {
|
||||
super("HttpStatusException is occured.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. 기능 : Exception message를 초기화하는 Constructor
|
||||
* 2. 처리 개요 : Exception message를 초기화하는 Constructor
|
||||
* -
|
||||
* 3. 주의사항
|
||||
*
|
||||
* @param msg Exception message
|
||||
**/
|
||||
public HttpStatusException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
public HttpStatusException(String msg, int status) {
|
||||
super(msg);
|
||||
this.status = status;
|
||||
}
|
||||
public HttpStatusException(String msg, String code, int status) {
|
||||
super(msg);
|
||||
this.setCode(code);
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
if (StringUtils.isBlank(code)) {
|
||||
return String.format("Http Status: %d", status);
|
||||
} else {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.eactive.eai.adapter.http.async;
|
||||
|
||||
import javax.servlet.AsyncEvent;
|
||||
import javax.servlet.AsyncListener;
|
||||
import javax.servlet.ServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
//@WebListener
|
||||
public class AppAsyncListener implements AsyncListener {
|
||||
|
||||
@Override
|
||||
public void onComplete(AsyncEvent asyncEvent) throws IOException {
|
||||
// System.out.println("AppAsyncListener onComplete");
|
||||
// we can do resource cleanup activity here
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(AsyncEvent asyncEvent) throws IOException {
|
||||
// System.out.println("AppAsyncListener onError");
|
||||
//we can return error response to client
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartAsync(AsyncEvent asyncEvent) throws IOException {
|
||||
// System.out.println("AppAsyncListener onStartAsync");
|
||||
//we can log the event here
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeout(AsyncEvent asyncEvent) throws IOException {
|
||||
// System.out.println("AppAsyncListener onTimeout");
|
||||
//we can send appropriate response to client
|
||||
ServletResponse response = asyncEvent.getAsyncContext().getResponse();
|
||||
PrintWriter out = response.getWriter();
|
||||
out.write("TimeOut Error in Processing");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.eactive.eai.adapter.http.async;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
//@WebListener
|
||||
public class AppContextListener implements ServletContextListener {
|
||||
|
||||
public void contextInitialized(ServletContextEvent servletContextEvent) {
|
||||
// Default Timeout Setting 60sec
|
||||
ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 200, 60000L,
|
||||
TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(100));
|
||||
servletContextEvent.getServletContext().setAttribute("executor",
|
||||
executor);
|
||||
// System.out.println("AppContextListener contextInitialized::set ThreadPoolExecutor");
|
||||
}
|
||||
|
||||
public void contextDestroyed(ServletContextEvent servletContextEvent) {
|
||||
ThreadPoolExecutor executor = (ThreadPoolExecutor) servletContextEvent
|
||||
.getServletContext().getAttribute("executor");
|
||||
executor.shutdown();
|
||||
// System.out.println("AppContextListener contextDestroyed::shutdown ThreadPoolExecutor");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.eactive.eai.adapter.http.async;
|
||||
|
||||
import com.eactive.eai.adapter.RequestDispatcher;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.ServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Properties;
|
||||
|
||||
public class AsyncRequestProcessor implements Runnable {
|
||||
private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
private AsyncContext asyncContext;
|
||||
|
||||
private String adapterGroupName;
|
||||
private String adapterName;
|
||||
private String encode = "euc-kr";
|
||||
|
||||
public AsyncRequestProcessor() {
|
||||
}
|
||||
|
||||
public AsyncRequestProcessor(AsyncContext asyncCtx) {
|
||||
this.asyncContext = asyncCtx;
|
||||
|
||||
// TODO : define adapter name rule
|
||||
this.adapterGroupName = "_EAI_IN_HTT_SyS";
|
||||
this.adapterName = "_EAI_IN_HTT_SyS{ALL}";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// System.out.println("Async Supported? "
|
||||
// + asyncContext.getRequest().isAsyncSupported());
|
||||
|
||||
ServletRequest request = asyncContext.getRequest();
|
||||
|
||||
ServletInputStream sis;
|
||||
byte[] requestBytes = null;
|
||||
byte[] result = null;
|
||||
try {
|
||||
sis = request.getInputStream();
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(1024);
|
||||
int i=0;
|
||||
byte[] cbuf = new byte[1024];
|
||||
while( ( i = sis.read(cbuf, 0, 1024) ) != -1 ) {
|
||||
if (i==1024){
|
||||
bb.put(cbuf);
|
||||
}else{
|
||||
byte[] tail = new byte[i];
|
||||
System.arraycopy(cbuf, 0, tail, 0, i);
|
||||
bb.put(tail);
|
||||
}
|
||||
}
|
||||
requestBytes = new byte[bb.position()];
|
||||
bb.position(0);
|
||||
bb.get(requestBytes);
|
||||
|
||||
Properties prop = new Properties();
|
||||
// call Inbound Proxy
|
||||
result = service(requestBytes, prop);
|
||||
PrintWriter out = asyncContext.getResponse().getWriter();
|
||||
String encodedResponse = new String(result, encode);
|
||||
out.write(encodedResponse);
|
||||
} catch (IOException e1) {
|
||||
logger.error("AsyncRequestProcessor IOException", e1);
|
||||
} catch (Exception e) {
|
||||
logger.error("AsyncRequestProcessor Exception", e);
|
||||
}
|
||||
finally {
|
||||
if(asyncContext != null) {
|
||||
asyncContext.complete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected byte[] service(byte[] message, Properties prop) throws Exception {
|
||||
Object obj = RequestDispatcher.getRequestDispatcher(this.adapterGroupName).handle(adapterName, message, prop);
|
||||
if(obj == null) return null;
|
||||
if(obj instanceof byte[]) {
|
||||
return (byte[])obj;
|
||||
} else if(obj instanceof String) {
|
||||
return ((String)obj).getBytes();
|
||||
} else {
|
||||
throw new Exception("RECEAIAHA001");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.eactive.eai.adapter.http.async;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
//@WebServlet(urlPatterns = "/AsyncServlet", asyncSupported = true)
|
||||
public class AsyncServlet extends HttpServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request,
|
||||
HttpServletResponse response) throws ServletException, IOException {
|
||||
handle(request, response);
|
||||
}
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request,
|
||||
HttpServletResponse response) throws ServletException, IOException {
|
||||
handle(request, response);
|
||||
}
|
||||
|
||||
private void handle(HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
// long startTime = System.currentTimeMillis();
|
||||
// System.out.println("AsyncServlet Start::Name="
|
||||
// + Thread.currentThread().getName() + "::ID="
|
||||
// + Thread.currentThread().getId());
|
||||
|
||||
request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
|
||||
|
||||
AsyncContext asyncCtx = request.startAsync();
|
||||
AppAsyncListener asyncListener = new AppAsyncListener();
|
||||
asyncCtx.addListener(asyncListener);
|
||||
asyncCtx.setTimeout(30000); // Async Timeout 30 secs
|
||||
|
||||
// ThreadPoolExecutor executor = (ThreadPoolExecutor) request
|
||||
// .getServletContext().getAttribute("executor");
|
||||
// executor.execute(new AsyncRequestProcessor(asyncCtx));
|
||||
|
||||
asyncCtx.start(new AsyncRequestProcessor(asyncCtx));
|
||||
|
||||
// long endTime = System.currentTimeMillis();
|
||||
// System.out.println("AsyncServlet End::Name="
|
||||
// + Thread.currentThread().getName() + "::ID="
|
||||
// + Thread.currentThread().getId() + "::Time Taken="
|
||||
// + (endTime - startTime) + " ms.");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+538
@@ -0,0 +1,538 @@
|
||||
|
||||
package com.eactive.eai.adapter.http.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.UnrecoverableKeyException;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.apache.hc.client5.http.classic.HttpClient;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
|
||||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
|
||||
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
|
||||
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.hc.client5.http.ssl.TrustAllStrategy;
|
||||
import org.apache.hc.core5.http.HttpRequest;
|
||||
import org.apache.hc.core5.http.HttpRequestInterceptor;
|
||||
import org.apache.hc.core5.http.HttpResponseInterceptor;
|
||||
import org.apache.hc.core5.ssl.SSLContextBuilder;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterGroupVO;
|
||||
import com.eactive.eai.adapter.AdapterManager;
|
||||
import com.eactive.eai.adapter.Keys;
|
||||
import com.eactive.eai.adapter.http.secure.HttpClient5SSLContextFactory;
|
||||
import com.eactive.eai.common.TransactionContextKeys;
|
||||
import com.eactive.eai.common.util.HttpAdapterExtraLogUtil;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.httpouttlsinfo.HttpOutTlsInfoVO;
|
||||
import com.eactive.eai.util.TestModeChecker;
|
||||
|
||||
public abstract class HttpClient5AdapterServiceSupport implements HttpClientAdapterService, HttpClientAdapterServiceKey {
|
||||
protected static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
protected HttpClient client;
|
||||
PoolingHttpClientConnectionManager connectionManager;
|
||||
|
||||
private static boolean testMode = TestModeChecker.isTestMode();
|
||||
|
||||
public synchronized void close() {
|
||||
if(connectionManager != null) {
|
||||
connectionManager.close();
|
||||
}
|
||||
}
|
||||
|
||||
protected HttpClientAdapterVO setting(Properties prop, Properties tempProp) {
|
||||
String url = null;
|
||||
String parameterName = null;
|
||||
String method = null;
|
||||
String contentType = null;
|
||||
String encode = null;
|
||||
String testCallYn = null;
|
||||
String adapterGroupName = null;
|
||||
String adapterName = null;
|
||||
String responseType = null;
|
||||
String urlEncodeYn = null;
|
||||
String adapterHeader = null;
|
||||
|
||||
boolean useMtls = false;
|
||||
String keyStorePath = null;
|
||||
String keyStorePassword = null;
|
||||
String trustStorePath = null;
|
||||
String trustStorePassword = null;
|
||||
|
||||
int connectionTimeout = 0;
|
||||
int timeout = -1;
|
||||
int traceLevel = 0;
|
||||
long slowTranTime = 2000L;
|
||||
|
||||
url = prop.getProperty(URL);
|
||||
|
||||
String url2grp = tempProp.getProperty(URL);
|
||||
if (!StringUtils.isEmpty(url2grp)) {
|
||||
url = url2grp;
|
||||
}
|
||||
method = prop.getProperty(METHOD);
|
||||
parameterName = prop.getProperty(PARAMETER_NAME, PARAMETER_MESSAGE);
|
||||
contentType = prop.getProperty(CONTENT_TYPE, "");
|
||||
responseType = prop.getProperty(RESPONSE_TYPE, "SYNC");
|
||||
urlEncodeYn = prop.getProperty(URL_ENCODE_YN, "N");
|
||||
String connectionTimeoutTemp = prop.getProperty(CONNECTION_TIMEOUT, "0");
|
||||
|
||||
String httpVersion = prop.getProperty(HTTP_VERSION);
|
||||
adapterHeader = prop.getProperty(ADAPTER_HEADER, "");
|
||||
|
||||
String traceLevelTemp = prop.getProperty(TRACE_LEVEL, "0");
|
||||
String timeoutTemp = prop.getProperty(HTTP_TIME_OUT, "0");
|
||||
|
||||
useMtls = StringUtils.equalsIgnoreCase(prop.getProperty(HttpClientAdapterServiceKey.USE_MTLS),"Y");
|
||||
keyStorePath = prop.getProperty(HttpClientAdapterServiceKey.KEYSTORE_PATH);
|
||||
keyStorePassword = prop.getProperty(HttpClientAdapterServiceKey.KEYSTORE_PWD);
|
||||
trustStorePath = prop.getProperty(HttpClientAdapterServiceKey.TRUSTSTORE_PATH);
|
||||
trustStorePassword = prop.getProperty(HttpClientAdapterServiceKey.TRUSTSTORE_PWD);
|
||||
|
||||
encode = tempProp.getProperty(Keys.CHARSET_KEY_OUT, Charset.defaultCharset().name());
|
||||
testCallYn = tempProp.getProperty(TESTCALL_YN, "N");
|
||||
adapterGroupName = tempProp.getProperty(ADAPTER_GROUP_NAME);
|
||||
adapterName = tempProp.getProperty(ADAPTER_NAME);
|
||||
// 인터페이스 정보에 설정된 타임아웃
|
||||
String interfaceTimeout = tempProp.getProperty("INTERFACE_TIME_OUT");
|
||||
if (StringUtils.isNotBlank(interfaceTimeout) && NumberUtils.toInt(interfaceTimeout) > 0) {
|
||||
timeoutTemp = interfaceTimeout;
|
||||
}
|
||||
|
||||
url = StringUtils.trim(url);
|
||||
parameterName = StringUtils.trim(parameterName);
|
||||
method = StringUtils.trim(method);
|
||||
encode = StringUtils.trim(encode);
|
||||
contentType = StringUtils.trim(contentType);
|
||||
|
||||
try {
|
||||
connectionTimeout = Integer.parseInt(connectionTimeoutTemp);
|
||||
} catch (Exception e) {
|
||||
logger.debug("HttpClientAdapterSupport] connectionTimeoutTemp not int");
|
||||
}
|
||||
|
||||
try {
|
||||
timeout = Integer.parseInt(timeoutTemp);
|
||||
} catch (NumberFormatException e) {
|
||||
logger.warn("HttpClientAdapterSupport] timeoutTemp not int", e);
|
||||
}
|
||||
|
||||
try {
|
||||
traceLevel = Integer.parseInt(traceLevelTemp);
|
||||
} catch (Exception e) {
|
||||
logger.warn("HttpClientAdapterSupport] traceLevelTemp not int", e);
|
||||
}
|
||||
|
||||
// client 중 BODY,POST,GET,NexCore 방식만 적용
|
||||
HashMap<String, String> adapterHeaders = new HashMap<>();
|
||||
try {
|
||||
Matcher matcher = Pattern
|
||||
.compile("\\{[ ]*\"[ ]*([a-zA-Z0-9_-]+)[ ]*\"[ ]*,[ ]*\"[ ]*([a-zA-Z0-9/;,\\*=_ -]+)[ ]*\"[ ]*\\}[ ]*")
|
||||
.matcher(adapterHeader.trim());
|
||||
while (matcher.find()) {
|
||||
String key = matcher.group(1).trim();
|
||||
String value = matcher.group(2).trim();
|
||||
adapterHeaders.put(key, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if(logger.isDebug()) logger.debug("HttpClientAdapterSupport] adapter header parsing error " + adapterHeader);
|
||||
}
|
||||
// GW 전달 로직 추가
|
||||
// tempProp로 넘겨서 하는 이유는 outboundProp가 clone 안하는 로직이라 outboundProp로 넘길시 문제의 소지가있음
|
||||
String transactionId = tempProp.getProperty(HttpClientAdapterServiceKey.TRANSACTION_ID);
|
||||
String instanceId = tempProp.getProperty(HttpClientAdapterServiceKey.INSTANCE_ID);
|
||||
if (StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(instanceId)) {
|
||||
adapterHeaders.put(HttpClientAdapterServiceKey.TRANSACTION_ID, transactionId);
|
||||
adapterHeaders.put(HttpClientAdapterServiceKey.INSTANCE_ID, instanceId);
|
||||
}
|
||||
|
||||
HttpClientAdapterVO vo = new HttpClientAdapterVO();
|
||||
vo.setUrl(url);
|
||||
vo.setMethod(method);
|
||||
vo.setParameterName(parameterName);
|
||||
vo.setEncode(encode);
|
||||
vo.setContentType(contentType);
|
||||
vo.setTestCallYn(testCallYn);
|
||||
vo.setResponseType(responseType);
|
||||
vo.setUrlEncodeYn(urlEncodeYn);
|
||||
vo.setAdapterGroupName(adapterGroupName);
|
||||
vo.setAdapterName(adapterName);
|
||||
|
||||
vo.setMaxTotalConnections(getMaxTotalConnections());
|
||||
vo.setMaxHostConnections(getMaxConnectionsPerHost());
|
||||
vo.setConnectionTimeout(connectionTimeout);
|
||||
|
||||
vo.setTimeout(timeout);
|
||||
vo.setTraceLevel(traceLevel);
|
||||
vo.setSlowTranTime(slowTranTime);
|
||||
vo.setAdapterHeaders(adapterHeaders);
|
||||
if (StringUtils.equalsAny(httpVersion, HTTP_VERSION_FULL_1_1, HTTP_VERSION_1_1)) {
|
||||
vo.setHttpVersion(HTTP_VERSION_1_1);
|
||||
}
|
||||
else if (StringUtils.equals(httpVersion, HTTP_VERSION_2_0)) {
|
||||
vo.setHttpVersion(HTTP_VERSION_2_0);
|
||||
}
|
||||
else {
|
||||
vo.setHttpVersion(HTTP_VERSION_1_0);
|
||||
}
|
||||
vo.setUseMtls(useMtls);
|
||||
vo.setKeyStorePath(keyStorePath);
|
||||
vo.setKeyStorePassword(keyStorePassword);
|
||||
vo.setTrustStorePath(trustStorePath);
|
||||
vo.setTrustStorePassword(trustStorePassword);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug(vo.toString());
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
protected HttpClient5AdapterServiceSupport() {
|
||||
}
|
||||
|
||||
public synchronized void init(String adapterName,
|
||||
boolean useMtls, HttpOutTlsInfoVO mtlsInfo,
|
||||
int maxTotalConnections, int maxHostConnections) {
|
||||
|
||||
if(connectionManager != null) {
|
||||
if( (connectionManager.getMaxTotal() == maxTotalConnections)
|
||||
&& (connectionManager.getDefaultMaxPerRoute() == maxHostConnections) ) {
|
||||
if(logger.isInfo()) {
|
||||
logger.info("ConnectionManager REUSE : AdapterName=" + adapterName
|
||||
+ " MaxTotalConnection (" + connectionManager.getMaxTotal()
|
||||
+ ") MaxHostConnections (" + connectionManager.getDefaultMaxPerRoute()
|
||||
+ ")"
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else {
|
||||
if(logger.isWarn()) {
|
||||
logger.warn("ConnectionManager RELOAD : AdapterName=" + adapterName
|
||||
+ " MaxTotalConnection (" + connectionManager.getMaxTotal() + " -> " + maxTotalConnections
|
||||
+ ") MaxHostConnections (" + connectionManager.getDefaultMaxPerRoute() + " -> " + maxHostConnections
|
||||
+ ")"
|
||||
);
|
||||
}
|
||||
connectionManager.close();
|
||||
connectionManager = null;
|
||||
this.client = null;
|
||||
}
|
||||
}
|
||||
if(logger.isWarn()) {
|
||||
logger.warn("ConnectionManager INIT : AdapterName=" + adapterName
|
||||
+ " MaxTotalConnection (" + maxTotalConnections
|
||||
+ ") MaxHostConnections (" + maxHostConnections
|
||||
+ ")"
|
||||
);
|
||||
}
|
||||
|
||||
PoolingHttpClientConnectionManagerBuilder cmBuilder = PoolingHttpClientConnectionManagerBuilder.create();
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if(logger.isWarn()) {
|
||||
logger.warn("mTLS Config : AdapterName=" + adapterName
|
||||
+ " useMtls (" + useMtls + ") mtlsInfo - " + mtlsInfo);
|
||||
}
|
||||
|
||||
SSLContext sslContext = null;
|
||||
if (useMtls && mtlsInfo != null) {
|
||||
String storeType = mtlsInfo.getStoreType();
|
||||
String keyStoreInfo = mtlsInfo.getKeystoreInfo();
|
||||
String keyStorePassword = mtlsInfo.getKeystorePassword();
|
||||
String trustStoreInfo = mtlsInfo.getTruststoreInfo();
|
||||
String trustStorePassword = mtlsInfo.getTruststorePassword();
|
||||
|
||||
String[] tlsVersions = null;
|
||||
// {
|
||||
// "TLSv1.0"
|
||||
// , "TLSv1.1"
|
||||
// , "TLSv1.2"
|
||||
// , "TLSv1.3"
|
||||
// };
|
||||
String[] cipherSuites = null;
|
||||
// {
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
|
||||
// "TLS_RSA_WITH_AES_256_CBC_SHA256",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384",
|
||||
// "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384",
|
||||
// "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
|
||||
// "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256",
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_RSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_DHE_RSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_DHE_DSS_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_RSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_RSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_DHE_RSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_DHE_DSS_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_RSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_RSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_EMPTY_RENEGOTIATION_INFO_SCSV"
|
||||
// };
|
||||
|
||||
if( StringUtils.isAnyEmpty(keyStoreInfo, keyStorePassword) ) {
|
||||
throw new Exception("mTLS keyStore config error");
|
||||
}
|
||||
|
||||
boolean skipTrust = false;
|
||||
// kbank에서는 trustStore를 사용하지 않음
|
||||
if( StringUtils.isAnyEmpty(trustStoreInfo, trustStorePassword) ) {
|
||||
// throw new Exception("mTLS trustStore config error");
|
||||
if(logger.isWarn()) logger.warn("Skip trustStore validation adapterName : " + adapterName);
|
||||
skipTrust = true;
|
||||
}
|
||||
|
||||
sslContext = HttpClient5SSLContextFactory.createMTLSContextFromContent(
|
||||
storeType,
|
||||
keyStoreInfo, keyStorePassword,
|
||||
trustStoreInfo, trustStorePassword, skipTrust
|
||||
,tlsVersions, cipherSuites);
|
||||
|
||||
// SSLConnectionSocketFactory 생성
|
||||
// Exception in thread "main" javax.net.ssl.SSLPeerUnverifiedException: Certificate for <localhost> doesn't match common name of the certificate subject: elink
|
||||
// at org.apache.hc.client5.http.ssl.DefaultHostnameVerifier.matchCN(DefaultHostnameVerifier.java:180)
|
||||
// at org.apache.hc.client5.http.ssl.DefaultHostnameVerifier.verify(DefaultHostnameVerifier.java:118)
|
||||
// at org.apache.hc.client5.http.ssl.TlsSessionValidator.verifySession(TlsSessionValidator.java:112)
|
||||
// => NoopHostnameVerifier.INSTANCE
|
||||
|
||||
SSLConnectionSocketFactory sslSocketFactory = null;
|
||||
if(testMode) {
|
||||
// Hostname verifier 비활성화 (테스트용)
|
||||
sslSocketFactory = new SSLConnectionSocketFactory(
|
||||
sslContext
|
||||
, NoopHostnameVerifier.INSTANCE
|
||||
);
|
||||
}
|
||||
else {
|
||||
sslSocketFactory = new SSLConnectionSocketFactory(
|
||||
sslContext
|
||||
);
|
||||
}
|
||||
cmBuilder.setSSLSocketFactory(sslSocketFactory);
|
||||
}
|
||||
else {
|
||||
sslContext = SSLContextBuilder.create().loadTrustMaterial(new TrustAllStrategy()).build();
|
||||
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
|
||||
cmBuilder.setSSLSocketFactory(csf);
|
||||
}
|
||||
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException
|
||||
| CertificateException | IOException | UnrecoverableKeyException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
this.connectionManager = cmBuilder.build();
|
||||
|
||||
connectionManager.setMaxTotal(maxTotalConnections);
|
||||
connectionManager.setDefaultMaxPerRoute(maxHostConnections);
|
||||
|
||||
|
||||
HttpRequestInterceptor requestLogInterceptor = (request, entity, context) -> {
|
||||
try {
|
||||
String uuid = (String) context.getAttribute(TransactionContextKeys.TRANSACTION_UUID);
|
||||
String contextAdapterGroupName = (String) context.getAttribute(HttpClientAdapterServiceKey.ADAPTER_GROUP_NAME);
|
||||
String contextAdapterName = (String) context.getAttribute(HttpClientAdapterServiceKey.ADAPTER_NAME);
|
||||
String url = request.getUri().toString();
|
||||
|
||||
Integer logProcessNo = (Integer) context.getAttribute(HttpClientAdapterServiceKey.LOG_PROCESS_NO);
|
||||
if (logProcessNo == null || logProcessNo <= 0) {
|
||||
logProcessNo = 200;
|
||||
}
|
||||
|
||||
HttpAdapterExtraLogUtil.insertHttpAdapterExtraLog(uuid, logProcessNo,
|
||||
contextAdapterGroupName, contextAdapterName, request.getHeaders(), url, request.getMethod());
|
||||
} catch (URISyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
|
||||
HttpResponseInterceptor responseLogInterceptor = (response, entity, context) -> {
|
||||
try{
|
||||
HttpRequest request = (HttpRequest)context.getAttribute("http.request");
|
||||
String url = request.getUri().toString();
|
||||
String uuid = (String) context.getAttribute(TransactionContextKeys.TRANSACTION_UUID);
|
||||
String contextAdapterGroupName = (String) context.getAttribute(HttpClientAdapterServiceKey.ADAPTER_GROUP_NAME);
|
||||
String contextAdapterName = (String) context.getAttribute(HttpClientAdapterServiceKey.ADAPTER_NAME);
|
||||
Integer logProcessNo = (Integer) context.getAttribute(HttpClientAdapterServiceKey.LOG_PROCESS_NO);
|
||||
if (logProcessNo == null || logProcessNo <= 0) {
|
||||
logProcessNo = 300;
|
||||
}else if(logProcessNo == 400){
|
||||
return;
|
||||
}else{
|
||||
logProcessNo += 100;
|
||||
}
|
||||
|
||||
HttpAdapterExtraLogUtil.insertHttpAdapterExtraLog(uuid, logProcessNo,
|
||||
contextAdapterGroupName, contextAdapterName, response.getHeaders(), url, request.getMethod(), response.getCode());
|
||||
} catch (URISyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
|
||||
this.client = HttpClients.custom()
|
||||
.addRequestInterceptorLast(requestLogInterceptor)
|
||||
.addResponseInterceptorFirst(responseLogInterceptor)
|
||||
.setConnectionManager(connectionManager)
|
||||
.build();
|
||||
}
|
||||
|
||||
public int getConnectionsInPool() {
|
||||
if (this.client == null || this.connectionManager == null)
|
||||
return 0;
|
||||
return this.connectionManager.getTotalStats().getAvailable() + this.connectionManager.getTotalStats().getLeased();
|
||||
}
|
||||
|
||||
public int getMaxTotalConnections() {
|
||||
if (this.client == null || this.connectionManager == null)
|
||||
return 0;
|
||||
return this.connectionManager.getMaxTotal();
|
||||
}
|
||||
|
||||
public int getMaxConnectionsPerHost() {
|
||||
if (this.client == null || this.connectionManager == null)
|
||||
return 0;
|
||||
return this.connectionManager.getDefaultMaxPerRoute();
|
||||
}
|
||||
|
||||
public String getResponseCharSet(String httpResponseCharset, String adapterGroupName) {
|
||||
if (StringUtils.isNotBlank(httpResponseCharset)) {
|
||||
return httpResponseCharset;
|
||||
} else {
|
||||
AdapterManager m = AdapterManager.getInstance();
|
||||
AdapterGroupVO agvo = m.getAdapterGroupVO(adapterGroupName);
|
||||
return agvo.getMessageEncode();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* encoding에 대한 정의 필요 고려할 문제
|
||||
* 1. 어댑터그룹이 UJN 타입일경우 return 은 UTF-8 로 getBytes 된
|
||||
* 데이터여야됨.
|
||||
* 2. http response에 받은 encoding 타입은 고려대상임. 결론 : response Encoding 과
|
||||
* 어댑터의 MESSAGE_TYPE 다르면 변환, 나머지면 return 어댑터의 ENCODE은 요청전문 encoding에서만 사용
|
||||
*
|
||||
* 예) 어댑터의 ENCODE, 어댑터의 MESSAGE TYPE, RESPNSE ENCODING TYPE, 처리내용
|
||||
* 예) ,ECU-KR ,JSN , EUC-KR , 응답받은 메시지를 return 예) ,ECU-KR , JSN , UTF-8 , 응답받은 메시지를 new
|
||||
* String(xxx,"UTF-8") => getBytes("EUC-KR") 예) ,ECU-KR , UJN , EUC-KR , 응답받은
|
||||
* 메시지를 new String(xxx,"EUC-KR") => getBytes("UTF-8") 예) ,ECU-KR , UJN , UTF-8 ,
|
||||
* 응답받은 메시지를 return 예) ,UTF-8 , JSN , EUC-KR , 응답받은 메시지를 return
|
||||
* 예) ,UTF-8 , JSN , UTF-8 , 응답받은 메시지를 new String(xxx,"UTF-8") => getBytes(EUC-KR") 예) ,UTF-8 ,
|
||||
* UJN , EUC-KR , 응답받은 메시지를 new String(xxx,"EUC-KR") => getBytes("UTF-8") 예)
|
||||
* ,UTF-8 , UJN , UTF-8 , 응답받은 메시지를 return
|
||||
*
|
||||
*
|
||||
*/
|
||||
public byte[] getEncodingConvert(String httpResponseCharset, String adapterGroupName, byte[] message) {
|
||||
byte[] responseMessage = null;
|
||||
if (StringUtils.isNotBlank(httpResponseCharset)) {
|
||||
AdapterManager m = AdapterManager.getInstance();
|
||||
AdapterGroupVO agvo = m.getAdapterGroupVO(adapterGroupName);
|
||||
String adapterTypeEncoding = StringUtils.defaultIfBlank(agvo.getMessageEncode(),
|
||||
Charset.defaultCharset().name());
|
||||
Charset responseCharset = null;
|
||||
Charset adapterCharset = null;
|
||||
try {
|
||||
responseCharset = Charset.forName(httpResponseCharset);
|
||||
adapterCharset = Charset.forName(adapterTypeEncoding);
|
||||
if (responseCharset.compareTo(adapterCharset) == 0) {
|
||||
responseMessage = message;
|
||||
} else {// http 응답 charset 과 어댑터의 charset 이 다를경우 charset 변환 필요
|
||||
String encodingString = new String(message, httpResponseCharset);
|
||||
responseMessage = encodingString.getBytes(adapterTypeEncoding);
|
||||
}
|
||||
} catch (UnsupportedCharsetException | UnsupportedEncodingException e) {
|
||||
responseMessage = message;
|
||||
}
|
||||
} else {
|
||||
responseMessage = message;
|
||||
}
|
||||
return responseMessage;
|
||||
}
|
||||
|
||||
public void configureHttpClient(RequestConfig.Builder requestConfigBuilder, HttpClientAdapterVO vo, HttpUriRequestBase method) {
|
||||
|
||||
if (vo.getConnectionTimeout() != 0) {
|
||||
requestConfigBuilder.setConnectTimeout(vo.getConnectionTimeout(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
if (vo.getTimeout() > -1) {
|
||||
requestConfigBuilder.setResponseTimeout(vo.getTimeout(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/*
|
||||
if (HTTP_VERSION_1_1.equals(vo.getHttpVersion())) {
|
||||
method.addHeader("Connection", "close");
|
||||
}
|
||||
*/
|
||||
|
||||
org.apache.hc.core5.http.HttpVersion httpVersion ;
|
||||
if (HTTP_VERSION_1_0.equals(vo.getHttpVersion())) {
|
||||
httpVersion = org.apache.hc.core5.http.HttpVersion.HTTP_1_0;
|
||||
}
|
||||
else if (HTTP_VERSION_2_0.equals(vo.getHttpVersion())) {
|
||||
httpVersion = org.apache.hc.core5.http.HttpVersion.HTTP_2_0;
|
||||
}
|
||||
else {
|
||||
httpVersion = org.apache.hc.core5.http.HttpVersion.HTTP_1_1;
|
||||
method.addHeader("Connection", "close");
|
||||
}
|
||||
method.setVersion(httpVersion);
|
||||
|
||||
RequestConfig requestConfig = requestConfigBuilder
|
||||
.setConnectTimeout(vo.getConnectionTimeout(), TimeUnit.MILLISECONDS) // 연결 시간 초과를 5초로 설정
|
||||
.setResponseTimeout(vo.getTimeout(), TimeUnit.MILLISECONDS) // 응답 시간 초과를 5초로 설정
|
||||
.build();
|
||||
|
||||
method.setConfig(requestConfig);
|
||||
}
|
||||
|
||||
public abstract Object execute(Properties prop, Object message, Properties tempProp) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.eactive.eai.adapter.http.client;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterPropManager;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpLoggerManager;
|
||||
import com.eactive.eai.adapter.listener.AdapterException;
|
||||
import com.eactive.eai.adapter.listener.AdapterListenerSupport;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class HttpClientAdapterListener extends AdapterListenerSupport {
|
||||
final static Logger logger2 = HttpLoggerManager.getInstance().getLogger();
|
||||
final static Logger errLogger = HttpLoggerManager.getInstance().getErrLogger();
|
||||
|
||||
public void start() throws AdapterException {
|
||||
|
||||
try {
|
||||
if (info == null) {
|
||||
throw new Exception("HttpClientAdapterListener | AdapterVO가 NULL입니다.");
|
||||
}
|
||||
AdapterPropManager manager = AdapterPropManager.getInstance();
|
||||
Properties outboundProp = manager.getProperties(info.getPropGroupName());
|
||||
String adapterGroupName = info.getAdapterGroupName();
|
||||
String adapterName = info.getName();
|
||||
String httpType = outboundProp.getProperty(HttpClientAdapterServiceKey.HTTP_TYPE);
|
||||
HttpClientAdapterServiceFactory.createFactory(httpType, adapterGroupName, adapterName);
|
||||
if(logger2.isInfo()) {
|
||||
logger2.info("[HttpClientAdapterListener] AdapterName [" + info.getAdapterGroupName() + "." + info.getName()
|
||||
+ "] httpType [" + httpType + "] start");
|
||||
}
|
||||
info.setStatus(true);
|
||||
} catch (Exception e) {
|
||||
logger.error(info.getName() + " | HttpClientAdapterListener | cannot start : " + e.getMessage(), e);
|
||||
throw new AdapterException(info.getName() + " | HttpClientAdapterListener | cannot start - " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() throws AdapterException {
|
||||
try {
|
||||
if (info == null) {
|
||||
throw new Exception("HttpClientAdapterListener | AdapterVO가 NULL입니다.");
|
||||
}
|
||||
String adapterName = info.getName();
|
||||
HttpClientAdapterServiceFactory.removeFactory(adapterName);
|
||||
if(logger2.isInfo()) {
|
||||
logger2.info("[HttpClientAdapterListener] AdapterName [" + info.getAdapterGroupName() + "." + info.getName()
|
||||
+ "] adapter info [" + info + "] stop");
|
||||
}
|
||||
info.setStatus(false);
|
||||
} catch (Exception e) {
|
||||
logger.error(info.getName() + " | HttpClientAdapterListener | cannot stop : " + e.getMessage(), e);
|
||||
throw new AdapterException(info.getName() + " | HttpClientAdapterListener | cannot stop - " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
|
||||
package com.eactive.eai.adapter.http.client;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import com.eactive.eai.adapter.ElinkDefaultAdapter;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class HttpClientAdapterMain extends ElinkDefaultAdapter {
|
||||
private static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
public Object execute(Properties prop, Object message, Properties tempProp) throws Exception {
|
||||
String httpType = prop.getProperty(HttpClientAdapterServiceKey.HTTP_TYPE); // Http Type에 대한 class name
|
||||
String adapterGroupName = tempProp.getProperty(com.eactive.eai.adapter.Keys.OUT_ADAPTER_GROUP_NAME);
|
||||
String adapterName = tempProp.getProperty(com.eactive.eai.adapter.Keys.OUT_ADAPTER_NAME);
|
||||
HttpClientAdapterService service = HttpClientAdapterServiceFactory.createFactory(httpType, adapterGroupName, adapterName);
|
||||
if (service == null) {
|
||||
throw new Exception("Can not create HttpClientAdapterService httpType=" + httpType +" adapterName="+adapterName);
|
||||
}
|
||||
return service.execute(prop, message, tempProp);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
package com.eactive.eai.adapter.http.client;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import com.eactive.eai.httpouttlsinfo.HttpOutTlsInfoVO;
|
||||
|
||||
public interface HttpClientAdapterService {
|
||||
public Object execute(Properties prop, Object message, Properties tempProp) throws Exception;
|
||||
public void close();
|
||||
public void init(String adapterName,
|
||||
boolean useMtls, HttpOutTlsInfoVO mtlsInfo,
|
||||
int maxTotalConnections, int maxHostConnections);
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
package com.eactive.eai.adapter.http.client;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.apache.commons.lang.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.common.util.Logger;
|
||||
import com.eactive.eai.httpouttlsinfo.HttpOutTlsInfoManager;
|
||||
import com.eactive.eai.httpouttlsinfo.HttpOutTlsInfoVO;
|
||||
|
||||
public class HttpClientAdapterServiceFactory {
|
||||
private static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
private static ConcurrentHashMap<String, HttpClientAdapterService> adapters = new ConcurrentHashMap<>();
|
||||
private static ConcurrentHashMap<String, String> adapterClass = new ConcurrentHashMap<>();
|
||||
|
||||
private HttpClientAdapterServiceFactory() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static HttpClientAdapterService createFactory(String className, String adapterGroupName, String adapterName) throws Exception {
|
||||
HttpClientAdapterService service = null;
|
||||
if (adapters.containsKey(adapterName)) {
|
||||
service = adapters.get(adapterName);
|
||||
if (className.equals(adapterClass.get(adapterName))) {
|
||||
return service;
|
||||
}
|
||||
else {
|
||||
service.close();
|
||||
service = null;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Class cl = Class.forName(className);
|
||||
service = (HttpClientAdapterService) cl.newInstance();
|
||||
|
||||
boolean useMtls = false;
|
||||
|
||||
int maxTotalConnections = HttpClientAdapterServiceKey.DEFAULT_MAX_TOTAL_CONNECTIONS;
|
||||
int maxHostConnections = HttpClientAdapterServiceKey.DEFAULT_MAX_CONNECTION_PER_HOST;
|
||||
AdapterVO adapterVo = AdapterManager.getInstance().getAdapterVO(adapterGroupName, adapterName);
|
||||
AdapterPropManager manager = AdapterPropManager.getInstance();
|
||||
Properties outboundProp = manager.getProperties(adapterVo.getPropGroupName());
|
||||
|
||||
try {
|
||||
maxTotalConnections = Integer.parseInt(outboundProp.getProperty(HttpClientAdapterServiceKey.MAX_TOTAL_CONNECTIONS));
|
||||
}
|
||||
catch(NumberFormatException ex) {
|
||||
maxTotalConnections = HttpClientAdapterServiceKey.DEFAULT_MAX_TOTAL_CONNECTIONS;
|
||||
if(logger.isWarn()) {
|
||||
logger.warn("HttpClientAdapterService. : {}, adatperName : {} UseDefault {}={}, {}"
|
||||
, className, adapterName,HttpClientAdapterServiceKey.MAX_TOTAL_CONNECTIONS, maxTotalConnections, ex.toString());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
maxHostConnections = Integer.parseInt(outboundProp.getProperty(HttpClientAdapterServiceKey.MAX_CONNECTION_PER_HOST));
|
||||
}
|
||||
catch(NumberFormatException ex) {
|
||||
maxHostConnections = HttpClientAdapterServiceKey.DEFAULT_MAX_CONNECTION_PER_HOST;
|
||||
if(logger.isWarn()) {
|
||||
logger.warn("HttpClientAdapterService. : {}, adatperName : {} UseDefault {}={}, {}"
|
||||
, className, adapterName,HttpClientAdapterServiceKey.MAX_CONNECTION_PER_HOST, maxHostConnections, ex.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// clientID -> mTLS 인증정보 획득
|
||||
useMtls = StringUtils.equalsIgnoreCase(outboundProp.getProperty(HttpClientAdapterServiceKey.USE_MTLS),"Y");
|
||||
|
||||
AdapterGroupVO adapterGroup = AdapterManager.getInstance().getAdapterGroup(adapterGroupName);
|
||||
// outboundProp.getProperty(HttpClientAdapterServiceKey.CLIENT_ID);
|
||||
String clientId = adapterGroup.getClientId();
|
||||
|
||||
if(logger.isInfo()) {
|
||||
logger.info("MTLS adapterName. : {}, useMtls. : {}, clientId : {}", adapterName, useMtls, clientId);
|
||||
}
|
||||
HttpOutTlsInfoVO mtlsInfo = null;
|
||||
if(useMtls) {
|
||||
HttpOutTlsInfoManager tlsManager = HttpOutTlsInfoManager.getInstance();
|
||||
if(StringUtils.isNotEmpty(clientId)) {
|
||||
mtlsInfo = tlsManager.getHttpOutTlsInfo(clientId);
|
||||
}
|
||||
}
|
||||
// keyStorePath = outboundProp.getProperty(HttpClientAdapterServiceKey.KEYSTORE_PATH);
|
||||
// keyStorePassword = outboundProp.getProperty(HttpClientAdapterServiceKey.KEYSTORE_PWD);
|
||||
// trustStorePath = outboundProp.getProperty(HttpClientAdapterServiceKey.TRUSTSTORE_PATH);
|
||||
// trustStorePassword = outboundProp.getProperty(HttpClientAdapterServiceKey.TRUSTSTORE_PWD);
|
||||
|
||||
service.init(adapterName,
|
||||
useMtls, mtlsInfo,
|
||||
maxTotalConnections, maxHostConnections);
|
||||
|
||||
if (service != null) {
|
||||
adapters.put(adapterName, service);
|
||||
adapterClass.put(adapterName, className);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Cannot create a HttpClientAdapterService. : {}, adatperName : {}", className, adapterName, e);
|
||||
throw e;
|
||||
}
|
||||
return service;
|
||||
}
|
||||
|
||||
public static void removeFactory(String adapterName) {
|
||||
String className = null;
|
||||
if (adapters.containsKey(adapterName)) {
|
||||
adapters.remove(adapterName);
|
||||
className = adapterClass.remove(adapterName);
|
||||
if(logger.isWarn()) {
|
||||
logger.warn("Remove HttpClientAdapterService. : {}, adatperName : {}", className, adapterName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
|
||||
package com.eactive.eai.adapter.http.client;
|
||||
|
||||
public interface HttpClientAdapterServiceKey {
|
||||
|
||||
public static final String ADAPTER_GROUP_NAME = "ADAPTER_GROUP_NAME";
|
||||
public static final String ADAPTER_NAME = "ADAPTER_NAME";
|
||||
|
||||
public static final String HTTP_TYPE = "HTTP_TYPE";
|
||||
public static final String URL = "URL";
|
||||
public static final String URL_2GRP = "URL_2GRP";
|
||||
|
||||
public static final String METHOD = "METHOD";
|
||||
public static final String PARAMETER_NAME = "PARAMETER_NAME";
|
||||
|
||||
public static final String TRAN_CODE = "TRAN_CODE";
|
||||
public static final String LOCAL_CALL = "LOCAL_CALL";
|
||||
public static final String LOCAL_CALL_Y = "Y";
|
||||
public static final String LOG_PROCESS_NO = "LOG_PROCESS_NO";
|
||||
public static final String LOCAL_IP = "LOCAL_IP";
|
||||
|
||||
public static final String ENCODE = "ENCODE";
|
||||
|
||||
public static final String CONTENT_TYPE = "CONTENT_TYPE";
|
||||
public static final String CONTENT_TYPE_KEY = "Content-Type";
|
||||
|
||||
public static final String METHOD_GET = "GET";
|
||||
public static final String METHOD_POST = "POST";
|
||||
|
||||
public static final String RESPONSE_TYPE = "RESPONSE_TYPE";
|
||||
public static final String RESPONSE_TYPE_SYNC = "SYNC";
|
||||
public static final String RESPONSE_TYPE_ASYNC = "ASYN";
|
||||
|
||||
public static final String METHOD_SOAP = "SOAP";
|
||||
public static final String METHOD_SOAP_ACTION = "SOAPAction";
|
||||
|
||||
public static final String REST_OPTION = "REST_OPTION";
|
||||
public static final String TOUTVAL = "TOUTVAL";
|
||||
|
||||
public static final String CONNECTION_STALE_CHECKING_ENABLED = "CONNECTION_STALE_CHECKING_ENABLED";
|
||||
|
||||
// 호스트 마다 최대 연결 수
|
||||
public static final int DEFAULT_MAX_CONNECTION_PER_HOST = 10;
|
||||
public static final String MAX_CONNECTION_PER_HOST = "MAX_CONNECTION_PER_HOST";
|
||||
// 최대 HTTP 연결 수
|
||||
public static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 100;
|
||||
public static final String MAX_TOTAL_CONNECTIONS = "MAX_TOTAL_CONNECTIONS";
|
||||
|
||||
static final String PARAMETER_MESSAGE = "_message_";
|
||||
|
||||
static final String URL_ENCODE_YN = "URL_ENCODE_YN";
|
||||
|
||||
static final String CONNECTION_TIMEOUT = "CONNECTION_TIMEOUT";
|
||||
static final String ADAPTER_HEADER = "ADAPTER_HEADER";
|
||||
|
||||
public static final String HTTP_TIME_OUT = "HTTP_TIME_OUT";
|
||||
public static final int DEFAULT_TIME_OUT = 30 * 1000;
|
||||
|
||||
public static final String TESTCALL_URL = "TESTCALL_URL";
|
||||
public static final String TESTCALL_YN = "TESTCALL_YN";
|
||||
public static final String TRACE_LEVEL = "TRACE_LEVEL";
|
||||
|
||||
public static final String HEALTHCHECK_URL = "HEALTHCHECK_URL";
|
||||
public static final String HEALTHCHECK_TIMEOUTMS = "HEALTHCHECK_TIMEOUTMS";
|
||||
|
||||
public static final String INBOUND_METHOD = "INBOUND_METHOD";
|
||||
public static final String INBOUND_URI = "INBOUND_URI";
|
||||
public static final String INBOUND_EXTURI = "INBOUND_EXTURI";
|
||||
public static final String INBOUND_HEADER = "INBOUND_HEADER";
|
||||
|
||||
public static final String HTTP_VERSION = "HTTP_VERSION";
|
||||
public static final String HTTP_VERSION_1_0 = "1.0";
|
||||
public static final String HTTP_VERSION_1_1 = "1.1";
|
||||
public static final String HTTP_VERSION_2_0 = "2.0";
|
||||
|
||||
public static final String HTTP_VERSION_FULL_1_1 = "HTTP_1_1";
|
||||
public static final String HTTP_VERSION_FULL_1_0 = "HTTP_1_0";
|
||||
|
||||
public static final String USE_MTLS = "USE_MTLS";
|
||||
public static final String CLIENT_ID = "CLIENT_ID";
|
||||
public static final String KEYSTORE_PATH = "KEYSTORE_PATH";
|
||||
public static final String KEYSTORE_PWD = "KEYSTORE_PWD";
|
||||
public static final String TRUSTSTORE_PATH = "TRUSTSTORE_PATH";
|
||||
public static final String TRUSTSTORE_PWD = "TRUSTSTORE_PWD";
|
||||
|
||||
public static final String TRANSACTION_ID = "transactionId";
|
||||
public static final String INSTANCE_ID = "instanceId";
|
||||
|
||||
public static final String TRANSACTION_SVC_PATTERN ="TRANSACTION_SVC_PATTERN";//(SYNC/ASYN)-(SYNC/ASYN)
|
||||
}
|
||||
+439
@@ -0,0 +1,439 @@
|
||||
|
||||
package com.eactive.eai.adapter.http.client;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpMethodBase;
|
||||
import org.apache.commons.httpclient.HttpVersion;
|
||||
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
||||
import org.apache.commons.httpclient.protocol.Protocol;
|
||||
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterGroupVO;
|
||||
import com.eactive.eai.adapter.AdapterManager;
|
||||
import com.eactive.eai.adapter.Keys;
|
||||
import com.eactive.eai.adapter.http.secure.CustomHttpsSocketFactory;
|
||||
import com.eactive.eai.adapter.http.secure.CustomSSLProtocolSocketFactory;
|
||||
import com.eactive.eai.adapter.http.secure.EasySSLProtocolSocketFactory;
|
||||
import com.eactive.eai.adapter.http.secure.SSLContextFactory;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.httpouttlsinfo.HttpOutTlsInfoManager;
|
||||
import com.eactive.eai.httpouttlsinfo.HttpOutTlsInfoVO;
|
||||
|
||||
public abstract class HttpClientAdapterServiceSupport implements HttpClientAdapterService, HttpClientAdapterServiceKey {
|
||||
protected static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
protected HttpClient client;
|
||||
private MultiThreadedHttpConnectionManager connectionManager;
|
||||
|
||||
public synchronized void close() {
|
||||
if (connectionManager != null) {
|
||||
connectionManager.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
protected HttpClientAdapterVO setting(Properties prop, Properties tempProp) {
|
||||
String url = null;
|
||||
String parameterName = null;
|
||||
String method = null;
|
||||
String contentType = null;
|
||||
String encode = null;
|
||||
String testCallYn = null;
|
||||
String adapterGroupName = null;
|
||||
String adapterName = null;
|
||||
String responseType = null;
|
||||
String urlEncodeYn = null;
|
||||
String adapterHeader = null;
|
||||
|
||||
boolean useMtls = false;
|
||||
String keyStorePath = null;
|
||||
String keyStorePassword = null;
|
||||
String trustStorePath = null;
|
||||
String trustStorePassword = null;
|
||||
|
||||
int connectionTimeout = 0;
|
||||
int timeout = -1;
|
||||
int traceLevel = 0;
|
||||
long slowTranTime = 2000L;
|
||||
|
||||
url = prop.getProperty(URL);
|
||||
|
||||
String url2grp = tempProp.getProperty(URL);
|
||||
if (!StringUtils.isEmpty(url2grp)) {
|
||||
url = url2grp;
|
||||
}
|
||||
method = prop.getProperty(METHOD);
|
||||
parameterName = prop.getProperty(PARAMETER_NAME, PARAMETER_MESSAGE);
|
||||
contentType = prop.getProperty(CONTENT_TYPE, "");
|
||||
responseType = prop.getProperty(RESPONSE_TYPE, "SYNC");
|
||||
urlEncodeYn = prop.getProperty(URL_ENCODE_YN, "N");
|
||||
String connectionTimeoutTemp = prop.getProperty(CONNECTION_TIMEOUT, "0");
|
||||
|
||||
String httpVersion = prop.getProperty(HTTP_VERSION);
|
||||
adapterHeader = prop.getProperty(ADAPTER_HEADER, "");
|
||||
|
||||
String traceLevelTemp = prop.getProperty(TRACE_LEVEL, "0");
|
||||
String timeoutTemp = prop.getProperty(HTTP_TIME_OUT);
|
||||
|
||||
useMtls = StringUtils.equalsIgnoreCase(prop.getProperty(HttpClientAdapterServiceKey.USE_MTLS), "Y");
|
||||
keyStorePath = prop.getProperty(HttpClientAdapterServiceKey.KEYSTORE_PATH);
|
||||
keyStorePassword = prop.getProperty(HttpClientAdapterServiceKey.KEYSTORE_PWD);
|
||||
trustStorePath = prop.getProperty(HttpClientAdapterServiceKey.TRUSTSTORE_PATH);
|
||||
trustStorePassword = prop.getProperty(HttpClientAdapterServiceKey.TRUSTSTORE_PWD);
|
||||
|
||||
encode = tempProp.getProperty(Keys.CHARSET_KEY_OUT, Charset.defaultCharset().name());
|
||||
testCallYn = tempProp.getProperty(TESTCALL_YN, "N");
|
||||
adapterGroupName = tempProp.getProperty(ADAPTER_GROUP_NAME);
|
||||
adapterName = tempProp.getProperty(ADAPTER_NAME);
|
||||
// 인터페이스 정보에 설정된 타임아웃
|
||||
String interfaceTimeout = tempProp.getProperty("INTERFACE_TIME_OUT");
|
||||
if (StringUtils.isNotBlank(interfaceTimeout) && NumberUtils.toInt(interfaceTimeout) > 0) {
|
||||
timeoutTemp = interfaceTimeout;
|
||||
}
|
||||
|
||||
url = StringUtils.trim(url);
|
||||
parameterName = StringUtils.trim(parameterName);
|
||||
method = StringUtils.trim(method);
|
||||
encode = StringUtils.trim(encode);
|
||||
contentType = StringUtils.trim(contentType);
|
||||
|
||||
if (timeoutTemp == null) {
|
||||
timeoutTemp = "0";
|
||||
} else {
|
||||
timeoutTemp = timeoutTemp.trim();
|
||||
}
|
||||
|
||||
try {
|
||||
connectionTimeout = Integer.parseInt(connectionTimeoutTemp);
|
||||
} catch (Exception e) {
|
||||
logger.debug("HttpClientAdapterSupport] connectionTimeoutTemp not int");
|
||||
}
|
||||
try {
|
||||
timeout = Integer.parseInt(timeoutTemp);
|
||||
} catch (NumberFormatException e) {
|
||||
logger.warn("HttpClientAdapterSupport] timeoutTemp not int", e);
|
||||
}
|
||||
|
||||
try {
|
||||
traceLevel = Integer.parseInt(traceLevelTemp);
|
||||
} catch (Exception e) {
|
||||
logger.warn("HttpClientAdapterSupport] traceLevelTemp not int", e);
|
||||
}
|
||||
|
||||
// client 중 BODY,POST,GET,NexCore 방식만 적용
|
||||
HashMap<String, String> adapterHeaders = new HashMap<>();
|
||||
try {
|
||||
Matcher matcher = Pattern.compile(
|
||||
"\\{[ ]*\"[ ]*([a-zA-Z0-9_-]+)[ ]*\"[ ]*,[ ]*\"[ ]*([a-zA-Z0-9/;,\\*=_ -]+)[ ]*\"[ ]*\\}[ ]*")
|
||||
.matcher(adapterHeader.trim());
|
||||
while (matcher.find()) {
|
||||
String key = matcher.group(1).trim();
|
||||
String value = matcher.group(2).trim();
|
||||
adapterHeaders.put(key, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (logger.isDebug())
|
||||
logger.debug("HttpClientAdapterSupport] adapter header parsing error " + adapterHeader);
|
||||
}
|
||||
// GW 전달 로직 추가
|
||||
// tempProp로 넘겨서 하는 이유는 outboundProp가 clone 안하는 로직이라 outboundProp로 넘길시 문제의 소지가있음
|
||||
String transactionId = tempProp.getProperty(HttpClientAdapterServiceKey.TRANSACTION_ID);
|
||||
String instanceId = tempProp.getProperty(HttpClientAdapterServiceKey.INSTANCE_ID);
|
||||
if (StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(instanceId)) {
|
||||
adapterHeaders.put(HttpClientAdapterServiceKey.TRANSACTION_ID, transactionId);
|
||||
adapterHeaders.put(HttpClientAdapterServiceKey.INSTANCE_ID, instanceId);
|
||||
}
|
||||
|
||||
HttpClientAdapterVO vo = new HttpClientAdapterVO();
|
||||
vo.setUrl(url);
|
||||
vo.setMethod(method);
|
||||
vo.setParameterName(parameterName);
|
||||
vo.setEncode(encode);
|
||||
vo.setContentType(contentType);
|
||||
vo.setTestCallYn(testCallYn);
|
||||
vo.setResponseType(responseType);
|
||||
vo.setUrlEncodeYn(urlEncodeYn);
|
||||
vo.setAdapterGroupName(adapterGroupName);
|
||||
vo.setAdapterName(adapterName);
|
||||
|
||||
vo.setMaxTotalConnections(getMaxTotalConnections());
|
||||
vo.setMaxHostConnections(getMaxConnectionsPerHost());
|
||||
vo.setConnectionTimeout(connectionTimeout);
|
||||
|
||||
vo.setTimeout(timeout);
|
||||
vo.setTraceLevel(traceLevel);
|
||||
vo.setSlowTranTime(slowTranTime);
|
||||
vo.setAdapterHeaders(adapterHeaders);
|
||||
if (StringUtils.equalsAny(httpVersion, HTTP_VERSION_FULL_1_1, HTTP_VERSION_1_1)) {
|
||||
vo.setHttpVersion(HTTP_VERSION_1_1);
|
||||
} else {
|
||||
vo.setHttpVersion(HTTP_VERSION_1_0);
|
||||
}
|
||||
vo.setUseMtls(useMtls);
|
||||
vo.setKeyStorePath(keyStorePath);
|
||||
vo.setKeyStorePassword(keyStorePassword);
|
||||
vo.setTrustStorePath(trustStorePath);
|
||||
vo.setTrustStorePassword(trustStorePassword);
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug(vo.toString());
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
protected HttpClientAdapterServiceSupport() {
|
||||
|
||||
}
|
||||
|
||||
public synchronized void init(String adapterName, boolean useMtls, HttpOutTlsInfoVO mtlsInfo,
|
||||
int maxTotalConnections, int maxHostConnections) {
|
||||
if (connectionManager != null) {
|
||||
if ((connectionManager.getParams().getMaxTotalConnections() == maxTotalConnections)
|
||||
&& (connectionManager.getParams().getDefaultMaxConnectionsPerHost() == maxHostConnections)) {
|
||||
if (logger.isInfo()) {
|
||||
logger.info("ConnectionManager REUSE : AdapterName=" + adapterName + " MaxTotalConnection ("
|
||||
+ connectionManager.getParams().getMaxTotalConnections() + ") MaxHostConnections ("
|
||||
+ connectionManager.getParams().getDefaultMaxConnectionsPerHost() + ")");
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("ConnectionManager RELOAD : AdapterName=" + adapterName + " MaxTotalConnection ("
|
||||
+ connectionManager.getParams().getMaxTotalConnections() + " -> " + maxTotalConnections
|
||||
+ ") MaxHostConnections (" + connectionManager.getParams().getDefaultMaxConnectionsPerHost()
|
||||
+ " -> " + maxHostConnections + ")");
|
||||
}
|
||||
connectionManager.shutdown();
|
||||
connectionManager = null;
|
||||
this.client = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("ConnectionManager INIT : AdapterName=" + adapterName + " MaxTotalConnection ("
|
||||
+ maxTotalConnections + ") MaxHostConnections (" + maxHostConnections + ")");
|
||||
}
|
||||
connectionManager = new MultiThreadedHttpConnectionManager();
|
||||
connectionManager.getParams().setStaleCheckingEnabled(true);
|
||||
connectionManager.getParams().setMaxTotalConnections(maxTotalConnections);
|
||||
connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxHostConnections);
|
||||
|
||||
this.client = new HttpClient(connectionManager);
|
||||
}
|
||||
|
||||
public int getConnectionsInPool() {
|
||||
if (this.client == null)
|
||||
return 0;
|
||||
MultiThreadedHttpConnectionManager mgr = (MultiThreadedHttpConnectionManager) this.client
|
||||
.getHttpConnectionManager();
|
||||
return mgr.getConnectionsInPool();
|
||||
}
|
||||
|
||||
public int getMaxTotalConnections() {
|
||||
if (this.client == null)
|
||||
return 0;
|
||||
MultiThreadedHttpConnectionManager mgr = (MultiThreadedHttpConnectionManager) this.client
|
||||
.getHttpConnectionManager();
|
||||
return mgr.getParams().getMaxTotalConnections();
|
||||
}
|
||||
|
||||
public int getMaxConnectionsPerHost() {
|
||||
if (this.client == null)
|
||||
return 0;
|
||||
MultiThreadedHttpConnectionManager mgr = (MultiThreadedHttpConnectionManager) this.client
|
||||
.getHttpConnectionManager();
|
||||
return mgr.getParams().getDefaultMaxConnectionsPerHost();
|
||||
}
|
||||
|
||||
public String getResponseCharSet(String httpResponseCharset, String adapterGroupName) {
|
||||
if (StringUtils.isNotBlank(httpResponseCharset)) {
|
||||
return httpResponseCharset;
|
||||
} else {
|
||||
AdapterManager m = AdapterManager.getInstance();
|
||||
AdapterGroupVO agvo = m.getAdapterGroupVO(adapterGroupName);
|
||||
return agvo.getMessageEncode();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* encoding에 대한 정의 필요 고려할 문제 1. 어댑터그룹이 UJN 타입일경우 return 은 UTF-8 로 getBytes 된
|
||||
* 데이터여야됨. 2. http response에 받은 encoding 타입은 고려대상임. 결론 : response Encoding 과
|
||||
* 어댑터의 MESSAGE_TYPE 다르면 변환, 나머지면 return 어댑터의 ENCODE은 요청전문 encoding에서만 사용
|
||||
*
|
||||
* 예) 어댑터의 ENCODE, 어댑터의 MESSAGE TYPE, RESPNSE ENCODING TYPE, 처리내용 예) ,ECU-KR
|
||||
* ,JSN , EUC-KR , 응답받은 메시지를 return 예) ,ECU-KR , JSN , UTF-8 , 응답받은 메시지를 new
|
||||
* String(xxx,"UTF-8") => getBytes("EUC-KR") 예) ,ECU-KR , UJN , EUC-KR , 응답받은
|
||||
* 메시지를 new String(xxx,"EUC-KR") => getBytes("UTF-8") 예) ,ECU-KR , UJN , UTF-8 ,
|
||||
* 응답받은 메시지를 return 예) ,UTF-8 , JSN , EUC-KR , 응답받은 메시지를 return 예) ,UTF-8 , JSN
|
||||
* , UTF-8 , 응답받은 메시지를 new String(xxx,"UTF-8") => getBytes(EUC-KR") 예) ,UTF-8 ,
|
||||
* UJN , EUC-KR , 응답받은 메시지를 new String(xxx,"EUC-KR") => getBytes("UTF-8") 예)
|
||||
* ,UTF-8 , UJN , UTF-8 , 응답받은 메시지를 return
|
||||
*
|
||||
*
|
||||
*/
|
||||
public byte[] getEncodingConvert(String httpResponseCharset, String adapterGroupName, byte[] message) {
|
||||
byte[] responseMessage = null;
|
||||
if (StringUtils.isNotBlank(httpResponseCharset)) {
|
||||
AdapterManager m = AdapterManager.getInstance();
|
||||
AdapterGroupVO agvo = m.getAdapterGroupVO(adapterGroupName);
|
||||
String adapterTypeEncoding = StringUtils.defaultIfBlank(agvo.getMessageEncode(),
|
||||
Charset.defaultCharset().name());
|
||||
Charset responseCharset = null;
|
||||
Charset adapterCharset = null;
|
||||
try {
|
||||
responseCharset = Charset.forName(httpResponseCharset);
|
||||
adapterCharset = Charset.forName(adapterTypeEncoding);
|
||||
if (responseCharset.compareTo(adapterCharset) == 0) {
|
||||
responseMessage = message;
|
||||
} else {// http 응답 charset 과 어댑터의 charset 이 다를경우 charset 변환 필요
|
||||
String encodingString = new String(message, httpResponseCharset);
|
||||
responseMessage = encodingString.getBytes(adapterTypeEncoding);
|
||||
}
|
||||
} catch (UnsupportedCharsetException | UnsupportedEncodingException e) {
|
||||
responseMessage = message;
|
||||
}
|
||||
} else {
|
||||
responseMessage = message;
|
||||
}
|
||||
return responseMessage;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void configureHttpClient(HttpClient mclient, HttpMethodBase method, HttpClientAdapterVO vo, String url,
|
||||
String contentType) throws Exception {
|
||||
if (StringUtils.isNotBlank(vo.getEncode())) {
|
||||
mclient.getParams().setContentCharset(vo.getEncode());
|
||||
}
|
||||
|
||||
if (vo.getConnectionTimeout() != 0) {
|
||||
mclient.getHttpConnectionManager().getParams().setConnectionTimeout(vo.getConnectionTimeout());
|
||||
}
|
||||
|
||||
if (vo.getTimeout() > -1) {
|
||||
mclient.getHttpConnectionManager().getParams().setSoTimeout(vo.getTimeout());
|
||||
}
|
||||
|
||||
if (HTTP_VERSION_1_1.equals(vo.getHttpVersion())) {
|
||||
method.setRequestHeader("Connection", "close");
|
||||
method.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
|
||||
} else {
|
||||
method.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0);
|
||||
}
|
||||
|
||||
method.setFollowRedirects(false);
|
||||
|
||||
if (StringUtils.startsWith(url, "https")) {
|
||||
if (vo.isUseMtls()) {
|
||||
HttpOutTlsInfoManager tlsManager = HttpOutTlsInfoManager.getInstance();
|
||||
String clientId = vo.getClientId();
|
||||
HttpOutTlsInfoVO mtlsInfo = tlsManager.getHttpOutTlsInfo(clientId);
|
||||
String storeType = mtlsInfo.getStoreType();
|
||||
String keyStoreInfo = mtlsInfo.getKeystoreInfo();
|
||||
String keyStorePassword = mtlsInfo.getKeystorePassword();
|
||||
String trustStoreInfo = mtlsInfo.getTruststoreInfo();
|
||||
String trustStorePassword = mtlsInfo.getTruststorePassword();
|
||||
|
||||
// Server negotiation 에 의해 변경될 수 있음
|
||||
String[] tlsVersions = null;
|
||||
// {
|
||||
// "TLSv1.0"
|
||||
// , "TLSv1.1"
|
||||
// , "TLSv1.2"
|
||||
// , "TLSv1.3"
|
||||
// };
|
||||
String[] cipherSuites = null;
|
||||
// {
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
|
||||
// "TLS_RSA_WITH_AES_256_CBC_SHA256",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384",
|
||||
// "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384",
|
||||
// "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
|
||||
// "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256",
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_RSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_DHE_RSA_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_DHE_DSS_WITH_AES_256_CBC_SHA",
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_RSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256",
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_RSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_DHE_RSA_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_DHE_DSS_WITH_AES_128_CBC_SHA",
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_RSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384",
|
||||
// "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_RSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256",
|
||||
// "TLS_EMPTY_RENEGOTIATION_INFO_SCSV"
|
||||
// };
|
||||
|
||||
if (StringUtils.isAnyEmpty(keyStoreInfo, keyStorePassword)) {
|
||||
throw new Exception("mTLS keyStore config error");
|
||||
}
|
||||
|
||||
boolean skipTrust = false;
|
||||
// kbank에서는 trustStore를 사용하지 않음
|
||||
if (StringUtils.isAnyEmpty(trustStoreInfo, trustStorePassword)) {
|
||||
// throw new Exception("mTLS trustStore config error");
|
||||
if (logger.isWarn())
|
||||
logger.warn("Skip trustStore validation adapterName : " + vo.getAdapterName());
|
||||
skipTrust = true;
|
||||
}
|
||||
|
||||
SSLContext sslContext = SSLContextFactory.createMTLSContextFromContent(storeType, keyStoreInfo,
|
||||
keyStorePassword, trustStoreInfo, trustStorePassword, skipTrust, tlsVersions, cipherSuites);
|
||||
// SSLProtocolSocketFactory 생성
|
||||
ProtocolSocketFactory socketFactory = new CustomSSLProtocolSocketFactory(sslContext);
|
||||
Protocol.registerProtocol("https", new Protocol("https", socketFactory, 443));
|
||||
} else {
|
||||
Protocol.registerProtocol("https",
|
||||
new Protocol("https", new CustomHttpsSocketFactory(new EasySSLProtocolSocketFactory()), 443));
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.equalsIgnoreCase(method.getName(), "POST")
|
||||
|| StringUtils.equalsIgnoreCase(method.getName(), "PUT")) {
|
||||
if (StringUtils.isNotBlank(contentType)) {
|
||||
if (StringUtils.isBlank(vo.getEncode())) {
|
||||
method.setRequestHeader("Content-Type", contentType);
|
||||
} else {
|
||||
method.setRequestHeader("Content-Type",
|
||||
String.format("%s; charset=%s", contentType, vo.getEncode()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract Object execute(Properties prop, Object message, Properties tempProp) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.eactive.eai.adapter.http.client;
|
||||
|
||||
public enum HttpClientAdapterServiceType {
|
||||
STANDARD,
|
||||
EXTORG,
|
||||
QUICS,
|
||||
REST,
|
||||
UNKNOWN;
|
||||
public static HttpClientAdapterServiceType getValue(String value) {
|
||||
try {
|
||||
return valueOf(value.toUpperCase());
|
||||
} catch (Exception e) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
|
||||
package com.eactive.eai.adapter.http.client;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class HttpClientAdapterVO {
|
||||
protected String url = null;
|
||||
protected String parameterName = null;
|
||||
protected String method = null;
|
||||
|
||||
protected String contentType = null;
|
||||
|
||||
protected String encode = null;
|
||||
protected String testCallYn = null;
|
||||
|
||||
protected String adapterGroupName = null;
|
||||
protected String adapterName = null;
|
||||
protected String responseType = null;
|
||||
protected String urlEncodeYn = null;
|
||||
|
||||
protected int connectionTimeout = 0;
|
||||
protected int maxTotalConnections = 50;
|
||||
protected int maxHostConnections = 10;
|
||||
protected int timeout = -1;
|
||||
protected int traceLevel = 0;
|
||||
|
||||
protected long slowTranTime = 2000L;
|
||||
|
||||
protected String httpVersion = null;
|
||||
|
||||
// client header 값 추가
|
||||
protected HashMap<String, String> adapterHeaders = null;
|
||||
|
||||
// mTLS
|
||||
boolean useMtls = false;
|
||||
String clientId = null;
|
||||
String keyStorePath = null;
|
||||
String keyStorePassword = null;
|
||||
String trustStorePath = null;
|
||||
String trustStorePassword = null;
|
||||
|
||||
public int getMaxTotalConnections() {
|
||||
return maxTotalConnections;
|
||||
}
|
||||
|
||||
public void setMaxTotalConnections(int maxTotalConnections) {
|
||||
this.maxTotalConnections = maxTotalConnections;
|
||||
}
|
||||
|
||||
public int getMaxHostConnections() {
|
||||
return maxHostConnections;
|
||||
}
|
||||
|
||||
public void setMaxHostConnections(int maxHostConnections) {
|
||||
this.maxHostConnections = maxHostConnections;
|
||||
}
|
||||
|
||||
public String getHttpVersion() {
|
||||
return httpVersion;
|
||||
}
|
||||
|
||||
public void setHttpVersion(String httpVersion) {
|
||||
this.httpVersion = httpVersion;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getParameterName() {
|
||||
return parameterName;
|
||||
}
|
||||
|
||||
public void setParameterName(String parameterName) {
|
||||
this.parameterName = parameterName;
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
public void setContentType(String contentType) {
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
public String getEncode() {
|
||||
return encode;
|
||||
}
|
||||
|
||||
public void setEncode(String encode) {
|
||||
this.encode = encode;
|
||||
}
|
||||
|
||||
public String getTestCallYn() {
|
||||
return testCallYn;
|
||||
}
|
||||
|
||||
public void setTestCallYn(String testCallYn) {
|
||||
this.testCallYn = testCallYn;
|
||||
}
|
||||
|
||||
public String getAdapterGroupName() {
|
||||
return adapterGroupName;
|
||||
}
|
||||
|
||||
public void setAdapterGroupName(String adapterGroupName) {
|
||||
this.adapterGroupName = adapterGroupName;
|
||||
}
|
||||
|
||||
public String getAdapterName() {
|
||||
return adapterName;
|
||||
}
|
||||
|
||||
public void setAdapterName(String adapterName) {
|
||||
this.adapterName = adapterName;
|
||||
}
|
||||
|
||||
public String getResponseType() {
|
||||
return responseType;
|
||||
}
|
||||
|
||||
public void setResponseType(String responseType) {
|
||||
this.responseType = responseType;
|
||||
}
|
||||
|
||||
public String getUrlEncodeYn() {
|
||||
return urlEncodeYn;
|
||||
}
|
||||
|
||||
public void setUrlEncodeYn(String urlEncodeYn) {
|
||||
this.urlEncodeYn = urlEncodeYn;
|
||||
}
|
||||
|
||||
public int getConnectionTimeout() {
|
||||
return connectionTimeout;
|
||||
}
|
||||
|
||||
public void setConnectionTimeout(int connectionTimeout) {
|
||||
this.connectionTimeout = connectionTimeout;
|
||||
}
|
||||
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public int getTraceLevel() {
|
||||
return traceLevel;
|
||||
}
|
||||
|
||||
public void setTraceLevel(int traceLevel) {
|
||||
this.traceLevel = traceLevel;
|
||||
}
|
||||
|
||||
public long getSlowTranTime() {
|
||||
return slowTranTime;
|
||||
}
|
||||
|
||||
public void setSlowTranTime(long slowTranTime) {
|
||||
this.slowTranTime = slowTranTime;
|
||||
}
|
||||
|
||||
public HashMap<String, String> getAdapterHeaders() {
|
||||
return adapterHeaders;
|
||||
}
|
||||
|
||||
public void setAdapterHeaders(HashMap<String, String> adapterHeaders) {
|
||||
this.adapterHeaders = adapterHeaders;
|
||||
}
|
||||
|
||||
|
||||
public boolean isUseMtls() {
|
||||
return useMtls;
|
||||
}
|
||||
|
||||
public void setUseMtls(boolean useMtls) {
|
||||
this.useMtls = useMtls;
|
||||
}
|
||||
|
||||
public String getClientId() {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public void setClientId(String clientId) {
|
||||
this.clientId = clientId;
|
||||
}
|
||||
|
||||
public String getKeyStorePath() {
|
||||
return keyStorePath;
|
||||
}
|
||||
|
||||
public void setKeyStorePath(String keyStorePath) {
|
||||
this.keyStorePath = keyStorePath;
|
||||
}
|
||||
|
||||
public String getKeyStorePassword() {
|
||||
return keyStorePassword;
|
||||
}
|
||||
|
||||
public void setKeyStorePassword(String keyStorePassword) {
|
||||
this.keyStorePassword = keyStorePassword;
|
||||
}
|
||||
|
||||
public String getTrustStorePath() {
|
||||
return trustStorePath;
|
||||
}
|
||||
|
||||
public void setTrustStorePath(String trustStorePath) {
|
||||
this.trustStorePath = trustStorePath;
|
||||
}
|
||||
|
||||
public String getTrustStorePassword() {
|
||||
return trustStorePassword;
|
||||
}
|
||||
|
||||
public void setTrustStorePassword(String trustStorePassword) {
|
||||
this.trustStorePassword = trustStorePassword;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("HttpClientAdapterVO [adapterGroupName=" + adapterGroupName + ", adapterGroupName=" + adapterGroupName + ", responseType=" + responseType );
|
||||
sb.append("\n");
|
||||
sb.append("url=" + url + ", parameterName=" + parameterName + ", method=" + method );
|
||||
sb.append("\n");
|
||||
sb.append(", contentType=" + contentType + ", encode=" + encode + ", testCallYn=" + testCallYn );
|
||||
sb.append("\n");
|
||||
sb.append(", urlEncodeYn=" + urlEncodeYn + ", connectionTimeout=" + connectionTimeout );
|
||||
sb.append("\n");
|
||||
sb.append(", maxTotalConnections=" + maxTotalConnections + ", maxHostConnections=" + maxHostConnections );
|
||||
sb.append("\n");
|
||||
sb.append(", timeout=" + timeout + ", traceLevel=" + traceLevel + ", slowTranTime=" + slowTranTime );
|
||||
sb.append("\n");
|
||||
sb.append(", httpVersion=" + httpVersion + "]" );
|
||||
if(adapterHeaders != null) {
|
||||
sb.append("\nadapter header [\n");
|
||||
for (String key : adapterHeaders.keySet()) {
|
||||
sb.append(key).append("=").append(adapterHeaders.get(key));
|
||||
sb.append("\n");
|
||||
}
|
||||
sb.append("\n]");
|
||||
}
|
||||
sb.append("\n");
|
||||
sb.append(", useMtls=" + useMtls + ", clientId=" + clientId);
|
||||
sb.append(", keyStorePath=" + keyStorePath + ", keyStorePassword=" + keyStorePassword );
|
||||
sb.append(", trustStorePath=" + trustStorePath + ", trustStorePassword=" + trustStorePassword );
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
package com.eactive.eai.adapter.http.client.impl;
|
||||
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.client.HttpClient5AdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceKey;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
import com.eactive.eai.common.TransactionContextKeys;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||
import org.apache.hc.core5.http.io.entity.StringEntity;
|
||||
import org.apache.hc.core5.http.protocol.BasicHttpContext;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class HttpClient5AdapterServiceBody extends HttpClient5AdapterServiceSupport {
|
||||
public Object execute(Properties prop, Object data, Properties tempProp) throws Exception {
|
||||
HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
|
||||
boolean isAsync = false;
|
||||
if (RESPONSE_TYPE_ASYNC.equals(vo.getResponseType())) {
|
||||
isAsync = true;
|
||||
}
|
||||
|
||||
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
|
||||
|
||||
boolean useForwardProxy = StringUtils.equalsIgnoreCase(prop.getProperty("FORWARD_PROXY_USE_YN", "N"), "Y");
|
||||
String forwardProxyUrl = prop.getProperty("FORWARD_PROXY_URL");
|
||||
|
||||
if(useForwardProxy) {
|
||||
java.net.URL url = new java.net.URL(forwardProxyUrl);
|
||||
|
||||
// 프로토콜, 호스트, 포트 추출
|
||||
String protocol = url.getProtocol();
|
||||
String host = url.getHost();
|
||||
int port = url.getPort();
|
||||
HttpHost proxy = new HttpHost(protocol,host, port);
|
||||
requestConfigBuilder.setProxy(proxy);
|
||||
}
|
||||
|
||||
String sendData = "";
|
||||
if (data instanceof String) {
|
||||
sendData = (String) data;
|
||||
} else if (data instanceof byte[]) {
|
||||
sendData = new String((byte[]) data, vo.getEncode());
|
||||
}
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectNode jsonNode = (ObjectNode) mapper.readTree(sendData);
|
||||
ObjectNode headerPart = (ObjectNode) jsonNode.get("header_part");
|
||||
|
||||
if( headerPart.get("mciIntfId") != null && headerPart.get("mciIntfId").asText().trim().length() > 0 ) {
|
||||
headerPart.put("eaiIntfId", "");
|
||||
sendData = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
|
||||
}
|
||||
|
||||
if ("Y".equals(vo.getUrlEncodeYn())) {
|
||||
sendData = java.net.URLEncoder.encode(sendData);
|
||||
}
|
||||
|
||||
ContentType contentTypeObj ;
|
||||
if (StringUtils.isNotEmpty(vo.getContentType())) {
|
||||
//주석이유 : ContentType.create에서 "application/json;charset=UTF-8" 이런 형태면 오류발생
|
||||
// String contentTypeValue ;
|
||||
// if (StringUtils.isNotEmpty(vo.getEncode())) {
|
||||
// contentTypeValue = vo.getContentType() + "; charset=" + vo.getEncode();
|
||||
// } else {
|
||||
// contentTypeValue = vo.getContentType();
|
||||
// }
|
||||
contentTypeObj = ContentType.create(vo.getContentType(), vo.getEncode());
|
||||
}else{
|
||||
contentTypeObj = ContentType.APPLICATION_JSON;
|
||||
}
|
||||
|
||||
HttpPost method = new HttpPost(vo.getUrl());
|
||||
// POST 요청 본문 설정
|
||||
if (sendData != null) {
|
||||
method.setEntity(new StringEntity(sendData, contentTypeObj));
|
||||
}
|
||||
|
||||
if (vo.getAdapterHeaders() != null) {
|
||||
for (String key : vo.getAdapterHeaders().keySet()) {
|
||||
method.setHeader(key, vo.getAdapterHeaders().get(key));
|
||||
}
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("" + CONTENT_TYPE_KEY + "(" + contentTypeObj + ")");
|
||||
}
|
||||
|
||||
configureHttpClient(requestConfigBuilder, vo, method);
|
||||
|
||||
int status = -1;
|
||||
|
||||
try {
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug(
|
||||
"SEND (" + vo.getAdapterGroupName() + ") = [" + sendData + "]");
|
||||
logger.debug("SEND (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"SEND [" + sendData + "]" + CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
|
||||
String uuid = tempProp.getProperty(TransactionContextKeys.TRANSACTION_UUID);
|
||||
HttpContext context = new BasicHttpContext();
|
||||
context.setAttribute(TransactionContextKeys.TRANSACTION_UUID, uuid);
|
||||
context.setAttribute(HttpClientAdapterServiceKey.ADAPTER_GROUP_NAME, vo.getAdapterGroupName());
|
||||
context.setAttribute(HttpClientAdapterServiceKey.ADAPTER_NAME, vo.getAdapterName());
|
||||
Integer logProcessNo = (Integer) tempProp.get(HttpClientAdapterServiceKey.LOG_PROCESS_NO);
|
||||
if (logProcessNo != null && logProcessNo > 0) {
|
||||
context.setAttribute(HttpClientAdapterServiceKey.LOG_PROCESS_NO, logProcessNo);
|
||||
}
|
||||
|
||||
byte[] responseMessage = null;
|
||||
String responseString = "";
|
||||
try (CloseableHttpResponse response = (CloseableHttpResponse) this.client.execute(method, context)) {
|
||||
status = response.getCode();
|
||||
responseMessage = EntityUtils.toByteArray(response.getEntity());
|
||||
responseString = new String(responseMessage, vo.getEncode());
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("[received status]" + status);
|
||||
logger.debug("HttpClientAdapterServiceBody] RECV (" + vo.getAdapterGroupName() + ") = [" + responseString + "]");
|
||||
}
|
||||
} catch (ConnectException e) {
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceBody] responseType =" + vo.getResponseType());
|
||||
}
|
||||
throw new Exception("excuteMethod java.net.ConnectException = " + e.getMessage());
|
||||
}
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
|
||||
if (status >= 400 && status < 500) {
|
||||
String errMsg = String.format(
|
||||
"http receive status fail value=%d adapterName=%s.%s uri=%s method=%s response Data=%s",
|
||||
status, vo.getAdapterGroupName(), vo.getAdapterName(), vo.getUrl(), "post", responseString);
|
||||
throw new Exception(errMsg);
|
||||
} else if(status != HttpStatus.SC_OK) {
|
||||
String errMsg = String.format(
|
||||
"http receive status fail value=%d adapterName=%s.%s uri=%s method=%s response Data=%s",
|
||||
status, vo.getAdapterGroupName(), vo.getAdapterName(), vo.getUrl(), "post", responseString);
|
||||
throw new Exception(errMsg);
|
||||
}
|
||||
|
||||
if (isAsync && stopWatch.getTime() > vo.getSlowTranTime() && (logger.isWarn())) {
|
||||
logger.warn("URL[" + vo.getUrl() + "] lazy async response time = "
|
||||
+ stopWatch.toString() + " responseMessage=" + responseString);
|
||||
}
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"RECV [" + responseString + "]"
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
return responseMessage;
|
||||
|
||||
} catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"SocketTimeoutException : " + ste.toString(), ste);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("SocketTimeoutException : " + ste.toString(), ste);
|
||||
}
|
||||
throw ste;
|
||||
} catch (ConnectException ce) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"Connection Exception : " + ce.toString(), ce);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("Connection Exception : " + ce.toString(), ce);
|
||||
}
|
||||
throw ce;
|
||||
} catch (Exception e) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"Exception : " + e.toString(), e);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("Exception : " + e.toString(), e);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
+895
@@ -0,0 +1,895 @@
|
||||
package com.eactive.eai.adapter.http.client.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.eactive.eai.common.property.PropManager;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
import org.apache.hc.client5.http.classic.HttpClient;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpDelete;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPut;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.HttpStatus;
|
||||
import org.apache.hc.core5.http.NameValuePair;
|
||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||
import org.apache.hc.core5.http.io.entity.StringEntity;
|
||||
import org.apache.hc.core5.http.message.BasicNameValuePair;
|
||||
import org.apache.hc.core5.http.protocol.BasicHttpContext;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.apache.hc.core5.net.URIBuilder;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.SAXReader;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpMethodType;
|
||||
import com.eactive.eai.adapter.http.client.HttpClient5AdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceKey;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceKey;
|
||||
import com.eactive.eai.common.TransactionContextKeys;
|
||||
import com.eactive.eai.common.authoutbound.AccessTokenManagerByDB;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.message.MessageType;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.jayway.jsonpath.DocumentContext;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import com.openbanking.eai.common.token.AccessTokenManager;
|
||||
import com.openbanking.eai.common.token.AccessTokenVO;
|
||||
import com.openbanking.eai.common.token.OAuth2AccessTokenVO;
|
||||
|
||||
/**
|
||||
* 1. 기능 : EAI HTTP OutBound 용 어댑터로 수동 시스템의 HTTP 웹 컴포넌트를 GET/POST 방식으로 호출할 수 있는
|
||||
* 기능을 제공한다.<br>
|
||||
* 2. 처리 개요 : <br>
|
||||
* * - 2020-09-25: Http Header 받아서 처리할 수 있게 기능 추가<br>
|
||||
* 3. 주의사항 <br>
|
||||
*
|
||||
* @author :
|
||||
* @version : v 1.0.0
|
||||
* @see : HttpClientAdapterServiceFactory.java,
|
||||
* HttpClientAdapterServiceSupport.java
|
||||
* @since :
|
||||
*
|
||||
*/
|
||||
public class HttpClient5AdapterServiceRest extends HttpClient5AdapterServiceSupport
|
||||
implements HttpClientAdapterServiceKey {
|
||||
public static final String TYPE_VARIABLE_URL_REQUEST = "variableUrlRequest";
|
||||
public static final String TYPE_SIMPLE_REQUEST = "simpleRequest";
|
||||
public static final String REST_OPTION = "REST_OPTION";
|
||||
//public static final String HEADER_CONTENT_TYPE = "Content-Type";
|
||||
public static final String HEADER_GROUP = "HEADER_GROUP";
|
||||
public static final String HTTP_STATUS = "HTTP_STATUS";
|
||||
public static final String HEADER_KEYS = "HEADER_KEYS";
|
||||
public static final String AUTH_TOKEN = "AUTH_TOKEN";
|
||||
|
||||
/**
|
||||
* 1. 기능 : REST API 통신에 사용 <br>
|
||||
* 2. 처리 개요 : - 속성 정보를 설정 하고 수동 시스템 서비스를 호출 한다. <br>
|
||||
* 3. 주의사항 <br>
|
||||
*
|
||||
* @param prop Http Adapter 속성 정보
|
||||
* @return 반환 된 Object
|
||||
* @exception Exception 수동 시스템 간 통신 중 발생
|
||||
**/
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
public Object execute(Properties prop, Object data, Properties tempProp) throws Exception {
|
||||
HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
|
||||
// 2025.04.09 Authorization 헤더값을 프로퍼티(AUTH_TOKEN)에서 지정하여 설정할 수 있도록 수정
|
||||
// 2025.05.29 프로퍼티 그룹 AUTH_TOKEN이 없을때 Null Point Exception 발생하여 수정
|
||||
String bizCode = "";
|
||||
String authToken = "";
|
||||
try {
|
||||
Properties authProp = PropManager.getInstance().getProperties(AUTH_TOKEN); //Authorization
|
||||
bizCode = vo.getAdapterGroupName().substring(1, 4);
|
||||
authToken = authProp.getProperty(bizCode, "");
|
||||
} catch (Exception e) {
|
||||
logger.debug("Property Group AUTH_TOKEN is not Exist !!");
|
||||
}
|
||||
|
||||
logger.debug("authToken :::::::::::::::::::::::::::::::::::" + authToken);
|
||||
|
||||
String headerGroupName = prop.getProperty(HEADER_GROUP);
|
||||
String relayResponseHeaderKeys = prop.getProperty(HEADER_KEYS, "");
|
||||
boolean useAdapterToken = StringUtils.equalsIgnoreCase(prop.getProperty("ADAPTER_TOKEN_USE_YN", "N"), "Y");
|
||||
|
||||
boolean useForwardProxy = StringUtils.equalsIgnoreCase(prop.getProperty("FORWARD_PROXY_USE_YN", "N"), "Y");
|
||||
String forwardProxyUrl = prop.getProperty("FORWARD_PROXY_URL");
|
||||
|
||||
// ex) $.dataHeader.GW_RSLT_CD
|
||||
String tokenErrorCodeKey = prop.getProperty("ADAPTER_TOKEN_ERROR_CODE_KEY");
|
||||
String tokenErrorCodeValues = prop.getProperty("ADAPTER_TOKEN_ERROR_CODE_VALUES");
|
||||
String tokenErrorHttpStatusCode = prop.getProperty("ADAPTER_TOKEN_ERROR_HTTP_STATUS_CODE"); // 200 or 400번대
|
||||
|
||||
// 레이아웃 메시지 타입 REST URL 추출 시 활용. Default JSON
|
||||
String messageType = prop.getProperty("MESSAGE_TYPE", MessageType.JSON);
|
||||
String inboundMethod = tempProp.getProperty(HttpAdapterServiceKey.INBOUND_METHOD, "POST");
|
||||
|
||||
/**
|
||||
* @formatter:off
|
||||
* TSEAIHE02.RESTOPTION 정보 => JSON 형태로 구성
|
||||
* - type : simpleRequest, variableUrlRequest
|
||||
* - extraPath : 어댑터 프로퍼티 URL에 추가될 HTTP REST URL
|
||||
* - method : get, delete, post, put
|
||||
* - contentType: application/json(default), application/x-www-form-urlencoded
|
||||
* - adapterTokenUseYn: Y, N(default)
|
||||
* ex)
|
||||
* {"type":"simpleRequest","extraPath":"v2.0/accout/balance","method":"post"}
|
||||
* @formatter:on
|
||||
*/
|
||||
|
||||
String restOptionData = tempProp.getProperty(REST_OPTION);
|
||||
|
||||
JSONObject restOptionObject = parseJson(restOptionData);
|
||||
if (restOptionObject == null) {
|
||||
throw new Exception("OptionData parsing result is NULL");
|
||||
}
|
||||
|
||||
String rmethod = (String) restOptionObject.getOrDefault("method", inboundMethod);
|
||||
String contentType = (String) restOptionObject.getOrDefault("contentType", "application/json");
|
||||
String adapterTokenUseYn = (String) restOptionObject.get("adapterTokenUseYn");
|
||||
if (StringUtils.isNotBlank(adapterTokenUseYn)) { // interface 에 설정된게 우선한다.
|
||||
useAdapterToken = StringUtils.equalsIgnoreCase(adapterTokenUseYn, "Y");
|
||||
}
|
||||
|
||||
HttpClient mclient = this.client;
|
||||
String sendData = "";
|
||||
if (data instanceof String) {
|
||||
sendData = (String) data;
|
||||
} else if (data instanceof byte[]) {
|
||||
sendData = new String((byte[]) data, vo.getEncode());
|
||||
}
|
||||
|
||||
////mclient.getParams().setContentCharset(vo.getEncode());
|
||||
|
||||
JSONObject dataObject = null;
|
||||
Object httpHeader = null;
|
||||
Object dataContent = null;
|
||||
|
||||
if (MessageType.JSON.equals(messageType)) {
|
||||
|
||||
Object parsed = parseJsonGeneric(sendData);
|
||||
|
||||
if(parsed instanceof JSONObject) {
|
||||
dataObject = parseJson(sendData);
|
||||
if (dataObject != null && StringUtils.isNotBlank(headerGroupName)) {
|
||||
httpHeader = dataObject.get(headerGroupName);
|
||||
dataObject.remove(headerGroupName);
|
||||
}
|
||||
|
||||
dataContent = dataObject;
|
||||
if (dataObject.containsKey("innerList")) {
|
||||
JSONArray innerList = (JSONArray) dataObject.get("innerList");
|
||||
dataContent = innerList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if (MessageType.JSON.equals(messageType)) {
|
||||
// dataObject = parseJson(sendData);
|
||||
// if (dataObject != null && StringUtils.isNotBlank(headerGroupName)) {
|
||||
// httpHeader = dataObject.get(headerGroupName);
|
||||
// dataObject.remove(headerGroupName);
|
||||
// }
|
||||
// }
|
||||
|
||||
logger.debug("HttpClientAdapterServiceRest] dataObject1 = [" + dataObject + "]");
|
||||
|
||||
// interface 에 설정된게 우선한다.
|
||||
String uri = null;
|
||||
if (dataObject == null) {
|
||||
uri = changeUrl(messageType, vo.getUrl(), restOptionData, sendData);
|
||||
} else {
|
||||
uri = changeUrl(messageType, vo.getUrl(), restOptionData, dataObject);
|
||||
}
|
||||
|
||||
logger.debug("HttpClientAdapterServiceRest] dataObject2 = [" + dataObject + "]");
|
||||
|
||||
// ////if (vo.getConnectionTimeout() != 0) {
|
||||
// mclient.getHttpConnectionManager().getParams().setConnectionTimeout(vo.getConnectionTimeout());
|
||||
// }
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") URL=[" + vo.getUrl() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") PARAMETER_NAME=[" + vo.getParameterName() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") ENCODE=[" + vo.getEncode() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") RESPONSE_TYPE=[" + vo.getResponseType() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") URL_ENCODE_YN=[" + vo.getUrlEncodeYn() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") CONNECTION_TIMEOUT=[" + vo.getConnectionTimeout() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") REST_OPTION=[" + restOptionData + "] ");
|
||||
}
|
||||
|
||||
HttpUriRequestBase method = null;
|
||||
|
||||
|
||||
// 메소드 타입에 따라 HttpRequestBase 인스턴스 생성
|
||||
switch (HttpMethodType.getValue(rmethod)) {
|
||||
case GET:
|
||||
method = new HttpGet(uri);
|
||||
break;
|
||||
case DELETE:
|
||||
method = new HttpDelete(uri);
|
||||
break;
|
||||
case POST:
|
||||
method = new HttpPost(uri);
|
||||
break;
|
||||
case PUT:
|
||||
method = new HttpPut(uri);
|
||||
break;
|
||||
default:
|
||||
method = new HttpGet(uri);
|
||||
break;
|
||||
}
|
||||
|
||||
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
|
||||
|
||||
if(useForwardProxy) {
|
||||
java.net.URL url = new java.net.URL(forwardProxyUrl);
|
||||
|
||||
// 프로토콜, 호스트, 포트 추출
|
||||
String protocol = url.getProtocol();
|
||||
String host = url.getHost();
|
||||
int port = url.getPort();
|
||||
HttpHost proxy = new HttpHost(protocol,host, port);
|
||||
requestConfigBuilder.setProxy(proxy);
|
||||
}
|
||||
|
||||
configureHttpClient(requestConfigBuilder, vo, method);
|
||||
|
||||
switch (HttpMethodType.getValue(rmethod)) {
|
||||
case GET:
|
||||
case DELETE:
|
||||
HashMap<String, String> h = getParameters(dataObject);
|
||||
URIBuilder uriBuilder = new URIBuilder(uri);
|
||||
for (Map.Entry<String, String> entry : h.entrySet()) {
|
||||
uriBuilder.addParameter(entry.getKey(), entry.getValue());
|
||||
}
|
||||
URI fullUri = uriBuilder.build();
|
||||
if (method instanceof HttpGet) {
|
||||
((HttpGet) method).setUri(fullUri);
|
||||
} else if (method instanceof HttpDelete) {
|
||||
((HttpDelete) method).setUri(fullUri);
|
||||
}
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpClientAdapterServiceRest] (get Method) QueryString = [" + fullUri.getQuery() + "]");
|
||||
}
|
||||
break;
|
||||
case PUT:
|
||||
case POST:
|
||||
// assignPostBody(dataObject, contentType, vo.getEncode(), method);
|
||||
assignPostBody(dataContent, contentType, vo.getEncode(), method);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") method.getParams()=["
|
||||
+ method.getEntity() + "] ");
|
||||
}
|
||||
|
||||
OAuth2AccessTokenVO accessToken = null;
|
||||
if (useAdapterToken) {
|
||||
AccessTokenManagerByDB tokenManager = AccessTokenManagerByDB.getInstance();
|
||||
accessToken = (OAuth2AccessTokenVO) tokenManager.getAccessTokenVO(vo.getAdapterGroupName());
|
||||
|
||||
// 토큰이 없거나 만료 됐으면 재발급
|
||||
if (accessToken == null || accessToken.isExpired()) {
|
||||
String oldToken = accessToken == null ? null : accessToken.getAccessToken();
|
||||
accessToken = (OAuth2AccessTokenVO) tokenManager.retryAccessTokenVO(vo.getAdapterGroupName(), prop,
|
||||
oldToken);
|
||||
}
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") TOKEN = ["
|
||||
+ accessToken + "]");
|
||||
}
|
||||
|
||||
setAuthHeaders(method, accessToken);
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName()
|
||||
+ ") RequestHeader [Authorization=" + method.getHeader("Authorization") + "]");
|
||||
}
|
||||
}
|
||||
|
||||
if(!StringUtils.isBlank(authToken)) {
|
||||
setAuthHeaders(method, authToken);
|
||||
}
|
||||
|
||||
// 전달 header 셋팅
|
||||
// Adapter 레벨 header 보다 data로 넘어온 httpHeader를 우선 적용(header명이 같다면 덮어씀)
|
||||
assignRequestHeaders(method, httpHeader);
|
||||
|
||||
int status = -1;
|
||||
|
||||
try {
|
||||
byte[] responseMessage = null;
|
||||
Header[] responseHeaders;
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
if (dataObject != null) {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") = ["
|
||||
+ dataObject.toJSONString() + "]");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(dataObject.toJSONString()));
|
||||
} else {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") = [" + sendData
|
||||
+ "]");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
}
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"SEND [" + sendData + "]" + CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
|
||||
String uuid = tempProp.getProperty(TransactionContextKeys.TRANSACTION_UUID);
|
||||
HttpContext context = new BasicHttpContext();
|
||||
context.setAttribute(TransactionContextKeys.TRANSACTION_UUID, uuid);
|
||||
context.setAttribute(HttpClientAdapterServiceKey.ADAPTER_GROUP_NAME, vo.getAdapterGroupName());
|
||||
context.setAttribute(HttpClientAdapterServiceKey.ADAPTER_NAME, vo.getAdapterName());
|
||||
Integer logProcessNo = (Integer) tempProp.get(HttpClientAdapterServiceKey.LOG_PROCESS_NO);
|
||||
if (logProcessNo != null && logProcessNo > 0) {
|
||||
context.setAttribute(HttpClientAdapterServiceKey.LOG_PROCESS_NO, logProcessNo);
|
||||
}
|
||||
|
||||
try {
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("[method getName]" + method.getMethod());
|
||||
logger.debug("[method getEntity]" + method.getEntity());
|
||||
logger.debug("[method getRequestHeaders]" + method.getHeaders().toString());
|
||||
logger.debug("[method getURI]" + method.getPath());
|
||||
}
|
||||
try (CloseableHttpResponse response = (CloseableHttpResponse) mclient.execute(method, context)) {
|
||||
status = response.getCode();
|
||||
|
||||
responseMessage = EntityUtils.toByteArray(response.getEntity());
|
||||
responseHeaders = response.getHeaders();
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("[received status]" + status);
|
||||
logger.debug("HttpClientAdapterServiceRest] RECV (" + vo.getAdapterGroupName() + ") = [" + responseMessage + "]");
|
||||
}
|
||||
}
|
||||
} catch (ConnectException e) {
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRest] responseType =" + vo.getResponseType());
|
||||
}
|
||||
throw new Exception("excuteMethod java.net.ConnectException = " + e.getMessage());
|
||||
}
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
/**
|
||||
* @formatter:off
|
||||
* OAuth 토큰 응답 체크(Adapter properties로 설정)
|
||||
* 응답코드에 따라 유효한 토큰 확인(토큰 재발급 여부 확인)
|
||||
* API 서비스 마다 정책이 다름(보통 400대에서 체크하나 200에서도 체크할 수 있음)
|
||||
*
|
||||
* TOKEN_ERROR_CODE_KEY: 응답 json error code key
|
||||
* __ex) $.dataHeader.resultCode
|
||||
* TOKEN_ERROR_CODE_VALUES: 토큰 재발급이 필요한 응답 error codes(ex: O0001,O0002,O0003)
|
||||
* TOKEN_ERROR_HTTP_STATUS_CODE: 보통 400대에서 체크하나 API 제공자에 따라 200에서도 체클할수 있음
|
||||
* ex) TOKEN_ERROR_HTTP_STATUS_CODE = 200
|
||||
* @formatter:on
|
||||
*/
|
||||
boolean needReissue = false;
|
||||
|
||||
Properties responseHeaderProp = assignRelayDataToInbound(tempProp, responseHeaders);
|
||||
String responseString = new String(responseMessage, vo.getEncode());
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
tempProp.put(HttpAdapterServiceKey.OUTBOUND_PROPERTY_MAP, map);
|
||||
map.put(HttpAdapterServiceKey.OUTBOUND_RESPONSE_HEADERS, responseHeaderProp);
|
||||
map.put(HttpAdapterServiceKey.OUTBOUND_RESPONSE_MESSAGE, responseString);
|
||||
map.put(HttpAdapterServiceKey.OUTBOUND_RESPONSE_STATUS, status);
|
||||
|
||||
if (status >= 200 && status <= 207) {
|
||||
if (useAdapterToken && StringUtils.equals(tokenErrorHttpStatusCode, "200")) {
|
||||
needReissue = checkTokenRetry(responseMessage, tokenErrorCodeKey, tokenErrorCodeValues,
|
||||
vo.getEncode());
|
||||
}
|
||||
} else if (status == 302) {
|
||||
if (!StringUtils.contains(relayResponseHeaderKeys, "Location")) {
|
||||
relayResponseHeaderKeys += ",Location";
|
||||
}
|
||||
} else {
|
||||
String errMsg = String.format(
|
||||
"http receive status fail value=%d adapterName=%s.%s uri=%s method=%s response Data=%s",
|
||||
status, vo.getAdapterGroupName(), vo.getAdapterName(), uri, rmethod, responseString);
|
||||
// logger.error(errMsg);
|
||||
|
||||
if (status >= 400 && status < 500) {
|
||||
if (useAdapterToken) {
|
||||
needReissue = checkTokenRetry(responseMessage, tokenErrorCodeKey, tokenErrorCodeValues,
|
||||
vo.getEncode());
|
||||
}
|
||||
|
||||
if (!needReissue) {
|
||||
throw new Exception(errMsg);
|
||||
}
|
||||
} else {
|
||||
|
||||
throw new Exception(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRest] responseType =" + vo.getResponseType());
|
||||
logger.debug("HttpClientAdapterServiceRest] RECV " + vo.getEncode() + "(" + vo.getAdapterGroupName() + ") = [" + new String(responseMessage, vo.getEncode()) + "]");
|
||||
// logger.debug("HttpClientAdapterServiceRest] RECV MS949 (" + vo.getAdapterGroupName() + ") = [" + new String(responseMessage, "MS949") + "]");
|
||||
// logger.debug("HttpClientAdapterServiceRest] RECV euc-kr (" + vo.getAdapterGroupName() + ") = [" + new String(responseMessage, "euc-kr") + "]");
|
||||
logger.debug("HttpClientAdapterServiceRest] RECV (" + vo.getAdapterGroupName() + ") = " + CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
if (useAdapterToken) {
|
||||
if (responseMessage == null) {
|
||||
throw new Exception("responseMessage is NULL, HttpStatus=" + status);
|
||||
}
|
||||
|
||||
// OAuth 토큰 재요청 코드 확인
|
||||
if (needReissue) {
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRest] retry access token response code = ["
|
||||
+ vo.getResponseType() + "] message = [" + new String(responseMessage, vo.getEncode())
|
||||
+ "]");
|
||||
}
|
||||
|
||||
// 토큰 재발급
|
||||
AccessTokenManager tokenManager = AccessTokenManager.getInstance();
|
||||
String oldToken = accessToken == null ? null : accessToken.getAccessToken();
|
||||
OAuth2AccessTokenVO newaccessToken = (OAuth2AccessTokenVO) tokenManager
|
||||
.retryAccessTokenVO(vo.getAdapterGroupName(), prop, oldToken);
|
||||
method.setHeader("Authorization", newaccessToken.getAuthorization());
|
||||
|
||||
setAuthHeaders(method, newaccessToken);
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName()
|
||||
+ ") RETRY TOKEN = [" + newaccessToken + "]");
|
||||
}
|
||||
|
||||
try (CloseableHttpResponse response = (CloseableHttpResponse) mclient.execute(method)) {
|
||||
status = response.getCode();
|
||||
responseMessage = EntityUtils.toByteArray(response.getEntity());
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("[received status]" + status);
|
||||
logger.debug("HttpClientAdapterServiceRest] RECV (" + vo.getAdapterGroupName() + ") = [" + new String(responseMessage, vo.getEncode()) + "]");
|
||||
}
|
||||
}catch (IOException e){
|
||||
throw new Exception("retry excuteMethod Exceptioin = " + e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
if (status == 302) {
|
||||
if (!StringUtils.contains(relayResponseHeaderKeys, "Location")) {
|
||||
relayResponseHeaderKeys += ",Location";
|
||||
}
|
||||
} else {
|
||||
String errMsg = String.format(
|
||||
"http receive status fail value=%d adapterName=%s.%s uri=%s method=%s response Data=%s",
|
||||
status, vo.getAdapterGroupName(), vo.getAdapterName(), uri, rmethod, responseMessage);
|
||||
throw new Exception(errMsg);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRest] RETRY responseType =" + vo.getResponseType());
|
||||
logger.debug("HttpClientAdapterServiceRest] RETRY RECV " + vo.getEncode() + "("
|
||||
+ vo.getAdapterGroupName() + ") = [" + new String(responseMessage, vo.getEncode())
|
||||
+ "]");
|
||||
logger.debug("HttpClientAdapterServiceRest] RETRY RECV (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
// ----------------------------------------------------------
|
||||
}
|
||||
}
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"RECV [" + new String(responseMessage, vo.getEncode()) + "]"
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
|
||||
return assignResponseHeaders(method, responseMessage, headerGroupName, vo.getEncode(), messageType,
|
||||
relayResponseHeaderKeys, status, responseHeaderProp);
|
||||
} catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceRest] SocketTimeoutException (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ste.toString(),
|
||||
ste);
|
||||
}
|
||||
logger.error("HttpClientAdapterServiceRest] SocketTimeoutException (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ste.toString(), ste);
|
||||
throw ste;
|
||||
} catch (ConnectException ce) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceRest] Connection Exception (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ce.toString(),
|
||||
ce);
|
||||
}
|
||||
logger.error("HttpClientAdapterServiceRest] Connection Exception (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ce.toString(), ce);
|
||||
throw ce;
|
||||
} catch (Exception e) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(), e.toString(), e);
|
||||
}
|
||||
logger.error("HttpClientAdapterServiceRest] Exception (" + vo.getAdapterGroupName() + ") : " + e.toString(),
|
||||
e);
|
||||
throw e;
|
||||
} finally {
|
||||
if (status != HttpStatus.SC_OK) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = String.valueOf(status);
|
||||
String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug(resMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Properties assignRelayDataToInbound(Properties prop, Header[] responseHeaders) {
|
||||
Properties headerProp = new Properties();
|
||||
if (responseHeaders == null || responseHeaders.length == 0) {
|
||||
return headerProp;
|
||||
}
|
||||
|
||||
for (Header header : responseHeaders) {
|
||||
headerProp.put(header.getName(), header.getValue());
|
||||
}
|
||||
|
||||
return headerProp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 현재 1단계 레이아웃에만 적용되도록 구현됨.
|
||||
*
|
||||
* @param method
|
||||
* @param responseMessage
|
||||
* @param headerGroupName
|
||||
* @param encode
|
||||
* @param messageType
|
||||
* @param relayHeaderKeys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private String assignResponseHeaders(HttpUriRequestBase method, byte[] responseMessage, String headerGroupName,
|
||||
String encode, String messageType, String relayHeaderKeys, int status, Properties responseHeaderProp) throws Exception {
|
||||
if (!MessageType.JSON.equals(messageType) || StringUtils.isBlank(headerGroupName)
|
||||
|| StringUtils.isBlank(relayHeaderKeys)) {
|
||||
return new String(responseMessage, encode);
|
||||
}
|
||||
|
||||
String[] relayKeyArr = org.springframework.util.StringUtils.tokenizeToStringArray(relayHeaderKeys, ",");
|
||||
JSONObject headerJson = new JSONObject();
|
||||
for (String key : relayKeyArr) {
|
||||
String value = responseHeaderProp.getProperty(key);
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
headerJson.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
headerJson.put(HTTP_STATUS, String.valueOf(status));
|
||||
|
||||
if (headerJson.size() <= 0) {
|
||||
return new String(responseMessage, encode);
|
||||
}
|
||||
|
||||
JSONObject message = null;
|
||||
if (responseMessage == null || responseMessage.length == 0) {
|
||||
message = new JSONObject();
|
||||
} else {
|
||||
message = parseJson(new String(responseMessage, encode));
|
||||
if (message == null) {
|
||||
message = new JSONObject();
|
||||
message.put("Malformed_Response_Message", new String(responseMessage, encode));
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(headerGroupName) && message != null)
|
||||
message.put(headerGroupName, headerJson);
|
||||
return message.toJSONString();
|
||||
}
|
||||
|
||||
private void setAuthHeaders(HttpUriRequestBase method, String authorization) throws Exception {
|
||||
method.setHeader("Authorization",
|
||||
String.format("%s", authorization));
|
||||
}
|
||||
|
||||
private void setAuthHeaders(HttpUriRequestBase method, OAuth2AccessTokenVO accessToken) throws Exception {
|
||||
method.setHeader("Authorization",
|
||||
String.format("%s %s", AccessTokenVO.BEARER_TYPE, accessToken.getAccessToken()));
|
||||
}
|
||||
|
||||
private boolean checkTokenRetry(byte[] responseMessage, String tokenErrorCodeKey, String tokenErrorCodeValues,
|
||||
String encode) {
|
||||
if (responseMessage == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(tokenErrorCodeKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(tokenErrorCodeValues)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
DocumentContext jsonContext = JsonPath.parse(new String(responseMessage, encode));
|
||||
String responseCode = jsonContext.read(tokenErrorCodeKey);
|
||||
if (StringUtils.isBlank(responseCode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] arr = org.springframework.util.StringUtils.tokenizeToStringArray(tokenErrorCodeValues, ",");
|
||||
return ArrayUtils.contains(arr, responseCode);
|
||||
} catch (Exception e) {
|
||||
logger.error("HttpClientAdapterServiceRest] checkTokenRetry error=" + e.getMessage());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void assignRequestHeaders(HttpUriRequestBase method, Object httpHeader) {
|
||||
if (httpHeader == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (httpHeader instanceof JSONObject) {
|
||||
JSONObject jsonObject = (JSONObject) httpHeader;
|
||||
for (Object key : jsonObject.keySet()) {
|
||||
Object obj = jsonObject.get(key);
|
||||
if ((obj instanceof JSONObject) || (obj instanceof JSONArray)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
method.setHeader((String) key, (String) obj);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Request Header :" + (String) key + "=[" + (String) obj + "]");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void assignPostBody(Object eaiBody, String contentType, String charset, HttpUriRequestBase method) {
|
||||
if (eaiBody == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (StringUtils.contains(contentType, "application/x-www-form-urlencoded")) {
|
||||
if (eaiBody instanceof JSONObject) {
|
||||
List<NameValuePair> params = new ArrayList<>();
|
||||
JSONObject jsonObject = (JSONObject) eaiBody;
|
||||
for (Object key : jsonObject.keySet()) {
|
||||
params.add(new BasicNameValuePair((String) key, (String) jsonObject.get(key)));
|
||||
}
|
||||
|
||||
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, StandardCharsets.UTF_8);
|
||||
method.setEntity(entity);
|
||||
}
|
||||
} else {
|
||||
ContentType contentTypeObj = ContentType.create(contentType, charset);
|
||||
|
||||
// 요청 본문을 StringEntity 객체로 생성
|
||||
StringEntity entity = new StringEntity(eaiBody.toString(), contentTypeObj);
|
||||
|
||||
// 요청에 본문 추가
|
||||
method.setEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
private HashMap<String, String> getParameters(Object message) throws Exception {
|
||||
|
||||
HashMap<String, String> result = new HashMap<String, String>();
|
||||
|
||||
if (message != null) {
|
||||
if (message instanceof JSONObject) {
|
||||
JSONObject jsonObject = (JSONObject) message;
|
||||
for (Object key : jsonObject.keySet()) {
|
||||
Object obj = jsonObject.get(key);
|
||||
if ((obj instanceof JSONObject) || (obj instanceof JSONArray)) {
|
||||
continue;
|
||||
}
|
||||
result.put((String) key, getStringValue(obj));
|
||||
}
|
||||
} else {
|
||||
String[] messages = ((String) message).split("&");
|
||||
String[] data = null;
|
||||
|
||||
for (int i = 0; i < messages.length; i++) {
|
||||
data = messages[i].split("=", 2);
|
||||
if (data.length == 2) {
|
||||
result.put(data[0], data[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getStringValue(Object obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (obj instanceof String) {
|
||||
return (String) obj;
|
||||
} else if (obj instanceof Long) {
|
||||
return Long.toString((Long) obj);
|
||||
} else if (obj instanceof BigDecimal) {
|
||||
return ((BigDecimal) obj).toPlainString();
|
||||
} else {
|
||||
return (String) obj;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object parseJsonGeneric(String jsonData) {
|
||||
if (StringUtils.isBlank(jsonData)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
JSONParser parser = new JSONParser();
|
||||
Object parsedObj = parser.parse(jsonData);
|
||||
return parsedObj;
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private JSONObject parseJson(String message) throws Exception {
|
||||
if (message == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (JSONObject) JSONValue.parse(message);
|
||||
}
|
||||
|
||||
private Document convertXmlDocument(String message) throws Exception {
|
||||
SAXReader builder = new SAXReader();
|
||||
Document document = builder.read(new StringReader(message));
|
||||
return document;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private String changeUrl(String messageType, String url, String restOption, Object sendData) {
|
||||
if ((MessageType.JSON.equals(messageType) || MessageType.XML.equals(messageType))) {
|
||||
try {
|
||||
if (StringUtils.isBlank(restOption)) {
|
||||
return url;
|
||||
}
|
||||
List<String> urlVariableList = new ArrayList<String>();
|
||||
JSONObject restOptionObject = parseJson(restOption);
|
||||
|
||||
if (restOptionObject == null)
|
||||
throw new Exception("restOption is NULL");
|
||||
|
||||
String type = (String) restOptionObject.get("type");
|
||||
String requestExtraPath = (String) restOptionObject.get("extraPath");
|
||||
url = getUrl(url, requestExtraPath);
|
||||
if (TYPE_VARIABLE_URL_REQUEST.equals(type)) {
|
||||
Pattern p = Pattern.compile("\\{(.*?)\\}");
|
||||
Matcher m = p.matcher(requestExtraPath);
|
||||
List<String> uriVariables = new ArrayList<>();
|
||||
while(m.find()) {
|
||||
String fieldName = m.group(1);
|
||||
uriVariables.add(fieldName);
|
||||
}
|
||||
if (MessageType.JSON.equals(messageType)) {
|
||||
JSONObject jsonObject;
|
||||
if (sendData instanceof JSONObject) {
|
||||
jsonObject = (JSONObject) sendData;
|
||||
} else {
|
||||
jsonObject = (JSONObject) JSONValue.parse((String) sendData);
|
||||
}
|
||||
for (Object tempObject : uriVariables) {
|
||||
String urlVaribleId = (String) tempObject;
|
||||
String uriVariable = (String) jsonObject.get(urlVaribleId);
|
||||
urlVariableList.add(uriVariable);
|
||||
jsonObject.remove(urlVaribleId);
|
||||
}
|
||||
} else if (MessageType.XML.equals(messageType)) {
|
||||
Document doc;
|
||||
if (sendData instanceof Document) {
|
||||
doc = (Document) sendData;
|
||||
} else {
|
||||
doc = convertXmlDocument((String) sendData);
|
||||
}
|
||||
for (Object tempObject : uriVariables) {
|
||||
String urlVaribleId = (String) tempObject;
|
||||
Element element = (Element) doc.selectSingleNode("//" + urlVaribleId);
|
||||
urlVariableList.add(element.getText());
|
||||
if (doc.getRootElement() == element) {
|
||||
doc = null;
|
||||
} else {
|
||||
element.getParent().remove(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (urlVariableList.size() > 0) {
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).build();
|
||||
Object[] urls = urlVariableList.toArray();
|
||||
url = uriComponents.expand(urls).toUriString();
|
||||
logger.debug("HttpClientAdapterServiceRest] after extand url=[" + url + "] ");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to change url", e);
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
private String getUrl(String baseUrl, String extraPath) {
|
||||
if (StringUtils.isBlank(extraPath)) {
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
if (StringUtils.contains(extraPath, "http://") || StringUtils.contains(extraPath, "https://")) {
|
||||
return extraPath;
|
||||
}
|
||||
|
||||
String targetUrl = baseUrl;
|
||||
if (!targetUrl.endsWith("/")) {
|
||||
targetUrl += "/";
|
||||
}
|
||||
if (extraPath.startsWith("/")) {
|
||||
targetUrl += extraPath.substring(1);
|
||||
} else {
|
||||
targetUrl += extraPath;
|
||||
}
|
||||
|
||||
logger.debug("HttpClientAdapterServiceRest] after concatenate url=[" + targetUrl + "] ");
|
||||
return targetUrl;
|
||||
}
|
||||
}
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
//
|
||||
//package com.eactive.eai.adapter.http.client.impl;
|
||||
//
|
||||
//import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
//import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
//import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
//import com.eactive.eai.common.util.CommonLib;
|
||||
//import org.apache.commons.httpclient.HttpClient;
|
||||
//import org.apache.commons.httpclient.HttpStatus;
|
||||
//import org.apache.commons.httpclient.HttpVersion;
|
||||
//import org.apache.commons.httpclient.methods.PostMethod;
|
||||
//
|
||||
//import java.net.ConnectException;
|
||||
//import java.net.SocketTimeoutException;
|
||||
//import java.util.Properties;
|
||||
//
|
||||
//
|
||||
///**
|
||||
//* 1. 기능 : EAI HTTP OutBound 용 어댑터로 수동 시스템의 HTTP 웹 컴포넌트를
|
||||
//* GET/POST 방식으로 호출할 수 있는 기능을 제공한다.
|
||||
//* 2. 처리 개요 :
|
||||
//* * - 2009.11.24 retry 로직 제거 : 요청
|
||||
//* 3. 주의사항
|
||||
//*
|
||||
//* @author :
|
||||
//* @version : v 1.0.0
|
||||
//* @see : WLIAdapterFactory.java, WLIAdapter.java, WLIDefaultAdapter.java
|
||||
//* @since :
|
||||
//
|
||||
//*/
|
||||
//public class HttpClientAdapterServiceAnnounce extends HttpClientAdapterServiceSupport{
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 전문과 같은 Raw Data 통신에 사용
|
||||
// * 2. 처리 개요 :
|
||||
// * - 속성 정보를 설정 하고 수동 시스템 서비스를 호출 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param prop Http Adapter 속성 정보
|
||||
// * @param message 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// public Object execute(Properties prop, byte[] data, Properties tempProp) throws Exception {
|
||||
//
|
||||
// HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
//
|
||||
// if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
// logger.debug("HttpClientAdapterServiceAnnounce] SEND (" + vo.getMethod() + ") ");
|
||||
// }
|
||||
// PostMethod method = new PostMethod(vo.getUrl());
|
||||
// if (vo.getTimeout() > -1) {
|
||||
// method.getParams().setSoTimeout(vo.getTimeout());
|
||||
// }
|
||||
// HttpClient mclient = this.client;
|
||||
// if ( vo.getConnectionTimeout() != 0){
|
||||
// mclient.getParams().setParameter("http.connection.timeout", vo.getConnectionTimeout());
|
||||
// }
|
||||
// method.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0);
|
||||
//
|
||||
// // Announce Data Size : 113 Bytes
|
||||
// int annMinSize = 113;
|
||||
//
|
||||
// String srvCode = "";
|
||||
// String recipient = "";
|
||||
// String body = "";
|
||||
// String inurl = "";
|
||||
//
|
||||
// if (data.length >= annMinSize) {
|
||||
// if (vo.getEncode() != null && vo.getEncode().length() > 0) {
|
||||
// if (logger.isDebug()) {
|
||||
// logger.debug("HttpClientAdapterServiceAnnounce] ENCODE(" + vo.getEncode() + ") ");
|
||||
// }
|
||||
// srvCode = new String(data, 0, 6, vo.getEncode());
|
||||
// recipient = new String(data, 6, 7, vo.getEncode());
|
||||
// body = new String(data, 13, 100, vo.getEncode());
|
||||
// if (data.length > 153) {
|
||||
// inurl = new String(data, 113, data.length - annMinSize, vo.getEncode());
|
||||
// }
|
||||
// } else {
|
||||
// srvCode = new String(data, 0, 6);
|
||||
// recipient = new String(data, 6, 7);
|
||||
// body = new String(data, 13, 100);
|
||||
// if (data.length > 153) {
|
||||
// inurl = new String(data, 113, data.length - annMinSize);
|
||||
// }
|
||||
// }
|
||||
// method.addParameter("SRV_CODE", srvCode);
|
||||
// method.addParameter("RECIPIENT", recipient);
|
||||
// method.addParameter("BODY", body);
|
||||
// if (data.length > 153) {
|
||||
// method.addParameter("URL", inurl);
|
||||
// }
|
||||
//
|
||||
// } else {
|
||||
// if (logger.isError()) {
|
||||
// logger.error("HttpClientAdapterServiceAnnounce] Invalid Data Length (" + data.length + ") < " + annMinSize);
|
||||
// }
|
||||
// throw new Exception("HttpClientAdapterServiceAnnounce Error : MessageSize(" + (data.length) + " Bytes) too small < " + annMinSize);
|
||||
// }
|
||||
//
|
||||
// int status = -1;
|
||||
//
|
||||
// try {
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// if (data.length > 153){
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(),
|
||||
// "[URL]"+ inurl + "\n"
|
||||
// + "[SRV_CODE]" + srvCode + "\n"
|
||||
// + "[RECIPIENT]" + recipient + "\n"
|
||||
// + "[BODY]" + body + "\n"
|
||||
// + "SEND ["+new String(data)+"]"+CommonLib.getDumpMessage(data) +"\n"
|
||||
// );
|
||||
// }else{
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(),
|
||||
// "[SRV_CODE]" + srvCode + "\n"
|
||||
// + "[RECIPIENT]" + recipient + "\n"
|
||||
// + "[BODY]" + body + "\n"
|
||||
// + "SEND ["+new String(data)+"]"+CommonLib.getDumpMessage(data) +"\n"
|
||||
// );
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// //mclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
|
||||
// //mclient.getParams().setContentCharset("EUC_KR");
|
||||
// status = mclient.executeMethod(method);
|
||||
//
|
||||
// //status 200(정상) 이외에는 error 처리
|
||||
// if (status !=200){
|
||||
// try{
|
||||
// logger.error("HttpClientAdapterServiceAnnounce] receive status["+status+"] data[" + new String(method.getResponseBody()) +"]");
|
||||
// }catch(Exception e){
|
||||
// logger.error(e);
|
||||
// }
|
||||
// throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="+vo.getAdapterGroupName()+"."+vo.getAdapterName(),status);
|
||||
// }
|
||||
// byte[] responseMessage = method.getResponseBody();
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(), "RECV ["+new String(responseMessage)+"]"+CommonLib.getDumpMessage(responseMessage) );
|
||||
// }
|
||||
// return responseMessage;
|
||||
//
|
||||
// } catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServiceAnnounce] SocketTimeoutException : " + ste.toString() ,ste);
|
||||
// }
|
||||
// if (logger.isError()){
|
||||
// logger.error("HttpClientAdapterServiceAnnounce] SocketTimeoutException : " + ste.toString() ,ste);
|
||||
// }
|
||||
// throw ste;
|
||||
// } catch(ConnectException ce) {
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServiceAnnounce] Connection Exception : " + ce.toString() ,ce);
|
||||
// }
|
||||
// if (logger.isError()){
|
||||
// logger.error("HttpClientAdapterServiceAnnounce] Connection Exception : " + ce.toString() ,ce);
|
||||
// }
|
||||
// throw ce;
|
||||
// } catch(Exception e) {
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServiceAnnounce] Exception : " + e.toString() ,e);
|
||||
// }
|
||||
// if (logger.isError()){
|
||||
// logger.error("HttpClientAdapterServiceAnnounce] Exception : " + e.toString() ,e);
|
||||
// }
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if (status != HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
// if (logger.isDebug()) {
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
//}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
package com.eactive.eai.adapter.http.client.impl;
|
||||
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.HttpVersion;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
import org.apache.commons.httpclient.params.HttpConnectionParams;
|
||||
import org.apache.commons.httpclient.protocol.Protocol;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
|
||||
import com.eactive.eai.adapter.Keys;
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
import com.eactive.eai.adapter.http.secure.CustomHttpsSocketFactory;
|
||||
import com.eactive.eai.adapter.http.secure.EasySSLProtocolSocketFactory;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
|
||||
public class HttpClientAdapterServiceBody extends HttpClientAdapterServiceSupport {
|
||||
public Object execute(Properties prop, Object data, Properties tempProp) throws Exception {
|
||||
HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
|
||||
boolean isAsync = false;
|
||||
if (RESPONSE_TYPE_ASYNC.equals(vo.getResponseType())) {
|
||||
isAsync = true;
|
||||
}
|
||||
PostMethod method = new PostMethod(vo.getUrl());
|
||||
HttpClient mclient = this.client;
|
||||
|
||||
configureHttpClient(mclient, method, vo, vo.getUrl(), vo.getContentType());
|
||||
|
||||
String sendData = "";
|
||||
if (data instanceof String) {
|
||||
sendData = (String) data;
|
||||
} else if (data instanceof byte[]) {
|
||||
sendData = new String((byte[]) data, vo.getEncode());
|
||||
}
|
||||
|
||||
mclient.getParams().setContentCharset(vo.getEncode());
|
||||
|
||||
String contentTypeValue = null;
|
||||
if (StringUtils.isNotEmpty(vo.getContentType())) {
|
||||
if (StringUtils.isNotEmpty(vo.getEncode())) {
|
||||
contentTypeValue = vo.getContentType() + "; charset=" + vo.getEncode();
|
||||
} else {
|
||||
contentTypeValue = vo.getContentType();
|
||||
}
|
||||
method.setRequestHeader(CONTENT_TYPE_KEY, contentTypeValue);
|
||||
}
|
||||
if (vo.getAdapterHeaders() != null) {
|
||||
for (String key : vo.getAdapterHeaders().keySet()) {
|
||||
method.addRequestHeader(key, vo.getAdapterHeaders().get(key));
|
||||
}
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("HttpClientAdapterServiceBody] " + CONTENT_TYPE_KEY + "(" + contentTypeValue + ")");
|
||||
}
|
||||
|
||||
if ("Y".equals(vo.getUrlEncodeYn())) {
|
||||
sendData = java.net.URLEncoder.encode(sendData);
|
||||
}
|
||||
|
||||
method.setRequestBody(sendData);
|
||||
|
||||
int status = -1;
|
||||
|
||||
try {
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug(
|
||||
"HttpClientAdapterServiceBody] SEND (" + vo.getAdapterGroupName() + ") = [" + sendData + "]");
|
||||
logger.debug("HttpClientAdapterServiceBody] SEND (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"SEND [" + sendData + "]" + CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
if (vo.getUrl() != null && vo.getUrl().startsWith("https")) {
|
||||
Protocol.registerProtocol("https",
|
||||
new Protocol("https", new CustomHttpsSocketFactory(new EasySSLProtocolSocketFactory()), 443));
|
||||
}
|
||||
status = mclient.executeMethod(method);
|
||||
stopWatch.stop();
|
||||
|
||||
String responseCharSet = getResponseCharSet(method.getResponseCharSet(), vo.getAdapterGroupName());
|
||||
|
||||
byte[] responseMessage = method.getResponseBody();
|
||||
// status 200(정상) 이외에는 error 처리
|
||||
if (status != 200) {
|
||||
logger.error("HttpClientAdapterServiceBody] receive status[" + status + "] data["
|
||||
+ new String(responseMessage) + "]");
|
||||
throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="
|
||||
+ vo.getAdapterGroupName() + "." + vo.getAdapterName(), status);
|
||||
}
|
||||
|
||||
if (isAsync && stopWatch.getTime() > vo.getSlowTranTime() && (logger.isWarn())) {
|
||||
logger.warn("HttpClientAdapterServiceBody] URL[" + vo.getUrl() + "] lazy async response time = "
|
||||
+ stopWatch.toString() + " responseMessage=" + new String(responseMessage, responseCharSet));
|
||||
|
||||
}
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"RECV [" + new String(responseMessage, vo.getEncode()) + "]"
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
return responseMessage;
|
||||
|
||||
} catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceBody] SocketTimeoutException : " + ste.toString(), ste);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceBody] SocketTimeoutException : " + ste.toString(), ste);
|
||||
}
|
||||
throw ste;
|
||||
} catch (ConnectException ce) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceBody] Connection Exception : " + ce.toString(), ce);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceBody] Connection Exception : " + ce.toString(), ce);
|
||||
}
|
||||
throw ce;
|
||||
} catch (Exception e) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceBody] Exception : " + e.toString(), e);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceBody] Exception : " + e.toString(), e);
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
method.releaseConnection();
|
||||
if (status != HttpStatus.SC_OK) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = String.valueOf(status);
|
||||
String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug(resMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+667
@@ -0,0 +1,667 @@
|
||||
package com.eactive.eai.adapter.http.client.impl;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.BitSet;
|
||||
import java.util.Formatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.httpclient.Header;
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpMethodBase;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
import org.apache.commons.httpclient.methods.PutMethod;
|
||||
import org.apache.commons.httpclient.protocol.Protocol;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import com.eactive.eai.adapter.http.EntityEnclosingDeleteMethod;
|
||||
import com.eactive.eai.adapter.http.EntityEnclosingGetMethod;
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpMethodType;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceKey;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceKey;
|
||||
import com.eactive.eai.adapter.http.dynamic.impl.HttpAdapterServiceBypass;
|
||||
import com.eactive.eai.adapter.http.secure.CustomHttpsSocketFactory;
|
||||
import com.eactive.eai.adapter.http.secure.EasySSLProtocolSocketFactory;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.jayway.jsonpath.DocumentContext;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import com.openbanking.eai.common.token.AccessTokenManager;
|
||||
import com.openbanking.eai.common.token.AccessTokenVO;
|
||||
import com.openbanking.eai.common.token.OAuth2AccessTokenVO;
|
||||
|
||||
public class HttpClientAdapterServiceBypass extends HttpClientAdapterServiceSupport
|
||||
implements HttpClientAdapterServiceKey {
|
||||
public static final String TYPE_BYPASS_REQUEST = "bypassRequest";
|
||||
public static final String REST_OPTION = "REST_OPTION";
|
||||
|
||||
protected boolean doSendUrlFragment = true;
|
||||
protected boolean doHandleCompression = false;
|
||||
protected boolean doForwardIP = true;
|
||||
|
||||
@SuppressWarnings({ "deprecation", "unchecked" })
|
||||
public Object execute(Properties prop, Object data, Properties tempProp) throws Exception {
|
||||
HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
|
||||
boolean useAdapterToken = StringUtils.equalsIgnoreCase(prop.getProperty("ADAPTER_TOKEN_USE_YN", "N"), "Y");
|
||||
|
||||
// ex) $.dataHeader.GW_RSLT_CD
|
||||
String tokenErrorCodeKey = prop.getProperty("ADAPTER_TOKEN_ERROR_CODE_KEY");
|
||||
String tokenErrorCodeValues = prop.getProperty("ADAPTER_TOKEN_ERROR_CODE_VALUES");
|
||||
String tokenErrorHttpStatusCode = prop.getProperty("ADAPTER_TOKEN_ERROR_HTTP_STATUS_CODE"); // 200 or 400번대
|
||||
|
||||
/**
|
||||
* @formatter:off
|
||||
* TSEAIHE02.RESTOPTION 정보 => JSON 형태로 구성
|
||||
* - type : simpleRequest, variableUrlRequest
|
||||
* - extraPath : 어댑터 프로퍼티 URL에 추가될 HTTP REST URL
|
||||
* - method : get, delete, post, put
|
||||
* - contentType : application/json, application/x-www-form-urlencoded
|
||||
* - adapterTokenUseYn: Y, N(default)
|
||||
* ex)
|
||||
* {"type":"variableUrlRequest","extraPath":"/aaa/{userId}","uriVariables":["userId"]}
|
||||
* @formatter:on
|
||||
*/
|
||||
String restOptionData = tempProp.getProperty(REST_OPTION, "{}");
|
||||
|
||||
JSONObject restOptionObject = parseJson(restOptionData);
|
||||
|
||||
String adapterTokenUseYn = (String) restOptionObject.get("adapterTokenUseYn");
|
||||
if (StringUtils.isNotBlank(adapterTokenUseYn)) { // interface 에 설정된게 우선한다.
|
||||
useAdapterToken = StringUtils.equalsIgnoreCase(adapterTokenUseYn, "Y");
|
||||
}
|
||||
|
||||
String inboundMethod = tempProp.getProperty(HttpAdapterServiceKey.INBOUND_METHOD, "POST");
|
||||
String inboundRewritePath = tempProp.getProperty(HttpAdapterServiceKey.INBOUND_REWRITE_PATH, "");
|
||||
String inboundQueryString = tempProp.getProperty(HttpAdapterServiceKey.INBOUND_QUERY_STRING, "");
|
||||
Properties inboundHeaders = (Properties) tempProp.get(HttpAdapterServiceKey.INBOUND_HEADER);
|
||||
Map<String, String> inboundPathVariables = (Map<String, String>) tempProp
|
||||
.get(HttpAdapterServiceKey.INBOUND_PATH_VARIABLES);
|
||||
|
||||
String restMethod = (String) restOptionObject.get("method");
|
||||
if (StringUtils.isBlank(restMethod)) {
|
||||
restMethod = inboundMethod;
|
||||
}
|
||||
|
||||
String url = getRewriteUrl(vo, inboundRewritePath, inboundQueryString, restOptionObject, inboundPathVariables);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpClientAdapterServiceBypass] url = [" + url + "]");
|
||||
}
|
||||
|
||||
HttpClient mclient = this.client;
|
||||
|
||||
HttpMethodBase method = generateMethod(restMethod, url);
|
||||
|
||||
assignRequestHeaders(method, inboundHeaders, tempProp);
|
||||
|
||||
if (hasBody(data, inboundHeaders)) {
|
||||
if (data instanceof byte[]) {
|
||||
((EntityEnclosingMethod) method).setRequestBody(new ByteArrayInputStream((byte[]) data));
|
||||
} else if (data instanceof String) {
|
||||
((EntityEnclosingMethod) method).setRequestBody((String) data);
|
||||
}
|
||||
}
|
||||
|
||||
String contentType = (String) restOptionObject.get("contentType");
|
||||
if (StringUtils.isBlank(contentType) && inboundHeaders != null) {
|
||||
contentType = getIgnoreCaseProp(inboundHeaders, HttpHeaders.CONTENT_TYPE);
|
||||
contentType = StringUtils.substringBefore(contentType, ";");
|
||||
}
|
||||
configureHttpClient(mclient, method, vo, url, contentType);
|
||||
|
||||
OAuth2AccessTokenVO accessToken = null;
|
||||
if (useAdapterToken) {
|
||||
AccessTokenManager tokenManager = AccessTokenManager.getInstance();
|
||||
accessToken = (OAuth2AccessTokenVO) tokenManager.getAccessTokenVO(vo.getAdapterGroupName());
|
||||
|
||||
// 토큰이 없거나 만료 됐으면 재발급
|
||||
if (accessToken == null || accessToken.isExpired()) {
|
||||
String oldToken = accessToken == null ? null : accessToken.getAccessToken();
|
||||
accessToken = (OAuth2AccessTokenVO) tokenManager.retryAccessTokenVO(vo.getAdapterGroupName(), prop,
|
||||
oldToken);
|
||||
}
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpClientAdapterServiceBypass] SEND (" + vo.getAdapterGroupName() + ") TOKEN = ["
|
||||
+ accessToken + "]");
|
||||
}
|
||||
|
||||
setAuthHeaders(method, accessToken);
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpClientAdapterServiceBypass] SEND (" + vo.getAdapterGroupName()
|
||||
+ ") RequestHeader [Authorization=" + method.getRequestHeader("Authorization") + "]");
|
||||
}
|
||||
}
|
||||
|
||||
int status = -1;
|
||||
|
||||
try {
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
byte[] responseMessage = null;
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"SEND [Bypass Request..]" + CommonLib.getDumpMessage(data));
|
||||
}
|
||||
try {
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("[method getName]" + method.getName());
|
||||
logger.debug("[method getRequestCharSet]" + method.getRequestCharSet());
|
||||
logger.debug("[method getRequestHeaders]" + method.getRequestHeaders().toString());
|
||||
logger.debug("[method getQueryString]" + method.getQueryString());
|
||||
logger.debug("[method getParams]" + method.getParams().toString());
|
||||
logger.debug("[method getURI]" + method.getURI());
|
||||
}
|
||||
if (vo.getUrl() != null && vo.getUrl().startsWith("https")) {
|
||||
Protocol.registerProtocol("https",
|
||||
new Protocol("https", new CustomHttpsSocketFactory(new EasySSLProtocolSocketFactory()), 443));
|
||||
}
|
||||
status = mclient.executeMethod(method);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("[received status]" + status);
|
||||
}
|
||||
} catch (ConnectException e) {
|
||||
responseMessage = method.getResponseBody();
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceBypass] responseType =" + vo.getResponseType());
|
||||
logger.debug("HttpClientAdapterServiceBypass] RECV (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
throw new Exception("excuteMethod java.net.ConnectException = " + e.getMessage());
|
||||
}
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
/**
|
||||
* @formatter:off
|
||||
* OAuth 토큰 응답 체크(Adapter properties로 설정)
|
||||
* 응답코드에 따라 유효한 토큰 확인(토큰 재발급 여부 확인)
|
||||
* API 서비스 마다 정책이 다름(보통 400대에서 체크하나 200에서도 체크할 수 있음)
|
||||
*
|
||||
* TOKEN_ERROR_CODE_KEY: 응답 json error code key
|
||||
* __ex) $.dataHeader.resultCode
|
||||
* TOKEN_ERROR_CODE_VALUES: 토큰 재발급이 필요한 응답 error codes(ex: O0001,O0002,O0003)
|
||||
* TOKEN_ERROR_HTTP_STATUS_CODE: 보통 400대에서 체크하나 API 제공자에 따라 200에서도 체클할수 있음
|
||||
* ex) TOKEN_ERROR_HTTP_STATUS_CODE = 200
|
||||
* @formatter:on
|
||||
*/
|
||||
boolean needReissue = false;
|
||||
|
||||
if (status == 200) {
|
||||
responseMessage = method.getResponseBody();
|
||||
if (useAdapterToken && StringUtils.equals(tokenErrorHttpStatusCode, "200")) {
|
||||
needReissue = checkTokenRetry(responseMessage, tokenErrorCodeKey, tokenErrorCodeValues,
|
||||
vo.getEncode());
|
||||
}
|
||||
} else if (status >= 400 && status < 500) {
|
||||
responseMessage = method.getResponseBody();
|
||||
if (useAdapterToken) {
|
||||
needReissue = checkTokenRetry(responseMessage, tokenErrorCodeKey, tokenErrorCodeValues,
|
||||
vo.getEncode());
|
||||
}
|
||||
|
||||
if (!needReissue) {
|
||||
// body 값이 없을때만 exception 처리하고, body가 있으면 bypass 한다.
|
||||
if (responseMessage == null || responseMessage.length == 0) {
|
||||
String errMsg = String.format(
|
||||
"http receive status fail value=%d adapterName=%s.%s uri=%s method=%s response Data=%s",
|
||||
status, vo.getAdapterGroupName(), vo.getAdapterName(), url, method, responseMessage);
|
||||
throw new Exception(errMsg);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
responseMessage = method.getResponseBody();
|
||||
// body 값이 없을때만 exception 처리하고, body가 있으면 bypass 한다.
|
||||
if (responseMessage == null || responseMessage.length == 0) {
|
||||
String errMsg = String.format(
|
||||
"http receive status fail value=%d adapterName=%s.%s uri=%s method=%s response Data=%s",
|
||||
status, vo.getAdapterGroupName(), vo.getAdapterName(), url, method, responseMessage);
|
||||
throw new Exception(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
if (useAdapterToken) {
|
||||
if (responseMessage == null) {
|
||||
throw new Exception("responseMessage is NULL, HttpStatus=" + status);
|
||||
}
|
||||
|
||||
// OAuth 토큰 재요청 코드 확인
|
||||
if (needReissue) {
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceBypass] retry access token response code = ["
|
||||
+ vo.getResponseType() + "] message = [" + new String(responseMessage, vo.getEncode())
|
||||
+ "]");
|
||||
}
|
||||
|
||||
// 토큰 재발급
|
||||
AccessTokenManager tokenManager = AccessTokenManager.getInstance();
|
||||
String oldToken = accessToken == null ? null : accessToken.getAccessToken();
|
||||
OAuth2AccessTokenVO newAccessToken = (OAuth2AccessTokenVO) tokenManager
|
||||
.retryAccessTokenVO(vo.getAdapterGroupName(), prop, oldToken);
|
||||
method.setRequestHeader("Authorization", newAccessToken.getAuthorization());
|
||||
|
||||
setAuthHeaders(method, newAccessToken);
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceBypass] SEND (" + vo.getAdapterGroupName()
|
||||
+ ") RETRY TOKEN = [" + newAccessToken + "]");
|
||||
}
|
||||
|
||||
try {
|
||||
status = mclient.executeMethod(method);
|
||||
} catch (ConnectException e) {
|
||||
throw new Exception("retry excuteMethod Exceptioin = " + e.getMessage());
|
||||
}
|
||||
|
||||
if (status == 200) {
|
||||
responseMessage = method.getResponseBody();
|
||||
} else {
|
||||
responseMessage = method.getResponseBody();
|
||||
// body 값이 없을때만 exception 처리하고, body가 있으면 bypass 한다.
|
||||
if (responseMessage == null || responseMessage.length == 0) {
|
||||
String errMsg = String.format(
|
||||
"http receive status fail value=%d adapterName=%s.%s uri=%s method=%s response Data=%s",
|
||||
status, vo.getAdapterGroupName(), vo.getAdapterName(), url, method,
|
||||
responseMessage);
|
||||
throw new Exception(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceBypass] RETRY responseType =" + vo.getResponseType());
|
||||
logger.debug("HttpClientAdapterServiceBypass] RETRY RECV (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"RECV [Bypass Request..]" + CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
assignRelayDataToInbound(tempProp, method.getResponseHeaders(), status);
|
||||
return responseMessage;
|
||||
} catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceBypass] SocketTimeoutException (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ste.toString(),
|
||||
ste);
|
||||
}
|
||||
logger.error("HttpClientAdapterServiceBypass] SocketTimeoutException (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ste.toString(), ste);
|
||||
throw ste;
|
||||
} catch (ConnectException ce) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceBypass] Connection Exception (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ce.toString(),
|
||||
ce);
|
||||
}
|
||||
logger.error("HttpClientAdapterServiceBypass] Connection Exception (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ce.toString(), ce);
|
||||
throw ce;
|
||||
} catch (Exception e) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(), e.toString(), e);
|
||||
}
|
||||
logger.error(
|
||||
"HttpClientAdapterServiceBypass] Exception (" + vo.getAdapterGroupName() + ") : " + e.toString(),
|
||||
e);
|
||||
throw e;
|
||||
} finally {
|
||||
method.releaseConnection();
|
||||
if (status != HttpStatus.SC_OK) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = String.valueOf(status);
|
||||
String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug(resMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasBody(Object data, Properties inboundHeaders) {
|
||||
if (data == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// // spec: RFC 2616, sec 4.3
|
||||
// String contentLength = getIgnoreCaseProp(inboundHeaders, HttpHeaders.CONTENT_LENGTH);
|
||||
// String transferEncoding = getIgnoreCaseProp(inboundHeaders, HttpHeaders.TRANSFER_ENCODING);
|
||||
// if (StringUtils.isNotBlank(contentLength) || StringUtils.isNotBlank(transferEncoding)) {
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
|
||||
// client adapter 단독으로 사용 가능하게 수정
|
||||
return true;
|
||||
}
|
||||
|
||||
private HttpMethodBase generateMethod(String restMethod, String url) {
|
||||
HttpMethodBase method = null;
|
||||
switch (HttpMethodType.getValue(restMethod)) {
|
||||
case GET:
|
||||
method = new EntityEnclosingGetMethod(url);
|
||||
break;
|
||||
case DELETE:
|
||||
method = new EntityEnclosingDeleteMethod(url);
|
||||
break;
|
||||
case POST:
|
||||
method = new PostMethod(url);
|
||||
break;
|
||||
case PUT:
|
||||
method = new PutMethod(url);
|
||||
break;
|
||||
default:
|
||||
// set default method
|
||||
method = new PostMethod(url);
|
||||
break;
|
||||
}
|
||||
return method;
|
||||
}
|
||||
|
||||
private String getRewriteUrl(HttpClientAdapterVO vo, String inboundRewritePath, String queryString,
|
||||
JSONObject restOptionObject, Map<String, String> inboundPathVariables) {
|
||||
String fragment = null;
|
||||
if (StringUtils.isNotBlank(queryString)) {
|
||||
int fragIdx = queryString.indexOf('#');
|
||||
if (fragIdx >= 0) {
|
||||
fragment = queryString.substring(fragIdx + 1);
|
||||
queryString = queryString.substring(0, fragIdx);
|
||||
}
|
||||
}
|
||||
|
||||
String restExtraPath = (String) restOptionObject.get("extraPath");
|
||||
if (StringUtils.isBlank(restExtraPath)) {
|
||||
StringBuilder uri = new StringBuilder(500);
|
||||
uri.append(vo.getUrl()).append(inboundRewritePath);
|
||||
|
||||
if (StringUtils.isNotBlank(queryString)) {
|
||||
uri.append('?');
|
||||
uri.append(encodeUriQuery(queryString, false));
|
||||
}
|
||||
|
||||
if (doSendUrlFragment && fragment != null) {
|
||||
uri.append('#');
|
||||
uri.append(encodeUriQuery(fragment, false));
|
||||
}
|
||||
|
||||
return uri.toString();
|
||||
} else {
|
||||
String type = (String) restOptionObject.get("type");
|
||||
String url = getUrl(vo.getUrl(), restExtraPath);
|
||||
|
||||
if (StringUtils.equalsIgnoreCase(type, HttpClientAdapterServiceRest.TYPE_VARIABLE_URL_REQUEST)) {
|
||||
inboundPathVariables = mergeInboundPathVariables(inboundPathVariables, queryString);
|
||||
List<String> urlVariableValueList = new ArrayList<>();
|
||||
JSONArray uriVariables = (JSONArray) restOptionObject.get("uriVariables");
|
||||
if (uriVariables != null && !uriVariables.isEmpty() && inboundPathVariables != null
|
||||
&& !inboundPathVariables.isEmpty()) {
|
||||
for (Object tempObject : uriVariables) {
|
||||
String urlVaribleId = (String) tempObject;
|
||||
String uriVariableValue = inboundPathVariables.get(urlVaribleId);
|
||||
urlVariableValueList.add(uriVariableValue);
|
||||
}
|
||||
|
||||
if (!urlVariableValueList.isEmpty()) {
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).build();
|
||||
Object[] urls = urlVariableValueList.toArray();
|
||||
url = uriComponents.expand(urls).toUriString();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (StringUtils.isNotBlank(queryString)) {
|
||||
url += "?" + encodeUriQuery(queryString, false);
|
||||
}
|
||||
|
||||
if (doSendUrlFragment && fragment != null) {
|
||||
url += "#" + encodeUriQuery(fragment, false);
|
||||
}
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> mergeInboundPathVariables(Map<String, String> inboundPathVariables,
|
||||
String queryString) {
|
||||
if (StringUtils.isBlank(queryString)) {
|
||||
return inboundPathVariables;
|
||||
}
|
||||
|
||||
if (inboundPathVariables == null) {
|
||||
inboundPathVariables = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
String[] pairs = queryString.split("&");
|
||||
for (String pair : pairs) {
|
||||
int idx = pair.indexOf("=");
|
||||
// URLDecoder.decode 필요??
|
||||
inboundPathVariables.put(pair.substring(0, idx), pair.substring(idx + 1));
|
||||
}
|
||||
|
||||
return inboundPathVariables;
|
||||
}
|
||||
|
||||
private String getUrl(String baseUrl, String extraPath) {
|
||||
if (StringUtils.isBlank(extraPath)) {
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
if (StringUtils.contains(extraPath, "http://") || StringUtils.contains(extraPath, "https://")) {
|
||||
return extraPath;
|
||||
}
|
||||
|
||||
String targetUrl = baseUrl;
|
||||
if (!targetUrl.endsWith("/")) {
|
||||
targetUrl += "/";
|
||||
}
|
||||
if (extraPath.startsWith("/")) {
|
||||
targetUrl += extraPath.substring(1);
|
||||
} else {
|
||||
targetUrl += extraPath;
|
||||
}
|
||||
|
||||
return targetUrl;
|
||||
}
|
||||
|
||||
protected CharSequence encodeUriQuery(CharSequence in, boolean encodePercent) {
|
||||
// Note that I can't simply use URI.java to encode because it will escape
|
||||
// pre-existing escaped things.
|
||||
StringBuilder outBuf = null;
|
||||
Formatter formatter = null;
|
||||
for (int i = 0; i < in.length(); i++) {
|
||||
char c = in.charAt(i);
|
||||
boolean escape = true;
|
||||
if (c < 128) {
|
||||
if (asciiQueryChars.get(c) && !(encodePercent && c == '%')) {
|
||||
escape = false;
|
||||
}
|
||||
} else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {// not-ascii
|
||||
escape = false;
|
||||
}
|
||||
if (!escape) {
|
||||
if (outBuf != null)
|
||||
outBuf.append(c);
|
||||
} else {
|
||||
// escape
|
||||
if (outBuf == null) {
|
||||
outBuf = new StringBuilder(in.length() + 5 * 3);
|
||||
outBuf.append(in, 0, i);
|
||||
formatter = new Formatter(outBuf);
|
||||
}
|
||||
// leading %, 0 padded, width 2, capital hex
|
||||
formatter.format("%%%02X", (int) c);// TODO
|
||||
}
|
||||
}
|
||||
return outBuf != null ? outBuf : in;
|
||||
}
|
||||
|
||||
protected static final BitSet asciiQueryChars;
|
||||
static {
|
||||
char[] c_unreserved = "_-!.~'()*".toCharArray();// plus alphanum
|
||||
char[] c_punct = ",;:$&+=".toCharArray();
|
||||
char[] c_reserved = "/@".toCharArray();// plus punct. Exclude '?'; RFC-2616 3.2.2. Exclude '[', ']';
|
||||
// https://www.ietf.org/rfc/rfc1738.txt, unsafe characters
|
||||
asciiQueryChars = new BitSet(128);
|
||||
for (char c = 'a'; c <= 'z'; c++)
|
||||
asciiQueryChars.set(c);
|
||||
for (char c = 'A'; c <= 'Z'; c++)
|
||||
asciiQueryChars.set(c);
|
||||
for (char c = '0'; c <= '9'; c++)
|
||||
asciiQueryChars.set(c);
|
||||
for (char c : c_unreserved)
|
||||
asciiQueryChars.set(c);
|
||||
for (char c : c_punct)
|
||||
asciiQueryChars.set(c);
|
||||
for (char c : c_reserved)
|
||||
asciiQueryChars.set(c);
|
||||
|
||||
asciiQueryChars.set('%');// leave existing percent escapes in place
|
||||
}
|
||||
|
||||
private void assignRelayDataToInbound(Properties prop, Header[] responseHeaders, int status) {
|
||||
if (responseHeaders == null || responseHeaders.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
Properties headerProp = new Properties();
|
||||
for (Header header : responseHeaders) {
|
||||
headerProp.put(header.getName(), header.getValue());
|
||||
}
|
||||
|
||||
headerProp.put(HttpAdapterServiceBypass.HTTP_STATUS, String.valueOf(status));
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put(HttpAdapterServiceKey.OUTBOUND_RESPONSE_HEADERS, headerProp);
|
||||
|
||||
prop.put(HttpAdapterServiceKey.OUTBOUND_PROPERTY_MAP, map);
|
||||
}
|
||||
|
||||
private void setAuthHeaders(HttpMethodBase method, OAuth2AccessTokenVO accessToken) throws Exception {
|
||||
method.setRequestHeader("Authorization",
|
||||
String.format("%s %s", AccessTokenVO.BEARER_TYPE, accessToken.getAccessToken()));
|
||||
}
|
||||
|
||||
private boolean checkTokenRetry(byte[] responseMessage, String tokenErrorCodeKey, String tokenErrorCodeValues,
|
||||
String encode) {
|
||||
if (responseMessage == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(tokenErrorCodeKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(tokenErrorCodeValues)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
DocumentContext jsonContext = JsonPath.parse(new String(responseMessage, encode));
|
||||
String responseCode = jsonContext.read(tokenErrorCodeKey);
|
||||
if (StringUtils.isBlank(responseCode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] arr = org.springframework.util.StringUtils.tokenizeToStringArray(tokenErrorCodeValues, ",");
|
||||
return ArrayUtils.contains(arr, responseCode);
|
||||
} catch (Exception e) {
|
||||
logger.error("HttpClientAdapterServiceBypass] checkTokenRetry error=" + e.getMessage());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void assignRequestHeaders(HttpMethodBase method, Properties headerProp, Properties inProp) {
|
||||
if (headerProp == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Entry<Object, Object> e : headerProp.entrySet()) {
|
||||
String key = (String) e.getKey();
|
||||
String value = (String) e.getValue();
|
||||
|
||||
if (StringUtils.equalsAnyIgnoreCase(key, HttpAdapterServiceBypass.HOP_BY_HOP_HEADERS)
|
||||
|| StringUtils.equalsIgnoreCase(key, HttpHeaders.CONTENT_LENGTH)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (doHandleCompression && StringUtils.equalsIgnoreCase(key, HttpHeaders.ACCEPT_ENCODING)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
method.setRequestHeader(key, value);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Request Header :" + key + "=[" + value + "]");
|
||||
}
|
||||
}
|
||||
|
||||
if (doForwardIP) {
|
||||
String forHeaderName = "X-Forwarded-For";
|
||||
String forHeader = inProp.getProperty(HttpAdapterServiceKey.INBOUND_REMOTE_ADDR);
|
||||
if (StringUtils.isNotBlank(forHeader)) {
|
||||
String existingForHeader = headerProp.getProperty(forHeaderName);
|
||||
if (existingForHeader != null) {
|
||||
forHeader = existingForHeader + ", " + forHeader;
|
||||
}
|
||||
method.setRequestHeader(forHeaderName, forHeader);
|
||||
}
|
||||
|
||||
String protoHeaderName = "X-Forwarded-Proto";
|
||||
String protoHeader = inProp.getProperty(HttpAdapterServiceKey.INBOUND_SCHEME);
|
||||
if (StringUtils.isNotBlank(protoHeader)) {
|
||||
method.setRequestHeader(protoHeaderName, protoHeader);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private JSONObject parseJson(String message) throws Exception {
|
||||
if (message == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (JSONObject) JSONValue.parse(message);
|
||||
}
|
||||
|
||||
public static final String getIgnoreCaseProp(Properties prop, String key) {
|
||||
if (prop == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (Entry<Object, Object> e : prop.entrySet()) {
|
||||
if (StringUtils.equalsIgnoreCase(key, (String) e.getKey())) {
|
||||
return (String) e.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
//
|
||||
//package com.eactive.eai.adapter.http.client.impl;
|
||||
//
|
||||
//import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
//import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
//import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
//import com.eactive.eai.common.util.CommonLib;
|
||||
//import org.apache.commons.httpclient.HttpClient;
|
||||
//import org.apache.commons.httpclient.HttpStatus;
|
||||
//import org.apache.commons.httpclient.HttpVersion;
|
||||
//import org.apache.commons.httpclient.methods.PostMethod;
|
||||
//
|
||||
//import java.net.ConnectException;
|
||||
//import java.net.SocketTimeoutException;
|
||||
//import java.util.Properties;
|
||||
//
|
||||
//
|
||||
///**
|
||||
//* 1. 기능 : EAI HTTP OutBound 용 어댑터로 수동 시스템의 HTTP 웹 컴포넌트를
|
||||
//* GET/POST 방식으로 호출할 수 있는 기능을 제공한다.
|
||||
//* 2. 처리 개요 :
|
||||
//* * - 2009.11.24 retry 로직 제거 : 요청
|
||||
//* 3. 주의사항
|
||||
//*
|
||||
//* @author :
|
||||
//* @version : v 1.0.0
|
||||
//* @see : WLIAdapterFactory.java, WLIAdapter.java, WLIDefaultAdapter.java
|
||||
//* @since :
|
||||
//
|
||||
//*/
|
||||
//public class HttpClientAdapterServiceChangedate extends HttpClientAdapterServiceSupport{
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 전문과 같은 Raw Data 통신에 사용
|
||||
// * 2. 처리 개요 :
|
||||
// * - 속성 정보를 설정 하고 수동 시스템 서비스를 호출 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param prop Http Adapter 속성 정보
|
||||
// * @param message 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// public Object execute(Properties prop, byte[] data, Properties tempProp) throws Exception {
|
||||
//
|
||||
// HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
//
|
||||
// if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
// logger.debug("HttpClientAdapterServiceChangedate] SEND (" + vo.getMethod() + ") ");
|
||||
// }
|
||||
// PostMethod method = new PostMethod(vo.getUrl());
|
||||
// if (vo.getTimeout() > -1) {
|
||||
// method.getParams().setSoTimeout(vo.getTimeout());
|
||||
// }
|
||||
// HttpClient mclient = this.client;
|
||||
// if ( vo.getConnectionTimeout() != 0){
|
||||
// mclient.getParams().setParameter("http.connection.timeout", vo.getConnectionTimeout());
|
||||
// }
|
||||
// method.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0);
|
||||
//
|
||||
// // Data Size : 15 Bytes
|
||||
// int popupMinSize = 15;
|
||||
//
|
||||
// String req = "";
|
||||
// String date = "";
|
||||
//
|
||||
// if (data.length >= popupMinSize) {
|
||||
// if (vo.getEncode() != null && vo.getEncode().length() > 0) {
|
||||
// if (logger.isDebug()) {
|
||||
// logger.debug("HttpClientAdapterServiceChangedate] ENCODE(" + vo.getEncode() + ") ");
|
||||
// }
|
||||
// req = new String(data, 0, 7, vo.getEncode()).trim();
|
||||
// date = new String(data, 7, 8, vo.getEncode());
|
||||
// } else {
|
||||
// req = new String(data, 0, 7 ).trim();
|
||||
// date = new String(data, 7, 8 );
|
||||
// }
|
||||
// method.addParameter("req", req);
|
||||
// method.addParameter("date", date);
|
||||
//
|
||||
// } else {
|
||||
// if (logger.isError()) {
|
||||
// logger.error("HttpClientAdapterServiceChangedate] Invalid Data Length (" + data.length + ") < "
|
||||
// + popupMinSize);
|
||||
// }
|
||||
// throw new Exception("HttpClientAdapterServiceChangedate Error : MessageSize(" + (data.length)
|
||||
// + " Bytes) too small < " + popupMinSize);
|
||||
// }
|
||||
//
|
||||
// int status = -1;
|
||||
//
|
||||
// try {
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(),
|
||||
// "[req]"+ req + "\n"
|
||||
// + "[date]" + date + "\n"
|
||||
// + "SEND ["+new String(data)+"]"+CommonLib.getDumpMessage(data) +"\n"
|
||||
// );
|
||||
// }
|
||||
// //mclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
|
||||
// //mclient.getParams().setContentCharset("EUC_KR");
|
||||
// status = mclient.executeMethod(method);
|
||||
// //status 200(정상) 이외에는 error 처리
|
||||
// if (status !=200){
|
||||
// try{
|
||||
// logger.error("HttpClientAdapterServiceChangedate] receive status["+status+"] data[" + new String(method.getResponseBody()) +"]");
|
||||
// }catch(Exception e){
|
||||
// logger.error(e);
|
||||
// }
|
||||
// throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="+vo.getAdapterGroupName()+"."+vo.getAdapterName(),status);
|
||||
// }
|
||||
// byte[] responseMessage = method.getResponseBody();
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(), "RECV ["+new String(responseMessage)+"]"+CommonLib.getDumpMessage(responseMessage) );
|
||||
// }
|
||||
// return responseMessage;
|
||||
//
|
||||
// } catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient// 3.1일 경우
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServiceChangedate] SocketTimeoutException : " + ste.toString() ,ste);
|
||||
// }
|
||||
// if (logger.isError()){
|
||||
// logger.error("HttpClientAdapterServiceChangedate] SocketTimeoutException : " + ste.toString() ,ste);
|
||||
// }
|
||||
// throw ste;
|
||||
// } catch(ConnectException ce) {
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServiceChangedate] Connection Exception : " + ce.toString() ,ce);
|
||||
// }
|
||||
// if (logger.isError()){
|
||||
// logger.error("HttpClientAdapterServiceChangedate] Connection Exception : " + ce.toString() ,ce);
|
||||
// }
|
||||
// throw ce;
|
||||
// } catch(Exception e) {
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServiceChangedate] Exception : " + e.toString() ,e);
|
||||
// }
|
||||
// if (logger.isError()){
|
||||
// logger.error("HttpClientAdapterServiceChangedate] Exception : " + e.toString() ,e);
|
||||
// }
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if (status != HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
// if (logger.isDebug()) {
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//}
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
//
|
||||
//package com.eactive.eai.adapter.http.client.impl;
|
||||
//
|
||||
//import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
//import com.eactive.eai.common.util.CommonLib;
|
||||
//
|
||||
//import java.util.Hashtable;
|
||||
//import java.util.Map;
|
||||
//import java.util.Properties;
|
||||
//
|
||||
////import k4m.eCross.client.Context;
|
||||
//
|
||||
//
|
||||
///**
|
||||
//* 1. 기능 : EAI HTTP OutBound 용 어댑터로 수동 시스템의 HTTP 웹 컴포넌트를
|
||||
//* GET/POST 방식으로 호출할 수 있는 기능을 제공한다.
|
||||
//* 2. 처리 개요 :
|
||||
//* * - 2009.11.24 retry 로직 제거 : 요청
|
||||
//* 3. 주의사항
|
||||
//*
|
||||
//* @author :
|
||||
//* @version : v 1.0.0
|
||||
//* @see : WLIAdapterFactory.java, WLIAdapter.java, WLIDefaultAdapter.java
|
||||
//* @since :
|
||||
//
|
||||
//*/
|
||||
//public class HttpClientAdapterServiceExtorg extends HttpClientAdapterServiceSupport{
|
||||
//
|
||||
// @Override
|
||||
// protected void init(){
|
||||
// ;
|
||||
// }
|
||||
// /**
|
||||
// * 1. 기능 : 전문과 같은 Raw Data 통신에 사용
|
||||
// * 2. 처리 개요 :
|
||||
// * - 속성 정보를 설정 하고 수동 시스템 서비스를 호출 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param prop Http Adapter 속성 정보
|
||||
// * @param message 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// public Object execute(Properties prop, byte[] data, Properties tempProp) throws Exception
|
||||
// {
|
||||
// HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
// String msgNumber = "";
|
||||
// String ReceiverID = "";
|
||||
//
|
||||
// if (logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapterServiceExtorg: Body Size] ["+data.length+"]");
|
||||
// }
|
||||
//
|
||||
// if(data.length > 23){
|
||||
// msgNumber = new String(data, 19, 4);
|
||||
// if (logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapterServiceExtorg: msgNumber] ["+msgNumber+"]");
|
||||
// }
|
||||
// //String ReceiverID = "770000";
|
||||
//
|
||||
// /* 20081124 add MP at KIBO start------------------------------------*/
|
||||
// /* ReceiverID extract Message */
|
||||
// ReceiverID = new String(data, 12, 7).trim();
|
||||
// /* 20081124 add MP at KIBO start------------------------------------*/
|
||||
//
|
||||
// if (logger.isDebug()){
|
||||
// logger.debug("HttpClientAdapterServiceExtorg: ReceiverID] ["+ReceiverID+"]");
|
||||
// }
|
||||
// }else{
|
||||
// logger.warn("Invalid message length - Length["+data.length+"]");
|
||||
// }
|
||||
//
|
||||
//
|
||||
// String url = prop.getProperty(URL);
|
||||
//
|
||||
// //flat데이타
|
||||
// String inputLine = " "+new String(data);
|
||||
// String outputString = "";
|
||||
//
|
||||
//
|
||||
// try
|
||||
// {
|
||||
// Map inputStream = new Hashtable();
|
||||
// Map outStream = new Hashtable();
|
||||
// String serviceName = "MPS.Send.FlatSender";
|
||||
// inputStream.put("MessageNumber",msgNumber);
|
||||
// inputStream.put("ReceiverID",ReceiverID);
|
||||
// // 트랜젝션 ID 15바이트를 처리
|
||||
// inputStream.put("FlatStream",inputLine);
|
||||
//
|
||||
//
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug( "[serviceName]"+ serviceName + "\n"
|
||||
// + "[MessageNumber]" + msgNumber + "\n"
|
||||
// + "[ReceiverID]" + ReceiverID + "\n"
|
||||
// + "[FlatStream]" + inputLine + "\n"
|
||||
// + "SEND ["+new String(data)+"]"+CommonLib.getDumpMessage(data) +"\n");
|
||||
// }
|
||||
//
|
||||
// try{
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(),
|
||||
// "[serviceName]"+ serviceName + "\n"
|
||||
// + "[MessageNumber]" + msgNumber + "\n"
|
||||
// + "[ReceiverID]" + ReceiverID + "\n"
|
||||
// + "[FlatStream]" + inputLine + "\n"
|
||||
// + "SEND ["+new String(data)+"]"+CommonLib.getDumpMessage(data) +"\n"
|
||||
// );
|
||||
// }
|
||||
// //eCross Partner 주소
|
||||
//// Context ctx = new Context(url,"demo","demo");
|
||||
//// outStream = ctx.CallService(serviceName,inputStream);
|
||||
// //결과전문
|
||||
// outputString = (String)outStream.get("ReplyStream");
|
||||
//
|
||||
// // 트렌젝션 ID 15바이트를 처리
|
||||
// try {
|
||||
// outputString = outputString.substring(15);
|
||||
// }catch(Exception ex){
|
||||
// logger.warn("substring error", ex);
|
||||
// }
|
||||
//
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(),
|
||||
// "RECV ["+outputString+"]"+CommonLib.getDumpMessage(outputString.getBytes()) +"\n"
|
||||
// );
|
||||
// }
|
||||
// }catch(Exception ee){
|
||||
// if(logger.isError()){
|
||||
// logger.error("HttpClientAdapterServiceExtorg] Exception"+ ee.getMessage(),ee);
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// if(logger.isError()){
|
||||
// logger.error("HttpClientAdapterServiceExtorg] Exception"+ e.getMessage(),e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug("RECV ["+outputString+"]"+CommonLib.getDumpMessage(outputString.getBytes()) +"\n");
|
||||
// }
|
||||
//
|
||||
// if (outputString == null){
|
||||
// throw new Exception("HttpClientAdapterServiceExtorg ] execute - input message parse fail");
|
||||
// }
|
||||
//
|
||||
// return outputString.getBytes();
|
||||
// }
|
||||
//
|
||||
//}
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
|
||||
package com.eactive.eai.adapter.http.client.impl;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterGroupVO;
|
||||
import com.eactive.eai.adapter.AdapterManager;
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
import com.eactive.eai.adapter.http.secure.CustomHttpsSocketFactory;
|
||||
import com.eactive.eai.adapter.http.secure.EasySSLProtocolSocketFactory;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.eactive.eai.common.util.MessageUtil;
|
||||
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.HttpVersion;
|
||||
import org.apache.commons.httpclient.methods.GetMethod;
|
||||
import org.apache.commons.httpclient.params.HttpConnectionParams;
|
||||
import org.apache.commons.httpclient.protocol.Protocol;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Properties;
|
||||
|
||||
public class HttpClientAdapterServiceGet extends HttpClientAdapterServiceSupport {
|
||||
public Object execute(Properties prop, Object data, Properties tempProp) throws Exception {
|
||||
HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
boolean isAsync = false;
|
||||
|
||||
GetMethod method = new GetMethod(vo.getUrl());
|
||||
HttpClient mclient = this.client;
|
||||
|
||||
configureHttpClient(mclient, method, vo, vo.getUrl(), vo.getContentType());
|
||||
|
||||
String sendData = "";
|
||||
if (data instanceof String) {
|
||||
sendData = URLEncoder.encode((String) data, vo.getEncode());
|
||||
} else if (data instanceof byte[]) {
|
||||
sendData = URLEncoder.encode(new String((byte[]) data, vo.getEncode()), vo.getEncode());
|
||||
}
|
||||
StringBuilder querySb = new StringBuilder();
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpClientAdapterServiceGet] GET Method URLEncoder");
|
||||
}
|
||||
|
||||
if (RESPONSE_TYPE_ASYNC.equals(vo.getResponseType())) {
|
||||
isAsync = true;
|
||||
querySb.append("type=async&");
|
||||
}
|
||||
querySb.append(vo.getParameterName()).append("=");
|
||||
querySb.append(sendData);
|
||||
method.setQueryString(querySb.toString());
|
||||
|
||||
String contentTypeValue = null;
|
||||
if (StringUtils.isNotEmpty(vo.getContentType())) {
|
||||
if (StringUtils.isNotEmpty(vo.getEncode())) {
|
||||
contentTypeValue = vo.getContentType() + "; charset=" + vo.getEncode();
|
||||
} else {
|
||||
contentTypeValue = vo.getContentType();
|
||||
}
|
||||
method.setRequestHeader(CONTENT_TYPE_KEY, contentTypeValue);
|
||||
}
|
||||
if (vo.getAdapterHeaders() != null) {
|
||||
for (String key : vo.getAdapterHeaders().keySet()) {
|
||||
method.addRequestHeader(key, vo.getAdapterHeaders().get(key));
|
||||
}
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("HttpClientAdapterServiceGet] " + CONTENT_TYPE_KEY + "(" + contentTypeValue + ")");
|
||||
}
|
||||
|
||||
int status = -1;
|
||||
try {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"SEND [" + sendData + "]" + CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
if (vo.getUrl() != null && vo.getUrl().startsWith("https")) {
|
||||
Protocol.registerProtocol("https",
|
||||
new Protocol("https", new CustomHttpsSocketFactory(new EasySSLProtocolSocketFactory()), 443));
|
||||
}
|
||||
status = mclient.executeMethod(method);
|
||||
stopWatch.stop();
|
||||
|
||||
String responseCharSet = getResponseCharSet(method.getResponseCharSet(), vo.getAdapterGroupName());
|
||||
|
||||
// status 200(정상) 이외에는 error 처리
|
||||
if (status != 200) {
|
||||
try {
|
||||
logger.error("HttpClientAdapterServiceGet] receive status[" + status + "] data["
|
||||
+ new String(method.getResponseBody()) + "]");
|
||||
} catch (Exception e) {
|
||||
logger.error(e);
|
||||
}
|
||||
throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="
|
||||
+ vo.getAdapterGroupName() + "." + vo.getAdapterName(), status);
|
||||
}
|
||||
|
||||
byte[] responseMessage = method.getResponseBody();
|
||||
if (isAsync && stopWatch.getTime() > vo.getSlowTranTime() && logger.isWarn()) {
|
||||
logger.warn("HttpClientAdapterServiceGet] URL[" + vo.getUrl() + "] lazy async response time = "
|
||||
+ stopWatch.toString() + " responseMessage=" + new String(responseMessage, responseCharSet));
|
||||
}
|
||||
|
||||
responseMessage = getEncodingConvert(responseCharSet, vo.getAdapterGroupName(), responseMessage);
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"RECV [" + new String(responseMessage, responseCharSet) + "]"
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
AdapterManager m = AdapterManager.getInstance();
|
||||
AdapterGroupVO agvo = m.getAdapterGroupVO(vo.getAdapterGroupName());
|
||||
if (MessageUtil.isBytesMessage(agvo.getMessageType())) {
|
||||
return responseMessage;
|
||||
} else {
|
||||
return new String(responseMessage, vo.getEncode());
|
||||
}
|
||||
} catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceGet] SocketTimeoutException : " + ste.toString(), ste);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceGet] SocketTimeoutException : " + ste.toString(), ste);
|
||||
}
|
||||
throw ste;
|
||||
} catch (ConnectException ce) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceGet] Connection Exception : " + ce.toString(), ce);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceGet] Connection Exception : " + ce.toString(), ce);
|
||||
}
|
||||
throw ce;
|
||||
} catch (Exception e) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceGet] Exception : " + e.toString(), e);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceGet] Exception : " + e.toString(), e);
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
method.releaseConnection();
|
||||
if (status != HttpStatus.SC_OK) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = String.valueOf(status);
|
||||
String resMsg = ExceptionUtil.make("RDCEAIAHA014", msgArgs);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug(resMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
//
|
||||
//package com.eactive.eai.adapter.http.client.impl;
|
||||
//
|
||||
//import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
//import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
//import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
//import com.eactive.eai.common.util.CommonLib;
|
||||
//import org.apache.commons.httpclient.HttpClient;
|
||||
//import org.apache.commons.httpclient.HttpStatus;
|
||||
//import org.apache.commons.httpclient.HttpVersion;
|
||||
//import org.apache.commons.httpclient.methods.PostMethod;
|
||||
//
|
||||
//import java.net.ConnectException;
|
||||
//import java.net.SocketTimeoutException;
|
||||
//import java.util.Properties;
|
||||
//
|
||||
//
|
||||
///**
|
||||
//* 1. 기능 : EAI HTTP OutBound 용 어댑터로 수동 시스템의 HTTP 웹 컴포넌트를
|
||||
//* GET/POST 방식으로 호출할 수 있는 기능을 제공한다.
|
||||
//* 2. 처리 개요 :
|
||||
//* * - 2009.11.24 retry 로직 제거 : 요청
|
||||
//* 3. 주의사항
|
||||
//*
|
||||
//* @author :
|
||||
//* @version : v 1.0.0
|
||||
//* @see : WLIAdapterFactory.java, WLIAdapter.java, WLIDefaultAdapter.java
|
||||
//* @since :
|
||||
//
|
||||
//*/
|
||||
//public class HttpClientAdapterServiceMemo extends HttpClientAdapterServiceSupport{
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 전문과 같은 Raw Data 통신에 사용
|
||||
// * 2. 처리 개요 :
|
||||
// * - 속성 정보를 설정 하고 수동 시스템 서비스를 호출 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param prop Http Adapter 속성 정보
|
||||
// * @param message 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// public Object execute(Properties prop, byte[] data, Properties tempProp) throws Exception {
|
||||
//
|
||||
// HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
//
|
||||
// if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
// logger.debug("HttpClientAdapterServiceMemo] SEND (" + vo.getMethod() + ") ");
|
||||
// }
|
||||
// PostMethod method = new PostMethod(vo.getUrl());
|
||||
// if (vo.getTimeout() > -1) {
|
||||
// method.getParams().setSoTimeout(vo.getTimeout());
|
||||
// }
|
||||
// HttpClient mclient = this.client;
|
||||
// if ( vo.getConnectionTimeout() != 0){
|
||||
// mclient.getParams().setParameter("http.connection.timeout", vo.getConnectionTimeout());
|
||||
// }
|
||||
// method.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0);
|
||||
// int memoMinSize = 587;
|
||||
//
|
||||
// String srvCode = "";
|
||||
// String serType = "";
|
||||
// String recipient = "";
|
||||
// String send = "";
|
||||
// String title = "";
|
||||
// String body = "";
|
||||
// String urgent = "";
|
||||
// String saveOption = "";
|
||||
// String rcvIp = "";
|
||||
//
|
||||
// if (data.length >= memoMinSize) {
|
||||
// if (vo.getEncode() != null && vo.getEncode().length() > 0) {
|
||||
// if (logger.isDebug()) {
|
||||
// logger.debug("HttpClientAdapterServiceMemo] ENCODE(" + vo.getEncode() + ") ");
|
||||
// }
|
||||
// srvCode = new String(data, 0, 6, vo.getEncode());
|
||||
// serType = new String(data, 6, 1, vo.getEncode());
|
||||
// recipient = new String(data, 7, 7, vo.getEncode());
|
||||
// send = new String(data, 14, 7, vo.getEncode());
|
||||
// title = new String(data, 21, 40, vo.getEncode());
|
||||
// body = new String(data, 61, 500, vo.getEncode());
|
||||
// urgent = new String(data, 561, 1, vo.getEncode());
|
||||
// saveOption = new String(data, 562, 1, vo.getEncode());
|
||||
// rcvIp = new String(data, 563, 24, vo.getEncode());
|
||||
//
|
||||
// } else {
|
||||
// srvCode = new String(data, 0, 6);
|
||||
// serType = new String(data, 6, 1);
|
||||
// recipient = new String(data, 7, 7);
|
||||
// send = new String(data, 14, 7);
|
||||
// title = new String(data, 21, 40);
|
||||
// body = new String(data, 61, 500);
|
||||
// urgent = new String(data, 561, 1);
|
||||
// saveOption = new String(data, 562, 1);
|
||||
// rcvIp = new String(data, 563, 24);
|
||||
// }
|
||||
// method.addParameter("SRV_CODE", srvCode );
|
||||
// method.addParameter("SER_TYPE", serType );
|
||||
// method.addParameter("RECIPIENT", recipient );
|
||||
// method.addParameter("SEND", send );
|
||||
// method.addParameter("TITLE", title );
|
||||
// method.addParameter("BODY", body );
|
||||
// method.addParameter("URGENT", urgent );
|
||||
// method.addParameter("SAVEOPTION", saveOption);
|
||||
// method.addParameter("RCV_IP", rcvIp );
|
||||
//
|
||||
// } else {
|
||||
// if (logger.isError()) {
|
||||
// logger.error("HttpClientAdapterServiceMemo] Invalid Data Length (" + data.length + ") < "
|
||||
// + memoMinSize);
|
||||
// }
|
||||
// throw new Exception("HttpClientAdapterServiceMemo Error : MessageSize(" + (data.length)
|
||||
// + " Bytes) too small < " + memoMinSize);
|
||||
// }
|
||||
//
|
||||
// int status = -1;
|
||||
//
|
||||
// try {
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(),
|
||||
// "[SRV_CODE] "+ srvCode + "\n"
|
||||
// + "[SER_TYPE] "+ serType + "\n"
|
||||
// + "[RECIPIENT] "+ recipient + "\n"
|
||||
// + "[SEND] "+ send + "\n"
|
||||
// + "[TITLE] "+ title + "\n"
|
||||
// + "[BODY] "+ body + "\n"
|
||||
// + "[URGENT] "+ urgent + "\n"
|
||||
// + "[SAVEOPTION] "+ saveOption + "\n"
|
||||
// + "[RCV_IP] "+ rcvIp + "\n"
|
||||
// + "SEND ["+new String(data)+"]"+CommonLib.getDumpMessage(data) );
|
||||
// }
|
||||
// //mclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
|
||||
// if (vo.getEncode() != null && vo.getEncode().length() > 0) {
|
||||
// mclient.getParams().setContentCharset(vo.getEncode());
|
||||
// }
|
||||
// else {
|
||||
// // ASIS 에서 DEFAULT로 EUC_KR로 설정했음
|
||||
// mclient.getParams().setContentCharset("EUC_KR");
|
||||
// }
|
||||
//
|
||||
// status = mclient.executeMethod(method);
|
||||
// //status 200(정상) 이외에는 error 처리
|
||||
// if (status !=200){
|
||||
// try{
|
||||
// logger.error("HttpClientAdapterServiceMemo] receive status["+status+"] data[" + new String(method.getResponseBody()) +"]");
|
||||
// }catch(Exception e){
|
||||
// logger.error(e);
|
||||
// }
|
||||
// throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="+vo.getAdapterGroupName()+"."+vo.getAdapterName(),status);
|
||||
// }
|
||||
// byte[] responseMessage = method.getResponseBody();
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(), "RECV ["+new String(responseMessage)+"]"+CommonLib.getDumpMessage(responseMessage) );
|
||||
// }
|
||||
// return responseMessage;
|
||||
//
|
||||
// } catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient
|
||||
// // 3.1일 경우
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
// "HttpClientAdapterServiceMemo] SocketTimeoutException : " + ste.toString(), ste);
|
||||
// }
|
||||
// if (logger.isError()) {
|
||||
// logger.error("HttpClientAdapterServiceMemo] SocketTimeoutException : " + ste.toString(), ste);
|
||||
// }
|
||||
// throw ste;
|
||||
// } catch (ConnectException ce) {
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
// "HttpClientAdapterServiceMemo] Connection Exception : " + ce.toString(), ce);
|
||||
// }
|
||||
// if (logger.isError()) {
|
||||
// logger.error("HttpClientAdapterServiceMemo] Connection Exception : " + ce.toString(), ce);
|
||||
// }
|
||||
// throw ce;
|
||||
// } catch (Exception e) {
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
// "HttpClientAdapterServiceMemo] Exception : " + e.toString(), e);
|
||||
// }
|
||||
// if (logger.isError()) {
|
||||
// logger.error("HttpClientAdapterServiceMemo] Exception : " + e.toString(), e);
|
||||
// }
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if (status != HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
// if (logger.isDebug()) {
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
||||
+227
@@ -0,0 +1,227 @@
|
||||
|
||||
package com.eactive.eai.adapter.http.client.impl;
|
||||
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
import com.eactive.eai.adapter.http.secure.CustomHttpsSocketFactory;
|
||||
import com.eactive.eai.adapter.http.secure.EasySSLProtocolSocketFactory;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.property.PropManager;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.eactive.eai.util.UrlUtil;
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.HttpVersion;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
import org.apache.commons.httpclient.params.HttpConnectionParams;
|
||||
import org.apache.commons.httpclient.protocol.Protocol;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Properties;
|
||||
|
||||
// Msa 는 LG Micro Service Architecture F/W과 통신하기 위한 Adapter로 관련 Site 전용
|
||||
public class HttpClientAdapterServiceMsa extends HttpClientAdapterServiceSupport {
|
||||
|
||||
public static String DEFAULT_ENCODE = "UTF-8";
|
||||
|
||||
public Object execute(Properties prop, Object data, Properties tempProp) throws Exception {
|
||||
HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
String svcName = tempProp.getProperty(TRAN_CODE, "");
|
||||
|
||||
if (StringUtils.isEmpty(svcName)) {
|
||||
throw new Exception("service name is empty");
|
||||
}
|
||||
|
||||
boolean isAsync = false;
|
||||
|
||||
if (vo.getUrl() == null) {
|
||||
throw new Exception("URL is NULL");
|
||||
}
|
||||
|
||||
if (vo.getUrl().endsWith(".ajmd") || vo.getUrl().endsWith(".axmd")) {
|
||||
isAsync = true;
|
||||
}
|
||||
|
||||
String msaUrl = vo.getUrl().replace("%" + TRAN_CODE + "%", svcName);
|
||||
|
||||
// ENVR_INFO_DVSN_CD = L
|
||||
// FRST_DMND_SYS_IP_VAL 로 라우팅하도록 함
|
||||
boolean localCall = LOCAL_CALL_Y.equals(tempProp.getProperty(LOCAL_CALL, "N"));
|
||||
if (localCall) {
|
||||
String newIp = tempProp.getProperty(LOCAL_IP);
|
||||
if (newIp == null) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("HttpClientAdapterServiceMsa] LOCAL_CALL FRST_DMND_SYS_IP_VAL is empty, SKIP : "
|
||||
+ msaUrl);
|
||||
}
|
||||
} else {
|
||||
String newPort = PropManager.getInstance().getProperty("LOCAL", "port", "7001");
|
||||
msaUrl = UrlUtil.replaceIpAddress(msaUrl, newIp, newPort);
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("HttpClientAdapterServiceMsa] LOCAL_CALL REPLACED_URL : " + msaUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PostMethod method = new PostMethod(msaUrl);
|
||||
HttpClient mclient = this.client;
|
||||
|
||||
configureHttpClient(mclient, method, vo, vo.getUrl(), vo.getContentType());
|
||||
|
||||
logger.debug("HttpClientAdapterServiceMsa] ENCODE(" + vo.getEncode() + ")");
|
||||
|
||||
try {
|
||||
mclient.getParams().setContentCharset(vo.getEncode());
|
||||
} catch (Exception e) {
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceMsa] ENCODE(" + vo.getEncode() + ") ERROR : " + e.toString());
|
||||
logger.error("HttpClientAdapterServiceMsa] ENCODE(" + vo.getEncode() + ") SKIP ");
|
||||
}
|
||||
}
|
||||
|
||||
String contentTypeValue = null;
|
||||
if (StringUtils.isNotEmpty(vo.getContentType())) {
|
||||
if (StringUtils.isNotEmpty(vo.getEncode())) {
|
||||
contentTypeValue = vo.getContentType() + "; charset=" + vo.getEncode();
|
||||
} else {
|
||||
contentTypeValue = vo.getContentType();
|
||||
}
|
||||
method.setRequestHeader(CONTENT_TYPE_KEY, contentTypeValue);
|
||||
}
|
||||
if (vo.getAdapterHeaders() != null) {
|
||||
for (String key : vo.getAdapterHeaders().keySet()) {
|
||||
method.addRequestHeader(key, vo.getAdapterHeaders().get(key));
|
||||
}
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("HttpClientAdapterServiceMsa] " + CONTENT_TYPE_KEY + "(" + contentTypeValue + ")");
|
||||
}
|
||||
|
||||
byte[] sendBytes = null;
|
||||
if (data instanceof String) {
|
||||
sendBytes = ((String) data).getBytes(vo.getEncode());
|
||||
} else if (data instanceof byte[]) {
|
||||
sendBytes = (byte[]) data;
|
||||
}
|
||||
|
||||
InputStream in = new ByteArrayInputStream(sendBytes); // "utf-8"
|
||||
method.setRequestBody(in);
|
||||
|
||||
int status = -1;
|
||||
|
||||
try {
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceMsa] SEND (" + vo.getAdapterGroupName() + ") = ["
|
||||
+ new String(sendBytes, vo.getEncode()) + "]");
|
||||
logger.debug("HttpClientAdapterServiceMsa] SEND (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(sendBytes));
|
||||
}
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"SEND [" + new String(sendBytes, vo.getEncode()) + "]" + CommonLib.getDumpMessage(sendBytes));
|
||||
}
|
||||
if (vo.getUrl() != null && vo.getUrl().startsWith("https")) {
|
||||
Protocol.registerProtocol("https",
|
||||
new Protocol("https", new CustomHttpsSocketFactory(new EasySSLProtocolSocketFactory()), 443));
|
||||
}
|
||||
status = mclient.executeMethod(method);
|
||||
stopWatch.stop();
|
||||
|
||||
String responseCharSet = getResponseCharSet(method.getResponseCharSet(), vo.getAdapterGroupName());
|
||||
|
||||
if (status != 200) {
|
||||
try {
|
||||
logger.error("HttpClientAdapterServiceMsa] receive status[" + status + "] data["
|
||||
+ new String(method.getResponseBody()) + "]");
|
||||
} catch (Exception e) {
|
||||
logger.error(e);
|
||||
}
|
||||
throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="
|
||||
+ vo.getAdapterGroupName() + "." + vo.getAdapterName(), status);
|
||||
}
|
||||
|
||||
// 응답으로 온 byte[]를 그대로 전달한다.
|
||||
byte[] responseMessage = method.getResponseBody();
|
||||
;
|
||||
if (isAsync && stopWatch.getTime() > vo.getSlowTranTime()) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("HttpClientAdapterServiceMsa] URL[" + msaUrl + "] lazy async response time = "
|
||||
+ stopWatch.toString() + " responseMessage="
|
||||
+ new String(responseMessage, responseCharSet));
|
||||
}
|
||||
}
|
||||
|
||||
responseMessage = getEncodingConvert(responseCharSet, vo.getAdapterGroupName(), responseMessage);
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceMsa] responseType =" + vo.getResponseType()
|
||||
+ ", responseCharSet=" + responseCharSet);
|
||||
logger.debug("HttpClientAdapterServiceMsa] RECV (" + vo.getAdapterGroupName() + ") = ["
|
||||
+ new String(responseMessage, responseCharSet) + "]");
|
||||
logger.debug("HttpClientAdapterServiceMsa] RECV (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"RECV [" + new String(responseMessage, responseCharSet) + "]"
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
return responseMessage;
|
||||
} catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceMsa] SocketTimeoutException : " + ste.toString() + " url="
|
||||
+ msaUrl,
|
||||
ste);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceMsa] SocketTimeoutException : " + ste.toString() + " url="
|
||||
+ msaUrl, ste);
|
||||
}
|
||||
throw ste;
|
||||
} catch (ConnectException ce) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceMsa] Connection Exception : " + ce.toString() + " url=" + msaUrl,
|
||||
ce);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error(
|
||||
"HttpClientAdapterServiceMsa] Connection Exception : " + ce.toString() + " url=" + msaUrl,
|
||||
ce);
|
||||
}
|
||||
throw ce;
|
||||
} catch (Exception e) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceMsa] Exception : " + e.toString() + " url=" + msaUrl, e);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceMsa] Exception : " + e.toString() + " url=" + msaUrl, e);
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
method.releaseConnection();
|
||||
if (status != HttpStatus.SC_OK) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = String.valueOf(status);
|
||||
String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug(resMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+227
@@ -0,0 +1,227 @@
|
||||
|
||||
package com.eactive.eai.adapter.http.client.impl;
|
||||
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
import com.eactive.eai.adapter.http.secure.CustomHttpsSocketFactory;
|
||||
import com.eactive.eai.adapter.http.secure.EasySSLProtocolSocketFactory;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.property.PropManager;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.eactive.eai.util.UrlUtil;
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.HttpVersion;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
import org.apache.commons.httpclient.params.HttpConnectionParams;
|
||||
import org.apache.commons.httpclient.protocol.Protocol;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Properties;
|
||||
|
||||
// NexCore F/W과 통신하기 위한 Adapter로 관련 Site 전용
|
||||
public class HttpClientAdapterServiceNexCore extends HttpClientAdapterServiceSupport {
|
||||
|
||||
public static String DEFAULT_ENCODE = "UTF-8";
|
||||
|
||||
public Object execute(Properties prop, Object data, Properties tempProp) throws Exception {
|
||||
HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
String svcName = tempProp.getProperty(TRAN_CODE, "");
|
||||
|
||||
if (StringUtils.isEmpty(svcName)) {
|
||||
throw new Exception("service name is empty");
|
||||
}
|
||||
|
||||
boolean isAsync = false;
|
||||
|
||||
if (vo.getUrl() == null) {
|
||||
throw new Exception("URL is NULL");
|
||||
}
|
||||
|
||||
if (vo.getUrl().endsWith(".ajmd") || vo.getUrl().endsWith(".axmd")) {
|
||||
isAsync = true;
|
||||
}
|
||||
|
||||
String nKesaUrl = vo.getUrl().replace("%" + TRAN_CODE + "%", svcName);
|
||||
|
||||
// ENVR_INFO_DVSN_CD = L
|
||||
// FRST_DMND_SYS_IP_VAL 로 라우팅하도록 함
|
||||
boolean localCall = LOCAL_CALL_Y.equals(tempProp.getProperty(LOCAL_CALL, "N"));
|
||||
if (localCall) {
|
||||
String newIp = tempProp.getProperty(LOCAL_IP);
|
||||
if (newIp == null) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("HttpClientAdapterServiceNexCore] LOCAL_CALL FRST_DMND_SYS_IP_VAL is empty, SKIP : "
|
||||
+ nKesaUrl);
|
||||
}
|
||||
} else {
|
||||
String newPort = PropManager.getInstance().getProperty("LOCAL", "port", "7001");
|
||||
nKesaUrl = UrlUtil.replaceIpAddress(nKesaUrl, newIp, newPort);
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("HttpClientAdapterServiceNexCore] LOCAL_CALL REPLACED_URL : " + nKesaUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PostMethod method = new PostMethod(nKesaUrl);
|
||||
HttpClient mclient = this.client;
|
||||
|
||||
configureHttpClient(mclient, method, vo, vo.getUrl(), vo.getContentType());
|
||||
|
||||
logger.debug("HttpClientAdapterServiceNexCore] ENCODE(" + vo.getEncode() + ")");
|
||||
|
||||
try {
|
||||
mclient.getParams().setContentCharset(vo.getEncode());
|
||||
} catch (Exception e) {
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceNexCore] ENCODE(" + vo.getEncode() + ") ERROR : " + e.toString());
|
||||
logger.error("HttpClientAdapterServiceNexCore] ENCODE(" + vo.getEncode() + ") SKIP ");
|
||||
}
|
||||
}
|
||||
|
||||
String contentTypeValue = null;
|
||||
if (StringUtils.isNotEmpty(vo.getContentType())) {
|
||||
if (StringUtils.isNotEmpty(vo.getEncode())) {
|
||||
contentTypeValue = vo.getContentType() + "; charset=" + vo.getEncode();
|
||||
} else {
|
||||
contentTypeValue = vo.getContentType();
|
||||
}
|
||||
method.setRequestHeader(CONTENT_TYPE_KEY, contentTypeValue);
|
||||
}
|
||||
if (vo.getAdapterHeaders() != null) {
|
||||
for (String key : vo.getAdapterHeaders().keySet()) {
|
||||
method.addRequestHeader(key, vo.getAdapterHeaders().get(key));
|
||||
}
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("HttpClientAdapterServiceNexCore] " + CONTENT_TYPE_KEY + "(" + contentTypeValue + ")");
|
||||
}
|
||||
|
||||
byte[] sendBytes = null;
|
||||
if (data instanceof String) {
|
||||
sendBytes = ((String) data).getBytes(vo.getEncode());
|
||||
} else if (data instanceof byte[]) {
|
||||
sendBytes = (byte[]) data;
|
||||
}
|
||||
|
||||
InputStream in = new ByteArrayInputStream(sendBytes); // "utf-8"
|
||||
method.setRequestBody(in);
|
||||
|
||||
int status = -1;
|
||||
|
||||
try {
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceNexCore] SEND (" + vo.getAdapterGroupName() + ") = ["
|
||||
+ new String(sendBytes, vo.getEncode()) + "]");
|
||||
logger.debug("HttpClientAdapterServiceNexCore] SEND (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(sendBytes));
|
||||
}
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"SEND [" + new String(sendBytes, vo.getEncode()) + "]" + CommonLib.getDumpMessage(sendBytes));
|
||||
}
|
||||
if (vo.getUrl() != null && vo.getUrl().startsWith("https")) {
|
||||
Protocol.registerProtocol("https",
|
||||
new Protocol("https", new CustomHttpsSocketFactory(new EasySSLProtocolSocketFactory()), 443));
|
||||
}
|
||||
status = mclient.executeMethod(method);
|
||||
stopWatch.stop();
|
||||
|
||||
String responseCharSet = getResponseCharSet(method.getResponseCharSet(), vo.getAdapterGroupName());
|
||||
|
||||
if (status != 200) {
|
||||
try {
|
||||
logger.error("HttpClientAdapterServiceNexCore] receive status[" + status + "] data["
|
||||
+ new String(method.getResponseBody()) + "]");
|
||||
} catch (Exception e) {
|
||||
logger.error(e);
|
||||
}
|
||||
throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="
|
||||
+ vo.getAdapterGroupName() + "." + vo.getAdapterName(), status);
|
||||
}
|
||||
|
||||
// 응답으로 온 byte[]를 그대로 전달한다.
|
||||
byte[] responseMessage = method.getResponseBody();
|
||||
;
|
||||
if (isAsync && stopWatch.getTime() > vo.getSlowTranTime()) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("HttpClientAdapterServiceNexCore] URL[" + nKesaUrl + "] lazy async response time = "
|
||||
+ stopWatch.toString() + " responseMessage="
|
||||
+ new String(responseMessage, responseCharSet));
|
||||
}
|
||||
}
|
||||
|
||||
responseMessage = getEncodingConvert(responseCharSet, vo.getAdapterGroupName(), responseMessage);
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceNexCore] responseType =" + vo.getResponseType()
|
||||
+ ", responseCharSet=" + responseCharSet);
|
||||
logger.debug("HttpClientAdapterServiceNexCore] RECV (" + vo.getAdapterGroupName() + ") = ["
|
||||
+ new String(responseMessage, responseCharSet) + "]");
|
||||
logger.debug("HttpClientAdapterServiceNexCore] RECV (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"RECV [" + new String(responseMessage, responseCharSet) + "]"
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
return responseMessage;
|
||||
} catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceNexCore] SocketTimeoutException : " + ste.toString() + " url="
|
||||
+ nKesaUrl,
|
||||
ste);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceNexCore] SocketTimeoutException : " + ste.toString() + " url="
|
||||
+ nKesaUrl, ste);
|
||||
}
|
||||
throw ste;
|
||||
} catch (ConnectException ce) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceNexCore] Connection Exception : " + ce.toString() + " url=" + nKesaUrl,
|
||||
ce);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error(
|
||||
"HttpClientAdapterServiceNexCore] Connection Exception : " + ce.toString() + " url=" + nKesaUrl,
|
||||
ce);
|
||||
}
|
||||
throw ce;
|
||||
} catch (Exception e) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceNexCore] Exception : " + e.toString() + " url=" + nKesaUrl, e);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceNexCore] Exception : " + e.toString() + " url=" + nKesaUrl, e);
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
method.releaseConnection();
|
||||
if (status != HttpStatus.SC_OK) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = String.valueOf(status);
|
||||
String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug(resMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
//
|
||||
//package com.eactive.eai.adapter.http.client.impl;
|
||||
//
|
||||
//import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
//import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
//import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
//import com.eactive.eai.common.util.CommonLib;
|
||||
//import org.apache.commons.httpclient.HttpClient;
|
||||
//import org.apache.commons.httpclient.HttpStatus;
|
||||
//import org.apache.commons.httpclient.HttpVersion;
|
||||
//import org.apache.commons.httpclient.methods.PostMethod;
|
||||
//
|
||||
//import java.net.ConnectException;
|
||||
//import java.net.SocketTimeoutException;
|
||||
//import java.util.Properties;
|
||||
//
|
||||
//
|
||||
///**
|
||||
//* 1. 기능 : EAI HTTP OutBound 용 어댑터로 수동 시스템의 HTTP 웹 컴포넌트를
|
||||
//* GET/POST 방식으로 호출할 수 있는 기능을 제공한다.
|
||||
//* 2. 처리 개요 :
|
||||
//* * - 2009.11.24 retry 로직 제거 : 요청
|
||||
//* 3. 주의사항
|
||||
//*
|
||||
//* @author :
|
||||
//* @version : v 1.0.0
|
||||
//* @see : WLIAdapterFactory.java, WLIAdapter.java, WLIDefaultAdapter.java
|
||||
//* @since :
|
||||
//
|
||||
//*/
|
||||
//public class HttpClientAdapterServicePopup extends HttpClientAdapterServiceSupport{
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 전문과 같은 Raw Data 통신에 사용
|
||||
// * 2. 처리 개요 :
|
||||
// * - 속성 정보를 설정 하고 수동 시스템 서비스를 호출 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param prop Http Adapter 속성 정보
|
||||
// * @param message 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// public Object execute(Properties prop, byte[] data, Properties tempProp) throws Exception {
|
||||
//
|
||||
// HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
//
|
||||
// if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
// logger.debug("HttpClientAdapter] SEND (" + vo.getMethod() + ") ");
|
||||
// }
|
||||
// PostMethod method = new PostMethod(vo.getUrl());
|
||||
// if (vo.getTimeout() > -1) {
|
||||
// method.getParams().setSoTimeout(vo.getTimeout());
|
||||
// }
|
||||
// HttpClient mclient = this.client;
|
||||
// if ( vo.getConnectionTimeout() != 0){
|
||||
// mclient.getParams().setParameter("http.connection.timeout", vo.getConnectionTimeout());
|
||||
// }
|
||||
// method.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0);
|
||||
//
|
||||
// // Popup Data Size : 153 Bytes
|
||||
// int popupMinSize = 153;
|
||||
// String srvCode = "";
|
||||
// String recipient = "";
|
||||
// String title = "";
|
||||
// String body = "";
|
||||
//
|
||||
//
|
||||
// if (data.length >= popupMinSize) {
|
||||
// if (vo.getEncode() != null && vo.getEncode().length() > 0) {
|
||||
// if (logger.isDebug()) {
|
||||
// logger.debug("HttpClientAdapterServicePopup] ENCODE(" + vo.getEncode() + ") ");
|
||||
// }
|
||||
// srvCode = new String(data, 0, 6, vo.getEncode());
|
||||
// recipient = new String(data, 6, 7, vo.getEncode());
|
||||
// title = new String(data, 13, 40, vo.getEncode());
|
||||
// body = new String(data, 53, 100, vo.getEncode());
|
||||
// } else {
|
||||
// srvCode = new String(data, 0, 6);
|
||||
// recipient = new String(data, 6, 7);
|
||||
// title = new String(data, 13, 40);
|
||||
// body = new String(data, 53, 100);
|
||||
// }
|
||||
// method.addParameter("SRV_CODE", srvCode);
|
||||
// method.addParameter("RECIPIENT", recipient);
|
||||
// method.addParameter("TITLE", title);
|
||||
// method.addParameter("BODY", body);
|
||||
// } else {
|
||||
// if (logger.isError()) {
|
||||
// logger.error("HttpClientAdapterServicePopup] Invalid Data Length (" + data.length + ") < "
|
||||
// + popupMinSize);
|
||||
// }
|
||||
// throw new Exception("HttpClientAdapterServicePopup Error : MessageSize(" + (data.length)
|
||||
// + " Bytes) too small < " + popupMinSize);
|
||||
// }
|
||||
//
|
||||
// int status = -1;
|
||||
//
|
||||
// try {
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(),
|
||||
// "[SRV_CODE] "+ srvCode + "\n"
|
||||
// + "[RECIPIENT] "+ recipient + "\n"
|
||||
// + "[TITLE] "+ title + "\n"
|
||||
// + "[BODY] "+ body + "\n"
|
||||
// + "SEND ["+new String(data)+"]"+CommonLib.getDumpMessage(data) );
|
||||
// }
|
||||
// //mclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
|
||||
// //mclient.getParams().setContentCharset("EUC_KR");
|
||||
// status = mclient.executeMethod(method);
|
||||
// //status 200(정상) 이외에는 error 처리
|
||||
// if (status !=200){
|
||||
// try{
|
||||
// logger.error("HttpClientAdapterServicePopup] receive status["+status+"] data[" + new String(method.getResponseBody()) +"]");
|
||||
// }catch(Exception e){
|
||||
// logger.error(e);
|
||||
// }
|
||||
// throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="+vo.getAdapterGroupName()+"."+vo.getAdapterName(),status);
|
||||
// }
|
||||
// byte[] responseMessage = method.getResponseBody();
|
||||
// if (vo.getTraceLevel() >= 3){
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(), "RECV ["+new String(responseMessage)+"]"+CommonLib.getDumpMessage(responseMessage) );
|
||||
// }
|
||||
// return responseMessage;
|
||||
//
|
||||
// } catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
// "HttpClientAdapterServicePopup] SocketTimeoutException : " + ste.toString(), ste);
|
||||
// }
|
||||
// if (logger.isError()) {
|
||||
// logger.error("HttpClientAdapterServicePopup] SocketTimeoutException : " + ste.toString(), ste);
|
||||
// }
|
||||
// throw ste;
|
||||
// } catch (ConnectException ce) {
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
// "HttpClientAdapterServicePopup] Connection Exception : " + ce.toString(), ce);
|
||||
// }
|
||||
// if (logger.isError()) {
|
||||
// logger.error("HttpClientAdapterServicePopup] Connection Exception : " + ce.toString(), ce);
|
||||
// }
|
||||
// throw ce;
|
||||
// } catch (Exception e) {
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
// "HttpClientAdapterServicePopup] Exception : " + e.toString(), e);
|
||||
// }
|
||||
// if (logger.isError()) {
|
||||
// logger.error("HttpClientAdapterServicePopup] Exception : " + e.toString(), e);
|
||||
// }
|
||||
// throw e;
|
||||
// } finally {
|
||||
// method.releaseConnection();
|
||||
// if (status != HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
// if (logger.isDebug()) {
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
||||
+224
@@ -0,0 +1,224 @@
|
||||
|
||||
package com.eactive.eai.adapter.http.client.impl;
|
||||
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
import org.apache.commons.httpclient.protocol.Protocol;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterGroupVO;
|
||||
import com.eactive.eai.adapter.AdapterManager;
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
import com.eactive.eai.adapter.http.secure.CustomHttpsSocketFactory;
|
||||
import com.eactive.eai.adapter.http.secure.EasySSLProtocolSocketFactory;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.eactive.eai.common.util.MessageUtil;
|
||||
|
||||
|
||||
/**
|
||||
* 1. 기능 : EAI HTTP OutBound 용 어댑터로 수동 시스템의 HTTP 웹 컴포넌트를
|
||||
* GET/POST 방식으로 호출할 수 있는 기능을 제공한다.
|
||||
* 2. 처리 개요 :
|
||||
* * - 2009.11.24 retry 로직 제거 : 요청
|
||||
* 3. 주의사항
|
||||
*
|
||||
* @author :
|
||||
* @version : v 1.0.0
|
||||
* @see : WLIAdapterFactory.java, WLIAdapter.java, WLIDefaultAdapter.java
|
||||
* @since :
|
||||
|
||||
*/
|
||||
public class HttpClientAdapterServicePost extends HttpClientAdapterServiceSupport{
|
||||
|
||||
/**
|
||||
* 1. 기능 : 전문과 같은 Raw Data 통신에 사용
|
||||
* 2. 처리 개요 :
|
||||
* - 속성 정보를 설정 하고 수동 시스템 서비스를 호출 한다.
|
||||
* 3. 주의사항
|
||||
*
|
||||
* @param prop Http Adapter 속성 정보
|
||||
* @param message 송신 전문
|
||||
* @return 반환 된 byte[] Object
|
||||
* @exception Exception 수동 시스템 간 통신 중 발생
|
||||
**/
|
||||
public Object execute(Properties prop, Object data, Properties tempProp) throws Exception
|
||||
{
|
||||
HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
boolean isAsync = false;
|
||||
|
||||
PostMethod method = new PostMethod(vo.getUrl());
|
||||
HttpClient mclient = this.client;
|
||||
|
||||
configureHttpClient(mclient, method, vo, vo.getUrl(), vo.getContentType());
|
||||
|
||||
String sendData = "";
|
||||
if (data instanceof String) {
|
||||
sendData = (String) data;
|
||||
} else if (data instanceof byte[]) {
|
||||
sendData = new String((byte[]) data, vo.getEncode());
|
||||
}
|
||||
|
||||
mclient.getParams().setContentCharset(vo.getEncode());
|
||||
|
||||
if ("Y".equals(vo.getUrlEncodeYn())){
|
||||
sendData = java.net.URLEncoder.encode(sendData);
|
||||
}
|
||||
|
||||
if(RESPONSE_TYPE_ASYNC.equals(vo.getResponseType())) {
|
||||
isAsync = true;
|
||||
method.addParameter("type", "async");
|
||||
}
|
||||
|
||||
method.addParameter(vo.getParameterName(), sendData);
|
||||
|
||||
String contentTypeValue = null;
|
||||
if(StringUtils.isNotEmpty(vo.getContentType())) {
|
||||
if(StringUtils.isNotEmpty(vo.getEncode())) {
|
||||
contentTypeValue = vo.getContentType() +"; charset=" + vo.getEncode();
|
||||
}
|
||||
else {
|
||||
contentTypeValue = vo.getContentType();
|
||||
}
|
||||
method.setRequestHeader(CONTENT_TYPE_KEY, contentTypeValue);
|
||||
}
|
||||
if (vo.getAdapterHeaders() != null){
|
||||
for(String key : vo.getAdapterHeaders().keySet()){
|
||||
method.addRequestHeader(key, vo.getAdapterHeaders().get(key));
|
||||
}
|
||||
}
|
||||
if(logger.isInfoEnabled()) {
|
||||
logger.info("HttpClientAdapterServicePost] "+CONTENT_TYPE_KEY+"("+contentTypeValue+")");
|
||||
}
|
||||
|
||||
int status = -1;
|
||||
|
||||
try {
|
||||
|
||||
if(logger.isDebug() && "N".equals(vo.getTestCallYn())){
|
||||
logger.debug("HttpClientAdapterServicePost] SEND ("+vo.getAdapterGroupName()+") = ["+sendData+"]");
|
||||
logger.debug("HttpClientAdapterServicePost] SEND ("+vo.getAdapterGroupName()+") = "+CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
if (vo.getTraceLevel() >= 3){
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(), "SEND ["+sendData+"]"+CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
if (vo.getUrl() != null && vo.getUrl().startsWith("https")) {
|
||||
Protocol.registerProtocol("https",
|
||||
new Protocol("https", new CustomHttpsSocketFactory(new EasySSLProtocolSocketFactory()), 443));
|
||||
}
|
||||
status = mclient.executeMethod(method);
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
String responseCharSet = getResponseCharSet(method.getResponseCharSet(),vo.getAdapterGroupName());
|
||||
|
||||
//status 200(정상) 이외에는 error 처리
|
||||
if (status !=200){
|
||||
try{
|
||||
logger.error("HttpClientAdapterServicePost] receive status["+status+"] data[" + new String(method.getResponseBody()) +"]");
|
||||
}catch(Exception e){
|
||||
logger.error(e);
|
||||
}
|
||||
throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="+vo.getAdapterGroupName()+"."+vo.getAdapterName(),status);
|
||||
}
|
||||
|
||||
byte[] responseMessage = method.getResponseBody();
|
||||
if (isAsync && stopWatch.getTime() > vo.getSlowTranTime()) {
|
||||
if (logger.isWarn()){
|
||||
logger.warn("HttpClientAdapterServicePost] URL[" + vo.getUrl() + "] lazy response time = " + stopWatch.toString()+ " responseMessage=" + new String(responseMessage,responseCharSet));
|
||||
}
|
||||
}
|
||||
responseMessage = getEncodingConvert(responseCharSet, vo.getAdapterGroupName(), responseMessage);
|
||||
// if(vo.getEncode() != null && vo.getEncode().length() > 0) {
|
||||
// Charset res = null;
|
||||
// Charset adpt = null;
|
||||
// byte[] orgResponse = method.getResponseBody();
|
||||
// try{
|
||||
// res = Charset.forName(responseCharSet);
|
||||
// adpt = Charset.forName(vo.getEncode());
|
||||
// if (res.compareTo(adpt)==0){
|
||||
// responseMessage = orgResponse;
|
||||
// }else{
|
||||
// String encodingString = new String(orgResponse, responseCharSet);
|
||||
// responseMessage = encodingString.getBytes(vo.getEncode());
|
||||
// }
|
||||
// }catch(UnsupportedCharsetException e){
|
||||
// responseMessage = orgResponse;
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// responseMessage = method.getResponseBody();
|
||||
// }
|
||||
//----------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())){
|
||||
logger.debug("HttpClientAdapterServicePost] responseType ="+ vo.getResponseType());
|
||||
logger.debug("HttpClientAdapterServicePost] RECV ("+vo.getAdapterGroupName()+") = ["+new String(responseMessage,responseCharSet)+"]");
|
||||
logger.debug("HttpClientAdapterServicePost] RECV ("+vo.getAdapterGroupName()+") = "+CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
if (vo.getTraceLevel() >= 3){
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(), "RECV ["+new String(responseMessage,responseCharSet)+"]"+CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
AdapterManager m = AdapterManager.getInstance();
|
||||
AdapterGroupVO agvo= m.getAdapterGroupVO(vo.getAdapterGroupName());
|
||||
if (MessageUtil.isBytesMessage(agvo.getMessageType())) {
|
||||
return responseMessage;
|
||||
} else {
|
||||
return new String(responseMessage, vo.getEncode());
|
||||
}
|
||||
} catch(SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
if (vo.getTraceLevel() >= 3){
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServicePost] SocketTimeoutException : " + ste.toString() ,ste);
|
||||
}
|
||||
if (logger.isError()){
|
||||
logger.error("HttpClientAdapterServicePost] SocketTimeoutException : " + ste.toString() ,ste);
|
||||
}
|
||||
throw ste;
|
||||
} catch(ConnectException ce) {
|
||||
if (vo.getTraceLevel() >= 3){
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServicePost] Connection Exception : " + ce.toString() ,ce);
|
||||
}
|
||||
if (logger.isError()){
|
||||
logger.error("HttpClientAdapterServicePost] Connection Exception : " + ce.toString() ,ce);
|
||||
}
|
||||
throw ce;
|
||||
} catch(Exception e) {
|
||||
if (vo.getTraceLevel() >= 3){
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServicePost] Exception : " + e.toString() ,e);
|
||||
}
|
||||
if (logger.isError()){
|
||||
logger.error("HttpClientAdapterServicePost] Exception : " + e.toString() ,e);
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
method.releaseConnection();
|
||||
if(status != HttpStatus.SC_OK) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = String.valueOf(status);
|
||||
String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
if(logger.isDebug()){
|
||||
logger.debug(resMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+829
@@ -0,0 +1,829 @@
|
||||
package com.eactive.eai.adapter.http.client.impl;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.httpclient.Header;
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpMethodBase;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.NameValuePair;
|
||||
import org.apache.commons.httpclient.methods.DeleteMethod;
|
||||
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
|
||||
import org.apache.commons.httpclient.methods.GetMethod;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
import org.apache.commons.httpclient.methods.PutMethod;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.SAXReader;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpMethodType;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceKey;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceKey;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.message.MessageType;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.jayway.jsonpath.DocumentContext;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import com.openbanking.eai.common.token.AccessTokenManager;
|
||||
import com.openbanking.eai.common.token.AccessTokenVO;
|
||||
import com.openbanking.eai.common.token.OAuth2AccessTokenVO;
|
||||
|
||||
/**
|
||||
* 1. 기능 : EAI HTTP OutBound 용 어댑터로 수동 시스템의 HTTP 웹 컴포넌트를 GET/POST 방식으로 호출할 수 있는
|
||||
* 기능을 제공한다.<br>
|
||||
* 2. 처리 개요 : <br>
|
||||
* * - 2020-09-25: Http Header 받아서 처리할 수 있게 기능 추가<br>
|
||||
* 3. 주의사항 <br>
|
||||
*
|
||||
* @author :
|
||||
* @version : v 1.0.0
|
||||
* @see : HttpClientAdapterServiceFactory.java,
|
||||
* HttpClientAdapterServiceSupport.java
|
||||
* @since :
|
||||
*
|
||||
*/
|
||||
public class HttpClientAdapterServiceRest extends HttpClientAdapterServiceSupport
|
||||
implements HttpClientAdapterServiceKey {
|
||||
public static final String TYPE_VARIABLE_URL_REQUEST = "variableUrlRequest";
|
||||
public static final String TYPE_SIMPLE_REQUEST = "simpleRequest";
|
||||
|
||||
public static final String REST_OPTION = "REST_OPTION";
|
||||
|
||||
////public static final String HEADER_CONTENT_TYPE = "Content-Type";
|
||||
public static final String HEADER_GROUP = "HEADER_GROUP";
|
||||
public static final String HTTP_STATUS = "HTTP_STATUS";
|
||||
public static final String HEADER_KEYS = "HEADER_KEYS";
|
||||
|
||||
/**
|
||||
* 1. 기능 : REST API 통신에 사용 <br>
|
||||
* 2. 처리 개요 : - 속성 정보를 설정 하고 수동 시스템 서비스를 호출 한다. <br>
|
||||
* 3. 주의사항 <br>
|
||||
*
|
||||
* @param prop Http Adapter 속성 정보
|
||||
* @param message 송신 전문(JSON)
|
||||
* @return 반환 된 Object
|
||||
* @exception Exception 수동 시스템 간 통신 중 발생
|
||||
**/
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
public Object execute(Properties prop, Object data, Properties tempProp) throws Exception {
|
||||
HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
|
||||
String headerGroupName = prop.getProperty(HEADER_GROUP);
|
||||
String relayResponseHeaderKeys = prop.getProperty(HEADER_KEYS, "");
|
||||
boolean useAdapterToken = StringUtils.equalsIgnoreCase(prop.getProperty("ADAPTER_TOKEN_USE_YN", "N"), "Y");
|
||||
|
||||
// ex) $.dataHeader.GW_RSLT_CD
|
||||
String tokenErrorCodeKey = prop.getProperty("ADAPTER_TOKEN_ERROR_CODE_KEY");
|
||||
String tokenErrorCodeValues = prop.getProperty("ADAPTER_TOKEN_ERROR_CODE_VALUES");
|
||||
String tokenErrorHttpStatusCode = prop.getProperty("ADAPTER_TOKEN_ERROR_HTTP_STATUS_CODE"); // 200 or 400번대
|
||||
|
||||
// 레이아웃 메시지 타입 REST URL 추출 시 활용. Default JSON
|
||||
String messageType = prop.getProperty("MESSAGE_TYPE", MessageType.JSON);
|
||||
String inboundMethod = tempProp.getProperty(HttpAdapterServiceKey.INBOUND_METHOD, "POST");
|
||||
|
||||
/**
|
||||
* @formatter:off
|
||||
* TSEAIHE02.RESTOPTION 정보 => JSON 형태로 구성
|
||||
* - type : simpleRequest, variableUrlRequest
|
||||
* - extraPath : 어댑터 프로퍼티 URL에 추가될 HTTP REST URL
|
||||
* - method : get, delete, post, put
|
||||
* - contentType: application/json(default), application/x-www-form-urlencoded
|
||||
* - adapterTokenUseYn: Y, N(default)
|
||||
* ex)
|
||||
* {"type":"simpleRequest","extraPath":"v2.0/accout/balance","method":"post"}
|
||||
* @formatter:on
|
||||
*/
|
||||
|
||||
String restOptionData = tempProp.getProperty(REST_OPTION);
|
||||
|
||||
JSONObject restOptionObject = parseJson(restOptionData);
|
||||
if (restOptionObject == null) {
|
||||
throw new Exception("OptionData parsing result is NULL");
|
||||
}
|
||||
|
||||
String rmethod = (String) restOptionObject.getOrDefault("method", inboundMethod);
|
||||
String contentType = (String) restOptionObject.getOrDefault("contentType", "application/json");
|
||||
String adapterTokenUseYn = (String) restOptionObject.get("adapterTokenUseYn");
|
||||
if (StringUtils.isNotBlank(adapterTokenUseYn)) { // interface 에 설정된게 우선한다.
|
||||
useAdapterToken = StringUtils.equalsIgnoreCase(adapterTokenUseYn, "Y");
|
||||
}
|
||||
|
||||
HttpClient mclient = this.client;
|
||||
String sendData = "";
|
||||
if (data instanceof String) {
|
||||
sendData = (String) data;
|
||||
} else if (data instanceof byte[]) {
|
||||
sendData = new String((byte[]) data, vo.getEncode());
|
||||
}
|
||||
|
||||
////mclient.getParams().setContentCharset(vo.getEncode());
|
||||
|
||||
JSONObject dataObject = null;
|
||||
Object httpHeader = null;
|
||||
if (MessageType.JSON.equals(messageType)) {
|
||||
dataObject = parseJson(sendData);
|
||||
if (dataObject != null && StringUtils.isNotBlank(headerGroupName)) {
|
||||
httpHeader = dataObject.get(headerGroupName);
|
||||
dataObject.remove(headerGroupName);
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("HttpClientAdapterServiceRest] dataObject1 = [" + dataObject + "]");
|
||||
|
||||
// interface 에 설정된게 우선한다.
|
||||
String uri = null;
|
||||
if (dataObject == null) {
|
||||
uri = changeUrl(messageType, vo.getUrl(), restOptionData, sendData);
|
||||
} else {
|
||||
uri = changeUrl(messageType, vo.getUrl(), restOptionData, dataObject);
|
||||
}
|
||||
|
||||
logger.debug("HttpClientAdapterServiceRest] dataObject2 = [" + dataObject + "]");
|
||||
|
||||
// ////if (vo.getConnectionTimeout() != 0) {
|
||||
// mclient.getHttpConnectionManager().getParams().setConnectionTimeout(vo.getConnectionTimeout());
|
||||
// }
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug(
|
||||
"HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") URL=[" + vo.getUrl() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") PARAMETER_NAME=["
|
||||
+ vo.getParameterName() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") ENCODE=["
|
||||
+ vo.getEncode() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") RESPONSE_TYPE=["
|
||||
+ vo.getResponseType() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") URL_ENCODE_YN=["
|
||||
+ vo.getUrlEncodeYn() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") CONNECTION_TIMEOUT=["
|
||||
+ vo.getConnectionTimeout() + "] ");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") REST_OPTION=["
|
||||
+ restOptionData + "] ");
|
||||
}
|
||||
|
||||
HttpMethodBase method = null;
|
||||
switch (HttpMethodType.getValue(rmethod)) {
|
||||
case GET:
|
||||
method = new GetMethod(uri);
|
||||
break;
|
||||
case DELETE:
|
||||
method = new DeleteMethod(uri);
|
||||
break;
|
||||
case POST:
|
||||
method = new PostMethod(uri);
|
||||
break;
|
||||
case PUT:
|
||||
method = new PutMethod(uri);
|
||||
break;
|
||||
default:
|
||||
// set default method
|
||||
method = new GetMethod(uri);
|
||||
break;
|
||||
}
|
||||
|
||||
configureHttpClient(mclient, method, vo, uri, contentType);
|
||||
|
||||
switch (HttpMethodType.getValue(rmethod)) {
|
||||
case GET:
|
||||
case DELETE:
|
||||
HashMap<String, String> h = getParameters(dataObject);
|
||||
NameValuePair[] params = new NameValuePair[h.size()];
|
||||
int count = 0;
|
||||
for (String key : h.keySet()) {
|
||||
String value = h.get(key);
|
||||
params[count] = new NameValuePair(key, value);
|
||||
count++;
|
||||
}
|
||||
method.setQueryString(params);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug(
|
||||
"HttpClientAdapterServiceRest] (get Method) QueryString = [" + method.getQueryString() + "]");
|
||||
}
|
||||
|
||||
break;
|
||||
case PUT:
|
||||
case POST:
|
||||
assignPostBody(dataObject, contentType, method);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") method.getParams()=["
|
||||
+ method.getParams().toString() + "] ");
|
||||
}
|
||||
|
||||
OAuth2AccessTokenVO accessToken = null;
|
||||
if (useAdapterToken) {
|
||||
AccessTokenManager tokenManager = AccessTokenManager.getInstance();
|
||||
accessToken = (OAuth2AccessTokenVO) tokenManager.getAccessTokenVO(vo.getAdapterGroupName());
|
||||
|
||||
// 토큰이 없거나 만료 됐으면 재발급
|
||||
if (accessToken == null || accessToken.isExpired()) {
|
||||
String oldToken = accessToken == null ? null : accessToken.getAccessToken();
|
||||
accessToken = (OAuth2AccessTokenVO) tokenManager.retryAccessTokenVO(vo.getAdapterGroupName(), prop,
|
||||
oldToken);
|
||||
}
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") TOKEN = ["
|
||||
+ accessToken + "]");
|
||||
}
|
||||
|
||||
setAuthHeaders(method, accessToken);
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName()
|
||||
+ ") RequestHeader [Authorization=" + method.getRequestHeader("Authorization") + "]");
|
||||
}
|
||||
}
|
||||
|
||||
// 전달 header 셋팅
|
||||
// Adapter 레벨 header 보다 data로 넘어온 httpHeader를 우선 적용(header명이 같다면 덮어씀)
|
||||
assignRequestHeaders(method, httpHeader);
|
||||
|
||||
int status = -1;
|
||||
|
||||
try {
|
||||
byte[] responseMessage = null;
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
if (dataObject != null) {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") = ["
|
||||
+ dataObject.toJSONString() + "]");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(dataObject.toJSONString()));
|
||||
} else {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") = [" + sendData
|
||||
+ "]");
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
}
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"SEND [" + sendData + "]" + CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
try {
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("[method getName]" + method.getName());
|
||||
logger.debug("[method getRequestCharSet]" + method.getRequestCharSet());
|
||||
logger.debug("[method getRequestHeaders]" + method.getRequestHeaders().toString());
|
||||
logger.debug("[method getQueryString]" + method.getQueryString());
|
||||
logger.debug("[method getParams]" + method.getParams().toString());
|
||||
logger.debug("[method getURI]" + method.getURI());
|
||||
}
|
||||
status = mclient.executeMethod(method);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("[received status]" + status);
|
||||
}
|
||||
} catch (ConnectException e) {
|
||||
responseMessage = method.getResponseBody();
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRest] responseType =" + vo.getResponseType());
|
||||
logger.debug("HttpClientAdapterServiceRest] RECV (" + vo.getAdapterGroupName() + ") = ["
|
||||
+ new String(responseMessage, vo.getEncode()) + "]");
|
||||
logger.debug("HttpClientAdapterServiceRest] RECV (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
throw new Exception("excuteMethod java.net.ConnectException = " + e.getMessage());
|
||||
}
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
/**
|
||||
* @formatter:off
|
||||
* OAuth 토큰 응답 체크(Adapter properties로 설정)
|
||||
* 응답코드에 따라 유효한 토큰 확인(토큰 재발급 여부 확인)
|
||||
* API 서비스 마다 정책이 다름(보통 400대에서 체크하나 200에서도 체크할 수 있음)
|
||||
*
|
||||
* TOKEN_ERROR_CODE_KEY: 응답 json error code key
|
||||
* __ex) $.dataHeader.resultCode
|
||||
* TOKEN_ERROR_CODE_VALUES: 토큰 재발급이 필요한 응답 error codes(ex: O0001,O0002,O0003)
|
||||
* TOKEN_ERROR_HTTP_STATUS_CODE: 보통 400대에서 체크하나 API 제공자에 따라 200에서도 체클할수 있음
|
||||
* ex) TOKEN_ERROR_HTTP_STATUS_CODE = 200
|
||||
* @formatter:on
|
||||
*/
|
||||
boolean needReissue = false;
|
||||
|
||||
if (status == 200) {
|
||||
responseMessage = method.getResponseBody();
|
||||
if (useAdapterToken && StringUtils.equals(tokenErrorHttpStatusCode, "200")) {
|
||||
needReissue = checkTokenRetry(responseMessage, tokenErrorCodeKey, tokenErrorCodeValues,
|
||||
vo.getEncode());
|
||||
}
|
||||
} else if (status == 302) {
|
||||
responseMessage = method.getResponseBody();
|
||||
if (!StringUtils.contains(relayResponseHeaderKeys, "Location")) {
|
||||
relayResponseHeaderKeys += ",Location";
|
||||
}
|
||||
} else if (status >= 400 && status < 500) {
|
||||
responseMessage = method.getResponseBody();
|
||||
if (useAdapterToken) {
|
||||
needReissue = checkTokenRetry(responseMessage, tokenErrorCodeKey, tokenErrorCodeValues,
|
||||
vo.getEncode());
|
||||
}
|
||||
|
||||
if (!needReissue) {
|
||||
String errMsg = String.format(
|
||||
"http receive status fail value=%d adapterName=%s.%s uri=%s method=%s response Data=%s",
|
||||
status, vo.getAdapterGroupName(), vo.getAdapterName(), uri, rmethod, responseMessage);
|
||||
throw new Exception(errMsg);
|
||||
}
|
||||
} else {
|
||||
responseMessage = method.getResponseBody();
|
||||
String errMsg = String.format(
|
||||
"http receive status fail value=%d adapterName=%s.%s uri=%s method=%s response Data=%s",
|
||||
status, vo.getAdapterGroupName(), vo.getAdapterName(), uri, rmethod, responseMessage);
|
||||
throw new Exception(errMsg);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRest] responseType =" + vo.getResponseType());
|
||||
logger.debug("HttpClientAdapterServiceRest] RECV " + vo.getEncode() + "(" + vo.getAdapterGroupName()
|
||||
+ ") = [" + new String(responseMessage, vo.getEncode()) + "]");
|
||||
logger.debug("HttpClientAdapterServiceRest] RECV MS949 (" + vo.getAdapterGroupName() + ") = ["
|
||||
+ new String(responseMessage, "MS949") + "]");
|
||||
logger.debug("HttpClientAdapterServiceRest] RECV euc-kr (" + vo.getAdapterGroupName() + ") = ["
|
||||
+ new String(responseMessage, "euc-kr") + "]");
|
||||
logger.debug("HttpClientAdapterServiceRest] RECV (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
if (useAdapterToken) {
|
||||
if (responseMessage == null) {
|
||||
throw new Exception("responseMessage is NULL, HttpStatus=" + status);
|
||||
}
|
||||
|
||||
// OAuth 토큰 재요청 코드 확인
|
||||
if (needReissue) {
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRest] retry access token response code = ["
|
||||
+ vo.getResponseType() + "] message = [" + new String(responseMessage, vo.getEncode())
|
||||
+ "]");
|
||||
}
|
||||
|
||||
// 토큰 재발급
|
||||
AccessTokenManager tokenManager = AccessTokenManager.getInstance();
|
||||
String oldToken = accessToken == null ? null : accessToken.getAccessToken();
|
||||
OAuth2AccessTokenVO newaccessToken = (OAuth2AccessTokenVO) tokenManager
|
||||
.retryAccessTokenVO(vo.getAdapterGroupName(), prop, oldToken);
|
||||
method.setRequestHeader("Authorization", newaccessToken.getAuthorization());
|
||||
|
||||
setAuthHeaders(method, newaccessToken);
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRest] SEND (" + vo.getAdapterGroupName()
|
||||
+ ") RETRY TOKEN = [" + newaccessToken + "]");
|
||||
}
|
||||
|
||||
try {
|
||||
status = mclient.executeMethod(method);
|
||||
} catch (ConnectException e) {
|
||||
throw new Exception("retry excuteMethod Exceptioin = " + e.getMessage());
|
||||
}
|
||||
|
||||
if (status == 200) {
|
||||
responseMessage = method.getResponseBody();
|
||||
} else if (status == 302) {
|
||||
responseMessage = method.getResponseBody();
|
||||
if (!StringUtils.contains(relayResponseHeaderKeys, "Location")) {
|
||||
relayResponseHeaderKeys += ",Location";
|
||||
}
|
||||
} else {
|
||||
responseMessage = method.getResponseBody();
|
||||
String errMsg = String.format(
|
||||
"http receive status fail value=%d adapterName=%s.%s uri=%s method=%s response Data=%s",
|
||||
status, vo.getAdapterGroupName(), vo.getAdapterName(), uri, rmethod, responseMessage);
|
||||
throw new Exception(errMsg);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRest] RETRY responseType =" + vo.getResponseType());
|
||||
logger.debug("HttpClientAdapterServiceRest] RETRY RECV " + vo.getEncode() + "("
|
||||
+ vo.getAdapterGroupName() + ") = [" + new String(responseMessage, vo.getEncode())
|
||||
+ "]");
|
||||
logger.debug("HttpClientAdapterServiceRest] RETRY RECV (" + vo.getAdapterGroupName() + ") = "
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
// ----------------------------------------------------------
|
||||
}
|
||||
}
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"RECV [" + new String(responseMessage, vo.getEncode()) + "]"
|
||||
+ CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
assignRelayDataToInbound(tempProp, method.getResponseHeaders());
|
||||
|
||||
return assignResponseHeaders(method, responseMessage, headerGroupName, vo.getEncode(), messageType,
|
||||
relayResponseHeaderKeys, status);
|
||||
} catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceRest] SocketTimeoutException (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ste.toString(),
|
||||
ste);
|
||||
}
|
||||
logger.error("HttpClientAdapterServiceRest] SocketTimeoutException (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ste.toString(), ste);
|
||||
throw ste;
|
||||
} catch (ConnectException ce) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceRest] Connection Exception (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ce.toString(),
|
||||
ce);
|
||||
}
|
||||
logger.error("HttpClientAdapterServiceRest] Connection Exception (" + vo.getAdapterGroupName() + ") : "
|
||||
+ ce.toString(), ce);
|
||||
throw ce;
|
||||
} catch (Exception e) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(), e.toString(), e);
|
||||
}
|
||||
logger.error("HttpClientAdapterServiceRest] Exception (" + vo.getAdapterGroupName() + ") : " + e.toString(),
|
||||
e);
|
||||
throw e;
|
||||
} finally {
|
||||
method.releaseConnection();
|
||||
if (status != HttpStatus.SC_OK) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = String.valueOf(status);
|
||||
String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug(resMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void assignRelayDataToInbound(Properties prop, Header[] responseHeaders) {
|
||||
if (responseHeaders == null || responseHeaders.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
Properties headerProp = new Properties();
|
||||
for (Header header : responseHeaders) {
|
||||
headerProp.put(header.getName(), header.getValue());
|
||||
}
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put(HttpAdapterServiceKey.OUTBOUND_RESPONSE_HEADERS, headerProp);
|
||||
|
||||
prop.put(HttpAdapterServiceKey.OUTBOUND_PROPERTY_MAP, map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 현재 1단계 레이아웃에만 적용되도록 구현됨.
|
||||
*
|
||||
* @param method
|
||||
* @param responseMessage
|
||||
* @param headerGroupName
|
||||
* @param encode
|
||||
* @param messageType
|
||||
* @param relayHeaderKeys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private String assignResponseHeaders(HttpMethodBase method, byte[] responseMessage, String headerGroupName,
|
||||
String encode, String messageType, String relayHeaderKeys, int status) throws Exception {
|
||||
if (!MessageType.JSON.equals(messageType) || StringUtils.isBlank(headerGroupName)
|
||||
|| StringUtils.isBlank(relayHeaderKeys)) {
|
||||
return new String(responseMessage, encode);
|
||||
}
|
||||
|
||||
String[] relayKeyArr = org.springframework.util.StringUtils.tokenizeToStringArray(relayHeaderKeys, ",");
|
||||
JSONObject headerJson = new JSONObject();
|
||||
for (String key : relayKeyArr) {
|
||||
Header header = method.getResponseHeader(key);
|
||||
if (header != null) {
|
||||
headerJson.put(header.getName(), header.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
headerJson.put(HTTP_STATUS, String.valueOf(status));
|
||||
|
||||
if (headerJson.size() <= 0) {
|
||||
return new String(responseMessage, encode);
|
||||
}
|
||||
|
||||
JSONObject message = null;
|
||||
if (responseMessage == null || responseMessage.length == 0) {
|
||||
message = new JSONObject();
|
||||
} else {
|
||||
message = parseJson(new String(responseMessage, encode));
|
||||
if (message == null) {
|
||||
message = new JSONObject();
|
||||
message.put("Malformed_Response_Message", new String(responseMessage, encode));
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(headerGroupName) && message != null)
|
||||
message.put(headerGroupName, headerJson);
|
||||
return message.toJSONString();
|
||||
}
|
||||
|
||||
private void setAuthHeaders(HttpMethodBase method, OAuth2AccessTokenVO accessToken) throws Exception {
|
||||
method.setRequestHeader("Authorization",
|
||||
String.format("%s %s", AccessTokenVO.BEARER_TYPE, accessToken.getAccessToken()));
|
||||
}
|
||||
|
||||
private boolean checkTokenRetry(byte[] responseMessage, String tokenErrorCodeKey, String tokenErrorCodeValues,
|
||||
String encode) {
|
||||
if (responseMessage == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(tokenErrorCodeKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(tokenErrorCodeValues)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
DocumentContext jsonContext = JsonPath.parse(new String(responseMessage, encode));
|
||||
String responseCode = jsonContext.read(tokenErrorCodeKey);
|
||||
if (StringUtils.isBlank(responseCode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] arr = org.springframework.util.StringUtils.tokenizeToStringArray(tokenErrorCodeValues, ",");
|
||||
return ArrayUtils.contains(arr, responseCode);
|
||||
} catch (Exception e) {
|
||||
logger.error("HttpClientAdapterServiceRest] checkTokenRetry error=" + e.getMessage());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void assignRequestHeaders(HttpMethodBase method, Object httpHeader) {
|
||||
if (httpHeader == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (httpHeader instanceof JSONObject) {
|
||||
JSONObject jsonObject = (JSONObject) httpHeader;
|
||||
for (Object key : jsonObject.keySet()) {
|
||||
Object obj = jsonObject.get(key);
|
||||
if ((obj instanceof JSONObject) || (obj instanceof JSONArray)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
method.setRequestHeader((String) key, (String) obj);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Request Header :" + (String) key + "=[" + (String) obj + "]");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void assignPostBody(Object eaiBody, String contentType, HttpMethodBase method) {
|
||||
if (eaiBody == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (StringUtils.contains(contentType, "application/x-www-form-urlencoded")) {
|
||||
handleJsonValue("", eaiBody, method);
|
||||
} else {
|
||||
((EntityEnclosingMethod) method).setRequestBody(eaiBody.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void handleJsonValue(String keyPath, Object value, HttpMethodBase method) {
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (value instanceof JSONObject) {
|
||||
handleJsonObject(keyPath, (JSONObject) value, method);
|
||||
} else if (value instanceof JSONArray) {
|
||||
handleJsonArray(keyPath, (JSONArray) value, method);
|
||||
} else {
|
||||
String valueStr = String.valueOf(value);
|
||||
if (StringUtils.isBlank(keyPath)) {
|
||||
((EntityEnclosingMethod) method).setRequestBody(valueStr);
|
||||
} else {
|
||||
if (keyPath.endsWith("]")) {
|
||||
((PostMethod) method).addParameter(keyPath, valueStr);
|
||||
} else {
|
||||
((PostMethod) method).setParameter(keyPath, valueStr);
|
||||
}
|
||||
}
|
||||
logger.debug(keyPath + "=" + valueStr);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* req_list[0].tran_no=12345<br>
|
||||
* req_list[0].org_tran_amt=12000<br>
|
||||
* req_list[1].tran_no=12445<br>
|
||||
* req_list[1].org_tran_amt=11000
|
||||
*/
|
||||
private void handleJsonArray(String keyPath, JSONArray jsonArray, HttpMethodBase method) {
|
||||
for (int i = 0; i < jsonArray.size(); i++) {
|
||||
Object object = jsonArray.get(i);
|
||||
String newKeyPath = null;
|
||||
if (StringUtils.isBlank(keyPath)) {
|
||||
newKeyPath = "NA[" + i + "]";
|
||||
} else {
|
||||
newKeyPath = keyPath + "[" + i + "]";
|
||||
}
|
||||
handleJsonValue(newKeyPath, object, method);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* obj.aaa=111<br>
|
||||
* obj.bbb=222<br>
|
||||
*/
|
||||
private void handleJsonObject(String keyPath, JSONObject jsonObject, HttpMethodBase method) {
|
||||
for (Object key : jsonObject.keySet()) {
|
||||
Object object = jsonObject.get(key);
|
||||
String newKeyPath = StringUtils.isBlank(keyPath) ? "" : keyPath + ".";
|
||||
newKeyPath += key;
|
||||
handleJsonValue(newKeyPath, object, method);
|
||||
}
|
||||
}
|
||||
|
||||
private HashMap<String, String> getParameters(Object message) throws Exception {
|
||||
|
||||
HashMap<String, String> result = new HashMap<String, String>();
|
||||
|
||||
if (message != null) {
|
||||
if (message instanceof JSONObject) {
|
||||
JSONObject jsonObject = (JSONObject) message;
|
||||
for (Object key : jsonObject.keySet()) {
|
||||
Object obj = jsonObject.get(key);
|
||||
if ((obj instanceof JSONObject) || (obj instanceof JSONArray)) {
|
||||
continue;
|
||||
}
|
||||
result.put((String) key, getStringValue(obj));
|
||||
}
|
||||
} else {
|
||||
String[] messages = ((String) message).split("&");
|
||||
String[] data = null;
|
||||
|
||||
for (int i = 0; i < messages.length; i++) {
|
||||
data = messages[i].split("=", 2);
|
||||
if (data.length == 2) {
|
||||
result.put(data[0], data[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getStringValue(Object obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (obj instanceof String) {
|
||||
return (String) obj;
|
||||
} else if (obj instanceof Long) {
|
||||
return Long.toString((Long) obj);
|
||||
} else if (obj instanceof BigDecimal) {
|
||||
return ((BigDecimal) obj).toPlainString();
|
||||
} else {
|
||||
return (String) obj;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private JSONObject parseJson(String message) throws Exception {
|
||||
if (message == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (JSONObject) JSONValue.parse(message);
|
||||
}
|
||||
|
||||
private Document convertXmlDocument(String message) throws Exception {
|
||||
SAXReader builder = new SAXReader();
|
||||
Document document = builder.read(new StringReader(message));
|
||||
return document;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private String changeUrl(String messageType, String url, String restOption, Object sendData) {
|
||||
if ((MessageType.JSON.equals(messageType) || MessageType.XML.equals(messageType))) {
|
||||
try {
|
||||
if (StringUtils.isBlank(restOption)) {
|
||||
return url;
|
||||
}
|
||||
List<String> urlVariableList = new ArrayList<String>();
|
||||
JSONObject restOptionObject = parseJson(restOption);
|
||||
|
||||
if (restOptionObject == null)
|
||||
throw new Exception("restOption is NULL");
|
||||
|
||||
String type = (String) restOptionObject.get("type");
|
||||
String requestExtraPath = (String) restOptionObject.get("extraPath");
|
||||
url = getUrl(url, requestExtraPath);
|
||||
if (TYPE_VARIABLE_URL_REQUEST.equals(type)) {
|
||||
JSONArray uriVariables = (JSONArray) restOptionObject.get("uriVariables");
|
||||
if (MessageType.JSON.equals(messageType)) {
|
||||
JSONObject jsonObject;
|
||||
if (sendData instanceof JSONObject) {
|
||||
jsonObject = (JSONObject) sendData;
|
||||
} else {
|
||||
jsonObject = (JSONObject) JSONValue.parse((String) sendData);
|
||||
}
|
||||
for (Object tempObject : uriVariables) {
|
||||
String urlVaribleId = (String) tempObject;
|
||||
String uriVariable = (String) jsonObject.get(urlVaribleId);
|
||||
urlVariableList.add(uriVariable);
|
||||
jsonObject.remove(urlVaribleId);
|
||||
}
|
||||
} else if (MessageType.XML.equals(messageType)) {
|
||||
Document doc;
|
||||
if (sendData instanceof Document) {
|
||||
doc = (Document) sendData;
|
||||
} else {
|
||||
doc = convertXmlDocument((String) sendData);
|
||||
}
|
||||
for (Object tempObject : uriVariables) {
|
||||
String urlVaribleId = (String) tempObject;
|
||||
Element element = (Element) doc.selectSingleNode("//" + urlVaribleId);
|
||||
urlVariableList.add(element.getText());
|
||||
if (doc.getRootElement() == element) {
|
||||
doc = null;
|
||||
} else {
|
||||
element.getParent().remove(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (urlVariableList.size() > 0) {
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).build();
|
||||
Object[] urls = urlVariableList.toArray();
|
||||
url = uriComponents.expand(urls).toUriString();
|
||||
logger.debug("HttpClientAdapterServiceRest] after extand url=[" + url + "] ");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to change url", e);
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
private String getUrl(String baseUrl, String extraPath) {
|
||||
if (StringUtils.isBlank(extraPath)) {
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
if (StringUtils.contains(extraPath, "http://") || StringUtils.contains(extraPath, "https://")) {
|
||||
return extraPath;
|
||||
}
|
||||
|
||||
String targetUrl = baseUrl;
|
||||
if (!targetUrl.endsWith("/")) {
|
||||
targetUrl += "/";
|
||||
}
|
||||
if (extraPath.startsWith("/")) {
|
||||
targetUrl += extraPath.substring(1);
|
||||
} else {
|
||||
targetUrl += extraPath;
|
||||
}
|
||||
|
||||
logger.debug("HttpClientAdapterServiceRest] after concatenate url=[" + targetUrl + "] ");
|
||||
return targetUrl;
|
||||
}
|
||||
}
|
||||
+546
@@ -0,0 +1,546 @@
|
||||
//
|
||||
//package com.eactive.eai.adapter.http.client.impl;
|
||||
//
|
||||
//import java.io.StringReader;
|
||||
//import java.net.ConnectException;
|
||||
//import java.net.SocketTimeoutException;
|
||||
//import java.net.URLDecoder;
|
||||
//import java.net.URLEncoder;
|
||||
//import java.util.Arrays;
|
||||
//import java.util.HashMap;
|
||||
//import java.util.List;
|
||||
//import java.util.Map;
|
||||
//import java.util.Properties;
|
||||
//
|
||||
//import org.apache.commons.httpclient.HttpClient;
|
||||
//import org.apache.commons.httpclient.HttpMethodBase;
|
||||
//import org.apache.commons.httpclient.HttpStatus;
|
||||
//import org.apache.commons.httpclient.HttpVersion;
|
||||
//import org.apache.commons.httpclient.NameValuePair;
|
||||
//import org.apache.commons.httpclient.methods.DeleteMethod;
|
||||
//import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
|
||||
//import org.apache.commons.httpclient.methods.GetMethod;
|
||||
//import org.apache.commons.httpclient.methods.PostMethod;
|
||||
//import org.apache.commons.httpclient.methods.PutMethod;
|
||||
//import org.apache.commons.httpclient.protocol.Protocol;
|
||||
//import org.apache.commons.lang.StringUtils;
|
||||
//import org.apache.commons.lang.time.StopWatch;
|
||||
//import org.dom4j.Document;
|
||||
//import org.dom4j.Element;
|
||||
//import org.dom4j.io.SAXReader;
|
||||
//import org.json.simple.JSONObject;
|
||||
//import org.json.simple.JSONValue;
|
||||
//import org.springframework.web.util.UriComponents;
|
||||
//import org.springframework.web.util.UriComponentsBuilder;
|
||||
//
|
||||
//import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
//import com.eactive.eai.adapter.http.HttpMethodType;
|
||||
//import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceKey;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
//import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
//import com.eactive.eai.adapter.http.secure.CustomHttpsSocketFactory;
|
||||
//import com.eactive.eai.adapter.http.secure.EasySSLProtocolSocketFactory;
|
||||
//import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
//import com.eactive.eai.common.message.MessageType;
|
||||
//import com.eactive.eai.common.util.CommonLib;
|
||||
//import com.openbanking.eai.common.token.AccessTokenManager;
|
||||
//import com.openbanking.eai.common.token.OAuth2AccessTokenVO;
|
||||
//
|
||||
//
|
||||
///**
|
||||
//* 1. 기능 : EAI HTTP OutBound 용 어댑터로 수동 시스템의 HTTP 웹 컴포넌트를
|
||||
//* GET/POST 방식으로 호출할 수 있는 기능을 제공한다.
|
||||
//* 2. 처리 개요 :
|
||||
//* * - 2009.11.24 retry 로직 제거 : 요청
|
||||
//* 3. 주의사항
|
||||
//*
|
||||
//* @author :
|
||||
//* @version : v 1.0.0
|
||||
//* @see :
|
||||
//* @since :
|
||||
//
|
||||
//*/
|
||||
//public class HttpClientAdapterServiceRestForCODEF extends HttpClientAdapterServiceSupport implements HttpClientAdapterServiceKey {
|
||||
//
|
||||
// public static final String TYPE_VARIABLE_URL_REQUEST = "variableUrlRequest";
|
||||
// public static final String TYPE_SIMPLE_REQUEST = "simpleRequest";
|
||||
//
|
||||
// public static final String HTTP_HEADER = "HTTP_HEADER";
|
||||
// public static final String EAI_HEADER = "EAI_HEADER";
|
||||
// public static final String EAI_BODY = "EAI_BODY";
|
||||
//
|
||||
// public static final String REST_OPTION = "REST_OPTION";
|
||||
// public static final String REST_TYPE = "type";
|
||||
// public static final String REST_EXTRA_PATH = "extraPath";
|
||||
// public static final String REST_METHOD = "method";
|
||||
//
|
||||
// public static final String HEADER_CONTENT_TYPE = "Content-Type";
|
||||
// public static final String HEADER_ACEPT = "Acept";
|
||||
//
|
||||
// public static final List<String> RETRY_RESPONSE_CODE = Arrays.asList("invalid_token");
|
||||
//
|
||||
// /**
|
||||
// * 1. 기능 : 전문과 같은 Raw Data 통신에 사용
|
||||
// * 2. 처리 개요 :
|
||||
// * - 속성 정보를 설정 하고 수동 시스템 서비스를 호출 한다.
|
||||
// * 3. 주의사항
|
||||
// *
|
||||
// * @param prop Http Adapter 속성 정보
|
||||
// * @param message 송신 전문
|
||||
// * @return 반환 된 byte[] Object
|
||||
// * @exception Exception 수동 시스템 간 통신 중 발생
|
||||
// **/
|
||||
// public Object execute(Properties prop, byte[] data, Properties tempProp) throws Exception {
|
||||
// HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
//
|
||||
// // 레이아웃 메세지 타입 REST URL 추출 시 활용. Default JSON
|
||||
// String messageType = prop.getProperty("MESSAGE_TYPE", MessageType.JSON);
|
||||
// /* TSEAIHE02.RESTOPTION 정보 -> JSON 형태로 구성
|
||||
// * type : simpleRequest, variableUrlRequest
|
||||
// * extraPath : 어댑터 프로퍼티 URL에 추가될 HTTP REST URL
|
||||
// * method : get, delete, post, put
|
||||
// *
|
||||
// * ex) {"type":"simpleRequest", "extraPath":"v2.0/account/balance", "method": "post"}
|
||||
// */
|
||||
//// String restOptionData = tempProp.getProperty(REST_OPTION);
|
||||
// String restOptionData = prop.getProperty(REST_OPTION);
|
||||
//
|
||||
// JSONObject restOptionObject = parseJson(restOptionData);
|
||||
// if(restOptionObject == null) {
|
||||
// throw new Exception("OptionData parsing result is NULL");
|
||||
// }
|
||||
// String rmethod = restOptionObject.containsKey("method")
|
||||
// ? StringUtils.upperCase((String) restOptionObject.get("method"))
|
||||
// : "POST";
|
||||
//
|
||||
// String sendData = new String(data);
|
||||
// Object eaiHeader = null;
|
||||
// Object eaiBody = null;
|
||||
// HttpClient mclient = this.client;
|
||||
//
|
||||
// HashMap<String,String> header = new HashMap<String,String>();
|
||||
//// for(Object obj : headerObject.keySet()){
|
||||
//// String key = (String)obj;
|
||||
//// header.put(key, (String)headerObject.get(key));
|
||||
//// }
|
||||
// //interface 에 설정된게 우선한다.
|
||||
// String uri = changeUrl(messageType, vo.getUrl(), restOptionData, sendData);
|
||||
//
|
||||
// HttpMethodBase method = null;
|
||||
// switch (HttpMethodType.getValue(rmethod)) {
|
||||
// case GET :
|
||||
// method = new GetMethod(uri);
|
||||
// break;
|
||||
// case DELETE :
|
||||
// method = new DeleteMethod(uri);
|
||||
// break;
|
||||
// case POST :
|
||||
// method = new PostMethod(uri);
|
||||
// break;
|
||||
// case PUT :
|
||||
// method = new PutMethod(uri);
|
||||
// break;
|
||||
// default :
|
||||
// // set default method
|
||||
// method = new GetMethod(uri);
|
||||
// break;
|
||||
// }
|
||||
//
|
||||
// //2008.02.15 : Timeout 처리로직 변경 (0 : 무한대 설정가능하게 함)
|
||||
// if(vo.getTimeout() > -1) {
|
||||
// method.getParams().setSoTimeout(vo.getTimeout());
|
||||
// }
|
||||
//
|
||||
// if ( vo.getConnectionTimeout() != 0) {
|
||||
// method.getParams().setParameter("http.connection.timeout", vo.getConnectionTimeout());
|
||||
// }
|
||||
// method.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0);
|
||||
//
|
||||
// if(uri != null && uri.startsWith("https")){
|
||||
// Protocol.registerProtocol("https", new Protocol("https",
|
||||
// new CustomHttpsSocketFactory(new EasySSLProtocolSocketFactory()), 443));
|
||||
// }
|
||||
//
|
||||
// if (logger.isDebug()&& "N".equals(vo.getTestCallYn())) {
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] SEND ("+vo.getAdapterGroupName() +") URL=["+vo.getUrl()+"] ");
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] SEND ("+vo.getAdapterGroupName() +") PARAMETER_NAME=["+vo.getParameterName()+"] ");
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] SEND ("+vo.getAdapterGroupName() +") ENCODE=["+vo.getEncode()+"] ");
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] SEND ("+vo.getAdapterGroupName() +") RESPONSE_TYPE=["+vo.getResponseType()+"] ");
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] SEND ("+vo.getAdapterGroupName() +") URL_ENCODE_YN=["+vo.getUrlEncodeYn()+"] ");
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] SEND ("+vo.getAdapterGroupName() +") CONNECTION_TIMEOUT=["+vo.getConnectionTimeout()+"] ");
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] SEND ("+vo.getAdapterGroupName() +") REST_OPTION=["+restOptionData+"] ");
|
||||
// }
|
||||
//
|
||||
// if(vo.getEncode() != null && vo.getEncode().length() > 0) {
|
||||
// try {
|
||||
// mclient.getParams().setContentCharset(vo.getEncode());
|
||||
// sendData = new String(data, vo.getEncode());
|
||||
// } catch(Exception e) {
|
||||
// if(logger.isError()){
|
||||
// logger.error("HttpClientAdapterServiceRestForCODEF] ENCODE("+vo.getEncode()+") ERROR : "+ e.toString());
|
||||
// logger.error("HttpClientAdapterServiceRestForCODEF] ENCODE("+vo.getEncode()+") SKIP ");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//// if ("Y".equals(urlEncodeYn)) {
|
||||
//// sendData = java.net.URLEncoder.encode(sendData);
|
||||
//// }
|
||||
//
|
||||
// /* 레이아웃 메시지 타입이 JSON 형태만 처리 구현. REST를 처리하기 위해 아래 구성을 따라야함
|
||||
// * REST 레이아웃 구성
|
||||
// * HTTP_HEADER (X) : HTTP 헤더에 추가 정보가 필요할 경우 정의
|
||||
// * EAI_HEADER
|
||||
// * EAI_BODY
|
||||
// */
|
||||
// if (MessageType.JSON.equals(messageType)) {
|
||||
// JSONObject dataObject = parseJson(sendData);
|
||||
// if(dataObject != null) {
|
||||
// eaiHeader = dataObject.containsKey(EAI_HEADER) ? dataObject.get(EAI_HEADER) : null;
|
||||
// eaiBody = dataObject.containsKey(EAI_BODY) ? dataObject.get(EAI_BODY) : null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// switch (HttpMethodType.getValue(rmethod)) {
|
||||
// case GET :
|
||||
// case DELETE :
|
||||
// HashMap<String,String> h = getParameters(eaiHeader);
|
||||
// NameValuePair[] params = new NameValuePair[h.size()];
|
||||
// int count =0;
|
||||
// for(String key : h.keySet()){
|
||||
// String value = h.get(key);
|
||||
// params[count] = new NameValuePair(key, URLEncoder.encode(value, vo.getEncode()));
|
||||
// count++;
|
||||
// }
|
||||
// method.setQueryString(params);
|
||||
// break;
|
||||
// case PUT :
|
||||
// case POST :
|
||||
// // OpenAPI는 json 타입. type이 다른 경우 헤더정보를 별도로 받아야함
|
||||
// method.setRequestHeader(HEADER_CONTENT_TYPE, "application/json; charset="+vo.getEncode());
|
||||
// if(eaiBody != null) {
|
||||
// ((EntityEnclosingMethod) method).setRequestBody(URLEncoder.encode(eaiBody.toString(), vo.getEncode()));
|
||||
// }
|
||||
//
|
||||
// break;
|
||||
// default :
|
||||
// break;
|
||||
// }
|
||||
//
|
||||
// AccessTokenManager tokenManager = AccessTokenManager.getInstance();
|
||||
// OAuth2AccessTokenVO accessToken = (OAuth2AccessTokenVO) tokenManager.getAccessTokenVO(vo.getAdapterGroupName());
|
||||
//
|
||||
// // 토큰이 없거나 토큰이 만료 됐으면 재발급
|
||||
// if (accessToken == null || accessToken.isExpired()) {
|
||||
// accessToken = (OAuth2AccessTokenVO) tokenManager.retryAccessTokenVO(vo.getAdapterGroupName(), prop);
|
||||
// }
|
||||
//
|
||||
// method.setRequestHeader("Authorization", accessToken.getAuthorization());
|
||||
//
|
||||
// if (logger.isDebug()&& "N".equals(vo.getTestCallYn())) {
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] SEND ("+vo.getAdapterGroupName() +") TOKEN=["+accessToken+"] ");
|
||||
// }
|
||||
//
|
||||
// // header 셋팅
|
||||
// if (header.size() > 0) {
|
||||
// for (String key : header.keySet()) {
|
||||
// method.setRequestHeader(key, header.get(key));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// int status = -1;
|
||||
//
|
||||
// try {
|
||||
//
|
||||
// if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] SEND ("+vo.getAdapterGroupName()+") = ["+sendData+"]");
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] SEND ("+vo.getAdapterGroupName()+") = "+CommonLib.getDumpMessage(sendData.getBytes()));
|
||||
// }
|
||||
// StopWatch stopWatch = new StopWatch();
|
||||
// stopWatch.start();
|
||||
//
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(), "SEND ["+sendData+"]"+CommonLib.getDumpMessage(sendData.getBytes()));
|
||||
// }
|
||||
// status = mclient.executeMethod(method);
|
||||
//
|
||||
// stopWatch.stop();
|
||||
//
|
||||
// //status 200(정상) 이외에는 error 처리
|
||||
// if (status !=200){
|
||||
// try{
|
||||
// logger.error("HttpClientAdapterServiceRestForCODEF] receive status["+status+"] data[" + new String(method.getResponseBody()) +"]");
|
||||
// }catch(Exception e){
|
||||
// logger.error(e);
|
||||
// }
|
||||
// throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="+vo.getAdapterGroupName()+"."+vo.getAdapterName(),status);
|
||||
// }
|
||||
//
|
||||
// byte[] responseMessage = method.getResponseBody();
|
||||
//
|
||||
// //----------------------------------------------------------
|
||||
// if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] responseType ="+ vo.getResponseType());
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] RECV ("+vo.getAdapterGroupName()+") = ["+new String(responseMessage)+"]");
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] RECV ("+vo.getAdapterGroupName()+") = "+CommonLib.getDumpMessage(responseMessage));
|
||||
// }
|
||||
// if ("ASYNC".equals(vo.getResponseType())) {
|
||||
// if (stopWatch.getTime() > vo.getSlowTranTime()) {
|
||||
// if (logger.isInfo()){
|
||||
// logger.info("HttpClientAdapterServiceRestForCODEF] dummy response time = " + stopWatch.toString()+ " responseMessage=" + new String(responseMessage));
|
||||
// }
|
||||
// }
|
||||
// if ( ! java.util.Arrays.equals(ElinkConfig.getAsyncDummyDataForAdapterGroup(adptGrpName).getBytes(), responseMessage)){
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"http return fail value= ["+responseMessage+"]" + CommonLib.getDumpMessage(responseMessage));
|
||||
// }
|
||||
// throw new Exception ("http return fail value= " + new String(responseMessage));
|
||||
// }
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(),"RECV ["+new String(responseMessage)+"]"+CommonLib.getDumpMessage(responseMessage));
|
||||
// }
|
||||
// return null;
|
||||
// } else {
|
||||
// /* OAuth 토큰 응답 체크
|
||||
// *
|
||||
// * OAuth 응답 코드
|
||||
// * invalid_token
|
||||
// */
|
||||
// JSONObject message = parseJson(URLDecoder.decode(new String(responseMessage), vo.getEncode()));
|
||||
// if(message == null) {
|
||||
// throw new Exception("responseMessage parsing result is NULL");
|
||||
// }
|
||||
//
|
||||
// String rspCode = (String) message.get("error");
|
||||
// String rspMessage = (String) message.get("message");
|
||||
//
|
||||
// // OAuth 토큰 재요청 코드 확인
|
||||
// if (RETRY_RESPONSE_CODE.contains(rspCode)) {
|
||||
// if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] retry access token response code= ["+rspCode+"] message= ["+rspMessage+"]");
|
||||
// }
|
||||
//
|
||||
// // 토큰 재발급
|
||||
// accessToken = (OAuth2AccessTokenVO) tokenManager.retryAccessTokenVO(vo.getAdapterGroupName(), prop);
|
||||
// method.setRequestHeader("Authorization", accessToken.getAuthorization());
|
||||
//
|
||||
// if (logger.isDebug()&& "N".equals(vo.getTestCallYn())) {
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] SEND ("+vo.getAdapterGroupName() +") RETRY TOKEN=["+accessToken+"] ");
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// status = mclient.executeMethod(method);
|
||||
// } catch (java.net.ConnectException e ) {
|
||||
// throw new Exception ("retry excuteMethod Exceptioin = " + e.toString());
|
||||
// }
|
||||
//
|
||||
// //status 200(정상) 이외에는 error 처리
|
||||
// if (status !=200) {
|
||||
// throw new Exception ("retry http receive status fail value= " + status + " adapterName="+vo.getAdapterGroupName()+"."+vo.getAdapterName() + " uri =" +uri +" method=" +rmethod);
|
||||
// }
|
||||
//
|
||||
// responseMessage = method.getResponseBody();
|
||||
//
|
||||
// if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] responseType ="+ vo.getResponseType());
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] retry RECV ("+vo.getAdapterGroupName()+") = ["+new String(responseMessage)+"]");
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] retry RECV ("+vo.getAdapterGroupName()+") = "+CommonLib.getDumpMessage(responseMessage));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(), "RECV ["+new String(responseMessage)+"]"+CommonLib.getDumpMessage(responseMessage));
|
||||
// }
|
||||
//
|
||||
// String decodedMsg = URLDecoder.decode(new String(responseMessage), vo.getEncode());
|
||||
//
|
||||
// return decodedMsg.getBytes(vo.getEncode());
|
||||
// } catch(SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServiceRestForCODEF] SocketTimeoutException ("+vo.getAdapterGroupName()+") : " + ste.toString(),ste);
|
||||
// }
|
||||
// logger.error("HttpClientAdapterServiceRestForCODEF] SocketTimeoutException ("+vo.getAdapterGroupName()+") : " + ste.toString() ,ste);
|
||||
// throw ste;
|
||||
// } catch(ConnectException ce) {
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServiceRestForCODEF] Connection Exception ("+vo.getAdapterGroupName()+") : " + ce.toString(),ce);
|
||||
// }
|
||||
// logger.error("HttpClientAdapterServiceRestForCODEF] Connection Exception ("+vo.getAdapterGroupName()+") : " + ce.toString() ,ce);
|
||||
// throw ce;
|
||||
// } catch(Exception e) {
|
||||
// if (vo.getTraceLevel() >= 3) {
|
||||
// HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),e.toString(),e);
|
||||
// }
|
||||
// logger.error("HttpClientAdapterServiceRestForCODEF] Exception ("+vo.getAdapterGroupName()+") : " + e.toString() ,e);
|
||||
// throw e;
|
||||
// } finally {
|
||||
// if(method != null) method.releaseConnection();
|
||||
// if(status != HttpStatus.SC_OK) {
|
||||
// String[] msgArgs = new String[1];
|
||||
// msgArgs[0] = String.valueOf(status);
|
||||
// String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
// if(logger.isDebug()){
|
||||
// logger.debug(resMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// private HashMap<String, String> getParameters(Object message) throws Exception {
|
||||
//
|
||||
// HashMap<String,String> result = new HashMap<String,String>();
|
||||
//
|
||||
// if (message != null) {
|
||||
// if (message instanceof JSONObject) {
|
||||
// JSONObject jsonObject = (JSONObject) message;
|
||||
// for (Object key : jsonObject.keySet()) {
|
||||
// result.put((String) key, (String) jsonObject.get(key));
|
||||
// }
|
||||
// } else {
|
||||
// String[] messages = ((String) message).split("&");
|
||||
// String[] data = null;
|
||||
//
|
||||
// for (int i=0; i< messages.length;i++) {
|
||||
// data = messages[i].split("=",2);
|
||||
// if (data.length == 2 ){
|
||||
// result.put(data[0], data[1]);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return result;
|
||||
// }
|
||||
//
|
||||
// private JSONObject parseJson(String message) throws Exception {
|
||||
// if (message == null) return null;
|
||||
//
|
||||
// return (JSONObject) JSONValue.parse(message);
|
||||
// }
|
||||
//
|
||||
// private Document convertXmlDocument(String message) throws Exception{
|
||||
// SAXReader builder = new SAXReader();
|
||||
// Document document = builder.read(new StringReader(message));
|
||||
// return document;
|
||||
// }
|
||||
//
|
||||
//// private Map<String, String> changeUrlWithMessage(String messageType, String url, String extraPath, String sendData){
|
||||
//// Map<String, String> responseMap = new HashMap<String, String>();
|
||||
////
|
||||
//// try {
|
||||
//// if (MessageType.JSON.equals(messageType)) {
|
||||
//// JSONObject jsonObject = parseJson(sendData);
|
||||
//// url = changeUrl(messageType, url, extraPath, jsonObject);
|
||||
//// responseMap.put(URL, url);
|
||||
//// } else if (MessageType.XML.equals(messageType)) {
|
||||
//// Document doc = convertXmlDocument(sendData);
|
||||
//// url = changeUrl(messageType, url, extraPath, doc);
|
||||
//// responseMap.put(URL, url);
|
||||
//// }
|
||||
//// } catch (Exception e) {
|
||||
//// logger.error("Failed to change url",e);
|
||||
//// }
|
||||
//// return responseMap;
|
||||
//// }
|
||||
//
|
||||
// private String changeUrl(String messageType, String url, String restOption, Object sendData) {
|
||||
// if ((MessageType.JSON.equals(messageType) || MessageType.XML.equals(messageType))) {
|
||||
// try {
|
||||
// if(StringUtils.isBlank(restOption)) {
|
||||
// return url;
|
||||
// }
|
||||
// JSONObject restOptionObject = parseJson(restOption);
|
||||
// if(restOptionObject == null) throw new Exception("restOption is null");
|
||||
//
|
||||
// String type = (String) restOptionObject.get("type");
|
||||
// String extraPath = (String) restOptionObject.get("extraPath") ;
|
||||
// String method = (String) restOptionObject.get("method");
|
||||
// url = getUrl(url, extraPath);
|
||||
// if (TYPE_VARIABLE_URL_REQUEST.equals(type)) {
|
||||
// HashMap<String, String> uriVariables = new HashMap<String, String>();
|
||||
// if (MessageType.JSON.equals(messageType)) {
|
||||
// JSONObject jsonObject = null;
|
||||
// JSONObject dataObject = null;
|
||||
// if (sendData instanceof JSONObject) {
|
||||
// jsonObject = (JSONObject) sendData;
|
||||
// } else {
|
||||
// jsonObject = (JSONObject) JSONValue.parse((String)sendData);
|
||||
// }
|
||||
//
|
||||
// switch (HttpMethodType.getValue(method)) {
|
||||
// case GET :
|
||||
// case DELETE :
|
||||
// dataObject = jsonObject.containsKey(EAI_HEADER) ? (JSONObject) jsonObject.get(EAI_HEADER) : null;
|
||||
// break;
|
||||
// case PUT :
|
||||
// case POST :
|
||||
// dataObject = jsonObject.containsKey(EAI_BODY) ? (JSONObject) jsonObject.get(EAI_BODY) : null;
|
||||
// break;
|
||||
// }
|
||||
//
|
||||
// if(dataObject != null) {
|
||||
// for (Object key : dataObject.keySet()) {
|
||||
// if (url.indexOf("{" + (String) key + "}") > 0) {
|
||||
// uriVariables.put((String) key, (String) dataObject.get(key));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } else if(MessageType.XML.equals(messageType)) {
|
||||
// Document doc ;
|
||||
// if (sendData instanceof Document) {
|
||||
// doc = (Document) sendData;
|
||||
// } else {
|
||||
// doc = convertXmlDocument((String)sendData);
|
||||
// }
|
||||
// if(doc !=null) {
|
||||
// for (Object branch : doc.content()) {
|
||||
// Element element = (Element) branch;
|
||||
//
|
||||
// if (url.indexOf("{" + element.getName() + "}") > 0) {
|
||||
// uriVariables.put(element.getName(), element.getText());
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//// for (Object tempObject : uriVariables) {
|
||||
//// String urlVaribleId = (String) tempObject;
|
||||
//// Element element = (Element) doc.selectSingleNode("//"+urlVaribleId);
|
||||
//// urlVariableList.add(element.getText());
|
||||
//// if (doc.getRootElement() == element) {
|
||||
////// doc = null;
|
||||
//// break;
|
||||
//// } else {
|
||||
//// element.getParent().remove(element);
|
||||
//// }
|
||||
//// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (uriVariables.size() > 0) {
|
||||
// UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).buildAndExpand(uriVariables);
|
||||
// url = uriComponents.toUriString();
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] after extand url=["+url+"] ");
|
||||
// }
|
||||
// }
|
||||
// } catch(Exception e) {
|
||||
// logger.error("Failed to change url",e);
|
||||
// }
|
||||
// }
|
||||
// return url;
|
||||
// }
|
||||
//
|
||||
// private String getUrl(String baseUrl, String extraPath) {
|
||||
// if (StringUtils.isBlank(extraPath)) {
|
||||
// return baseUrl;
|
||||
// }
|
||||
// if (baseUrl != null && baseUrl.endsWith("/") == false) {
|
||||
// baseUrl += "/";
|
||||
// }
|
||||
// String targetUrl = baseUrl + extraPath;
|
||||
// logger.debug("HttpClientAdapterServiceRestForCODEF] after concatenate url=["+targetUrl+"] ");
|
||||
// return targetUrl;
|
||||
// }
|
||||
//
|
||||
//}
|
||||
+492
@@ -0,0 +1,492 @@
|
||||
package com.eactive.eai.adapter.http.client.impl;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpMethodBase;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.HttpVersion;
|
||||
import org.apache.commons.httpclient.NameValuePair;
|
||||
import org.apache.commons.httpclient.methods.DeleteMethod;
|
||||
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
|
||||
import org.apache.commons.httpclient.methods.GetMethod;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
import org.apache.commons.httpclient.methods.PutMethod;
|
||||
import org.apache.commons.httpclient.params.HttpConnectionParams;
|
||||
import org.apache.commons.httpclient.protocol.Protocol;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.time.StopWatch;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.SAXReader;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpMethodType;
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceKey;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
import com.eactive.eai.adapter.http.secure.CustomHttpsSocketFactory;
|
||||
import com.eactive.eai.adapter.http.secure.EasySSLProtocolSocketFactory;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.message.MessageType;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.eactive.eai.env.ElinkConfig;
|
||||
import com.openbanking.eai.common.token.AccessTokenManager;
|
||||
import com.openbanking.eai.common.token.OAuth2AccessTokenVO;
|
||||
|
||||
public class HttpClientAdapterServiceRestForKFTC extends HttpClientAdapterServiceSupport
|
||||
implements HttpClientAdapterServiceKey {
|
||||
|
||||
public static final String TYPE_VARIABLE_URL_REQUEST = "variableUrlRequest";
|
||||
public static final String TYPE_SIMPLE_REQUEST = "simpleRequest";
|
||||
|
||||
public static final String HTTP_HEADER = "HTTP_HEADER";
|
||||
public static final String EAI_HEADER = "EAI_HEADER";
|
||||
public static final String EAI_BODY = "EAI_BODY";
|
||||
|
||||
public static final String REST_OPTION = "REST_OPTION";
|
||||
public static final String REST_TYPE = "type";
|
||||
public static final String REST_EXTRA_PATH = "extraPath";
|
||||
public static final String REST_METHOD = "method";
|
||||
|
||||
public static final String HEADER_CONTENT_TYPE = "Content-Type";
|
||||
|
||||
public static final List<String> RETRY_RESPONSE_CODE = Arrays.asList("O0001", "O0002", "O0003");
|
||||
|
||||
public Object execute(Properties prop, Object data, Properties tempProp) throws Exception {
|
||||
HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
|
||||
// 레이아웃 메세지 타입 REST URL 추출 시 활용. Default JSON
|
||||
String messageType = prop.getProperty("MESSAGE_TYPE", MessageType.JSON);
|
||||
/* TSEAIHE02.RESTOPTION 정보 -> JSON 형태로 구성
|
||||
* type : simpleRequest, variableUrlRequest
|
||||
* extraPath : 어댑터 프로퍼티 URL에 추가될 HTTP REST URL
|
||||
* method : get, delete, post, put
|
||||
*
|
||||
* ex) {"type":"simpleRequest", "extraPath":"v2.0/account/balance", "method": "post"}
|
||||
*/
|
||||
|
||||
String restOptionData = tempProp.getProperty(REST_OPTION);
|
||||
|
||||
JSONObject restOptionObject = parseJson(restOptionData);
|
||||
if (restOptionObject == null) {
|
||||
throw new Exception("OptionData parsing result is NULL");
|
||||
}
|
||||
String rmethod = restOptionObject.containsKey("method")
|
||||
? StringUtils.upperCase((String) restOptionObject.get("method"))
|
||||
: "POST";
|
||||
|
||||
Object eaiHeader = null;
|
||||
Object eaiBody = null;
|
||||
HttpClient mclient = this.client;
|
||||
|
||||
|
||||
|
||||
String sendData = "";
|
||||
if (data instanceof byte[]) {
|
||||
sendData = new String((byte[]) data, vo.getEncode());
|
||||
} else if (data instanceof String) {
|
||||
sendData = (String) data;
|
||||
}
|
||||
|
||||
HashMap<String,String> header = new HashMap<String,String>();
|
||||
//interface 에 설정된게 우선한다.
|
||||
String uri = changeUrl(messageType, vo.getUrl(), restOptionData, sendData);
|
||||
|
||||
HttpMethodBase method = null;
|
||||
switch (HttpMethodType.getValue(rmethod)) {
|
||||
case GET :
|
||||
method = new GetMethod(uri);
|
||||
break;
|
||||
case DELETE :
|
||||
method = new DeleteMethod(uri);
|
||||
break;
|
||||
case POST :
|
||||
method = new PostMethod(uri);
|
||||
break;
|
||||
case PUT :
|
||||
method = new PutMethod(uri);
|
||||
break;
|
||||
default :
|
||||
// set default method
|
||||
method = new GetMethod(uri);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (logger.isDebug()&& "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] SEND ("+vo.getAdapterGroupName() +") URL=["+vo.getUrl()+"] ");
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] SEND ("+vo.getAdapterGroupName() +") PARAMETER_NAME=["+vo.getParameterName()+"] ");
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] SEND ("+vo.getAdapterGroupName() +") ENCODE=["+vo.getEncode()+"] ");
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] SEND ("+vo.getAdapterGroupName() +") RESPONSE_TYPE=["+vo.getResponseType()+"] ");
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] SEND ("+vo.getAdapterGroupName() +") URL_ENCODE_YN=["+vo.getUrlEncodeYn()+"] ");
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] SEND ("+vo.getAdapterGroupName() +") CONNECTION_TIMEOUT=["+vo.getConnectionTimeout()+"] ");
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] SEND ("+vo.getAdapterGroupName() +") REST_OPTION=["+restOptionData+"] ");
|
||||
}
|
||||
|
||||
configureHttpClient(mclient, method, vo, vo.getUrl(), vo.getContentType());
|
||||
|
||||
if ("Y".equals(vo.getUrlEncodeYn())) {
|
||||
sendData = java.net.URLEncoder.encode(sendData);
|
||||
}
|
||||
|
||||
/* 레이아웃 메시지 타입이 JSON 형태만 처리 구현. REST를 처리하기 위해 아래 구성을 따라야함
|
||||
* REST 레이아웃 구성
|
||||
* HTTP_HEADER (X) : HTTP 헤더에 추가 정보가 필요할 경우 정의
|
||||
* EAI_HEADER
|
||||
* EAI_BODY
|
||||
*/
|
||||
if (MessageType.JSON.equals(messageType)) {
|
||||
JSONObject dataObject = parseJson(sendData);
|
||||
if(dataObject != null) {
|
||||
eaiHeader = dataObject.containsKey(EAI_HEADER) ? dataObject.get(EAI_HEADER) : null;
|
||||
eaiBody = dataObject.containsKey(EAI_BODY) ? dataObject.get(EAI_BODY) : null;
|
||||
}
|
||||
}
|
||||
|
||||
switch (HttpMethodType.getValue(rmethod)) {
|
||||
case GET :
|
||||
case DELETE :
|
||||
HashMap<String,String> h = getParameters(eaiHeader);
|
||||
NameValuePair[] params = new NameValuePair[h.size()];
|
||||
int count =0;
|
||||
for(String key : h.keySet()){
|
||||
String value = h.get(key);
|
||||
params[count] = new NameValuePair(key,value);
|
||||
count++;
|
||||
}
|
||||
method.setQueryString(params);
|
||||
break;
|
||||
case PUT :
|
||||
case POST :
|
||||
// OpenAPI는 json 타입. type이 다른 경우 헤더정보를 별도로 받아야함
|
||||
method.setRequestHeader(HEADER_CONTENT_TYPE, "application/json; charset="+vo.getEncode());
|
||||
if(eaiBody != null) {
|
||||
((EntityEnclosingMethod) method).setRequestBody(eaiBody.toString());
|
||||
}
|
||||
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
}
|
||||
|
||||
AccessTokenManager tokenManager = AccessTokenManager.getInstance();
|
||||
OAuth2AccessTokenVO accessToken = (OAuth2AccessTokenVO) tokenManager.getAccessTokenVO(vo.getAdapterGroupName());
|
||||
|
||||
// 토큰이 없거나 토큰이 만료 됐으면 재발급
|
||||
if (accessToken == null || accessToken.isExpired()) {
|
||||
String oldToken = accessToken == null ? null : accessToken.getAccessToken();
|
||||
accessToken = (OAuth2AccessTokenVO) tokenManager.retryAccessTokenVO(vo.getAdapterGroupName(), prop, oldToken);
|
||||
}
|
||||
|
||||
method.setRequestHeader("Authorization", accessToken.getAuthorization());
|
||||
|
||||
if (logger.isDebug()&& "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] SEND ("+vo.getAdapterGroupName() +") TOKEN=["+accessToken+"] ");
|
||||
}
|
||||
|
||||
// header 셋팅
|
||||
if (header.size() > 0) {
|
||||
for (String key : header.keySet()) {
|
||||
method.setRequestHeader(key, header.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
int status = -1;
|
||||
|
||||
try {
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] SEND ("+vo.getAdapterGroupName()+") = ["+sendData+"]");
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] SEND ("+vo.getAdapterGroupName()+") = "+CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(), "SEND ["+sendData+"]"+CommonLib.getDumpMessage(sendData));
|
||||
}
|
||||
status = mclient.executeMethod(method);
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
//status 200(정상) 이외에는 error 처리
|
||||
if (status !=200){
|
||||
try{
|
||||
logger.error("HttpClientAdapterServiceRestForKFTC] receive status["+status+"] data[" + new String(method.getResponseBody()) +"]");
|
||||
}catch(Exception e){
|
||||
logger.error(e);
|
||||
}
|
||||
throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="+vo.getAdapterGroupName()+"."+vo.getAdapterName(),status);
|
||||
}
|
||||
|
||||
byte[] responseMessage = method.getResponseBody();
|
||||
|
||||
//----------------------------------------------------------
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] responseType ="+ vo.getResponseType());
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] RECV ("+vo.getAdapterGroupName()+") = ["+new String(responseMessage)+"]");
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] RECV ("+vo.getAdapterGroupName()+") = "+CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
if ("ASYNC".equals(vo.getResponseType())) {
|
||||
if (stopWatch.getTime() > vo.getSlowTranTime()) {
|
||||
if (logger.isInfo()){
|
||||
logger.info("HttpClientAdapterServiceRestForKFTC] dummy response time = " + stopWatch.toString()+ " responseMessage=" + new String(responseMessage));
|
||||
}
|
||||
}
|
||||
if ( ! java.util.Arrays.equals(ElinkConfig.getAsyncDummyDataForAdapterGroup(vo.getAdapterGroupName()).getBytes(), responseMessage)){
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"http return fail value= ["+responseMessage+"]" + CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
throw new Exception ("http return fail value= " + new String(responseMessage));
|
||||
}
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(),"RECV ["+new String(responseMessage)+"]"+CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
return null;
|
||||
} else {
|
||||
/* OAuth 토큰 응답 체크
|
||||
*
|
||||
* OAuth 응답 코드
|
||||
* O0001 인증요청 거부-인증 파라미터 오류([error_code])
|
||||
* O0002 Access Token 거부
|
||||
* O0003 Access Token 만료
|
||||
*/
|
||||
JSONObject message = parseJson(new String(responseMessage, vo.getEncode()));
|
||||
if(message == null) {
|
||||
throw new Exception("responseMessage parsing result is NULL");
|
||||
}
|
||||
|
||||
String rspCode = (String) message.get("rsp_code");
|
||||
String rspMessage = (String) message.get("rsp_message");
|
||||
|
||||
// OAuth 토큰 재요청 코드 확인
|
||||
if (RETRY_RESPONSE_CODE.contains(rspCode)) {
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] retry access token response code= ["+rspCode+"] message= ["+rspMessage+"]");
|
||||
}
|
||||
|
||||
// 토큰 재발급
|
||||
String oldToken = accessToken == null ? null : accessToken.getAccessToken();
|
||||
accessToken = (OAuth2AccessTokenVO) tokenManager.retryAccessTokenVO(vo.getAdapterGroupName(), prop, oldToken);
|
||||
method.setRequestHeader("Authorization", accessToken.getAuthorization());
|
||||
|
||||
if (logger.isDebug()&& "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] SEND ("+vo.getAdapterGroupName() +") RETRY TOKEN=["+accessToken+"] ");
|
||||
}
|
||||
|
||||
try {
|
||||
status = mclient.executeMethod(method);
|
||||
} catch (java.net.ConnectException e ) {
|
||||
throw new Exception ("retry excuteMethod Exceptioin = " + e.toString());
|
||||
}
|
||||
|
||||
if (status !=200) {
|
||||
throw new Exception ("retry http receive status fail value= " + status + " adapterName="+vo.getAdapterGroupName()+"."+vo.getAdapterName() + " uri =" +uri +" method=" +rmethod);
|
||||
}
|
||||
|
||||
responseMessage = method.getResponseBody();
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] responseType ="+ vo.getResponseType());
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] retry RECV ("+vo.getAdapterGroupName()+") = ["+new String(responseMessage)+"]");
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] retry RECV ("+vo.getAdapterGroupName()+") = "+CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(vo.getAdapterGroupName()+vo.getAdapterName(), "RECV ["+new String(responseMessage)+"]"+CommonLib.getDumpMessage(responseMessage));
|
||||
}
|
||||
|
||||
return responseMessage;
|
||||
|
||||
} catch(SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServiceRestForKFTC] SocketTimeoutException ("+vo.getAdapterGroupName()+") : " + ste.toString(),ste);
|
||||
}
|
||||
logger.error("HttpClientAdapterServiceRestForKFTC] SocketTimeoutException ("+vo.getAdapterGroupName()+") : " + ste.toString() ,ste);
|
||||
throw ste;
|
||||
} catch(ConnectException ce) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),"HttpClientAdapterServiceRestForKFTC] Connection Exception ("+vo.getAdapterGroupName()+") : " + ce.toString(),ce);
|
||||
}
|
||||
logger.error("HttpClientAdapterServiceRestForKFTC] Connection Exception ("+vo.getAdapterGroupName()+") : " + ce.toString() ,ce);
|
||||
throw ce;
|
||||
} catch(Exception e) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName()+vo.getAdapterName(),e.toString(),e);
|
||||
}
|
||||
logger.error("HttpClientAdapterServiceRestForKFTC] Exception ("+vo.getAdapterGroupName()+") : " + e.toString() ,e);
|
||||
throw e;
|
||||
} finally {
|
||||
if(method != null) method.releaseConnection();
|
||||
if(status != HttpStatus.SC_OK) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = String.valueOf(status);
|
||||
String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
if(logger.isDebug()){
|
||||
logger.debug(resMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private HashMap<String, String> getParameters(Object message) throws Exception {
|
||||
HashMap<String,String> result = new HashMap<String,String>();
|
||||
if (message != null) {
|
||||
if (message instanceof JSONObject) {
|
||||
JSONObject jsonObject = (JSONObject) message;
|
||||
for (Object key : jsonObject.keySet()) {
|
||||
result.put((String) key, (String) jsonObject.get(key));
|
||||
}
|
||||
} else {
|
||||
String[] messages = ((String) message).split("&");
|
||||
String[] data = null;
|
||||
|
||||
for (int i=0; i< messages.length;i++) {
|
||||
data = messages[i].split("=",2);
|
||||
if (data.length == 2 ){
|
||||
result.put(data[0], data[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private JSONObject parseJson(String message) throws Exception {
|
||||
if (message == null) return null;
|
||||
|
||||
return (JSONObject) JSONValue.parse(message);
|
||||
}
|
||||
|
||||
private Document convertXmlDocument(String message) throws Exception{
|
||||
SAXReader builder = new SAXReader();
|
||||
Document document = builder.read(new StringReader(message));
|
||||
return document;
|
||||
}
|
||||
|
||||
// private Map<String, String> changeUrlWithMessage(String messageType, String url, String extraPath, String sendData){
|
||||
// Map<String, String> responseMap = new HashMap<String, String>();
|
||||
//
|
||||
// try {
|
||||
// if (MessageType.JSON.equals(messageType)) {
|
||||
// JSONObject jsonObject = parseJson(sendData);
|
||||
// url = changeUrl(messageType, url, extraPath, jsonObject);
|
||||
// responseMap.put(URL, url);
|
||||
// } else if (MessageType.XML.equals(messageType)) {
|
||||
// Document doc = convertXmlDocument(sendData);
|
||||
// url = changeUrl(messageType, url, extraPath, doc);
|
||||
// responseMap.put(URL, url);
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("Failed to change url",e);
|
||||
// }
|
||||
// return responseMap;
|
||||
// }
|
||||
|
||||
private String changeUrl(String messageType, String url, String restOption, Object sendData) {
|
||||
if ((MessageType.JSON.equals(messageType) || MessageType.XML.equals(messageType))) {
|
||||
try {
|
||||
if (StringUtils.isBlank(restOption)) {
|
||||
return url;
|
||||
}
|
||||
JSONObject restOptionObject = parseJson(restOption);
|
||||
if (restOptionObject == null)
|
||||
throw new Exception("restOption is null");
|
||||
|
||||
String type = (String) restOptionObject.get("type");
|
||||
String extraPath = (String) restOptionObject.get("extraPath");
|
||||
String method = (String) restOptionObject.get("method");
|
||||
url = getUrl(url, extraPath);
|
||||
if (TYPE_VARIABLE_URL_REQUEST.equals(type)) {
|
||||
HashMap<String, String> uriVariables = new HashMap<String, String>();
|
||||
if (MessageType.JSON.equals(messageType)) {
|
||||
JSONObject jsonObject = null;
|
||||
JSONObject dataObject = null;
|
||||
if (sendData instanceof JSONObject) {
|
||||
jsonObject = (JSONObject) sendData;
|
||||
} else {
|
||||
jsonObject = (JSONObject) JSONValue.parse((String) sendData);
|
||||
}
|
||||
|
||||
switch (HttpMethodType.getValue(method)) {
|
||||
case GET:
|
||||
case DELETE:
|
||||
dataObject = jsonObject.containsKey(EAI_HEADER) ? (JSONObject) jsonObject.get(EAI_HEADER)
|
||||
: null;
|
||||
break;
|
||||
case PUT:
|
||||
case POST:
|
||||
dataObject = jsonObject.containsKey(EAI_BODY) ? (JSONObject) jsonObject.get(EAI_BODY)
|
||||
: null;
|
||||
break;
|
||||
}
|
||||
|
||||
if (dataObject != null) {
|
||||
for (Object key : dataObject.keySet()) {
|
||||
if (url.indexOf("{" + (String) key + "}") > 0) {
|
||||
uriVariables.put((String) key, (String) dataObject.get(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (MessageType.XML.equals(messageType)) {
|
||||
Document doc;
|
||||
if (sendData instanceof Document) {
|
||||
doc = (Document) sendData;
|
||||
} else {
|
||||
doc = convertXmlDocument((String) sendData);
|
||||
}
|
||||
if (doc != null) {
|
||||
for (Object branch : doc.content()) {
|
||||
Element element = (Element) branch;
|
||||
|
||||
if (url.indexOf("{" + element.getName() + "}") > 0) {
|
||||
uriVariables.put(element.getName(), element.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (uriVariables.size() > 0) {
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(url)
|
||||
.buildAndExpand(uriVariables);
|
||||
url = uriComponents.toUriString();
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] after extand url=[" + url + "] ");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to change url", e);
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
private String getUrl(String baseUrl, String extraPath) {
|
||||
if (StringUtils.isBlank(extraPath)) {
|
||||
return baseUrl;
|
||||
}
|
||||
if (baseUrl != null && baseUrl.endsWith("/") == false) {
|
||||
baseUrl += "/";
|
||||
}
|
||||
String targetUrl = baseUrl + extraPath;
|
||||
logger.debug("HttpClientAdapterServiceRestForKFTC] after concatenate url=[" + targetUrl + "] ");
|
||||
return targetUrl;
|
||||
}
|
||||
}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
package com.eactive.eai.adapter.http.client.impl;
|
||||
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterVO;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
|
||||
public class HttpClientAdapterServiceSoap extends HttpClientAdapterServiceSupport {
|
||||
|
||||
public Object execute(Properties prop, Object data, Properties tempProp) throws Exception {
|
||||
|
||||
HttpClientAdapterVO vo = super.setting(prop, tempProp);
|
||||
String soapAction = prop.getProperty(METHOD_SOAP_ACTION);
|
||||
if (soapAction != null)
|
||||
soapAction = soapAction.trim();
|
||||
|
||||
if (logger.isDebug() && "N".equals(vo.getTestCallYn())) {
|
||||
logger.debug("HttpClientAdapter] SEND (" + vo.getMethod() + ") ");
|
||||
}
|
||||
PostMethod method = new PostMethod(vo.getUrl());
|
||||
if (vo.getTimeout() > -1) {
|
||||
method.getParams().setSoTimeout(vo.getTimeout());
|
||||
}
|
||||
HttpClient mclient = this.client;
|
||||
|
||||
byte[] sendBytes = null;
|
||||
if (data instanceof String) {
|
||||
sendBytes = ((String) data).getBytes(vo.getEncode());
|
||||
} else if (data instanceof byte[]) {
|
||||
sendBytes = (byte[]) data;
|
||||
}
|
||||
ByteArrayRequestEntity entity = new ByteArrayRequestEntity(sendBytes);
|
||||
|
||||
method.setRequestEntity(entity);
|
||||
method.setRequestHeader("SOAPAction", soapAction);
|
||||
|
||||
int status = -1;
|
||||
|
||||
configureHttpClient(mclient, method, vo, vo.getUrl(), vo.getContentType());
|
||||
|
||||
try {
|
||||
status = mclient.executeMethod(method);
|
||||
// status 200(정상) 이외에는 error 처리
|
||||
if (status != 200) {
|
||||
try {
|
||||
logger.error("HttpClientAdapterServiceSoap] receive status[" + status + "] data["
|
||||
+ new String(method.getResponseBody()) + "]");
|
||||
} catch (Exception e) {
|
||||
logger.error(e);
|
||||
}
|
||||
throw new HttpStatusException("수신시스템을 확인하세요. HTTP 에러[ " + status + "] adapterName="
|
||||
+ vo.getAdapterGroupName() + "." + vo.getAdapterName(), status);
|
||||
}
|
||||
byte[] responseMessage = method.getResponseBody();
|
||||
return responseMessage;
|
||||
|
||||
// return removeSoap(responseMessage,function).getBytes();
|
||||
|
||||
} catch (SocketTimeoutException ste) { // Read Timeout :: HttpClient 3.1일 경우
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceSoap] SocketTimeoutException : " + ste.toString(), ste);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceSoap] SocketTimeoutException : " + ste.toString(), ste);
|
||||
}
|
||||
throw ste;
|
||||
} catch (ConnectException ce) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceSoap] Connection Exception : " + ce.toString(), ce);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceSoap] Connection Exception : " + ce.toString(), ce);
|
||||
}
|
||||
throw ce;
|
||||
} catch (Exception e) {
|
||||
if (vo.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(vo.getAdapterGroupName() + vo.getAdapterName(),
|
||||
"HttpClientAdapterServiceSoap] Exception : " + e.toString(), e);
|
||||
}
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpClientAdapterServiceSoap] Exception : " + e.toString(), e);
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
method.releaseConnection();
|
||||
if (status != HttpStatus.SC_OK) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = String.valueOf(status);
|
||||
String resMsg = ExceptionUtil.make("RDCEAIAHA013", msgArgs);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug(resMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// private String addSoap(String message) throws Exception {
|
||||
// StringBuffer sb = new StringBuffer();
|
||||
// sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
|
||||
// sb.append("<soapenv:Body>");
|
||||
// sb.append(message);
|
||||
// sb.append("</soapenv:Body>");
|
||||
// sb.append("</soapenv:Envelope>");
|
||||
// return sb.toString();
|
||||
// }
|
||||
// private String getFunction(String message) throws Exception {
|
||||
// DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
// dbFactory.setIgnoringElementContentWhitespace(true);
|
||||
// DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
|
||||
// Document doc = docBuilder.parse(new InputSource(new StringReader(message.trim())));
|
||||
// return doc.getDocumentElement().getNodeName();
|
||||
// }
|
||||
// private String removeSoap(String result,String function) throws Exception {
|
||||
// DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
// dbFactory.setIgnoringElementContentWhitespace(true);
|
||||
// DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
|
||||
// Document doc = docBuilder.parse(new InputSource(new StringReader(result.trim())));
|
||||
// NodeList nodes = doc.getElementsByTagName(function + "Return");
|
||||
// return nodes.item(0).getFirstChild().getNodeValue();
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.server.EAIServerManager;
|
||||
import com.eactive.eai.common.util.DatetimeUtil;
|
||||
import com.eactive.eai.common.util.InboundErrorLogger;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.common.util.UUIDGenerator;
|
||||
import com.eactive.eai.inbound.error.InboundErrorInfoVO;
|
||||
import io.undertow.servlet.handlers.DefaultServlet;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
public class HttpAdapterCustom extends DefaultServlet implements HttpAdapterServiceKey {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// Adapter Logger
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
protected void service(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
HttpDynamicPortInAdapterManager manager = HttpDynamicPortInAdapterManager.getInstance();
|
||||
Integer localPort = request.getLocalPort();
|
||||
String requestUrl = request.getRequestURI(); // /api/v1/public/getUserInfo.svc
|
||||
|
||||
HttpDynamicInAdapterUri adptUri = findAdptUri(localPort, requestUrl, manager);
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("[HttpAdapterCustom] request adapter info : " + adptUri);
|
||||
}
|
||||
|
||||
if (adptUri == null) {
|
||||
logError(request, response, null);
|
||||
return;
|
||||
}
|
||||
|
||||
HttpAdapterService service = HttpAdapterServiceFactory.createFactory(adptUri.getAdptType());
|
||||
service.service(adptUri.getAdptGrpName(), adptUri.getAdptName(), request, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* url에서 뒤쪽 /이후를 제거하면서 찾는다.<br>
|
||||
* /api/v1/public/getUserInfo.svc
|
||||
*
|
||||
* @param apiUri
|
||||
* @return
|
||||
*/
|
||||
private HttpDynamicInAdapterUri findAdptUri(Integer localPort, String apiUri,
|
||||
HttpDynamicPortInAdapterManager manager) {
|
||||
apiUri = StringUtils.removeEnd(apiUri, "/");
|
||||
if (StringUtils.isBlank(apiUri)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
HttpDynamicInAdapterUri adptUri = null;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
adptUri = manager.getAdptUri(localPort, apiUri);
|
||||
if (adptUri != null) {
|
||||
return adptUri;
|
||||
}
|
||||
|
||||
// /api/v1/public
|
||||
apiUri = StringUtils.substringBeforeLast(apiUri, "/");
|
||||
if (StringUtils.isBlank(apiUri)) {
|
||||
return manager.getAdptUri(localPort, "/");
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void logError(HttpServletRequest request, HttpServletResponse response, String msg) {
|
||||
|
||||
InboundErrorInfoVO errorInfoVO = new InboundErrorInfoVO();
|
||||
|
||||
EAIServerManager eaiServerManager = EAIServerManager.getInstance();
|
||||
String serverName = eaiServerManager.getLocalServerName();
|
||||
String uuid = null;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
String instanceid1 = serverName.substring(0,2);
|
||||
String instanceid2 = serverName.substring(serverName.length()-2, serverName.length());
|
||||
String instid = instanceid1 + instanceid2;
|
||||
uuid = instid + UUIDGenerator.getUUID();
|
||||
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = request.getRequestURI();
|
||||
|
||||
String errorCode = "RECEAIIRP010";
|
||||
String errorMsg = null;
|
||||
if (msg == null) {
|
||||
errorMsg = ExceptionUtil.make(new Exception("cat not find uri"), errorCode, msgArgs);
|
||||
} else {
|
||||
errorMsg = ExceptionUtil.make(new Exception(msg), errorCode, msgArgs);
|
||||
}
|
||||
|
||||
errorInfoVO.setEaiSvcSno(uuid); // EAI서비스일련번호
|
||||
errorInfoVO.setAdptBwkGrpNm("HTTP_IN_NO_URI"); // 어댑터업무그룹명
|
||||
errorInfoVO.setErrCd(errorCode); // 에러코드
|
||||
errorInfoVO.setErrTxt(errorMsg); // 에러내용
|
||||
errorInfoVO.setErrTm(DatetimeUtil.getCurrentTime(new Date().getTime())); // 에러발생시각
|
||||
errorInfoVO.setErrDstcd(" ");
|
||||
sb.append("Remote Addr : ").append(request.getRemoteAddr()).append("\n")
|
||||
.append("Request URI : ").append(request.getRequestURI());
|
||||
errorInfoVO.setBwkDataTxt(sb.toString()); // 업무데이터내용
|
||||
errorInfoVO.setEaiSvrInstNm(serverName); // EAI서버인스턴스명
|
||||
|
||||
InboundErrorLogger.error(errorInfoVO);
|
||||
|
||||
response.setStatus(404);
|
||||
try {
|
||||
if (msg == null) {
|
||||
response.getWriter().println("can not find uri '"+request.getRequestURI()+"'");
|
||||
} else {
|
||||
response.getWriter().println(msg);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
// IGNORE
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
import com.eactive.eai.adapter.listener.AdapterException;
|
||||
import com.eactive.eai.adapter.listener.AdapterListenerSupport;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class HttpAdapterListener extends AdapterListenerSupport {
|
||||
|
||||
final static Logger logger2 = HttpLoggerManager.getInstance().getLogger();
|
||||
final static Logger errLogger = HttpLoggerManager.getInstance().getErrLogger();
|
||||
|
||||
public void start() throws AdapterException {
|
||||
HttpDynamicPortInAdapterManager manager = HttpDynamicPortInAdapterManager.getInstance();
|
||||
try {
|
||||
if (info == null) {
|
||||
throw new Exception("HttpAdapterListener | AdapterVO가 NULL입니다.");
|
||||
}
|
||||
manager.addAdapter(info);
|
||||
|
||||
try {
|
||||
manager.start(info);
|
||||
if(logger2.isInfo()) {
|
||||
logger2.info("[HttpAdapterListener] AdapterName [" + info.getAdapterGroupName() + "." + info.getName()
|
||||
+ "] config [" + info + "] start");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
manager.removeAdapter(info);
|
||||
throw e;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(info.getName() + " | HttpAdapterListener | cannot start : " + e.getMessage(), e);
|
||||
throw new AdapterException(info.getName() + " | HttpAdapterListener | cannot start - " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() throws AdapterException {
|
||||
HttpDynamicPortInAdapterManager manager = HttpDynamicPortInAdapterManager.getInstance();
|
||||
try {
|
||||
if (info == null) {
|
||||
throw new Exception("HttpAdapterListener | AdapterVO가 NULL입니다.");
|
||||
}
|
||||
|
||||
manager.removeAdapter(info);
|
||||
manager.stop(info);
|
||||
if(logger2.isInfo()) {
|
||||
logger2.info("[HttpAdapterListener] AdapterName [" + info.getAdapterGroupName() + "." + info.getName()
|
||||
+ "] adapter info [" + info + "] stop");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(info.getName() + " | HttpAdapterListener | cannot stop : " + e.getMessage(), e);
|
||||
throw new AdapterException(info.getName() + " | HttpAdapterListener | cannot stop - " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.server.EAIServerManager;
|
||||
import com.eactive.eai.common.util.DatetimeUtil;
|
||||
import com.eactive.eai.common.util.InboundErrorLogger;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.common.util.UUIDGenerator;
|
||||
import com.eactive.eai.inbound.error.InboundErrorInfoVO;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class HttpAdapterMain extends HttpServlet implements HttpAdapterServiceKey {
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void service(HttpServletRequest request, HttpServletResponse response) {
|
||||
Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
HttpDynamicInAdapterManager manager = HttpDynamicInAdapterManager.getInstance();
|
||||
String orgUri = request.getRequestURI().replaceAll(request.getContextPath(), "");// context path 제거
|
||||
|
||||
String customUri = getCustomUri(orgUri, 3); // adapter name 이후 제거
|
||||
HttpDynamicInAdapterUri adptUri = manager.getAdptUri(customUri);
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpDynamicInAdapter] service request uri : " + request.getRequestURI());
|
||||
}
|
||||
|
||||
if (adptUri == null) {
|
||||
logError(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
// rest일때만 하위 uri 허용
|
||||
if (!orgUri.equals(customUri) && !HttpAdapterServiceType.REST.toString().equals(adptUri.getAdptType())) {
|
||||
logError(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
HttpAdapterService service = HttpAdapterServiceFactory.createFactory(adptUri.getAdptType());
|
||||
if (service == null) {
|
||||
logger.debug("HttpDynamicInAdapter] HttpAdapterService create Error : " + adptUri.getAdptType());
|
||||
logError(request, response);
|
||||
} else {
|
||||
service.service(adptUri.getAdptGrpName(), adptUri.getAdptName(), request, response);
|
||||
}
|
||||
}
|
||||
|
||||
private void logError(HttpServletRequest request, HttpServletResponse response) {
|
||||
Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
InboundErrorInfoVO errorInfoVO = new InboundErrorInfoVO();
|
||||
|
||||
EAIServerManager eaiServerManager = EAIServerManager.getInstance();
|
||||
|
||||
String serverName = eaiServerManager.getLocalServerName();
|
||||
String uuid = null;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
//
|
||||
// String instanceid1 = "";
|
||||
// String instanceid2 = "";
|
||||
// if(serverName != null) {
|
||||
// //juns 서버명 변경에 따른 인스턴스 룰 추출변경
|
||||
// //ex)EAIDSG_i11 => EAIDS01_i01 결과 EA11
|
||||
// instanceid1 = serverName.substring(0,2);
|
||||
// // String instanceid2 = serverName.substring(serverName.length()-2, serverName.length());
|
||||
// instanceid2 = serverName.substring(serverName.length()-5, serverName.length()-4)
|
||||
// + serverName.substring(serverName.length()-1, serverName.length());
|
||||
// }
|
||||
String instid = eaiServerManager.getGroupInstId();
|
||||
uuid = instid + UUIDGenerator.getUUID();
|
||||
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = request.getRequestURI();
|
||||
|
||||
String errorCode = "RECEAIIRP010";
|
||||
String errorMsg = ExceptionUtil.make(new Exception("cat not find uri"), errorCode, msgArgs);
|
||||
|
||||
errorInfoVO.setEaiSvcSno(uuid); // EAI서비스일련번호
|
||||
errorInfoVO.setAdptBwkGrpNm("HTTP_IN_NO_URI"); // 어댑터업무그룹명
|
||||
errorInfoVO.setErrCd(errorCode); // 에러코드
|
||||
errorInfoVO.setErrTxt(errorMsg); // 에러내용
|
||||
errorInfoVO.setErrTm(DatetimeUtil.getCurrentTime(new Date().getTime())); // 에러발생시각
|
||||
errorInfoVO.setErrDstcd(" ");
|
||||
sb.append("Remote Addr : ").append(request.getRemoteAddr()).append("\n").append("Request URI : ")
|
||||
.append(request.getRequestURI());
|
||||
errorInfoVO.setBwkDataTxt(sb.toString()); // 업무데이터내용
|
||||
errorInfoVO.setEaiSvrInstNm(serverName); // EAI서버인스턴스명
|
||||
|
||||
InboundErrorLogger.error(errorInfoVO);
|
||||
|
||||
response.setStatus(404);
|
||||
try {
|
||||
response.getWriter().println("can not find uri '" + request.getRequestURI() + "'");
|
||||
} catch (Exception ex) {
|
||||
logger.error("write error", ex);
|
||||
}
|
||||
|
||||
}
|
||||
// public static void main(String args[]) throws Exception {
|
||||
// test();
|
||||
//}
|
||||
|
||||
// private static void test() throws Exception {
|
||||
// String orgUri = "/HTT/CbsInHttpSys/ABC/DEF";
|
||||
//// System.out.println(getCustomUri(orgUri,3));
|
||||
//
|
||||
// }
|
||||
public static String getCustomUri(String url, int length) {
|
||||
// return
|
||||
// Stream.of(url.split("/")).limit(length).collect(Collectors.joining("/")) ;
|
||||
String[] urls = url.split("/");
|
||||
List<String> newUrls = new ArrayList<>();
|
||||
Collections.addAll(newUrls, urls);
|
||||
return StringUtils.join(newUrls.subList(0, length).toArray(), "/");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public interface HttpAdapterService {
|
||||
public abstract void service(String adptGrpName, String adptName, HttpServletRequest request,
|
||||
HttpServletResponse response);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class HttpAdapterServiceFactory {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
static private ConcurrentHashMap<String, HttpAdapterService> h = new ConcurrentHashMap<String, HttpAdapterService>();
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public HttpAdapterServiceFactory() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 공통코드 HTTP_ADAPTER_SERVICE_CLASS 에 정의된 구현체를 생성 한다.
|
||||
*
|
||||
* @param className
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static HttpAdapterService createFactory(String className) {
|
||||
if (h.containsKey(className)) {
|
||||
return h.get(className);
|
||||
}
|
||||
|
||||
HttpAdapterService service = null;
|
||||
try {
|
||||
Class cl = Class.forName(className);
|
||||
service = (HttpAdapterService) cl.newInstance();
|
||||
if (service != null) {
|
||||
h.put(className, service);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Cannot create a HttpAdapterService. - {}", className);
|
||||
}
|
||||
|
||||
return service;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
public interface HttpAdapterServiceKey {
|
||||
// URL 파리미터 DEFAULT 이름
|
||||
static final String PARAMETER_MESSAGE = "_message_";
|
||||
|
||||
// URL 파리미터 이름
|
||||
static final String PARAMETER_NAME = "PARAMETER_NAME";
|
||||
|
||||
// Http Adapter 이름
|
||||
static final String HTTP_ADAPTER_NAME = "HTTP_ADAPTER_NAME";
|
||||
|
||||
// Http Adapter Group 이름
|
||||
static final String HTTP_ADAPTER_GROUP_NAME = "HTTP_ADAPTER_GROUP_NAME";
|
||||
|
||||
// Http Response Type 이름
|
||||
static final String RESPONSE_TYPE = "RESPONSE_TYPE";
|
||||
|
||||
public static final String RESPONSE_TYPE_SYNC = "SYNC";
|
||||
public static final String RESPONSE_TYPE_ASYNC = "ASYN";
|
||||
|
||||
// Http Encode 이름
|
||||
static final String ENCODE = "ENCODE";
|
||||
|
||||
// Http URL Decode 여부
|
||||
static final String URL_DECODE_YN = "URL_DECODE_YN";
|
||||
|
||||
// Body data 사용 여부
|
||||
static final String REQUEST_BODY_YN = "REQUEST_BODY_YN";
|
||||
|
||||
// 삭제할 데이타 길이
|
||||
static final String REMOVE_LENGTH = "REMOVE_LENGTH";
|
||||
|
||||
// 추가할 데이타
|
||||
static final String ADD_DATA = "ADD_DATA";
|
||||
|
||||
// 거래 로그
|
||||
static final String TRACE_LEVEL = "TRACE_LEVEL";
|
||||
|
||||
static final String ERROR_RESPONSE_FORMAT = "ERROR_RESPONSE_FORMAT";
|
||||
|
||||
// REST용 전달값
|
||||
static final String INBOUND_METHOD = "INBOUND_METHOD";
|
||||
static final String INBOUND_URI = "INBOUND_URI";
|
||||
static final String INBOUND_EXTURI = "INBOUND_EXTURI";
|
||||
static final String INBOUND_HEADER = "INBOUND_HEADER";
|
||||
static final String INBOUND_QUERY_STRING = "INBOUND_QUERY_STRING";
|
||||
static final String INBOUND_REWRITE_PATH = "INBOUND_REWRITE_PATH";
|
||||
static final String INBOUND_REMOTE_ADDR = "INBOUND_REMOTE_ADDR";
|
||||
static final String INBOUND_SCHEME = "INBOUND_SCHEME";
|
||||
static final String INBOUND_PATH_VARIABLES = "INBOUND_PATH_VARIABLES";
|
||||
static final String INBOUND_REQUEST_MESSAGE = "INBOUND_REQUEST_MESSAGE";
|
||||
static final String INBOUND_CLIENT_IP = "INBOUND_CLIENT_IP";
|
||||
|
||||
static final String INBOUND_SYNC_ASYNC_TYPE = "INBOUND_SYNC_ASYNC_TYPE";
|
||||
|
||||
public static final String OUTBOUND_RESPONSE_HEADERS = "OUTBOUND_RESPONSE_HEADERS";
|
||||
public static final String OUTBOUND_RESPONSE_MESSAGE = "OUTBOUND_RESPONSE_MESSAGE";
|
||||
public static final String OUTBOUND_RESPONSE_STATUS = "OUTBOUND_RESPONSE_STATUS";
|
||||
public static final String OUTBOUND_PROPERTY_MAP = "OUTBOUND_PROPERTY_MAP";
|
||||
|
||||
// API path
|
||||
static final String API_PATH = "API_PATH";
|
||||
|
||||
// 요청전 수행할 필터(쉼표(,)로 구분)
|
||||
static final String PRE_FILTERS = "PRE_FILTERS";
|
||||
|
||||
// 요청후 수행할 필터(쉼표(,)로 구분)
|
||||
static final String POST_FILTERS = "POST_FILTERS";
|
||||
|
||||
static final String ALLOW_IP = "ALLOW_IP";
|
||||
|
||||
static final String ENC_PATHS = "ENC_PATHS";
|
||||
|
||||
//응답 처리 용 표준 전문 오브젝트
|
||||
static final String STANDARD_MESSAGE_OBJECT = "STANDARD_MESSAGE_OBJECT";
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
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.RequestDispatcher;
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceKey;
|
||||
import com.eactive.eai.adapter.http.dynamic.filter.HttpAdapterFilter;
|
||||
import com.eactive.eai.adapter.http.dynamic.filter.HttpAdapterFilterFactory;
|
||||
import com.eactive.eai.adapter.http.dynamic.filter.HttpAdapterFilterType;
|
||||
import com.eactive.eai.data.entity.onl.adapter.AdapterProp;
|
||||
|
||||
public abstract class HttpAdapterServiceSupport implements HttpAdapterService, HttpAdapterServiceKey {
|
||||
protected long slowTranTime = 2000L;
|
||||
public static final String HEADER_NAME_CLIENT_ID = "x-elink-client-id";
|
||||
public static final String PROPERTIES_NAME_CLIENT_ID = "clientId";
|
||||
|
||||
public Object service(String adptGrpName, String adptName, Object message, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
try {
|
||||
String clientId = request.getHeader(HEADER_NAME_CLIENT_ID);
|
||||
if (StringUtils.isNotBlank(clientId)) {
|
||||
prop.put(PROPERTIES_NAME_CLIENT_ID, clientId);
|
||||
}
|
||||
|
||||
String transactionId = request.getHeader(HttpClientAdapterServiceKey.TRANSACTION_ID);
|
||||
if (StringUtils.isNotBlank(transactionId)) {
|
||||
prop.put(HttpClientAdapterServiceKey.TRANSACTION_ID, transactionId);
|
||||
}
|
||||
|
||||
String instanceId = request.getHeader(HttpClientAdapterServiceKey.INSTANCE_ID);
|
||||
if (StringUtils.isNotBlank(instanceId)) {
|
||||
prop.put(HttpClientAdapterServiceKey.INSTANCE_ID, instanceId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
message = doPreFilters(adptGrpName, adptName, message, prop, request, response);
|
||||
Object obj = RequestDispatcher.getRequestDispatcher(adptGrpName).handle(adptName, message, prop);
|
||||
obj = doPostFilters(adptGrpName, adptName, obj, prop, request, response);
|
||||
if (obj == null) {
|
||||
return null;
|
||||
} else if (obj instanceof byte[]) {
|
||||
return (byte[]) obj;
|
||||
} else if (obj instanceof String) {
|
||||
return (String) obj;
|
||||
} else {
|
||||
throw new Exception("RECEAIAHA001");
|
||||
}
|
||||
}
|
||||
|
||||
private Object doPreFilters(String adptGrpName, String adptName, Object message, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
AdapterManager manager = AdapterManager.getInstance();
|
||||
AdapterGroupVO group = manager.getAdapterGroup(adptGrpName);
|
||||
|
||||
if (!"HTC".equals(group.getType())) {
|
||||
String allowIp = prop.getProperty(ALLOW_IP,"");
|
||||
if (StringUtils.isNotBlank(allowIp)) {
|
||||
HttpAdapterFilter adapterFilter = HttpAdapterFilterFactory.createFilter(HttpAdapterFilterType.ADAPTERALLOWIPLISTFILTER.toString());
|
||||
if (adapterFilter != null) {
|
||||
message = adapterFilter.doPreFilter(adptGrpName, adptName, message, prop, request, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String preFiltersStr = prop.getProperty(PRE_FILTERS);
|
||||
if (StringUtils.isBlank(preFiltersStr)) {
|
||||
return message;
|
||||
}
|
||||
|
||||
String[] preFilters = StringUtils.split(preFiltersStr, ",");
|
||||
for (String filterName : preFilters) {
|
||||
if (StringUtils.isBlank(filterName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
HttpAdapterFilter adapterFilter = HttpAdapterFilterFactory.createFilter(filterName.trim());
|
||||
if (adapterFilter != null) {
|
||||
message = adapterFilter.doPreFilter(adptGrpName, adptName, message, prop, request, response);
|
||||
} else {
|
||||
throw new Exception("Failed get Filter Class: " + filterName);
|
||||
}
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
private Object doPostFilters(String adptGrpName, String adptName, Object resultMessage, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
String postFiltersStr = prop.getProperty(POST_FILTERS);
|
||||
if (StringUtils.isBlank(postFiltersStr)) {
|
||||
return resultMessage;
|
||||
}
|
||||
|
||||
String[] postFilters = StringUtils.split(postFiltersStr, ",");
|
||||
for (String filterName : postFilters) {
|
||||
if (StringUtils.isBlank(filterName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
HttpAdapterFilter adapterFilter = HttpAdapterFilterFactory.createFilter(filterName.trim());
|
||||
if (adapterFilter != null) {
|
||||
resultMessage = adapterFilter.doPostFilter(adptGrpName, adptName, resultMessage, prop, request,
|
||||
response);
|
||||
} else {
|
||||
throw new Exception("Failed get Filter Class: " + filterName);
|
||||
}
|
||||
}
|
||||
return resultMessage;
|
||||
}
|
||||
|
||||
public abstract void service(String adptGrpName, String adptName, HttpServletRequest request,
|
||||
HttpServletResponse response);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
public enum HttpAdapterServiceType {
|
||||
STANDARD,
|
||||
CHNL,
|
||||
PASS,
|
||||
REST,
|
||||
UNKNOWN
|
||||
;
|
||||
public static HttpAdapterServiceType getValue(String value) {
|
||||
try {
|
||||
return valueOf(value.toUpperCase());
|
||||
} catch (Exception e) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
import com.eactive.eai.adapter.*;
|
||||
import com.eactive.eai.agent.command.CommandException;
|
||||
import com.eactive.eai.common.lifecycle.Lifecycle;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleException;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleListener;
|
||||
import com.eactive.eai.common.util.CamelCaseUtil;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class HttpDynamicInAdapterManager implements Lifecycle {
|
||||
|
||||
public static String HTTP_PARAM_TYPE = "HTTP_TYPE";
|
||||
public static String API_PATH = "API_PATH";
|
||||
|
||||
Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
private static HttpDynamicInAdapterManager instance = new HttpDynamicInAdapterManager();
|
||||
|
||||
private ConcurrentHashMap<String, HttpDynamicInAdapterUri> adptUriMap = new ConcurrentHashMap<String, HttpDynamicInAdapterUri>();
|
||||
|
||||
public static HttpDynamicInAdapterManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public HttpDynamicInAdapterUri getAdptUri(String uri) {
|
||||
return adptUriMap.get( uri );
|
||||
}
|
||||
|
||||
public void removeAdptUri(AdapterVO avo) {
|
||||
try {
|
||||
AdapterGroupVO gvo = avo.getAdapterGroupVO();
|
||||
if ( gvo.getType().equals(Keys.TYPE_HTTP) && gvo.getGubun().equals("I") ) {
|
||||
removeAdptUri(avo.getAdapterGroupName(), avo.getName());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("HttpDynamicInAdapterManager] dynamic HTTP adpater remove error." + avo.toString(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeAdptUri(String adptGrpName, String adptName) {
|
||||
for( HttpDynamicInAdapterUri adptUri : adptUriMap.values() ) {
|
||||
if ( adptUri.isEqualsAdpt(adptGrpName, adptName) ) {
|
||||
adptUriMap.remove( adptUri.getUri() );
|
||||
if ( logger.isDebug() ) {
|
||||
logger.debug("HttpDynamicInAdapterManager] remove " + adptGrpName + "-" + adptName );
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setDynamicUrl(AdapterVO avo) throws Exception {
|
||||
try {
|
||||
AdapterGroupVO gvo = avo.getAdapterGroupVO();
|
||||
|
||||
if(gvo == null){
|
||||
removeAdptUriMapByAdapterGroupName(avo.getAdapterGroupName());
|
||||
}
|
||||
else if ( gvo.getType().equals(Keys.TYPE_HTTP) && gvo.getGubun().equals("I") ) {
|
||||
AdapterPropManager manager = AdapterPropManager.getInstance();
|
||||
Properties httpProp = manager.getProperties(avo.getPropGroupName());
|
||||
String httpType = httpProp.getProperty(HTTP_PARAM_TYPE,"STANDARD");
|
||||
String httpUrl = "/HTT/"+CamelCaseUtil.converCamelCaseWithBrace(avo.getAdapterGroupName(),"_");
|
||||
adptUriMap.put( httpUrl, new HttpDynamicInAdapterUri(avo.getAdapterGroupName(), avo.getName(), httpUrl, httpType));
|
||||
if ( logger.isDebug() ) {
|
||||
logger.debug("HttpDynamicInAdapterManager] set " + avo.getAdapterGroupName() + "-" + avo.getName() + ", uri:" + httpUrl );
|
||||
}
|
||||
} else if (gvo.getType().equals(Keys.TYPE_REST) && gvo.getGubun().equals("I")) {
|
||||
AdapterPropManager manager = AdapterPropManager.getInstance();
|
||||
Properties httpProp = manager.getProperties(avo.getPropGroupName());
|
||||
String httpType = httpProp.getProperty(HTTP_PARAM_TYPE, "REST");
|
||||
String httpUrl = httpProp.getProperty(API_PATH);
|
||||
httpUrl = StringUtils.removeEnd(httpUrl, "/");
|
||||
if (StringUtils.isNotBlank(httpUrl)) {
|
||||
// /api/vi/public
|
||||
if (!StringUtils.startsWith(httpUrl, "/")) {
|
||||
httpUrl = "/" + httpUrl;
|
||||
}
|
||||
|
||||
// 1. URL이 이미 있는데, 그 URL이 다른 어댑터에 이미 설정되어 있다면 오류처리(기존 어댑터 보호)
|
||||
HttpDynamicInAdapterUri adapterUri = adptUriMap.get(httpUrl);
|
||||
|
||||
logger.debug("#### setDynamicUrl:"+adptUriMap.toString());
|
||||
|
||||
if (adapterUri != null
|
||||
&& !StringUtils.equals(avo.getAdapterGroupName(), adapterUri.getAdptGrpName())) {
|
||||
throw new CommandException("The URI is already in use by another adapter, "+adapterUri.getAdptGrpName());
|
||||
}
|
||||
|
||||
// 2. 어댑터 기준 기존 데이터 삭제
|
||||
removeAdptUriMapByAdapterGroupName(avo.getAdapterGroupName());
|
||||
|
||||
// 3. 새로 추가
|
||||
adptUriMap.put(httpUrl,
|
||||
new HttpDynamicInAdapterUri(avo.getAdapterGroupName(), avo.getName(), httpUrl, httpType));
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpDynamicInAdapterManager] set " + avo.getAdapterGroupName() + "-"
|
||||
+ avo.getName() + ", uri:" + httpUrl);
|
||||
logger.debug("HttpDynamicInAdapterManager] adptUriMap " + adptUriMap.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (CommandException e) {
|
||||
throw new Exception(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
logger.error("HttpDynamicInAdapterManager] dynamic HTTP adapter add error." + avo.toString(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeAdptUriMapByAdapterGroupName(String adapterGroupName) throws Exception {
|
||||
for (HttpDynamicInAdapterUri adptUri : adptUriMap.values()) {
|
||||
if (StringUtils.equals(adptUri.getAdptGrpName(), adapterGroupName)) {
|
||||
adptUriMap.remove(adptUri.getUri());
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpDynamicInAdapterManager] remove " + adptUri.toString() + " for "+adapterGroupName);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLifecycleListener(LifecycleListener listener) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public LifecycleListener[] findLifecycleListeners() {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeLifecycleListener(LifecycleListener listener) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws LifecycleException {
|
||||
|
||||
AdapterManager adptManager = AdapterManager.getInstance();
|
||||
HashMap adptGrps = null;
|
||||
|
||||
try {
|
||||
adptGrps = adptManager.getAllAdapterGroupVO();
|
||||
|
||||
for(Object obj : adptGrps.values()) {
|
||||
AdapterGroupVO gvo = (AdapterGroupVO)obj;
|
||||
if(logger.isDebug()){
|
||||
logger.debug("adpter : "+gvo.getName()+"-->"+gvo.toString());
|
||||
}
|
||||
if ((gvo.getType().equals(Keys.TYPE_HTTP) || gvo.getType().equals(Keys.TYPE_REST))
|
||||
&& gvo.getGubun().equals("I")) {
|
||||
ArrayList<AdapterVO> adpts = null;
|
||||
Iterator i = gvo.getAdapters();
|
||||
|
||||
while ( i.hasNext() ) {
|
||||
AdapterVO avo = (AdapterVO)i.next();
|
||||
setDynamicUrl( avo );
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("HttpDynamicInAdapterManager] start error", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws LifecycleException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStarted() {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void reloadAdapterUrl(AdapterGroupVO reloadGroupVO) throws Exception {
|
||||
Iterator i = reloadGroupVO.getAdapters();
|
||||
while (i.hasNext()) {
|
||||
AdapterVO avo = (AdapterVO) i.next();
|
||||
setDynamicUrl(avo);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
public class HttpDynamicInAdapterUri {
|
||||
|
||||
public String adptGrpName = null;
|
||||
public String adptName = null;
|
||||
public String uri = null;
|
||||
public String adptType = null;
|
||||
public Integer listenPort = 0;
|
||||
|
||||
public HttpDynamicInAdapterUri(String adptGrpName, String adptName, Integer port, String uri, String adptType) {
|
||||
this.adptGrpName = adptGrpName;
|
||||
this.adptName = adptName;
|
||||
this.uri = uri;
|
||||
this.adptType = adptType;
|
||||
this.listenPort = port;
|
||||
}
|
||||
|
||||
public HttpDynamicInAdapterUri(String adptGrpName, String adptName, String uri, String adptType) {
|
||||
this.adptGrpName = adptGrpName;
|
||||
this.adptName = adptName;
|
||||
this.uri = uri;
|
||||
this.adptType = adptType;
|
||||
this.listenPort = 0;
|
||||
}
|
||||
|
||||
public boolean isEqualsAdpt(HttpDynamicInAdapterUri other) {
|
||||
return isEqualsAdpt(other.getAdptGrpName(), other.getAdptName());
|
||||
}
|
||||
|
||||
public boolean isEqualsAdpt(String adptGrpName, String adptNamer) {
|
||||
if ( adptGrpName != null && adptGrpName.equals(this.adptGrpName)
|
||||
&& adptName != null && adptName.equals(this.adptName) ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEqualsAdpt(String adptGrpName) {
|
||||
if ( adptGrpName != null && adptGrpName.equals(this.adptGrpName)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public String getAdptGrpName() {
|
||||
return adptGrpName;
|
||||
}
|
||||
|
||||
public void setAdptGrpName(String adptGrpName) {
|
||||
this.adptGrpName = adptGrpName;
|
||||
}
|
||||
|
||||
public String getAdptName() {
|
||||
return adptName;
|
||||
}
|
||||
|
||||
public void setAdptName(String adptName) {
|
||||
this.adptName = adptName;
|
||||
}
|
||||
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
public void setUri(String uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public String getAdptType() {
|
||||
return adptType;
|
||||
}
|
||||
|
||||
public void setAdptType(String adptType) {
|
||||
this.adptType = adptType;
|
||||
}
|
||||
|
||||
public Integer getListenPort() {
|
||||
return listenPort;
|
||||
}
|
||||
|
||||
public void setListenPort(Integer listenPort) {
|
||||
this.listenPort = listenPort;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("HttpDynamicInAdapterUri[ adptGrpName=").append(adptGrpName);
|
||||
sb.append(", adptName=").append(adptName);
|
||||
sb.append(", port=").append(listenPort);
|
||||
sb.append(", uri=").append(uri);
|
||||
sb.append(", adptType=").append(adptType);
|
||||
sb.append(" ]");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+511
@@ -0,0 +1,511 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
import com.eactive.eai.adapter.*;
|
||||
import com.eactive.eai.adapter.http.dynamic.wrapper.KeepAliveFalseWrapper;
|
||||
import com.eactive.eai.adapter.http.dynamic.wrapper.KeepAliveTrueWrapper;
|
||||
import com.eactive.eai.adapter.http.util.HttpsGenerator;
|
||||
import com.eactive.eai.agent.command.CommandException;
|
||||
import com.eactive.eai.common.lifecycle.Lifecycle;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleException;
|
||||
import com.eactive.eai.common.lifecycle.LifecycleListener;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import io.undertow.Handlers;
|
||||
import io.undertow.Undertow;
|
||||
import io.undertow.UndertowOptions;
|
||||
import io.undertow.server.handlers.*;
|
||||
import io.undertow.servlet.Servlets;
|
||||
import io.undertow.servlet.api.DeploymentInfo;
|
||||
import io.undertow.servlet.api.DeploymentManager;
|
||||
import io.undertow.servlet.api.ServletInfo;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.servlet.ServletException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class HttpDynamicPortInAdapterManager implements Lifecycle {
|
||||
public String HTTP_PARAM_TYPE = "HTTP_TYPE";
|
||||
public String HTTP_LISTEN_PORT = "LISTEN_PORT";
|
||||
public String API_PATH = "API_PATH";
|
||||
|
||||
public String HTTPS_USE_YN = "HTTPS_USE_YN";
|
||||
public String HTTPS_KEY_STORE = "HTTPS_KEY_STORE";
|
||||
public String HTTPS_KEY_PASS = "HTTPS_KEY_PASS";
|
||||
|
||||
public String MAX_CONNECTION = "MAX_CONNECTION";
|
||||
public String QUEUE_SIZE = "QUEUE_SIZE";
|
||||
public String ALLOW_IP = "ALLOW_IP";
|
||||
|
||||
public final String IO_THREADS = "IO_THREADS";
|
||||
public final String WORKER_THREADS = "WORKER_THREADS";
|
||||
|
||||
//keep alive 관련
|
||||
public final String KEEP_ALIVE_YN = "KEEP_ALIVE_YN";
|
||||
public final String KEEP_ALIVE_TIMEOUT = "KEEP_ALIVE_TIMEOUT";
|
||||
public final String MAX_REQUEST_COUNT = "MAX_REQUEST_COUNT";
|
||||
|
||||
Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
private static HttpDynamicPortInAdapterManager instance = new HttpDynamicPortInAdapterManager();
|
||||
|
||||
// url - Adapter Map
|
||||
private ConcurrentHashMap<String, HttpDynamicInAdapterUri> adptUriMap = new ConcurrentHashMap<String, HttpDynamicInAdapterUri>();
|
||||
|
||||
// adapter Name - Adapter Map
|
||||
private ConcurrentHashMap<String, HttpDynamicInAdapterUri> adptNameMap = new ConcurrentHashMap<String, HttpDynamicInAdapterUri>();
|
||||
|
||||
// Listen Port - Adapter Map
|
||||
private ConcurrentHashMap<Integer, ConcurrentHashMap> adptPortMap = new ConcurrentHashMap<Integer, ConcurrentHashMap>();
|
||||
|
||||
// Listen Port - Adapter Map
|
||||
private ConcurrentHashMap<Integer, Undertow> svrPortMap = new ConcurrentHashMap<Integer, Undertow>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void addAdapter(AdapterVO adapter) throws Exception {
|
||||
String adapterFullName;
|
||||
|
||||
HttpDynamicInAdapterUri oldAdapterUri, newAdapterUri;
|
||||
|
||||
String oldUri, newUri;
|
||||
|
||||
// Port별 Adapter 목록 매핑
|
||||
ConcurrentHashMap<String, HttpDynamicInAdapterUri> adptPortSubMap;
|
||||
|
||||
// 기존 Port Listener
|
||||
Undertow oldListener;
|
||||
|
||||
AdapterGroupVO gvo = adapter.getAdapterGroupVO();
|
||||
|
||||
if (gvo != null & gvo.getType().equals(Keys.TYPE_HTTP_CUSTOM) && gvo.getGubun().equals("I")) {
|
||||
// Server Adapter인 경우 초기화
|
||||
adapterFullName = adapter.getAdapterGroupName() + "." + adapter.getName();
|
||||
oldAdapterUri = adptNameMap.get(adapterFullName);
|
||||
|
||||
AdapterPropManager manager = AdapterPropManager.getInstance();
|
||||
Properties httpProp = manager.getProperties(adapter.getPropGroupName());
|
||||
String httpType = httpProp.getProperty(HTTP_PARAM_TYPE, "STANDARD");
|
||||
String portString = httpProp.getProperty(HTTP_LISTEN_PORT, "0");
|
||||
Integer listenPort = Integer.valueOf(portString);
|
||||
if (listenPort.intValue() == 0) {
|
||||
throw new CommandException("LISTEN_PORT is required.");
|
||||
}
|
||||
|
||||
newUri = httpProp.getProperty(API_PATH);
|
||||
if (!StringUtils.equals(newUri, "/")) {
|
||||
newUri = StringUtils.removeEnd(newUri, "/");
|
||||
}
|
||||
if (StringUtils.isBlank(newUri)) {
|
||||
throw new CommandException("API_PATH is required.");
|
||||
}
|
||||
|
||||
// /api/vi/public
|
||||
if (!StringUtils.startsWith(newUri, "/")) {
|
||||
newUri = "/" + newUri;
|
||||
}
|
||||
|
||||
newAdapterUri = new HttpDynamicInAdapterUri(adapter.getAdapterGroupName(), adapter.getName(), listenPort,
|
||||
newUri, httpType);
|
||||
|
||||
adptPortSubMap = null;
|
||||
oldListener = null;
|
||||
} else {
|
||||
// Client Adapter 인 경우 무시
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. Adapter 명으로 기 등록된 Adapter인지 확인
|
||||
// -- 이미 등록된 Adapter 이면
|
||||
if (oldAdapterUri != null) {
|
||||
|
||||
if (oldAdapterUri.getListenPort() != 0) {
|
||||
oldUri = adapterFullName + "/" + oldAdapterUri.getUri();
|
||||
|
||||
// 2. Port별 Adapter 정보 확인 및 제거
|
||||
Integer listenPort = oldAdapterUri.getListenPort();
|
||||
|
||||
adptPortSubMap = adptPortMap.get(listenPort);
|
||||
if (adptPortSubMap != null) {
|
||||
adptPortSubMap.remove(adapterFullName);
|
||||
}
|
||||
|
||||
// 2.1 해당 Port에 추가 Adapter가 없는 경우 해당 Listener 제거
|
||||
if (adptPortSubMap == null || adptPortSubMap.size() == 0) {
|
||||
|
||||
// 2.1.1 해당 Port에 추가 Adapter가 없는 경우 Port 매핑 제거
|
||||
adptPortMap.remove(listenPort);
|
||||
// 2.1.2 해당 Port에 추가 Adapter가 없는 경우 해당 Listener 제거
|
||||
oldListener = svrPortMap.get(listenPort);
|
||||
svrPortMap.remove(listenPort);
|
||||
if (oldListener != null)
|
||||
oldListener.stop();
|
||||
}
|
||||
|
||||
// 3. Adapter명 별 Adapter 정보 확인 및 제거
|
||||
adptNameMap.remove(adapterFullName);
|
||||
} else {
|
||||
oldUri = oldAdapterUri.getUri();
|
||||
// Default Adapter (WAS) 처리
|
||||
// adptUriMap만 처리
|
||||
adptUriMap.remove(oldUri);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 신규 Adapter 추가
|
||||
if (newAdapterUri.getListenPort() != 0) {
|
||||
// Port 별 Adapter 정보 등록
|
||||
adptPortSubMap = adptPortMap.get(newAdapterUri.getListenPort());
|
||||
if (adptPortSubMap == null) {
|
||||
adptPortSubMap = new ConcurrentHashMap<String, HttpDynamicInAdapterUri>();
|
||||
adptPortMap.put(newAdapterUri.getListenPort(), adptPortSubMap);
|
||||
}
|
||||
adptPortSubMap.put(newAdapterUri.getUri(), newAdapterUri);
|
||||
|
||||
// Adapter명 별 Adapter 정보 등록
|
||||
adptNameMap.put(adapterFullName, newAdapterUri);
|
||||
} else {
|
||||
// Default Adapter (WAS) 처리
|
||||
// adptUriMap만 등록
|
||||
adptUriMap.put(newUri, newAdapterUri);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void removeAdapter(AdapterVO adapter) throws Exception {
|
||||
|
||||
String adapterFullName;
|
||||
HttpDynamicInAdapterUri oldAdapterUri;
|
||||
|
||||
// Port별 Adapter 목록 매핑
|
||||
ConcurrentHashMap<String, HttpDynamicInAdapterUri> adptPortSubMap;
|
||||
|
||||
// 기존 Port Listener
|
||||
Undertow oldListener;
|
||||
|
||||
AdapterGroupVO gvo = adapter.getAdapterGroupVO();
|
||||
|
||||
if (gvo != null && gvo.getType().equals(Keys.TYPE_HTTP_CUSTOM) && gvo.getGubun().equals("I")) {
|
||||
// Server Adapter 인 경우 초기화
|
||||
adapterFullName = adapter.getAdapterGroupName() + "." + adapter.getName();
|
||||
oldAdapterUri = adptNameMap.get(adapterFullName);
|
||||
adptPortSubMap = null;
|
||||
oldListener = null;
|
||||
} else {
|
||||
// Client Adapter 인 경우 무시
|
||||
return;
|
||||
}
|
||||
|
||||
if (oldAdapterUri != null) {
|
||||
if (oldAdapterUri.getListenPort() != 0) {
|
||||
|
||||
// 2. Port 별 Adapter 정보 확인 및 제거
|
||||
Integer listenPort = oldAdapterUri.getListenPort();
|
||||
|
||||
adptPortSubMap = adptPortMap.get(listenPort);
|
||||
HttpDynamicInAdapterUri httpDynamicInAdapterUri = adptPortSubMap.get(oldAdapterUri.getUri());
|
||||
|
||||
// 20211201 어댑터 그룹체크, 동일하면 삭제
|
||||
if (httpDynamicInAdapterUri.getAdptGrpName().equals(oldAdapterUri.getAdptGrpName())) {
|
||||
adptPortSubMap.remove(oldAdapterUri.getUri());
|
||||
}
|
||||
|
||||
// 2.1 해당 Port에 추가 Adapter 없는 경우 해당 Listener 제거
|
||||
if (adptPortSubMap.size() == 0) {
|
||||
// 2.1.1 해당 Port에 추가 Adapter 없는 경우 Port 매핑 제거
|
||||
adptPortMap.remove(listenPort);
|
||||
// 2.1.2 해당 Port에 추가 Adapter 없는 경우 해당 Listener 제거
|
||||
oldListener = svrPortMap.get(listenPort);
|
||||
try {
|
||||
if (oldListener != null) {
|
||||
oldListener.stop();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// 중지 실패시 원복
|
||||
adptPortSubMap.put(oldAdapterUri.getUri(), oldAdapterUri);
|
||||
adptPortMap.put(listenPort, adptPortSubMap);
|
||||
throw t;
|
||||
}
|
||||
|
||||
// stop 성공후 삭제
|
||||
svrPortMap.remove(listenPort);
|
||||
}
|
||||
|
||||
// 3. Adapter명 별 Adapter 정보 확인 및 제거
|
||||
adptNameMap.remove(adapterFullName);
|
||||
} else {
|
||||
// Default Adapter(WAS) 처리
|
||||
// adptUriMap만 처리
|
||||
adptUriMap.remove(oldAdapterUri.getUri());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static HttpDynamicPortInAdapterManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public HttpDynamicInAdapterUri getAdptUri(String uri) {
|
||||
return adptUriMap.get(uri);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public HttpDynamicInAdapterUri getAdptUri(Integer port, String uri) {
|
||||
ConcurrentHashMap<String, HttpDynamicInAdapterUri> tmpAdptUriMap = adptPortMap.get(port);
|
||||
if (tmpAdptUriMap != null)
|
||||
return tmpAdptUriMap.get(uri);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLifecycleListener(LifecycleListener listener) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public LifecycleListener[] findLifecycleListeners() {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeLifecycleListener(LifecycleListener listener) {
|
||||
|
||||
}
|
||||
|
||||
public void start(AdapterVO adapter) throws Exception {
|
||||
AdapterGroupVO gvo = adapter.getAdapterGroupVO();
|
||||
|
||||
if (gvo != null && gvo.getType().equals(Keys.TYPE_HTTP_CUSTOM) && gvo.getGubun().equals("I")) {
|
||||
// Server Adapter 인 경우 정보 확인
|
||||
AdapterPropManager adapterManager = AdapterPropManager.getInstance();
|
||||
Properties httpProp = adapterManager.getProperties(adapter.getPropGroupName());
|
||||
|
||||
String httpsUseYn = httpProp.getProperty(HTTPS_USE_YN, "N");
|
||||
String httpsKeyStore = httpProp.getProperty(HTTPS_KEY_STORE, null);
|
||||
String httpsKeyPass = httpProp.getProperty(HTTPS_KEY_PASS, null);
|
||||
|
||||
Integer listenPort = Integer.valueOf(httpProp.getProperty(HTTP_LISTEN_PORT, "0"));
|
||||
Integer max_connection = Integer.valueOf(httpProp.getProperty(MAX_CONNECTION, "100"));
|
||||
Integer max_queue = Integer.valueOf(httpProp.getProperty(QUEUE_SIZE, "3000"));
|
||||
String allowIp = httpProp.getProperty(ALLOW_IP, null);
|
||||
|
||||
Integer ioThreads = Integer.valueOf(httpProp.getProperty(IO_THREADS, "2"));
|
||||
Integer workerThreads = Integer.valueOf(httpProp.getProperty(WORKER_THREADS, "16"));
|
||||
|
||||
String keepAliveYn = httpProp.getProperty(KEEP_ALIVE_YN, "Y");
|
||||
Integer keepAliveTimeout = Integer.valueOf(httpProp.getProperty(KEEP_ALIVE_TIMEOUT, "15"));
|
||||
Integer maxRequestCount = Integer.valueOf(httpProp.getProperty(MAX_REQUEST_COUNT, "-1"));
|
||||
|
||||
boolean isKeepAlive = false;
|
||||
if ("Y".equals(keepAliveYn)) {
|
||||
isKeepAlive = true;
|
||||
}
|
||||
|
||||
// Custom Adapter 인 경우 재시작
|
||||
if (listenPort != 0) {
|
||||
Undertow server = svrPortMap.get(listenPort);
|
||||
if (server != null) {
|
||||
// server.stop();
|
||||
// svrPortMap.remove(listenPort);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ServletInfo servletInfo = Servlets.servlet(HttpAdapterCustom.class);
|
||||
servletInfo.addMapping("/*");
|
||||
ServletInfo healthCheckServletInfo = Servlets.servlet(UndertowHealthCheckServlet.class);
|
||||
healthCheckServletInfo.addMapping("healthcheck.html");
|
||||
|
||||
DeploymentInfo servletBuilder = Servlets.deployment()
|
||||
.setClassLoader(HttpDynamicPortInAdapterManager.class.getClassLoader())
|
||||
.setDeploymentName("HttpDynamicCustomAdapter").setContextPath("/")
|
||||
.addServlet(healthCheckServletInfo).addServlet(servletInfo);
|
||||
if (isKeepAlive) {
|
||||
KeepAliveTrueWrapper wrapper = new KeepAliveTrueWrapper();
|
||||
wrapper.setKeepAliveTimeout(keepAliveTimeout);
|
||||
wrapper.setMaxRequestCount(maxRequestCount);
|
||||
servletBuilder.addInitialHandlerChainWrapper(wrapper);
|
||||
} else {
|
||||
servletBuilder.addInitialHandlerChainWrapper(new KeepAliveFalseWrapper());
|
||||
}
|
||||
DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
|
||||
manager.deploy();
|
||||
|
||||
PathHandler path = null;
|
||||
IPAddressAccessControlHandler ipAddressAccessControlHandler = null;
|
||||
RequestLimitingHandler requestLimitHandler = null;
|
||||
|
||||
try {
|
||||
path = Handlers.path(Handlers.redirect("/")).addPrefixPath("/", manager.start());
|
||||
if (allowIp != null && allowIp.length() > 1) {
|
||||
String[] allowIps = allowIp.split(",");
|
||||
ipAddressAccessControlHandler = Handlers.ipAccessControl(path, false);
|
||||
for (int i = 0; i < allowIps.length; i++) {
|
||||
ipAddressAccessControlHandler.addAllow(allowIps[i]);
|
||||
}
|
||||
}
|
||||
|
||||
RequestLimit requestLimit = new RequestLimit(max_connection, max_queue);
|
||||
requestLimit.setFailureHandler(ResponseCodeHandler.HANDLE_500);
|
||||
|
||||
if (ipAddressAccessControlHandler != null) {
|
||||
requestLimitHandler = Handlers.requestLimitingHandler(requestLimit,
|
||||
ipAddressAccessControlHandler);
|
||||
} else {
|
||||
requestLimitHandler = Handlers.requestLimitingHandler(requestLimit, path);
|
||||
}
|
||||
|
||||
} catch (ServletException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
SSLContext sslContext = null;
|
||||
if (StringUtils.equalsIgnoreCase(httpsUseYn, "Y")) {
|
||||
// if ((httpsKeyStore != null && httpsKeyStore.length() > 1)
|
||||
// && (httpsKeyPass != null && httpsKeyPass.length() > 1)) {
|
||||
sslContext = HttpsGenerator.createSSLContext(httpsKeyStore, httpsKeyPass);
|
||||
// }
|
||||
}
|
||||
|
||||
logger.debug("####[" + adapter.getName() + "] ioThreads-->" + ioThreads);
|
||||
logger.debug("####[" + adapter.getName() + "] workerThreads-->" + workerThreads);
|
||||
Undertow.Builder builder = Undertow.builder().setIoThreads(ioThreads).setWorkerThreads(workerThreads);
|
||||
if (sslContext != null) {
|
||||
builder.addHttpsListener(listenPort, "0.0.0.0", sslContext);
|
||||
// server = Undertow.builder().setIoThreads(ioThreads).setWorkerThreads(workerThreads)
|
||||
// .addHttpsListener(listenPort, "0.0.0.0", sslContext).setHandler(requestLimitHandler)
|
||||
// .build();
|
||||
} else {
|
||||
builder.addHttpListener(listenPort, "0.0.0.0");
|
||||
// server = Undertow.builder().setIoThreads(ioThreads).setWorkerThreads(workerThreads)
|
||||
// .addHttpListener(listenPort, "0.0.0.0").setHandler(requestLimitHandler).build();
|
||||
}
|
||||
if (isKeepAlive){
|
||||
builder.setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, keepAliveTimeout * 1000);
|
||||
}
|
||||
builder.setHandler(requestLimitHandler);
|
||||
server = builder.build();
|
||||
|
||||
try {
|
||||
server.start();
|
||||
} catch (Throwable t) {
|
||||
logger.error(t.getMessage());
|
||||
String adapterFullName = adapter.getAdapterGroupName() + "." + adapter.getName();
|
||||
throw new Exception("failed start: " + adapterFullName);
|
||||
}
|
||||
|
||||
svrPortMap.put(listenPort, server);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void stop(AdapterVO adapter) throws Exception {
|
||||
AdapterGroupVO gvo = adapter.getAdapterGroupVO();
|
||||
|
||||
if (gvo != null && gvo.getType().equals(Keys.TYPE_HTTP_CUSTOM) && gvo.getGubun().equals("I")) {
|
||||
// Server Adapter 인 경우 정보 확인
|
||||
AdapterPropManager adapterManager = AdapterPropManager.getInstance();
|
||||
|
||||
Properties httpProp = adapterManager.getProperties(adapter.getPropGroupName());
|
||||
String portString = httpProp.getProperty(HTTP_LISTEN_PORT, "0");
|
||||
Integer listenPort = Integer.valueOf(portString);
|
||||
|
||||
// Custom Adapter 인 경우 재시작
|
||||
if (listenPort != 0) {
|
||||
ConcurrentHashMap<String, HttpDynamicInAdapterUri> uriMap = adptPortMap.get(listenPort);
|
||||
// 해당 Port로 수신하는 Adapter가 1개 이상 존재하는 경우 재시작 하지 않음 - 성능/안전성 이슈
|
||||
if (uriMap != null && uriMap.size() > 0)
|
||||
return;
|
||||
|
||||
Undertow server = svrPortMap.get(listenPort);
|
||||
if (server != null) {
|
||||
server.stop();
|
||||
svrPortMap.remove(listenPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
public void start() throws LifecycleException {
|
||||
try {
|
||||
AdapterManager adptManager = AdapterManager.getInstance();
|
||||
HashMap adptGrps = adptManager.getAllAdapterGroupVO();
|
||||
|
||||
for (Object obj : adptGrps.values()) {
|
||||
AdapterGroupVO gvo = (AdapterGroupVO) obj;
|
||||
if (gvo.getType().equals(Keys.TYPE_HTTP_CUSTOM) && gvo.getGubun().equals("I")) {
|
||||
Iterator i = gvo.getAdapters();
|
||||
while (i.hasNext()) {
|
||||
AdapterVO avo = (AdapterVO) i.next();
|
||||
addAdapter(avo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// adptPortMap.forEach((port,uriList) -> {
|
||||
// Undertow server = svrPortMap.get(port);
|
||||
//
|
||||
// if (server == null) {
|
||||
// ConcurrentHashMap<String, HttpDynamicInAdapterUri> uriMap = uriList;
|
||||
// ServletInfo servletInfo = Servlets.servlet(com.eactive.eai.adapter.http.dynamic.HttpAdapterCustom.class);
|
||||
// uriMap.forEach((url,adapterUri) -> {
|
||||
// //servletInfo.addMapping(adapterUri.getUri());
|
||||
// servletInfo.addMapping(adapterUri.getUri() + "/*");
|
||||
// });
|
||||
//
|
||||
// DeploymentInfo servletBuilder = Servlets
|
||||
// .deployment()
|
||||
// .setClassLoader(HttpDynamicPortInAdapterManager.class.getClassLoader())
|
||||
// .setDeploymentName("HttpDynamicCustomAdapter")
|
||||
// .setContextPath("/")
|
||||
// .addServlet(servletInfo);
|
||||
// DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
|
||||
// manager.deploy();
|
||||
//
|
||||
// PathHandler path = null;
|
||||
//
|
||||
// try {
|
||||
// path = Handlers.path(Handlers.redirect("/")).addPrefixPath("/", manager.start());
|
||||
// } catch (ServletException e) {
|
||||
// logger.error(e.getMessage(), e);
|
||||
// }
|
||||
//
|
||||
// server = Undertow.builder().addHttpListener(port, "0.0.0.0").setHandler(path).build();
|
||||
//
|
||||
// svrPortMap.put(port, server);
|
||||
// server.start();
|
||||
//
|
||||
// }
|
||||
// });
|
||||
} catch (Exception e) {
|
||||
logger.error("HttpDynamicPortInAdapterManager] start error", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws LifecycleException {
|
||||
|
||||
try {
|
||||
adptPortMap.forEach((port, UriList) -> {
|
||||
Undertow server = svrPortMap.get(port);
|
||||
|
||||
if (server != null) {
|
||||
server.stop();
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
logger.error("HttpDynamicPortInAdapterManager] stop error", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStarted() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterPropManager;
|
||||
import com.eactive.eai.common.server.EAIServerManager;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Properties;
|
||||
|
||||
public class HttpLoggerManager {
|
||||
|
||||
private static HttpLoggerManager instance = new HttpLoggerManager();
|
||||
|
||||
private HashMap<String, Logger> pool = new HashMap<String,Logger>(); // KEY:adapterGroupName, VALUE:Logger
|
||||
private String eaiServer;
|
||||
private static String DEFAULT_LOGGER_NAME = "HTT";
|
||||
private static String ERROR_LOGGER_NAME = "ERRORHTT";
|
||||
|
||||
public HttpLoggerManager() {
|
||||
eaiServer = EAIServerManager.getInstance().getLocalServerName();
|
||||
if (eaiServer == null) eaiServer = "";
|
||||
}
|
||||
|
||||
public static HttpLoggerManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
this.stop();
|
||||
}
|
||||
|
||||
public void removeAll() {
|
||||
pool.clear();
|
||||
}
|
||||
|
||||
public void start(String adapterGroupName) {
|
||||
this.addLogger(adapterGroupName);
|
||||
}
|
||||
|
||||
public synchronized void addLogger(String adapterGroupName) {
|
||||
String loggerName = this.getLoggerName( adapterGroupName );
|
||||
Logger logger = pool.get( loggerName );
|
||||
if (logger == null) {
|
||||
synchronized (pool) {
|
||||
logger = pool.get(loggerName);
|
||||
if (logger == null) {
|
||||
Logger logger2 = Logger.getLogger(loggerName);
|
||||
pool.put(loggerName, logger2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getLoggerName(String adapterGroupName) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("FileLogger{").append(adapterGroupName).append("}_").append(this.eaiServer);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String getLoggerBase(String adapterGroupName) {
|
||||
StringBuffer strBuff = new StringBuffer();
|
||||
strBuff.append("FileLogger{").append(adapterGroupName).append("}");
|
||||
|
||||
return strBuff.toString();
|
||||
}
|
||||
|
||||
public void removeLogger(String adapterGroupName) {
|
||||
pool.remove(this.getLoggerName(adapterGroupName));
|
||||
}
|
||||
|
||||
public void stop(String adapterGroupName) {
|
||||
// adapterGroupNameÀÇ Session Stop & Clear
|
||||
this.removeLogger(adapterGroupName);
|
||||
}
|
||||
|
||||
public Logger getLogger(String adapterGroupName) {
|
||||
return getLoggerGroup(adapterGroupName);
|
||||
}
|
||||
|
||||
public Logger getErrLogger() {
|
||||
return getLoggerGroup(ERROR_LOGGER_NAME);
|
||||
}
|
||||
|
||||
public Logger getLoggerGroup(String adapterGroupName) {
|
||||
|
||||
Logger logger = pool.get(this.getLoggerName(adapterGroupName));
|
||||
if (logger == null) {
|
||||
if (DEFAULT_LOGGER_NAME.equals(adapterGroupName)) {
|
||||
this.addLogger(adapterGroupName);
|
||||
logger = pool.get(this.getLoggerName(adapterGroupName));
|
||||
} else {
|
||||
AdapterPropManager propManager = AdapterPropManager.getInstance();
|
||||
Properties loggerInfo = null;
|
||||
|
||||
try {
|
||||
loggerInfo = propManager.getProperties(getLoggerBase(adapterGroupName));
|
||||
} catch(Exception e) {
|
||||
// ¹«½Ã
|
||||
}
|
||||
|
||||
if (loggerInfo == null) {
|
||||
logger = getLogger();
|
||||
} else {
|
||||
this.addLogger(adapterGroupName);
|
||||
logger = pool.get(this.getLoggerName(adapterGroupName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return logger;
|
||||
}
|
||||
|
||||
public Logger getLogger() {
|
||||
Logger logger = pool.get(this.getLoggerName(this.DEFAULT_LOGGER_NAME));
|
||||
if (logger == null) {
|
||||
this.addLogger(DEFAULT_LOGGER_NAME);
|
||||
logger = pool.get(this.getLoggerName(DEFAULT_LOGGER_NAME));
|
||||
}
|
||||
|
||||
return logger;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.server.EAIServerManager;
|
||||
import com.eactive.eai.common.util.DatetimeUtil;
|
||||
import com.eactive.eai.common.util.InboundErrorLogger;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.common.util.UUIDGenerator;
|
||||
import com.eactive.eai.inbound.error.InboundErrorInfoVO;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Date;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class RestApiAdapterMain extends HttpServlet implements HttpAdapterServiceKey {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void service(HttpServletRequest request, HttpServletResponse response) {
|
||||
HttpDynamicInAdapterManager manager = HttpDynamicInAdapterManager.getInstance();
|
||||
// /ONLWeb/api/v1/public/getUserInfo.svc
|
||||
String apiUri = request.getRequestURI();
|
||||
// /api/v1/public/getUserInfo.svc
|
||||
apiUri = StringUtils.removeStart(apiUri, request.getContextPath());
|
||||
apiUri = StringUtils.removeEnd(apiUri, "/");
|
||||
HttpDynamicInAdapterUri adptUri = findAdptUri(apiUri, manager);
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpDynamicInAdapter] service request uri : " + request.getRequestURI());
|
||||
}
|
||||
|
||||
if (adptUri == null) {
|
||||
logError(request, response);
|
||||
return;
|
||||
}
|
||||
HttpAdapterService service = HttpAdapterServiceFactory.createFactory(adptUri.getAdptType());
|
||||
service.service(adptUri.getAdptGrpName(), adptUri.getAdptName(), request, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* url에서 뒤쪽 /이후를 제거하면서 찾는다.<br>
|
||||
* /api/v1/public/getUserInfo.svc
|
||||
*
|
||||
* @param apiUri
|
||||
* @return
|
||||
*/
|
||||
private HttpDynamicInAdapterUri findAdptUri(String apiUri, HttpDynamicInAdapterManager manager) {
|
||||
if (StringUtils.isBlank(apiUri)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
HttpDynamicInAdapterUri adptUri = manager.getAdptUri(apiUri);
|
||||
if (adptUri != null) {
|
||||
return adptUri;
|
||||
}
|
||||
|
||||
// /api/v1/public
|
||||
apiUri = StringUtils.substringBeforeLast(apiUri, "/");
|
||||
if (apiUri.length() < 4) { // /api
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void logError(HttpServletRequest request, HttpServletResponse response) {
|
||||
InboundErrorInfoVO errorInfoVO = new InboundErrorInfoVO();
|
||||
|
||||
EAIServerManager eaiServerManager = EAIServerManager.getInstance();
|
||||
String serverName = eaiServerManager.getLocalServerName();
|
||||
String uuid = null;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
String instanceid1 = serverName.substring(0, 2);
|
||||
String instanceid2 = serverName.substring(serverName.length() - 2, serverName.length());
|
||||
String instid = instanceid1 + instanceid2;
|
||||
uuid = instid + UUIDGenerator.getUUID();
|
||||
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = request.getRequestURI();
|
||||
|
||||
String errorCode = "RECEAIIRP010";
|
||||
String errorMsg = ExceptionUtil.make(new Exception("cat not find uri"), errorCode, msgArgs);
|
||||
|
||||
errorInfoVO.setEaiSvcSno(uuid); // EAI서비스일련번호
|
||||
errorInfoVO.setAdptBwkGrpNm("HTTP_IN_NO_URI"); // 어댑터업무그룹명
|
||||
errorInfoVO.setErrCd(errorCode); // 에러코드
|
||||
errorInfoVO.setErrTxt(errorMsg); // 에러내용
|
||||
errorInfoVO.setErrTm(DatetimeUtil.getCurrentTime(new Date().getTime())); // 에러발생시각
|
||||
errorInfoVO.setErrDstcd(" ");
|
||||
sb.append("Remote Addr : ").append(request.getRemoteAddr()).append("\n").append("Request URI : ")
|
||||
.append(request.getRequestURI());
|
||||
errorInfoVO.setBwkDataTxt(sb.toString()); // 업무데이터내용
|
||||
errorInfoVO.setEaiSvrInstNm(serverName); // EAI서버인스턴스명
|
||||
|
||||
InboundErrorLogger.error(errorInfoVO);
|
||||
|
||||
response.setStatus(404);
|
||||
try {
|
||||
response.getWriter().println("can not find uri '" + request.getRequestURI() + "'");
|
||||
} catch (Exception ex) {
|
||||
// IGNORE
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.eactive.eai.adapter.http.dynamic;
|
||||
|
||||
import io.undertow.servlet.handlers.DefaultServlet;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class UndertowHealthCheckServlet extends DefaultServlet {
|
||||
|
||||
protected void service(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
response.getWriter().print("");
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.filter;
|
||||
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceKey;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceSupport;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.util.IpUtil;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class AdapterAllowIPListFilter implements HttpAdapterFilter {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
@Override
|
||||
public Object doPreFilter(String adptGrpName, String adptName, Object message, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
|
||||
String requestIp = getClientIp(request);
|
||||
try {
|
||||
String allowedIps = prop.getProperty(HttpAdapterServiceKey.ALLOW_IP, "");
|
||||
if (StringUtils.isBlank(allowedIps)) {
|
||||
return message;
|
||||
}
|
||||
|
||||
if (!IpUtil.isMatchIp(allowedIps, requestIp)) {
|
||||
throw new HttpStatusException("This IP was not allowed-" + requestIp, HttpStatus.FORBIDDEN.value());
|
||||
}
|
||||
} catch (HttpStatusException e) {
|
||||
logger.debug(e.getMessage());
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new JwtAuthException(JwtAuthFilter.ERROR_AUTHORIZATION_FAIL, "Client IP check failed-" + requestIp);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
private String getClientIp(HttpServletRequest request) {
|
||||
String ipAddress = request.getHeader("X-FORWARDED-FOR");
|
||||
if (ipAddress == null) {
|
||||
ipAddress = request.getRemoteAddr();
|
||||
}
|
||||
if(ipAddress.indexOf(",") >= 0) {
|
||||
ipAddress = org.springframework.util.StringUtils.tokenizeToStringArray(ipAddress, ",")[0];
|
||||
}
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
private String assignClientId(Properties prop, HttpServletRequest request) {
|
||||
String clientId = null;
|
||||
try {
|
||||
// 이전 filter에서 셋팅한 clientId 확보
|
||||
clientId = prop.getProperty(HttpAdapterServiceSupport.PROPERTIES_NAME_CLIENT_ID);
|
||||
|
||||
// 이전 filter에서 셋팅한 값이 없다면 header 정보에서 확보
|
||||
if (StringUtils.isBlank(clientId)) {
|
||||
clientId = request.getHeader(HttpAdapterServiceSupport.HEADER_NAME_CLIENT_ID);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
|
||||
return clientId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object doPostFilter(String adptGrpName, String adptName, Object resultMessage, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
return resultMessage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.filter;
|
||||
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceSupport;
|
||||
import com.eactive.eai.authserver.service.OAuth2Manager;
|
||||
import com.eactive.eai.authserver.vo.ClientVO;
|
||||
import com.eactive.eai.common.message.EAIMessage;
|
||||
import com.eactive.eai.common.message.EAIMessageManager;
|
||||
import com.eactive.eai.common.property.PropGroupVO;
|
||||
import com.eactive.eai.common.property.PropManager;
|
||||
import com.eactive.eai.common.stdmessage.STDMessageManager;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.inbound.action.ActionFactory;
|
||||
import com.eactive.eai.inbound.action.RequestAction;
|
||||
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.nimbusds.jose.JWSVerifier;
|
||||
import com.nimbusds.jose.crypto.RSASSAVerifier;
|
||||
import com.nimbusds.jose.util.IOUtils;
|
||||
import com.nimbusds.jwt.SignedJWT;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.text.ParseException;
|
||||
import java.util.Base64;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
|
||||
public class ApiAuthFilter implements HttpAdapterFilter {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
public static final String PROP_GROUP_AUTH_SERVER = "OAuthServer";
|
||||
public static final String PROP_KEYSTORE_PATH = "certification.publicKeyPath";
|
||||
|
||||
public static final String ERROR_AUTHENTICATION_FAIL = "E.AUTHENTICATION_FAIL";
|
||||
public static final String ERROR_AUTHORIZATION_FAIL = "E.AUTHORIZATION_FAIL";
|
||||
public static final String ERROR_TOKEN_EXPIRED = "E.TOKEN_EXPIRED";
|
||||
|
||||
public static final String PAYLOAD_PARAM_NAME_SCOPE = "scope";
|
||||
public static final String PAYLOAD_PARAM_NAME_CLIENT_ID = "client_id";
|
||||
|
||||
private JWSVerifier jwsVerifier;
|
||||
|
||||
public ApiAuthFilter() {
|
||||
super();
|
||||
|
||||
PropGroupVO vo = PropManager.getInstance().getPropGroupVO(PROP_GROUP_AUTH_SERVER);
|
||||
String publicKeyPath = "/certificate/elink-oauth-dev.pub";
|
||||
if (vo != null) {
|
||||
publicKeyPath = vo.getProperty(PROP_KEYSTORE_PATH);
|
||||
} else {
|
||||
logger.warn("The properties has not been set.[" + PROP_GROUP_AUTH_SERVER + "]");
|
||||
}
|
||||
|
||||
Resource resource = new ClassPathResource(publicKeyPath);
|
||||
String publicKey = null;
|
||||
try {
|
||||
publicKey = IOUtils.readInputStreamToString(resource.getInputStream());
|
||||
publicKey = StringUtils.replace(publicKey, "-----BEGIN PUBLIC KEY-----", "");
|
||||
publicKey = StringUtils.replace(publicKey, "-----END PUBLIC KEY-----", "");
|
||||
publicKey = StringUtils.remove(publicKey, "\r");
|
||||
publicKey = StringUtils.remove(publicKey, "\n");
|
||||
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey));
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
jwsVerifier = new RSASSAVerifier((RSAPublicKey) keyFactory.generatePublic(keySpec));
|
||||
} catch (IOException | NoSuchAlgorithmException | RuntimeException | InvalidKeySpecException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object doPreFilter(String adptGrpName, String adptName, Object message, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
String apiId = assignApiId(adptGrpName, adptName, message, prop, request);
|
||||
if (StringUtils.isBlank(apiId)) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "Can not find apiId(apiCode) info");
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: 중복되는 표준메시지 조회로 리소드 낭비가 있을 수 있으므로 체크 자체를 추후 RequestProcessor혹은 RestProcessor로 이동 필요 해 보임
|
||||
* ApiRequestProcessor을 만드는것도 괜찮을 수 있을 것 같으나 관리가 안될 것 같음
|
||||
*/
|
||||
StandardMessage standardMessage = STDMessageManager.getInstance().getSTDMessage(apiId);
|
||||
String eaiSvcCd = StandardMessageManager.getInstance().getMapper().getEaiSvcCode(standardMessage);
|
||||
EAIMessage eaiMessage = EAIMessageManager.getInstance().getEAIMessage(eaiSvcCd);
|
||||
|
||||
if(StringUtils.isBlank(eaiMessage.getAuthType())){
|
||||
return message;
|
||||
}
|
||||
|
||||
switch (eaiMessage.getAuthType()){
|
||||
case "oauth":
|
||||
try {
|
||||
String token = ApiAuthFilter.extractJWTToken(request);
|
||||
SignedJWT signedJWT = SignedJWT.parse(token);
|
||||
|
||||
if (new Date().after(signedJWT.getJWTClaimsSet().getExpirationTime())) {
|
||||
throw new JwtAuthException(ERROR_TOKEN_EXPIRED, "Access token expired");
|
||||
}
|
||||
|
||||
if (!signedJWT.verify(jwsVerifier)) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "Token invalid");
|
||||
}
|
||||
|
||||
OAuth2Manager manager = OAuth2Manager.getInstance();
|
||||
HashSet<String> scopeSet = manager.getApiScopeMap().get(apiId);
|
||||
if (!verifyScope(scopeSet, signedJWT)) {
|
||||
throw new JwtAuthException(ERROR_AUTHORIZATION_FAIL,
|
||||
String.format("Insufficient [Scope](Allowed Scope=%s)", scopeSet.toString()));
|
||||
}
|
||||
|
||||
// header 값으로 전송된 clientId와 토큰 소유 clientId check
|
||||
if (!verifyClientId(prop, signedJWT)) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "client_id not matched(header/token)");
|
||||
}
|
||||
} catch (JwtAuthException e) {
|
||||
logger.debug(e.getMessage());
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "Token invalid");
|
||||
}
|
||||
break;
|
||||
case "api_key":
|
||||
String apiKeyName = PropManager.getInstance().getProperty("ApiConfig", "api.key.name", "x-api-key");
|
||||
String apiKey = request.getHeader(apiKeyName);
|
||||
if(apiKey == null){
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "Invalid or missing API key \""+apiKeyName+"\" in Http Header");
|
||||
}
|
||||
prop.setProperty(HttpAdapterServiceSupport.PROPERTIES_NAME_CLIENT_ID, apiKey);
|
||||
break;
|
||||
case "none":
|
||||
return message;
|
||||
}
|
||||
|
||||
String clientId = prop.getProperty(HttpAdapterServiceSupport.PROPERTIES_NAME_CLIENT_ID);
|
||||
ClientVO clientVO = OAuth2Manager.getInstance().getClientVO(clientId);
|
||||
if(clientVO == null){
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "Unregistered APP");
|
||||
}
|
||||
|
||||
if(clientVO.getApiMap().containsKey(eaiSvcCd) == false){
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "Unregistered API ["+eaiSvcCd+"] in ["+clientVO.getClientName()+"] APP");
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
private boolean verifyClientId(Properties prop, SignedJWT signedJWT) {
|
||||
try {
|
||||
// 이전 filter에서 셋팅한 clientId 확보
|
||||
String headerClientId = prop.getProperty(HttpAdapterServiceSupport.PROPERTIES_NAME_CLIENT_ID);
|
||||
String tokenClientId = (String) signedJWT.getJWTClaimsSet().getClaim(PAYLOAD_PARAM_NAME_CLIENT_ID);
|
||||
|
||||
// 이전 filter에서 셋팅한 값이 없다면 token 정보에서 확보
|
||||
if (StringUtils.isBlank(headerClientId)) {
|
||||
if (StringUtils.isNotBlank(tokenClientId)) {
|
||||
prop.put(HttpAdapterServiceSupport.PROPERTIES_NAME_CLIENT_ID, tokenClientId);
|
||||
}
|
||||
} else { // header로 전달된 clientId가 있을때만 비교
|
||||
if (!headerClientId.equals(tokenClientId)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private String assignApiId(String adptGrpName, String adptName, Object message, Properties prop,
|
||||
HttpServletRequest request) {
|
||||
String apiId = null;
|
||||
try {
|
||||
String actionName = prop.getProperty(Processor.REQUEST_ACTION);
|
||||
RequestAction action = ActionFactory.createAction(actionName);
|
||||
action.setAdapterInfo(adptGrpName, adptName, prop);
|
||||
String[] keys = action.perform(message);
|
||||
apiId = keys[0];
|
||||
|
||||
// PathVariable 지원 추가
|
||||
apiId = StandardMessageUtil.getMatchedKey(apiId, actionName);
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
|
||||
// // header 에서 확보
|
||||
// String apiId = request.getHeader(HEADER_NAME_API_CODE);
|
||||
// if (StringUtils.isBlank(apiId)) {
|
||||
// // url에서 확보
|
||||
// // /ONLWeb/api/v1/public/getUserInfo.svc/
|
||||
// apiId = request.getRequestURI();
|
||||
// // /ONLWeb/api/v1/public/getUserInfo.svc
|
||||
// apiId = StringUtils.removeEnd(apiId, "/");
|
||||
// // getUserInfo.svc
|
||||
// apiId = StringUtils.substringAfterLast(apiId, "/");
|
||||
// }
|
||||
|
||||
return apiId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object doPostFilter(String adptGrpName, String adptName, Object resultMessage, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
return resultMessage;
|
||||
}
|
||||
|
||||
private boolean verifyScope(HashSet<String> scopeSet, SignedJWT signedJWT) throws ParseException {
|
||||
if (scopeSet == null || scopeSet.isEmpty()) {
|
||||
return true; // If no scope is specified in the api, all are allowed
|
||||
}
|
||||
|
||||
String[] scopeArr = signedJWT.getJWTClaimsSet().getStringArrayClaim(PAYLOAD_PARAM_NAME_SCOPE);
|
||||
if (scopeArr == null || scopeArr.length == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (String scope : scopeArr) {
|
||||
if (scopeSet.contains(scope)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String extractJWTToken(HttpServletRequest request) throws JwtAuthException {
|
||||
String authorization = request.getHeader("Authorization");
|
||||
if (StringUtils.isBlank(authorization)) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "No have header info: Authorization");
|
||||
}
|
||||
|
||||
String[] components = authorization.split("\\s");
|
||||
|
||||
if (components.length != 2) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "Malformat [Authorization] content");
|
||||
}
|
||||
|
||||
if (!StringUtils.equalsIgnoreCase(components[0], "Bearer")) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "[Bearer] is needed");
|
||||
}
|
||||
|
||||
return components[1].trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.filter;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public interface HttpAdapterFilter {
|
||||
|
||||
public Object doPreFilter(String adptGrpName, String adptName, Object message, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception;
|
||||
|
||||
public Object doPostFilter(String adptGrpName, String adptName, Object resultMessage, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception;
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.filter;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class HttpAdapterFilterFactory {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
static private ConcurrentHashMap<String, HttpAdapterFilter> h = new ConcurrentHashMap<String, HttpAdapterFilter>();
|
||||
|
||||
public HttpAdapterFilterFactory() {
|
||||
super();
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static HttpAdapterFilter createFilter(String type) {
|
||||
if (h.containsKey(type)) {
|
||||
return (HttpAdapterFilter) h.get(type);
|
||||
}
|
||||
|
||||
HttpAdapterFilter filter = null;
|
||||
switch (HttpAdapterFilterType.getValue(type)) {
|
||||
case APIAUTHFILTER:
|
||||
filter = new ApiAuthFilter();
|
||||
break;
|
||||
case JWTAUTHFILTER:
|
||||
filter = new JwtAuthFilter();
|
||||
break;
|
||||
case IPWHITELISTFILTER:
|
||||
filter = new IpWhiteListFilter();
|
||||
break;
|
||||
case ADAPTERALLOWIPLISTFILTER:
|
||||
filter = new AdapterAllowIPListFilter();
|
||||
break;
|
||||
default:
|
||||
try {
|
||||
Class cl = Class.forName(type);
|
||||
filter = (HttpAdapterFilter) cl.newInstance();
|
||||
} catch (Exception e) {
|
||||
logger.error("Cannot create a HttpAdapterFilter. - {}", type);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (filter != null) {
|
||||
h.put(type, filter);
|
||||
return filter;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.filter;
|
||||
|
||||
public enum HttpAdapterFilterType {
|
||||
APIAUTHFILTER,
|
||||
JWTAUTHFILTER,
|
||||
IPWHITELISTFILTER,
|
||||
ADAPTERALLOWIPLISTFILTER,
|
||||
UNKNOWN;
|
||||
|
||||
public static HttpAdapterFilterType getValue(String type) {
|
||||
try {
|
||||
return valueOf(type.toUpperCase());
|
||||
} catch (Exception e) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.filter;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceSupport;
|
||||
import com.eactive.eai.authserver.service.OAuth2Manager;
|
||||
import com.eactive.eai.authserver.vo.ClientVO;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.util.IpUtil;
|
||||
|
||||
public class IpWhiteListFilter implements HttpAdapterFilter {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
@Override
|
||||
public Object doPreFilter(String adptGrpName, String adptName, Object message, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
String clientId = assignClientId(prop, request);
|
||||
if (StringUtils.isBlank(clientId)) {
|
||||
throw new JwtAuthException(JwtAuthFilter.ERROR_AUTHORIZATION_FAIL, "Can not find clientId info");
|
||||
}
|
||||
|
||||
String requestIp = getClientIp(request);
|
||||
try {
|
||||
ClientVO client = (ClientVO) OAuth2Manager.getInstance().getClientDeatilsStore().get(clientId);
|
||||
if (client == null) {
|
||||
throw new JwtAuthException(JwtAuthFilter.ERROR_AUTHORIZATION_FAIL, "Can not find client info");
|
||||
}
|
||||
|
||||
String allowedIps = client.getAllowedIp();
|
||||
if (StringUtils.isBlank(allowedIps)) {
|
||||
throw new JwtAuthException(JwtAuthFilter.ERROR_AUTHORIZATION_FAIL,
|
||||
"Allowed IP is not set in client info");
|
||||
}
|
||||
|
||||
if (!IpUtil.isMatchIp(allowedIps, requestIp)) {
|
||||
throw new JwtAuthException(JwtAuthFilter.ERROR_AUTHORIZATION_FAIL,
|
||||
"This IP was not allowed-" + requestIp);
|
||||
}
|
||||
} catch (JwtAuthException e) {
|
||||
logger.debug(e.getMessage());
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new JwtAuthException(JwtAuthFilter.ERROR_AUTHORIZATION_FAIL, "Client IP check failed-" + requestIp);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
private String getClientIp(HttpServletRequest request) {
|
||||
String ipAddress = request.getHeader("X-FORWARDED-FOR");
|
||||
if (ipAddress == null) {
|
||||
ipAddress = request.getRemoteAddr();
|
||||
}
|
||||
if(ipAddress.indexOf(",") >= 0) {
|
||||
ipAddress = org.springframework.util.StringUtils.tokenizeToStringArray(ipAddress, ",")[0];
|
||||
}
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
private String assignClientId(Properties prop, HttpServletRequest request) {
|
||||
String clientId = null;
|
||||
try {
|
||||
// 이전 filter에서 셋팅한 clientId 확보
|
||||
clientId = prop.getProperty(HttpAdapterServiceSupport.PROPERTIES_NAME_CLIENT_ID);
|
||||
|
||||
// 이전 filter에서 셋팅한 값이 없다면 header 정보에서 확보
|
||||
if (StringUtils.isBlank(clientId)) {
|
||||
clientId = request.getHeader(HttpAdapterServiceSupport.HEADER_NAME_CLIENT_ID);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
|
||||
return clientId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object doPostFilter(String adptGrpName, String adptName, Object resultMessage, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
return resultMessage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.filter;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class JwtAuthException extends Exception {
|
||||
private String code;
|
||||
|
||||
public JwtAuthException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public JwtAuthException(String code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public JwtAuthException(String code, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getClass().getName() + ": [" + code + "] " + getLocalizedMessage();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.filter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.text.ParseException;
|
||||
import java.util.Base64;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceSupport;
|
||||
import com.eactive.eai.authserver.service.OAuth2Manager;
|
||||
import com.eactive.eai.common.property.PropGroupVO;
|
||||
import com.eactive.eai.common.property.PropManager;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.inbound.action.ActionFactory;
|
||||
import com.eactive.eai.inbound.action.RequestAction;
|
||||
import com.eactive.eai.inbound.processor.Processor;
|
||||
import com.eactive.eai.message.StandardMessageUtil;
|
||||
import com.nimbusds.jose.JWSVerifier;
|
||||
import com.nimbusds.jose.crypto.RSASSAVerifier;
|
||||
import com.nimbusds.jose.util.IOUtils;
|
||||
import com.nimbusds.jwt.SignedJWT;
|
||||
|
||||
public class JwtAuthFilter implements HttpAdapterFilter {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
public static final String PROP_GROUP_AUTH_SERVER = "OAuthServer";
|
||||
public static final String PROP_KEYSTORE_PATH = "certification.publicKeyPath";
|
||||
|
||||
public static final String ERROR_AUTHENTICATION_FAIL = "E.AUTHENTICATION_FAIL";
|
||||
public static final String ERROR_AUTHORIZATION_FAIL = "E.AUTHORIZATION_FAIL";
|
||||
public static final String ERROR_TOKEN_EXPIRED = "E.TOKEN_EXPIRED";
|
||||
|
||||
public static final String PAYLOAD_PARAM_NAME_SCOPE = "scope";
|
||||
public static final String PAYLOAD_PARAM_NAME_CLIENT_ID = "client_id";
|
||||
|
||||
private JWSVerifier jwsVerifier;
|
||||
|
||||
public JwtAuthFilter() {
|
||||
super();
|
||||
|
||||
PropGroupVO vo = PropManager.getInstance().getPropGroupVO(PROP_GROUP_AUTH_SERVER);
|
||||
String publicKeyPath = "/certificate/elink-oauth-dev.pub";
|
||||
if (vo != null) {
|
||||
publicKeyPath = vo.getProperty(PROP_KEYSTORE_PATH);
|
||||
} else {
|
||||
logger.warn("The properties has not been set.[" + PROP_GROUP_AUTH_SERVER + "]");
|
||||
}
|
||||
|
||||
Resource resource = new ClassPathResource(publicKeyPath);
|
||||
String publicKey = null;
|
||||
try {
|
||||
publicKey = IOUtils.readInputStreamToString(resource.getInputStream());
|
||||
publicKey = StringUtils.replace(publicKey, "-----BEGIN PUBLIC KEY-----", "");
|
||||
publicKey = StringUtils.replace(publicKey, "-----END PUBLIC KEY-----", "");
|
||||
publicKey = StringUtils.remove(publicKey, "\r");
|
||||
publicKey = StringUtils.remove(publicKey, "\n");
|
||||
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey));
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
jwsVerifier = new RSASSAVerifier((RSAPublicKey) keyFactory.generatePublic(keySpec));
|
||||
} catch (IOException | NoSuchAlgorithmException | RuntimeException | InvalidKeySpecException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object doPreFilter(String adptGrpName, String adptName, Object message, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
String apiId = assignApiId(adptGrpName, adptName, message, prop, request);
|
||||
if (StringUtils.isBlank(apiId)) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "Can not find apiId(apiCode) info");
|
||||
}
|
||||
|
||||
try {
|
||||
String token = JwtAuthFilter.extractJWTToken(request);
|
||||
SignedJWT signedJWT = SignedJWT.parse(token);
|
||||
|
||||
if (new Date().after(signedJWT.getJWTClaimsSet().getExpirationTime())) {
|
||||
throw new JwtAuthException(ERROR_TOKEN_EXPIRED, "Access token expired");
|
||||
}
|
||||
|
||||
if (!signedJWT.verify(jwsVerifier)) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "Token invalid");
|
||||
}
|
||||
|
||||
OAuth2Manager manager = OAuth2Manager.getInstance();
|
||||
HashSet<String> scopeSet = manager.getApiScopeMap().get(apiId);
|
||||
if (!verifyScope(scopeSet, signedJWT)) {
|
||||
throw new JwtAuthException(ERROR_AUTHORIZATION_FAIL,
|
||||
String.format("Insufficient [Scope](Allowed Scope=%s)", scopeSet.toString()));
|
||||
}
|
||||
|
||||
// header 값으로 전송된 clientId와 토큰 소유 clientId check
|
||||
if (!verifyClientId(prop, signedJWT)) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "client_id not matched(header/token)");
|
||||
}
|
||||
} catch (JwtAuthException e) {
|
||||
logger.debug(e.getMessage());
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "Token invalid");
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
private boolean verifyClientId(Properties prop, SignedJWT signedJWT) {
|
||||
try {
|
||||
// 이전 filter에서 셋팅한 clientId 확보
|
||||
String headerClientId = prop.getProperty(HttpAdapterServiceSupport.PROPERTIES_NAME_CLIENT_ID);
|
||||
String tokenClientId = (String) signedJWT.getJWTClaimsSet().getClaim(PAYLOAD_PARAM_NAME_CLIENT_ID);
|
||||
|
||||
// 이전 filter에서 셋팅한 값이 없다면 token 정보에서 확보
|
||||
if (StringUtils.isBlank(headerClientId)) {
|
||||
if (StringUtils.isNotBlank(tokenClientId)) {
|
||||
prop.put(HttpAdapterServiceSupport.PROPERTIES_NAME_CLIENT_ID, tokenClientId);
|
||||
}
|
||||
} else { // header로 전달된 clientId가 있을때만 비교
|
||||
if (!headerClientId.equals(tokenClientId)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private String assignApiId(String adptGrpName, String adptName, Object message, Properties prop,
|
||||
HttpServletRequest request) {
|
||||
String apiId = null;
|
||||
try {
|
||||
String actionName = prop.getProperty(Processor.REQUEST_ACTION);
|
||||
RequestAction action = ActionFactory.createAction(actionName);
|
||||
action.setAdapterInfo(adptGrpName, adptName, prop);
|
||||
String[] keys = action.perform(message);
|
||||
apiId = keys[0];
|
||||
|
||||
// PathVariable 지원 추가
|
||||
apiId = StandardMessageUtil.getMatchedKey(apiId, actionName);
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
|
||||
// // header 에서 확보
|
||||
// String apiId = request.getHeader(HEADER_NAME_API_CODE);
|
||||
// if (StringUtils.isBlank(apiId)) {
|
||||
// // url에서 확보
|
||||
// // /ONLWeb/api/v1/public/getUserInfo.svc/
|
||||
// apiId = request.getRequestURI();
|
||||
// // /ONLWeb/api/v1/public/getUserInfo.svc
|
||||
// apiId = StringUtils.removeEnd(apiId, "/");
|
||||
// // getUserInfo.svc
|
||||
// apiId = StringUtils.substringAfterLast(apiId, "/");
|
||||
// }
|
||||
|
||||
return apiId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object doPostFilter(String adptGrpName, String adptName, Object resultMessage, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
return resultMessage;
|
||||
}
|
||||
|
||||
private boolean verifyScope(HashSet<String> scopeSet, SignedJWT signedJWT) throws ParseException {
|
||||
if (scopeSet == null || scopeSet.isEmpty()) {
|
||||
return true; // If no scope is specified in the api, all are allowed
|
||||
}
|
||||
|
||||
String[] scopeArr = signedJWT.getJWTClaimsSet().getStringArrayClaim(PAYLOAD_PARAM_NAME_SCOPE);
|
||||
if (scopeArr == null || scopeArr.length == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (String scope : scopeArr) {
|
||||
if (scopeSet.contains(scope)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String extractJWTToken(HttpServletRequest request) throws JwtAuthException {
|
||||
String authorization = request.getHeader("Authorization");
|
||||
if (StringUtils.isBlank(authorization)) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "No have header info: Authorization");
|
||||
}
|
||||
|
||||
String[] components = authorization.split("\\s");
|
||||
|
||||
if (components.length != 2) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "Malformat [Authorization] content");
|
||||
}
|
||||
|
||||
if (!StringUtils.equalsIgnoreCase(components[0], "Bearer")) {
|
||||
throw new JwtAuthException(ERROR_AUTHENTICATION_FAIL, "[Bearer] is needed");
|
||||
}
|
||||
|
||||
return components[1].trim();
|
||||
}
|
||||
}
|
||||
+508
@@ -0,0 +1,508 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.impl;
|
||||
|
||||
import java.net.HttpCookie;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
import org.apache.mina.common.ByteBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterManager;
|
||||
import com.eactive.eai.adapter.AdapterPropManager;
|
||||
import com.eactive.eai.adapter.AdapterVO;
|
||||
import com.eactive.eai.adapter.Keys;
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.dynamic.filter.JwtAuthException;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.common.util.MessageUtil;
|
||||
import com.eactive.eai.env.ElinkConfig;
|
||||
import com.eactive.eai.inbound.action.ActionFactory;
|
||||
import com.eactive.eai.inbound.action.RequestAction;
|
||||
import com.eactive.eai.inbound.processor.Processor;
|
||||
import com.eactive.eai.message.StandardMessageUtil;
|
||||
|
||||
public class HttpAdapterServiceBypass extends HttpAdapterServiceSupport {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
public static final String HTTP_STATUS = "HTTP_STATUS";
|
||||
public static final String PROPERTIES_NAME_HTTP_REQUEST_METHOD = "httpRequestMethod";
|
||||
public static final String[] HOP_BY_HOP_HEADERS = new String[] { "Connection", "Keep-Alive", "Proxy-Authenticate",
|
||||
"Proxy-Authorization", "TE", "Trailers", "Transfer-Encoding", "Upgrade" };
|
||||
|
||||
protected boolean doPreserveCookies = false;
|
||||
protected boolean doPreserveCookiePath = false;
|
||||
|
||||
/**
|
||||
* 1. 기능 : Header 값과 Body 값을 Bypass 한다.
|
||||
*
|
||||
* @param request Http Servlet Request
|
||||
* @return response Http Servlet Response
|
||||
* @exception Exception
|
||||
**/
|
||||
public void service(String adptGrpName, String adptName, HttpServletRequest request, HttpServletResponse response) {
|
||||
int traceLevel = 0;
|
||||
|
||||
AdapterVO adptVO = null;
|
||||
AdapterPropManager manager = null;
|
||||
|
||||
Properties httpProp = null;
|
||||
String responseType = null;
|
||||
String encode = null;
|
||||
|
||||
String traceLevelTemp = null;
|
||||
|
||||
StopWatch stopWatch = null;
|
||||
Properties prop = null;
|
||||
String errorResponseFormat = null;
|
||||
|
||||
try {
|
||||
AdapterManager adapterManager = AdapterManager.getInstance();
|
||||
adptVO = adapterManager.getAdapterVO(adptGrpName, adptName);
|
||||
if (adptVO == null) {
|
||||
throw new Exception("Adapter not found error");
|
||||
}
|
||||
|
||||
manager = AdapterPropManager.getInstance();
|
||||
|
||||
httpProp = manager.getProperties(adptVO.getPropGroupName());
|
||||
responseType = httpProp.getProperty(RESPONSE_TYPE, "SYNC");
|
||||
encode = StringUtils.defaultIfBlank(adptVO.getAdapterGroupVO().getMessageEncode(), "UTF-8");
|
||||
traceLevelTemp = httpProp.getProperty(TRACE_LEVEL, "0");
|
||||
errorResponseFormat = httpProp.getProperty(ERROR_RESPONSE_FORMAT);
|
||||
|
||||
try {
|
||||
traceLevel = Integer.parseInt(traceLevelTemp);
|
||||
} catch (Exception e) {
|
||||
logger.warn("traceLevel parsing error - " + traceLevelTemp);
|
||||
}
|
||||
|
||||
stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
logger.debug("시작 >> encode = [" + encode + "]");
|
||||
|
||||
String requestData = "";
|
||||
if (request.getHeader(HttpHeaders.CONTENT_LENGTH) != null
|
||||
|| request.getHeader(HttpHeaders.TRANSFER_ENCODING) != null) {
|
||||
ServletInputStream sis = request.getInputStream();
|
||||
ByteBuffer bb = ByteBuffer.allocate(1024).setAutoExpand(true);
|
||||
int i = 0;
|
||||
byte[] cbuf = new byte[1024];
|
||||
while ((i = sis.read(cbuf, 0, 1024)) != -1) {
|
||||
if (i == 1024) {
|
||||
bb.put(cbuf);
|
||||
} else {
|
||||
byte[] tail = new byte[i];
|
||||
System.arraycopy(cbuf, 0, tail, 0, i);
|
||||
bb.put(tail);
|
||||
}
|
||||
}
|
||||
byte[] data = new byte[bb.position()];
|
||||
bb.position(0);
|
||||
bb.get(data);
|
||||
requestData = new String(data, encode);
|
||||
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.txlog(adptGrpName + adptName,
|
||||
"RECV " + "[Bypass Request..]" + CommonLib.getDumpMessage(requestData));
|
||||
}
|
||||
}
|
||||
|
||||
prop = new Properties();
|
||||
prop.put(INBOUND_METHOD, request.getMethod());
|
||||
prop.put(INBOUND_URI, request.getRequestURI());
|
||||
prop.put(INBOUND_QUERY_STRING, StringUtils.defaultString(request.getQueryString()));
|
||||
prop.put(INBOUND_HEADER, getHeaders(request));
|
||||
if (StringUtils.equals(adptVO.getAdapterGroupVO().getType(), Keys.TYPE_REST)
|
||||
|| StringUtils.equals(adptVO.getAdapterGroupVO().getType(), Keys.TYPE_HTTP_CUSTOM)) {
|
||||
// /api/v1/public/getUserInfo.svc
|
||||
String extUrl = StringUtils.removeStart(request.getRequestURI(), request.getContextPath());
|
||||
prop.put(INBOUND_EXTURI, extUrl);
|
||||
} else {
|
||||
prop.put(INBOUND_EXTURI, getExtUri(request));
|
||||
}
|
||||
prop.put(Processor.REQUEST_ACTION, adptVO.getAdapterGroupVO().getRefClass());
|
||||
prop.put(API_PATH, httpProp.getProperty(API_PATH, ""));
|
||||
prop.put(INBOUND_REWRITE_PATH,
|
||||
getRewritePath(prop.getProperty(INBOUND_EXTURI), prop.getProperty(API_PATH)));
|
||||
prop.put(INBOUND_REMOTE_ADDR, request.getRemoteAddr());
|
||||
prop.put(INBOUND_SCHEME, request.getScheme());
|
||||
prop.put(PRE_FILTERS, httpProp.getProperty(PRE_FILTERS, ""));
|
||||
prop.put(POST_FILTERS, httpProp.getProperty(POST_FILTERS, ""));
|
||||
prop.put(PROPERTIES_NAME_HTTP_REQUEST_METHOD, request.getMethod());
|
||||
prop.put(ALLOW_IP, httpProp.getProperty(ALLOW_IP, ""));
|
||||
|
||||
Map<String, String> pathVariables = assignPathVariables(request, adptGrpName, adptName, null, prop);
|
||||
if (pathVariables != null) {
|
||||
prop.put(INBOUND_PATH_VARIABLES, pathVariables);
|
||||
}
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpAdapterServiceBypass] INBOUND_METHOD= " + prop.getProperty(INBOUND_METHOD));
|
||||
logger.debug("HttpAdapterServiceBypass] INBOUND_URI= " + prop.getProperty(INBOUND_URI));
|
||||
logger.debug("HttpAdapterServiceBypass] INBOUND_EXTURI= " + prop.getProperty(INBOUND_EXTURI));
|
||||
logger.debug(
|
||||
"HttpAdapterServiceBypass] INBOUND_QUERY_STRING= " + prop.getProperty(INBOUND_QUERY_STRING));
|
||||
}
|
||||
|
||||
// // check(현재 어댑터 = 서비스정보의 기동어댑터)
|
||||
// if (StringUtils.equals(adptVO.getAdapterGroupVO().getType(), Keys.TYPE_REST)) {
|
||||
// validateServiceAndAdapter(adptGrpName, adptName, message.getBytes(encode), prop);
|
||||
// }
|
||||
|
||||
// 로컬 서비스 호출
|
||||
String result = (String) service(adptGrpName, adptName, requestData, prop, request, response);
|
||||
|
||||
stopWatch.stop();
|
||||
logger.info("종료 >> encode = [" + encode + "]");
|
||||
|
||||
if (RESPONSE_TYPE_ASYNC.equals(responseType)) {
|
||||
String responseData = "";
|
||||
if (stopWatch.getTime() > slowTranTime) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("HttpAdapterServiceBypass] lazy async response time = " + stopWatch.toString()
|
||||
+ ", message = " + requestData);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == null) {
|
||||
responseData = ElinkConfig.getAsyncDummyDataForAdapterGroup(adptGrpName);;
|
||||
} else {
|
||||
responseData = result;
|
||||
}
|
||||
|
||||
long contentLength = result.getBytes(encode).length;
|
||||
response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength));
|
||||
response.setCharacterEncoding(encode);
|
||||
response.getWriter().print(responseData);
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.txlog(adptGrpName + adptName, "SEND " + "[" + responseData + "]"
|
||||
+ ((responseData == null) ? "" : CommonLib.getDumpMessage(responseData)));
|
||||
}
|
||||
} else {
|
||||
assignResponseHeaders(request, response, prop);
|
||||
|
||||
if (result != null) {
|
||||
|
||||
long contentLength = result.getBytes(encode).length;
|
||||
response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength));
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("Response Content Lenght(Reset)=" + contentLength);
|
||||
}
|
||||
|
||||
response.setCharacterEncoding(encode);
|
||||
response.getWriter().print(result);
|
||||
}
|
||||
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.txlog(adptGrpName + adptName,
|
||||
"SEND " + "[Bypass Response..]" + CommonLib.getDumpMessage(result));
|
||||
}
|
||||
}
|
||||
} catch (HttpStatusException e) {
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.error(adptGrpName + adptName, e.toString(), e);
|
||||
}
|
||||
logger.warn("HttpAdapter] " + adptGrpName + "-" + adptName + ">>" + e.getMessage());
|
||||
response.setStatus(e.getStatus());
|
||||
try {
|
||||
String errorMsg = MessageUtil.makeErrorMessageByMessageType(adptVO.getAdapterGroupVO().getMessageType(),
|
||||
encode, e.getCode(), e.getMessage(), errorResponseFormat);
|
||||
response.getWriter().println(errorMsg);
|
||||
} catch (Exception ex) {
|
||||
// IGNORE
|
||||
}
|
||||
} catch (JwtAuthException e) {
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.error(adptGrpName + adptName, e.toString(), e);
|
||||
}
|
||||
logger.error("HttpAdapterServiceRest] " + adptGrpName + "-" + adptName + ">>" + e.getMessage(), e);
|
||||
response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
||||
try {
|
||||
String errorMsg = MessageUtil.makeErrorMessageByMessageType(adptVO.getAdapterGroupVO().getMessageType(),
|
||||
encode, e.getCode(), e.getMessage(), errorResponseFormat);
|
||||
response.getWriter().println(errorMsg);
|
||||
} catch (Exception ex) {
|
||||
// IGNORE
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.error(adptGrpName + adptName, e.toString(), e);
|
||||
}
|
||||
logger.error("HttpAdapterServiceBypass] " + adptGrpName + "-" + adptName + ">>" + e.getMessage(), e);
|
||||
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
||||
try {
|
||||
response.getWriter().println(e.getMessage());
|
||||
String errCode = ExceptionUtil.getErrorCode(e, "RECEAIAHA003");
|
||||
throw new Exception(errCode);
|
||||
} catch (Exception ex) {
|
||||
// IGNORE
|
||||
logger.warn("HttpAdapterServiceBypass] " + adptGrpName + "-" + adptName + ">>" + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// private void validateServiceAndAdapter(String adptGrpName, String adptName, byte[] requestBytes, Properties prop)
|
||||
// throws JwtAuthException {
|
||||
// try {
|
||||
// String actionName = prop.getProperty(Processor.REQUEST_ACTION);
|
||||
// RequestAction action = ActionFactory.createAction(actionName);
|
||||
// action.setAdapterInfo(adptGrpName, adptName, prop);
|
||||
// String[] keys = action.perform(requestBytes);
|
||||
// String apiId = keys[0];
|
||||
//
|
||||
// // PathVariable 지원 추가
|
||||
// apiId = StandardMessageUtil.getMatchedKey(apiId, actionName);
|
||||
// if (apiId == null) {
|
||||
// throw new Exception("apiId");
|
||||
// }
|
||||
//
|
||||
//// STDMessageManager stdheaderManager = STDMessageManager.getInstance();
|
||||
//// STDMessage stdmessage = stdheaderManager.getSTDMessage(apiId);
|
||||
//// if (stdmessage == null) {
|
||||
//// throw new Exception("stdMsg");
|
||||
//// }
|
||||
//
|
||||
// long msgRcvTm = System.currentTimeMillis();
|
||||
// AdapterManager adapterManager = AdapterManager.getInstance();
|
||||
// AdapterGroupVO adptGrpVO = adapterManager.getAdapterGroupVO(adptGrpName);
|
||||
// ProcessVO processVO = setProcessVO(apiId, adptGrpVO.getOsidInstiNo(), msgRcvTm, adptGrpName, adptName);
|
||||
// ExtMessage extMessage = ExtMessageConverter.getExtMessageForNonStandard(requestBytes, requestBytes, processVO);
|
||||
//
|
||||
// EAIMessageManager eaiMsgManager = EAIMessageManager.getInstance();
|
||||
//// String eaiSvcCd = stdmessage.getEAITranName() + stdmessage.getSTDHeader().getMesgDvcd();
|
||||
// String eaiSvcCd = extMessage.getEAIServiceCode();
|
||||
// if (eaiSvcCd == null) {
|
||||
// eaiSvcCd = extMessage.getInterfaceId() + extMessage.getSendRecv() + extMessage.getDirection();
|
||||
// }
|
||||
// EAIMessage eaiMsg = eaiMsgManager.getEAIMessage(eaiSvcCd);
|
||||
// logger.warn("adptGrpName=[" + adptGrpName + "],eaiSvcCd=[" + eaiSvcCd + "],eaiMsg=[" + eaiMsg
|
||||
// + "],getSngSysItfTp=[" + "]");
|
||||
// if (eaiMsg == null) {
|
||||
// throw new Exception("eaiMsg");
|
||||
// }
|
||||
//
|
||||
// if (!StringUtils.equals(eaiMsg.getSngSysItfTp(), adptGrpName)) {
|
||||
// throw new Exception("adapter");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// String msg = String.format("The resource does not exist or does not have permissions(%s): %s",
|
||||
// e.getMessage(), prop.get(INBOUND_EXTURI));
|
||||
// throw new JwtAuthException(JwtAuthFilter.ERROR_AUTHORIZATION_FAIL, msg);
|
||||
// }
|
||||
// }
|
||||
|
||||
// private ProcessVO setProcessVO(String apiId, String osidInstiNo, long msgRcvTm, String adptGrpName,
|
||||
// String adptName) {
|
||||
// ProcessVO vo = new ProcessVO();
|
||||
// vo.setSelectedKey(apiId);
|
||||
// vo.setAdapterGroupName(adptGrpName);
|
||||
// vo.setAdapterName(adptName);
|
||||
// vo.setInAdapterOsidInstiNo(osidInstiNo);
|
||||
// vo.setErrTm(msgRcvTm);
|
||||
// return vo;
|
||||
// }
|
||||
|
||||
private String getRewritePath(String extUri, String basePath) {
|
||||
return StringUtils.removeStart(extUri, StringUtils.removeEnd(basePath, "/"));
|
||||
}
|
||||
|
||||
private Map<String, String> assignPathVariables(HttpServletRequest request, String adptGrpName, String adptName,
|
||||
Object requestBytes, Properties prop) {
|
||||
Map<String, String> variablesMap = null;
|
||||
// QueryString 있을때는 체크(X)
|
||||
if (StringUtils.isBlank(request.getQueryString())) {
|
||||
try {
|
||||
String actionName = prop.getProperty(Processor.REQUEST_ACTION);
|
||||
RequestAction action = ActionFactory.createAction(actionName);
|
||||
action.setAdapterInfo(adptGrpName, adptName, prop);
|
||||
String[] keys = action.perform(requestBytes);
|
||||
String requestPath = keys[0];
|
||||
|
||||
// PathVariable 지원 추가
|
||||
String ruledPath = StandardMessageUtil.getMatchedKey(requestPath, actionName);
|
||||
if (!StringUtils.equals(requestPath, ruledPath) && StringUtils.contains(ruledPath, "{")) {
|
||||
variablesMap = new AntPathMatcher().extractUriTemplateVariables(ruledPath, requestPath);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return variablesMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* request header 를 Properties로 조립
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
private Properties getHeaders(HttpServletRequest request) {
|
||||
Properties prop = new Properties();
|
||||
|
||||
Enumeration<String> headerNames = request.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String key = headerNames.nextElement();
|
||||
String value = request.getHeader(key);
|
||||
prop.setProperty(key, value);
|
||||
}
|
||||
|
||||
return prop;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void assignResponseHeaders(HttpServletRequest request, HttpServletResponse response, Properties prop) {
|
||||
Map<String, Object> outboundPropertyMap = (Map<String, Object>) prop.get(OUTBOUND_PROPERTY_MAP);
|
||||
if (outboundPropertyMap == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Properties headerProp = (Properties) outboundPropertyMap.get(OUTBOUND_RESPONSE_HEADERS);
|
||||
if (headerProp == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String httpStatus = "200";
|
||||
for (Entry<Object, Object> e : headerProp.entrySet()) {
|
||||
String key = (String) e.getKey();
|
||||
String value = (String) e.getValue();
|
||||
|
||||
if (StringUtils.equalsAnyIgnoreCase(key, HOP_BY_HOP_HEADERS)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (StringUtils.equalsIgnoreCase(key, HttpHeaders.CONTENT_LENGTH)) {
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("Response Content Lenght(Header)=" + value);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (StringUtils.equalsAnyIgnoreCase(key, HttpHeaders.SET_COOKIE, HttpHeaders.SET_COOKIE2)) {
|
||||
for (HttpCookie cookie : HttpCookie.parse(value)) {
|
||||
Cookie servletCookie = createProxyCookie(request, cookie);
|
||||
response.addCookie(servletCookie);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (StringUtils.equalsIgnoreCase(key, HTTP_STATUS)) {
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
httpStatus = value;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.equalsIgnoreCase(key, HttpHeaders.LOCATION)) {
|
||||
if (!StringUtils.startsWith(httpStatus, "3")) {
|
||||
httpStatus = "302";
|
||||
}
|
||||
}
|
||||
|
||||
response.addHeader(key, value);
|
||||
}
|
||||
response.setStatus(Integer.parseInt(httpStatus));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a proxy cookie from the original cookie.
|
||||
*
|
||||
* @param servletRequest original request
|
||||
* @param cookie original cookie
|
||||
* @return proxy cookie
|
||||
*/
|
||||
protected Cookie createProxyCookie(HttpServletRequest servletRequest, HttpCookie cookie) {
|
||||
String proxyCookieName = getProxyCookieName(cookie);
|
||||
Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue());
|
||||
servletCookie.setPath(this.doPreserveCookiePath ? cookie.getPath() : // preserve original cookie path
|
||||
buildProxyCookiePath(servletRequest) // set to the path of the proxy servlet
|
||||
);
|
||||
servletCookie.setComment(cookie.getComment());
|
||||
servletCookie.setMaxAge((int) cookie.getMaxAge());
|
||||
// don't set cookie domain
|
||||
servletCookie.setSecure(servletRequest.isSecure() && cookie.getSecure());
|
||||
servletCookie.setVersion(cookie.getVersion());
|
||||
servletCookie.setHttpOnly(cookie.isHttpOnly());
|
||||
return servletCookie;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cookie name prefixed with a proxy value so it won't collide with other
|
||||
* cookies.
|
||||
*
|
||||
* @param cookie cookie to get proxy cookie name for
|
||||
* @return non-conflicting proxy cookie name
|
||||
*/
|
||||
protected String getProxyCookieName(HttpCookie cookie) {
|
||||
return doPreserveCookies ? cookie.getName() : getCookieNamePrefix(cookie.getName()) + cookie.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create path for proxy cookie.
|
||||
*
|
||||
* @param servletRequest original request
|
||||
* @return proxy cookie path
|
||||
*/
|
||||
protected String buildProxyCookiePath(HttpServletRequest servletRequest) {
|
||||
String path = servletRequest.getContextPath(); // path starts with / or is empty string
|
||||
path += servletRequest.getServletPath(); // servlet path starts with / or is empty string
|
||||
if (path.isEmpty()) {
|
||||
path = "/";
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/** The string prefixing rewritten cookies. */
|
||||
protected String getCookieNamePrefix(String name) {
|
||||
return "!Proxy!eLink";
|
||||
}
|
||||
|
||||
/**
|
||||
* 어댑터 명 이후를 가져온다.
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
private String getExtUri(HttpServletRequest request) {
|
||||
String orgUri = request.getRequestURI().replaceAll(request.getContextPath(), "");
|
||||
String uri = getExtUri(orgUri, 3);
|
||||
if (uri != null && uri.trim().length() > 0) {
|
||||
return "/" + uri;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static String getExtUri(String url, int length) {
|
||||
String[] urls = url.split("/");
|
||||
List<String> newUrls = new ArrayList<>();
|
||||
Collections.addAll(newUrls, urls);
|
||||
return StringUtils.join(newUrls.subList(length, urls.length).toArray(), "/");
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String orgUri = "/HTT/CbsInNetSys/abcd/123456";
|
||||
String result = "";
|
||||
result = getExtUri(orgUri, 3);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
+943
@@ -0,0 +1,943 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.lang.time.StopWatch;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.mina.common.ByteBuffer;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
|
||||
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.Keys;
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpMethodType;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceKey;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceSupport;
|
||||
import com.eactive.eai.common.dao.DAOException;
|
||||
import com.eactive.eai.common.dao.DAOFactory;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.message.MessageType;
|
||||
import com.eactive.eai.common.property.PropManager;
|
||||
import com.eactive.eai.common.property.PropertyLoginKeys;
|
||||
import com.eactive.eai.common.server.EAIServerManager;
|
||||
import com.eactive.eai.common.session.ChannelSessionFacade;
|
||||
import com.eactive.eai.common.session.ChannelSessionFacade.LogoutType;
|
||||
import com.eactive.eai.common.session.SessionHistoryLogger;
|
||||
import com.eactive.eai.common.session.SessionManager;
|
||||
import com.eactive.eai.common.session.SessionVO;
|
||||
import com.eactive.eai.common.sessioninfo.SessionHistoryDTO;
|
||||
import com.eactive.eai.common.sessioninfo.SessionHistoryVO;
|
||||
import com.eactive.eai.common.util.ApplicationContextProvider;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.eactive.eai.common.util.DatetimeUtil;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.common.util.UUIDGenerator;
|
||||
import com.eactive.eai.env.ElinkConfig;
|
||||
import com.eactive.eai.inbound.error.InboundErrorInfoDAO;
|
||||
import com.eactive.eai.inbound.error.InboundErrorInfoVO;
|
||||
import com.eactive.eai.message.StandardMessage;
|
||||
import com.eactive.eai.message.manager.StandardMessageManager;
|
||||
import com.eactive.eai.message.parser.StandardReader;
|
||||
import com.eactive.eai.message.service.InterfaceMapper;
|
||||
import com.ext.eai.common.stdmessage.STDMessageErrorKeys;
|
||||
import com.ext.eai.common.stdmessage.STDMessageKeys;
|
||||
|
||||
public class HttpAdapterServiceChannel extends HttpAdapterServiceSupport {
|
||||
|
||||
private static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
private static String instid = null;
|
||||
private static String serverName = null;
|
||||
static {
|
||||
EAIServerManager eaiServerManager = EAIServerManager.getInstance();
|
||||
instid = eaiServerManager.getInstId();
|
||||
serverName = eaiServerManager.getLocalServerName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. 기능 : Http Servlet service 구현 메소드
|
||||
* 2. 처리 개요 : - 로컬 서비스 호출
|
||||
* 3. 주의사항
|
||||
*
|
||||
* @param request Http Servlet Request
|
||||
* @return response Http Servlet Response
|
||||
* @exception Exception PARAMETER_MESSAGE가 null 이거나 로컬 서비스 호출 중 오류가 발생할 경우
|
||||
**/
|
||||
public void service(String adptGrpName, String adptName, HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
try {
|
||||
|
||||
ChannelServiceDataBundle bundle = new ChannelServiceDataBundle(adptGrpName, adptName, request, response);
|
||||
|
||||
try {
|
||||
|
||||
/** SERVICE START **/
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
logger.info("시작 >> encode = [" + bundle.getEncode() + "] Method [" + request.getMethod() + "]");
|
||||
|
||||
response.addHeader("Access-Control-Allow-Origin", "*");
|
||||
response.addHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
|
||||
response.addHeader("Access-Control-Allow-Headers", "Origin,Accept,X-Requested-With,Access-Control-Request-Method,Access-Control-Request-Header,Authorization,Cookie,Content-Type,Content-Length");
|
||||
response.addHeader("Access-Control-Expose-Headers", "Set-Cookie,X-Powered-By");
|
||||
response.addHeader("Access-Control-Allow-Credentials", "true");
|
||||
response.addHeader("Access-Control-Max-Age", "3600");
|
||||
|
||||
switch (HttpMethodType.getValue(request.getMethod())) {
|
||||
case GET:
|
||||
case DELETE:
|
||||
case POST:
|
||||
case PUT:
|
||||
break;
|
||||
default:
|
||||
if (logger.isInfo()) {
|
||||
logger.info("HttpAdapterServiceChannel] SET RESPONSE HEADER FOR ACAO Method[" + request.getMethod() + "] remoteAddress=" + request.getRemoteAddr());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ChannelResponseDivider responseDivider = new ChannelResponseDivider(bundle);
|
||||
|
||||
/** Phase 1 **/
|
||||
switch(responseDivider.dividePhase1()) {
|
||||
|
||||
case HttpPoll:
|
||||
httpPollResponse(request, response, bundle);
|
||||
return; // * return service
|
||||
|
||||
case DupLoginCheck:
|
||||
dupLoginCheckResponse(request, response, bundle);
|
||||
return; // * return service
|
||||
|
||||
case LoginValidation:
|
||||
loginValidationResponse(request, response, bundle);
|
||||
return; // * return service
|
||||
|
||||
case LoginUuidValidation:
|
||||
loginUuidValidationResponse(request, response, bundle);
|
||||
return; // * return service
|
||||
|
||||
default:
|
||||
break; // go phase 2
|
||||
}
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
if(logger.isDebug()){
|
||||
String[] msgArgs = { adptGrpName, new String(bundle.getRequestBytes(), bundle.getEncode()) };
|
||||
String resMsg = ExceptionUtil.make("RICEAIAHA005", msgArgs);
|
||||
logger.debug("HttpAdapterServiceChannel] " + resMsg);
|
||||
}
|
||||
|
||||
// 로컬 서비스 호출
|
||||
logger.warn("HttpAdapterServiceChannel] REAL CALL HttpSession userId = " + bundle.getUserId() + ", message = " + new String(bundle.getRequestBytes(), bundle.getEncode()));
|
||||
Object obj = service(adptGrpName, adptName, bundle.getRequestBytes(), new Properties(), request, response );
|
||||
AdapterManager manager = AdapterManager.getInstance();
|
||||
AdapterGroupVO group = manager.getAdapterGroupVO(adptGrpName);
|
||||
|
||||
byte[] result = null;
|
||||
|
||||
if (obj instanceof byte[]) {
|
||||
result = (byte[])obj;
|
||||
}else if (obj instanceof String){
|
||||
result = ((String)obj).getBytes(StringUtils.defaultIfBlank(group.getMessageEncode(), Charset.defaultCharset().name()));
|
||||
}
|
||||
|
||||
/** Phase 2 **/
|
||||
switch(responseDivider.dividePhase2()) {
|
||||
|
||||
case Async:
|
||||
asyncResponse(request, response, bundle, stopWatch, result);
|
||||
break;
|
||||
|
||||
case LoginTransaction:
|
||||
result = loginResponse(request, response, bundle, result);
|
||||
break;
|
||||
|
||||
case LogoutTransaction:
|
||||
result = logoutResponse(request, response, bundle, result);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
String responseData = new String(result, bundle.getEncode()); // UI와 통신시(UTF-8) 변환오류로 ENCODE 제거
|
||||
logger.info("종료 >> encode = [" + bundle.getEncode() + "]");
|
||||
|
||||
if (bundle.getAddData().length() > 0) {
|
||||
responseData = bundle.getAddData() + responseData;
|
||||
}
|
||||
|
||||
response.setContentType("application/json"); // FIXME ContentType application/json으로 임시 셋팅 추후 어댑터 설정으로 변경 필요
|
||||
response.setCharacterEncoding(bundle.getEncode()); // UI와 통신시(UTF-8) 변환오류로 ENCODE 제거
|
||||
response.getWriter().print(responseData);
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpAdapterServiceChannel] SEND (" + adptGrpName + ") = [" + responseData + "]");
|
||||
logger.debug("HttpAdapterServiceChannel] SEND (" + adptGrpName + ") = " + ((responseData == null) ? "" : CommonLib.getDumpMessage(responseData.getBytes(bundle.getEncode()))));
|
||||
}
|
||||
|
||||
if (bundle.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(adptGrpName + adptName, "SEND " + "[" + responseData + "]" + ((responseData == null) ? "" : CommonLib.getDumpMessage(responseData.getBytes(bundle.getEncode()))));
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
if (bundle.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.error(adptGrpName + adptName, e.toString(), e);
|
||||
}
|
||||
logger.error("HttpAdapterServiceChannel] " + adptGrpName + "-" + adptName + " guid=" + bundle.getGuid(), e);
|
||||
response.setStatus(500);
|
||||
try {
|
||||
response.getWriter().println(e.getMessage());
|
||||
String errCode = ExceptionUtil.getErrorCode(e, "RECEAIAHA003");
|
||||
throw new Exception(errCode);
|
||||
} catch (Exception ex) {
|
||||
logger.warn("error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch (ServletException | IOException e) {
|
||||
logger.error("HttpAdapterServiceChannel] " + adptGrpName + "-" + adptName, e);
|
||||
response.setStatus(500);
|
||||
try {
|
||||
response.getWriter().println(e.getMessage());
|
||||
String errCode = ExceptionUtil.getErrorCode(e, "RECEAIAHA003");
|
||||
throw new Exception(errCode);
|
||||
} catch (Exception ex) {
|
||||
logger.warn("error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void httpPollResponse(HttpServletRequest request, HttpServletResponse response, ChannelServiceDataBundle bundle) throws IOException {
|
||||
|
||||
String message = new String(bundle.getRequestBytes(), bundle.getEncode());
|
||||
|
||||
if (logger.isInfo()) {
|
||||
logger.info("HttpAdapterServiceChannel] HTTPPOLL remoteAddress=" + request.getRemoteAddr() + " response=" + message);
|
||||
}
|
||||
String userId = message.substring(9, 9 + 9);
|
||||
String uuid = message.substring(9 + 9, 58);
|
||||
|
||||
String responseMessage;
|
||||
|
||||
SessionVO sVo = SessionManager.getInstance().getHttpLogin(uuid);
|
||||
String trimingId = StringUtils.trim(userId);
|
||||
|
||||
if (sVo == null) {
|
||||
//ERROR
|
||||
logger.warn("HttpAdapterServiceChannel] HTTPPOLL loginId=(" + trimingId + ") uuid[" + uuid + "] HTTPLOGIN not found");
|
||||
responseMessage = "POLLERR@@" + userId + uuid;
|
||||
|
||||
} else {
|
||||
//RESPONSE
|
||||
PropManager pManager = PropManager.getInstance();
|
||||
String cacheType = pManager.getProperty(SessionManager.SESSION_MANAGER_GROUPNAME,
|
||||
SessionManager.SESSION_MANAGER_CACHETYPE, "EHCACHE");
|
||||
logger.info("HttpAdapterServiceChannel] HTTPPOLL loginId=(" + trimingId + ") uuid[" + uuid + "] refresh HTTPLOGIN");
|
||||
|
||||
ChannelSessionFacade.getInstance().refreshHttpLoginCache(uuid, sVo);
|
||||
|
||||
responseMessage = "POLLRES@@" + userId + uuid;
|
||||
}
|
||||
|
||||
logger.info("HttpAdapterServiceChannel] HTTPPOLL loginId=(" + trimingId + ") uuid[" + uuid + "] RESPONSE[" + responseMessage + "]");
|
||||
|
||||
response.setCharacterEncoding(bundle.getEncode());
|
||||
response.getWriter().print(responseMessage);
|
||||
|
||||
}
|
||||
|
||||
private void loginValidationResponse(HttpServletRequest request, HttpServletResponse response, ChannelServiceDataBundle bundle) throws Exception {
|
||||
|
||||
Properties prop = PropManager.getInstance().getProperties(PropertyLoginKeys.LOGIN);
|
||||
|
||||
String CODE = prop.getProperty(PropertyLoginKeys.VALIDATION_LOGIN_CODE, STDMessageErrorKeys.NOT_LOGIN_ERROR_CODE_VALUE);
|
||||
String MSG = prop.getProperty(PropertyLoginKeys.VALIDATION_LOGIN_MSG, STDMessageErrorKeys.NOT_LOGIN_ERROR_MSG_VALUE);
|
||||
|
||||
logger.error("HttpAdapterServiceChannel] NOTLOGIN OR EXPIRED SESSION userId[" + bundle.getUserId() + "] - RETURN ERROR.");
|
||||
|
||||
StandardMessage standardMessage = null;
|
||||
StandardMessageManager standardManager = StandardMessageManager.getInstance();
|
||||
StandardReader reader = null;
|
||||
InterfaceMapper mapper = standardManager.getMapper();
|
||||
AdapterGroupVO vo = AdapterManager.getInstance().getAdapterGroupVO(bundle.getAdptGrpName());
|
||||
String messageType = vo.getMessageType();
|
||||
String messageEncode = vo.getMessageEncode();
|
||||
standardMessage = standardManager.getStandardMessage();
|
||||
reader = standardManager.getReader(messageType);
|
||||
reader.parse(standardMessage, bundle.getRequestBytes());
|
||||
// 표준전문 파싱 후 어댑터의 charset을 설정한다.
|
||||
standardMessage.setBizDataCharset(messageEncode);
|
||||
standardManager.getMessageCoordinator().coordinateSetStandardMessageError(standardMessage, mapper, "RECEAIAHA020", null);
|
||||
String responseData = standardMessage.getDataString(messageType, messageEncode);
|
||||
|
||||
// ExtMessage extMessage = ExtMessageConverter.getExtMessageForStandard(bundle.getRequestBytes(), AdapterManager.getInstance().getAdapterGroupVO(bundle.getAdptGrpName()).getMessageType());
|
||||
// byte[] result = ExtMessageConverter.getDuplicateLoginResponseForStandard(extMessage, "", MSG, CODE, MSG);
|
||||
// String responseData = new String(result, bundle.getEncode());
|
||||
|
||||
// response.setCharacterEncoding(bundle.getEncode());
|
||||
response.setContentType("application/json");// FIXME ContentType application/json으로 임시 셋팅 추후 어댑터 설정으로 변경필요"
|
||||
response.setCharacterEncoding(messageEncode);
|
||||
response.getWriter().print(responseData);
|
||||
|
||||
//정의되지 않는 메시지에 로그추가
|
||||
logUnknown(bundle,CODE,MSG);
|
||||
|
||||
|
||||
}
|
||||
private void loginUuidValidationResponse(HttpServletRequest request, HttpServletResponse response, ChannelServiceDataBundle bundle) throws Exception {
|
||||
|
||||
Properties prop = PropManager.getInstance().getProperties(PropertyLoginKeys.LOGIN);
|
||||
|
||||
String CODE = prop.getProperty(PropertyLoginKeys.VALIDATION_LOGIN_CODE, STDMessageErrorKeys.UUID_MISMATCH_ERROR_CODE_VALUE);
|
||||
String MSG = prop.getProperty(PropertyLoginKeys.VALIDATION_LOGIN_MSG, STDMessageErrorKeys.UUID_MISMATCH_ERROR_MSG_VALUE);
|
||||
|
||||
logger.error("HttpAdapterServiceChannel] UUID mismatch userId[" + bundle.getUserId() + "] - RETURN ERROR.");
|
||||
StandardMessage standardMessage = null;
|
||||
StandardMessageManager standardManager = StandardMessageManager.getInstance();
|
||||
StandardReader reader = null;
|
||||
InterfaceMapper mapper = standardManager.getMapper();
|
||||
AdapterGroupVO vo = AdapterManager.getInstance().getAdapterGroupVO(bundle.getAdptGrpName());
|
||||
String messageType = vo.getMessageType();
|
||||
String messageEncode = vo.getMessageEncode();
|
||||
standardMessage = standardManager.getStandardMessage();
|
||||
reader = standardManager.getReader(messageType);
|
||||
reader.parse(standardMessage, bundle.getRequestBytes());
|
||||
// 표준전문 파싱 후 어댑터의 charset을 설정한다.
|
||||
standardMessage.setBizDataCharset(messageEncode);
|
||||
standardManager.getMessageCoordinator().coordinateSetStandardMessageError(standardMessage, mapper, "RECEAIAHA022", null);
|
||||
String responseData = standardMessage.getDataString(messageType, messageEncode);
|
||||
// ExtMessage extMessage = ExtMessageConverter.getExtMessageForStandard(bundle.getRequestBytes(), AdapterManager.getInstance().getAdapterGroupVO(bundle.getAdptGrpName()).getMessageType());
|
||||
// byte[] result = ExtMessageConverter.getDuplicateLoginResponseForStandard(extMessage, "", MSG, CODE, MSG);
|
||||
// String responseData = new String(result, bundle.getEncode());
|
||||
|
||||
response.setContentType("application/json");// FIXME ContentType application/json으로 임시 셋팅 추후 어댑터 설정으로 변경필요"
|
||||
response.setCharacterEncoding(messageEncode);
|
||||
response.getWriter().print(responseData);
|
||||
|
||||
//정의되지 않는 메시지에 로그추가
|
||||
logUnknown(bundle,CODE,MSG);
|
||||
|
||||
}
|
||||
|
||||
//login validation 오류 로그 추가 2022-11-09
|
||||
private void logUnknown(ChannelServiceDataBundle bundle,String errorCode,String errorMsg) throws Exception {
|
||||
String uuid = instid + UUIDGenerator.getUUID();
|
||||
|
||||
InboundErrorInfoVO errorInfoVO = new InboundErrorInfoVO();
|
||||
errorInfoVO.setEaiSvcSno(uuid); // EAI서비스일련번호
|
||||
errorInfoVO.setAdptBwkGrpNm(bundle.getAdptGrpName()); // 어댑터업무그룹명
|
||||
errorInfoVO.setEaiSvcCd("");
|
||||
errorInfoVO.setErrCd(errorCode); // 에러코드
|
||||
errorInfoVO.setErrTxt(errorMsg); // 에러내용
|
||||
errorInfoVO.setErrTm(DatetimeUtil.getCurrentTimeMillis()); // 에러발생시각
|
||||
errorInfoVO.setErrDstcd("IE");
|
||||
errorInfoVO.setBzwkSvcKeyName("");
|
||||
errorInfoVO.setBwkDataTxt(new String(bundle.getRequestBytes(), bundle.getEncode())); // 업무데이터내용
|
||||
errorInfoVO.setEaiSvrInstNm(serverName); // EAI서버인스턴스명
|
||||
try {
|
||||
InboundErrorInfoDAO dao = ApplicationContextProvider.getContext().getBean(InboundErrorInfoDAO.class);
|
||||
dao.addInboundErrorInfo(errorInfoVO);
|
||||
} catch(DAOException e) {
|
||||
if (logger.isError()) logger.error("RequestProcessor] Unknown Message Logging Error", e);
|
||||
}
|
||||
|
||||
if (logger.isError()) {
|
||||
logger.error("======================================================================" );
|
||||
logger.error(" HttpAdapterServiceChannel] GET LOGIN VALIDATION ERROR MESSAGE");
|
||||
logger.error(" ADAPTER GROUP NAME [" + bundle.getAdptGrpName() + "]");
|
||||
logger.error(" ADAPTER NAME [" + bundle.getAdptName() + "]");
|
||||
logger.error(" USER_ID [" + bundle.getUserId() + "]");
|
||||
logger.error("======================================================================" );
|
||||
}
|
||||
}
|
||||
|
||||
private void dupLoginCheckResponse(HttpServletRequest request, HttpServletResponse response, ChannelServiceDataBundle bundle) throws Exception {
|
||||
|
||||
Properties prop = PropManager.getInstance().getProperties(PropertyLoginKeys.LOGIN);
|
||||
|
||||
logger.error("HttpAdapterServiceChannel] LOGIN-DUPLICATED loginUserId= " + bundle.getUserId());
|
||||
|
||||
String DUPLICATE_LOGIN_CODE = prop.getProperty(PropertyLoginKeys.DUPLICATE_LOGIN_CODE, STDMessageErrorKeys.DUP_LOGIN_ERROR_CODE_VALUE);
|
||||
String DUPLICATE_LOGIN_MSG = prop.getProperty(PropertyLoginKeys.DUPLICATE_LOGIN_MSG, STDMessageErrorKeys.DUP_LOGIN_ERROR_MSG_VALUE);
|
||||
StandardMessage standardMessage = null;
|
||||
StandardMessageManager standardManager = StandardMessageManager.getInstance();
|
||||
StandardReader reader = null;
|
||||
InterfaceMapper mapper = standardManager.getMapper();
|
||||
AdapterGroupVO vo = AdapterManager.getInstance().getAdapterGroupVO(bundle.getAdptGrpName());
|
||||
String messageType = vo.getMessageType();
|
||||
String messageEncode = vo.getMessageEncode();
|
||||
standardMessage = standardManager.getStandardMessage();
|
||||
reader = standardManager.getReader(messageType);
|
||||
reader.parse(standardMessage, bundle.getRequestBytes());
|
||||
// 표준전문 파싱 후 어댑터의 charset을 설정한다.
|
||||
standardMessage.setBizDataCharset(messageEncode);
|
||||
standardManager.getMessageCoordinator().coordinateSetStandardMessageError(standardMessage, mapper,"RECEAIAHA021", null);
|
||||
String responseData = standardMessage.getDataString(messageType, messageEncode);
|
||||
|
||||
// response.setCharacterEncoding(bundle.getEncode());
|
||||
response.getWriter().print(responseData);
|
||||
|
||||
String remoteIp = request.getHeader("X-Forwarded-For") != null ? request.getHeader("X-Forwarded-For") : request.getRemoteAddr();
|
||||
|
||||
SessionHistoryLogger.getInstance().log(
|
||||
new SessionHistoryDTO(new String(bundle.getRequestBytes(), bundle.getEncode()), SessionHistoryVO.HISTORYDSTCD_DUPLOGIN, "E", null, remoteIp, true));
|
||||
|
||||
}
|
||||
|
||||
private void asyncResponse(HttpServletRequest request, HttpServletResponse response, ChannelServiceDataBundle bundle, StopWatch stopWatch, byte[] serviced) throws IOException {
|
||||
|
||||
if (stopWatch.getTime() > slowTranTime) {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("HttpAdapterServiceChannel] lazy async response time = " + stopWatch.toString() + ", message = " + new String(bundle.getRequestBytes(), bundle.getEncode()));
|
||||
}
|
||||
}
|
||||
|
||||
String responseData = serviced == null ? ElinkConfig.getAsyncDummyData("K") : new String(serviced, bundle.getEncode());
|
||||
|
||||
response.setCharacterEncoding(bundle.getEncode());
|
||||
response.getWriter().print(responseData);
|
||||
|
||||
if (bundle.getTraceLevel() >= 3) {
|
||||
HttpMemoryLogger.txlog(bundle.getAdptGrpName() + bundle.getAdptName(), "SEND " + "[" + responseData + "]" + ((responseData == null) ? "" : CommonLib.getDumpMessage(responseData.getBytes())));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private byte[] loginResponse(HttpServletRequest request, HttpServletResponse response, ChannelServiceDataBundle bundle, byte[] serviced) throws Exception {
|
||||
|
||||
String uuid = "UUID" + UUIDGenerator.getUUID();
|
||||
// set uuid to STDMessage Header MCI_INTF_SESN_ID
|
||||
/*
|
||||
* 응답전문 변경 및 로그인 거래 일 경우에만 수행하므로 개발 용이성을 위해
|
||||
* ExtMessageConverter.getExtMessageForStandard 코드를 유지
|
||||
*/
|
||||
// ExtMessage resExtMessage = ExtMessageConverter.getExtMessageForStandard(serviced, AdapterManager.getInstance().getAdapterGroupVO(bundle.getAdptGrpName()).getMessageType());
|
||||
|
||||
// 정상응답인지 확인 : RSPNS_RSLT_DVSN_CD = N
|
||||
// String respRsltDvsnCode = resExtMessage.getResponseType();
|
||||
StandardMessage standardMessage = null;
|
||||
StandardMessageManager standardManager = StandardMessageManager.getInstance();
|
||||
StandardReader reader = null;
|
||||
InterfaceMapper mapper = standardManager.getMapper();
|
||||
AdapterGroupVO vo = AdapterManager.getInstance().getAdapterGroupVO(bundle.getAdptGrpName());
|
||||
String messageType = vo.getMessageType();
|
||||
String messageEncode = vo.getMessageEncode();
|
||||
standardMessage = standardManager.getStandardMessage();
|
||||
reader = standardManager.getReader(messageType);
|
||||
reader.parse(standardMessage, serviced);
|
||||
// 표준전문 파싱 후 어댑터의 charset을 설정한다.
|
||||
standardMessage.setBizDataCharset(messageEncode);
|
||||
String respRsltDvsnCode = mapper.getResponseType(standardMessage);
|
||||
if (STDMessageKeys.RESPONSE_TYPE_CODE_N.equals(respRsltDvsnCode)) { // FIXME
|
||||
|
||||
// 2023. 12. 12. KAKAOBANK-CARD userId From Response Standard Header
|
||||
bundle.setUserId(mapper.getUserId(standardMessage));
|
||||
|
||||
mapper.setSessionId(standardMessage, uuid);
|
||||
mapper.setInstCode(standardMessage, EAIServerManager.getInstance().getLocalServerName());
|
||||
// resExtMessage.setSessionId(uuid);
|
||||
// STDMessage stdMsg = (STDMessage)resExtMessage.getStdMessage();
|
||||
// byte[] bizData = (byte[]) resExtMessage.getBizData()[0];
|
||||
// JSONObject resultJson = (JSONObject) JSONValue.parse(new String((byte[]) bizData, MessageType.UJSON_ENCODE));
|
||||
|
||||
// serviced = ExtMessageConverter.toJsonBytes(resExtMessage, bundle.getEncode());
|
||||
JSONObject resultJson = (JSONObject) JSONValue.parse(standardMessage.getBizData());
|
||||
serviced = standardMessage.getDataBytes(messageType, messageEncode);
|
||||
ChannelSessionFacade.getInstance().login(bundle.getUserId(), uuid, resultJson);
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpAdapterServiceChannel] loginId=(" + bundle.getUserId() + ") uuid[" + uuid + "]");
|
||||
}
|
||||
|
||||
} else {
|
||||
logger.warn("HttpAdapterServiceChannel] LOGIN Failed RSPNS_RSLT_DVSN_CD =" + respRsltDvsnCode);
|
||||
}
|
||||
|
||||
String remoteIp = request.getHeader("X-Forwarded-For") != null ? request.getHeader("X-Forwarded-For") : request.getRemoteAddr();
|
||||
|
||||
SessionHistoryLogger.getInstance().log(
|
||||
new SessionHistoryDTO(new String(bundle.getRequestBytes(), bundle.getEncode()), SessionHistoryVO.HISTORYDSTCD_LOGIN, respRsltDvsnCode, uuid, remoteIp, true));
|
||||
|
||||
return serviced;
|
||||
}
|
||||
|
||||
private byte[] logoutResponse(HttpServletRequest request, HttpServletResponse response, ChannelServiceDataBundle bundle, byte[] serviced) throws Exception {
|
||||
|
||||
// ExtMessage resExtMessage = ExtMessageConverter.getExtMessageForStandard(serviced, AdapterManager.getInstance().getAdapterGroupVO(bundle.getAdptGrpName()).getMessageType());
|
||||
StandardMessage standardMessage = null;
|
||||
StandardMessageManager standardManager = StandardMessageManager.getInstance();
|
||||
StandardReader reader = null;
|
||||
InterfaceMapper mapper = standardManager.getMapper();
|
||||
AdapterGroupVO vo = AdapterManager.getInstance().getAdapterGroupVO(bundle.getAdptGrpName());
|
||||
String messageType = vo.getMessageType();
|
||||
String messageEncode = vo.getMessageEncode();
|
||||
standardMessage = standardManager.getStandardMessage();
|
||||
reader = standardManager.getReader(messageType);
|
||||
reader.parse(standardMessage, serviced);
|
||||
// 표준전문 파싱 후 어댑터의 charset을 설정한다.
|
||||
standardMessage.setBizDataCharset(messageEncode);
|
||||
String respRsltDvsnCode = mapper.getResponseType(standardMessage);
|
||||
|
||||
// 정상응답인지 확인 : RSPNS_RSLT_DVSN_CD = N
|
||||
// String respRsltDvsnCode = resExtMessage.getResponseType();
|
||||
String uuid = "";
|
||||
if (STDMessageKeys.RESPONSE_TYPE_CODE_N.contentEquals(respRsltDvsnCode)) {
|
||||
SessionVO sessionVo = SessionManager.getInstance().getLogin(bundle.getUserId());
|
||||
if (sessionVo == null) {
|
||||
logger.warn("HttpAdapterServiceChannel] LOGOUT Login Session Not found userId =" + bundle.getUserId());
|
||||
} else {
|
||||
uuid = sessionVo.getUuid();
|
||||
ChannelSessionFacade.getInstance().logout(sessionVo.getLoginUserId(), sessionVo.getUuid(), false, LogoutType.Logout);
|
||||
}
|
||||
} else {
|
||||
logger.warn("HttpAdapterServiceChannel] LOGOUT Failed RSPNS_RSLT_DVSN_CD =" + respRsltDvsnCode);
|
||||
}
|
||||
|
||||
SessionHistoryLogger.getInstance().log(
|
||||
new SessionHistoryDTO(new String(bundle.getRequestBytes(), bundle.getEncode()), SessionHistoryVO.HISTORYDSTCD_LOGOUT, respRsltDvsnCode, uuid, null, true));
|
||||
|
||||
return serviced;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ChannelServiceDataBundle {
|
||||
|
||||
private Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
private String adptGrpName;
|
||||
private String adptName;
|
||||
|
||||
private int traceLevel = 0;
|
||||
|
||||
private String responseType;
|
||||
private String urlDecodeYn;
|
||||
private String encode;
|
||||
private String requestBodyYn;
|
||||
private String parameterName;
|
||||
private String removeLength;
|
||||
private String addData;
|
||||
|
||||
private byte[] requestBytes;
|
||||
private int removeLen;
|
||||
|
||||
/** lazy setting **/
|
||||
private String guid;
|
||||
private String userId;
|
||||
|
||||
private String apiPath;
|
||||
private String extUri;
|
||||
private String standardApiPath;
|
||||
|
||||
public ChannelServiceDataBundle(String adptGrpName, String adptName, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
this.adptGrpName = adptGrpName;
|
||||
this.adptName = adptName;
|
||||
|
||||
AdapterVO adptVO = AdapterManager.getInstance().getAdapterVO(adptGrpName, adptName);
|
||||
if (adptVO == null) {
|
||||
throw new ServletException("Adapter not Found adptName=" + adptName);
|
||||
}
|
||||
|
||||
Properties httpProp = AdapterPropManager.getInstance().getProperties(adptVO.getPropGroupName());
|
||||
|
||||
this.responseType = httpProp.getProperty(HttpAdapterServiceKey.RESPONSE_TYPE, "SYNC");
|
||||
this.urlDecodeYn = httpProp.getProperty(HttpAdapterServiceKey.URL_DECODE_YN, "N");
|
||||
this.encode = httpProp.getProperty(HttpAdapterServiceKey.ENCODE, "MS949");
|
||||
// this.requestBodyYn = httpProp.getProperty(HttpAdapterServiceKey.REQUEST_BODY_YN, "N");
|
||||
this.requestBodyYn ="Y";
|
||||
this.parameterName = httpProp.getProperty(HttpAdapterServiceKey.PARAMETER_NAME, HttpAdapterServiceKey.PARAMETER_MESSAGE);
|
||||
this.removeLength = httpProp.getProperty(HttpAdapterServiceKey.REMOVE_LENGTH, "0");
|
||||
this.addData = httpProp.getProperty(HttpAdapterServiceKey.ADD_DATA, "");
|
||||
String traceLevelTemp = httpProp.getProperty(HttpAdapterServiceKey.TRACE_LEVEL, "0");
|
||||
this.apiPath = httpProp.getProperty(HttpAdapterServiceKey.API_PATH, "");
|
||||
|
||||
if (StringUtils.equals(adptVO.getAdapterGroupVO().getType(), Keys.TYPE_REST)
|
||||
|| StringUtils.equals(adptVO.getAdapterGroupVO().getType(), Keys.TYPE_HTTP_CUSTOM)) {
|
||||
// /api/v1/public/getUserInfo.svc
|
||||
String extUrl = StringUtils.removeStart(request.getRequestURI(), request.getContextPath());
|
||||
this.extUri = extUrl;
|
||||
} else {
|
||||
this.extUri = getExtUri(request);
|
||||
}
|
||||
|
||||
standardApiPath = extUri.replaceAll(apiPath, "");
|
||||
if (!standardApiPath.startsWith("/")) {
|
||||
standardApiPath = standardApiPath + "/";
|
||||
}
|
||||
|
||||
try {
|
||||
traceLevel = Integer.parseInt(traceLevelTemp);
|
||||
} catch (Exception e) {
|
||||
traceLevel = 0;
|
||||
}
|
||||
this.removeLen = 0;
|
||||
try {
|
||||
removeLen = Integer.parseInt(removeLength);
|
||||
} catch (Exception e) {
|
||||
removeLen = 0;
|
||||
}
|
||||
|
||||
if ("Y".equals(requestBodyYn)) {
|
||||
this.requestBytes = requestBytesSetting(request);
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.txlog(adptGrpName + adptName, "RECV " + "[" + new String(requestBytes, encode) + "]" + CommonLib.getDumpMessage(requestBytes));
|
||||
}
|
||||
} else {
|
||||
String paramValue = request.getParameter(parameterName);
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.txlog(adptGrpName + adptName, "RECV " + "[" + paramValue + "]" + ((paramValue == null) ? "" : CommonLib.getDumpMessage(paramValue.getBytes())));
|
||||
}
|
||||
this.requestBytes = requestBytesFromParameterValue(paramValue, urlDecodeYn, encode);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpAdapterServiceChannel] RECV (" + adptGrpName + ") = [" + paramValue + "]");
|
||||
logger.debug("HttpAdapterServiceChannel] RECV (" + adptGrpName + ") = " + ((paramValue == null) ? "" : CommonLib.getDumpMessage(paramValue.getBytes())));
|
||||
}
|
||||
}
|
||||
|
||||
if (removeLen > 0) {
|
||||
byte[] cuttedBytes = new byte[requestBytes.length - removeLen];
|
||||
System.arraycopy(requestBytes, removeLen, cuttedBytes, 0, cuttedBytes.length);
|
||||
this.requestBytes = cuttedBytes;
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpAdapterServiceChannel] RECV after remove (" + adptGrpName + ") = [" + new String(requestBytes, encode) + "]");
|
||||
logger.debug("HttpAdapterServiceChannel] RECV after remove (" + adptGrpName + ") = " + CommonLib.getDumpMessage(requestBytes));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private byte[] requestBytesSetting(HttpServletRequest request) throws IOException {
|
||||
|
||||
byte[] result = null;
|
||||
|
||||
ServletInputStream sis = request.getInputStream();
|
||||
ByteBuffer bb = ByteBuffer.allocate(1024).setAutoExpand(true);
|
||||
int i = 0;
|
||||
byte[] cbuf = new byte[1024];
|
||||
while ((i = sis.read(cbuf, 0, 1024)) != -1) {
|
||||
if (i == 1024) {
|
||||
bb.put(cbuf);
|
||||
} else {
|
||||
byte[] tail = new byte[i];
|
||||
System.arraycopy(cbuf, 0, tail, 0, i);
|
||||
bb.put(tail);
|
||||
}
|
||||
}
|
||||
result = new byte[bb.position()];
|
||||
bb.position(0);
|
||||
bb.get(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private byte[] requestBytesFromParameterValue(String paramValue, String urlDecodeYn, String encode) throws ServletException, UnsupportedEncodingException {
|
||||
|
||||
if (paramValue == null)
|
||||
throw new ServletException("RECEAIAHA002");
|
||||
|
||||
if ("Y".equals(urlDecodeYn)) {
|
||||
paramValue = URLDecoder.decode(paramValue, encode);
|
||||
}
|
||||
|
||||
return paramValue.getBytes(encode);
|
||||
}
|
||||
|
||||
public String getAdptGrpName() {
|
||||
return adptGrpName;
|
||||
}
|
||||
|
||||
public String getAdptName() {
|
||||
return adptName;
|
||||
}
|
||||
|
||||
public int getTraceLevel() {
|
||||
return traceLevel;
|
||||
}
|
||||
|
||||
public String getResponseType() {
|
||||
return responseType;
|
||||
}
|
||||
|
||||
public String getUrlDecodeYn() {
|
||||
return urlDecodeYn;
|
||||
}
|
||||
|
||||
public String getEncode() {
|
||||
return encode;
|
||||
}
|
||||
|
||||
public String getRequestBodyYn() {
|
||||
return requestBodyYn;
|
||||
}
|
||||
|
||||
public String getParameterName() {
|
||||
return parameterName;
|
||||
}
|
||||
|
||||
public String getRemoveLength() {
|
||||
return removeLength;
|
||||
}
|
||||
|
||||
public String getAddData() {
|
||||
return addData;
|
||||
}
|
||||
|
||||
public byte[] getRequestBytes() {
|
||||
return requestBytes;
|
||||
}
|
||||
|
||||
public int getRemoveLen() {
|
||||
return removeLen;
|
||||
}
|
||||
|
||||
public String getGuid() {
|
||||
return guid;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setGuid(String guid) {
|
||||
this.guid = guid;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getApiPath() {
|
||||
return apiPath;
|
||||
}
|
||||
|
||||
public void setApiPath(String apiPath) {
|
||||
this.apiPath = apiPath;
|
||||
}
|
||||
|
||||
public String getExtUri() {
|
||||
return extUri;
|
||||
}
|
||||
|
||||
public void setExtUri(String extUri) {
|
||||
this.extUri = extUri;
|
||||
}
|
||||
|
||||
public String getStandardApiPath() {
|
||||
return standardApiPath;
|
||||
}
|
||||
|
||||
public void setStandardApiPath(String standardApiPath) {
|
||||
this.standardApiPath = standardApiPath;
|
||||
}
|
||||
private String getExtUri(HttpServletRequest request) {
|
||||
String orgUri = request.getRequestURI().replaceAll(request.getContextPath(), "");
|
||||
String uri = getExtUri(orgUri, 3);
|
||||
if (uri != null && uri.trim().length() > 0) {
|
||||
return "/" + uri;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static String getExtUri(String url, int length) {
|
||||
String[] urls = url.split("/");
|
||||
List<String> newUrls = new ArrayList<>();
|
||||
Collections.addAll(newUrls, urls);
|
||||
return StringUtils.join(newUrls.subList(length, urls.length).toArray(), "/");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ChannelResponseDivider {
|
||||
|
||||
private Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
private ChannelServiceDataBundle bundle;
|
||||
|
||||
private boolean loginTransaction = false;
|
||||
private boolean logoutTransaction = false;
|
||||
private boolean skipTransaction = false;
|
||||
|
||||
public ChannelResponseDivider(ChannelServiceDataBundle bundle) {
|
||||
this.bundle = bundle;
|
||||
}
|
||||
|
||||
public enum ResponseType_Phase1 {
|
||||
HttpPoll, DupLoginCheck, LoginValidation, LoginUuidValidation, Skip
|
||||
}
|
||||
|
||||
public enum ResponseType_Phase2 {
|
||||
Async, LoginTransaction, LogoutTransaction, Skip
|
||||
}
|
||||
|
||||
public ResponseType_Phase1 dividePhase1() throws Exception {
|
||||
|
||||
if(bundle.getRequestBytes().length == 58) {
|
||||
String message = new String(bundle.getRequestBytes(), bundle.getEncode());
|
||||
if (message.startsWith("POLLREQ@@")) {
|
||||
return ResponseType_Phase1.HttpPoll;
|
||||
}
|
||||
}
|
||||
|
||||
AdapterGroupVO agvo = AdapterManager.getInstance().getAdapterGroupVO(bundle.getAdptGrpName());
|
||||
String LOGINSERVICEID = "";
|
||||
String LOGOUTSERVICEID = "";
|
||||
String SKIP_SERVICEID = "";
|
||||
|
||||
String userId = "";
|
||||
String uuid = "";
|
||||
|
||||
if (MessageType.JSON.equals(agvo.getMessageType())) {
|
||||
|
||||
PropManager pManager = PropManager.getInstance();
|
||||
Properties prop = pManager.getProperties(PropertyLoginKeys.LOGIN);
|
||||
LOGINSERVICEID = prop.getProperty(PropertyLoginKeys.LOGIN_SERVICE_ID);
|
||||
LOGOUTSERVICEID = prop.getProperty(PropertyLoginKeys.LOGOUT_SERVICE_ID);
|
||||
SKIP_SERVICEID = prop.getProperty(PropertyLoginKeys.SKIP_SERVICE_ID, "");// 비밀번호 변경 등등
|
||||
|
||||
String requestStr = new String(bundle.getRequestBytes(), bundle.getEncode());
|
||||
logger.debug("HttpAdapterServiceChannel] LOGIN_SERVICE_ID = " + LOGINSERVICEID);
|
||||
logger.debug("HttpAdapterServiceChannel] LOGOUT_SERVICE_ID = " + LOGOUTSERVICEID);
|
||||
logger.debug("HttpAdapterServiceChannel] SKIP_SERVICE_ID = " + SKIP_SERVICEID);
|
||||
logger.debug("HttpAdapterServiceChannel] Request-JSON = " + requestStr);
|
||||
|
||||
/*
|
||||
* 모든 거래가 서비스ID, 사용자ID를 가져오기 위해 표준전문 파싱을 하는것은 낭비라고 판단하여 JSON에서 직접 가져오도록 변경
|
||||
*/
|
||||
// 표준전문 파싱으로 변경.
|
||||
StandardMessage standardMessage = null;
|
||||
StandardMessageManager standardManager = StandardMessageManager.getInstance();
|
||||
StandardReader reader = null;
|
||||
InterfaceMapper mapper = standardManager.getMapper();
|
||||
AdapterGroupVO vo = AdapterManager.getInstance().getAdapterGroupVO(bundle.getAdptGrpName());
|
||||
String messageType = vo.getMessageType();
|
||||
String messageEncode = vo.getMessageEncode();
|
||||
standardMessage = standardManager.getStandardMessage();
|
||||
reader = standardManager.getReader(messageType);
|
||||
reader.parse(standardMessage, bundle.getRequestBytes());
|
||||
// 표준전문 파싱 후 어댑터의 charset을 설정한다.
|
||||
standardMessage.setBizDataCharset(messageEncode);
|
||||
|
||||
String standardApiPath = bundle.getStandardApiPath();
|
||||
|
||||
String serviceId = "";
|
||||
|
||||
if (standardApiPath.length() > 1) {
|
||||
serviceId = standardApiPath;
|
||||
} else {
|
||||
serviceId = mapper.getServiceId(standardMessage);
|
||||
}
|
||||
|
||||
uuid = mapper.getSessionId(standardMessage);
|
||||
userId = mapper.getUserId(standardMessage);
|
||||
bundle.setGuid(mapper.getGuid(standardMessage));
|
||||
bundle.setUserId(userId);
|
||||
|
||||
if (LOGINSERVICEID.equals(serviceId)) {
|
||||
|
||||
loginTransaction = true;
|
||||
// 2023.12.11 KAKAOBANK-CARD 중복 로그인 체크 안함
|
||||
// SessionVO sessionVo = SessionManager.getInstance().getLogin(userId);
|
||||
// if (sessionVo != null) {
|
||||
// String LOGIN_DUP_CHECK_YN = prop.getProperty(PropertyLoginKeys.LOGIN_DUP_CHECK_YN, "Y");
|
||||
// if ("Y".equals(LOGIN_DUP_CHECK_YN)) {
|
||||
// return ResponseType_Phase1.DupLoginCheck;
|
||||
// } else {
|
||||
// logger.error("HttpAdapterServiceChannel] LOGIN-DUPLICATED loginUserId= " + userId + " skip");
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
if (LOGOUTSERVICEID.equals(serviceId)) {
|
||||
logoutTransaction = true;
|
||||
}
|
||||
|
||||
//통합로그 비로그인 거래 처리(2022-08-12) AA요구사항
|
||||
//대내 MCI 및 전사공통 선처리에서 사용자 ID가 가상 사용자( 'VRxxxxxxx') 인 경우 로그인 여부등 유효성 검증 SKIP(대내MCI/전사공통)
|
||||
if (SKIP_SERVICEID.indexOf(serviceId) >= 0 /* || userId.startsWith("VR")*/) {
|
||||
skipTransaction = true;
|
||||
}
|
||||
logger.debug("HttpAdapterServiceChannel] loginTransaction = " + loginTransaction);
|
||||
logger.debug("HttpAdapterServiceChannel] logoutTransaction = " + logoutTransaction);
|
||||
logger.debug("HttpAdapterServiceChannel] skipTransaction = " + skipTransaction);
|
||||
|
||||
}
|
||||
|
||||
if (!(loginTransaction || skipTransaction)) {
|
||||
|
||||
// FIXME : 로그인 여부를 체크할 것인가 ?
|
||||
PropManager pManager = PropManager.getInstance();
|
||||
Properties prop = pManager.getProperties(PropertyLoginKeys.LOGIN);
|
||||
String LOGIN_VALIDATION_YN = prop.getProperty(PropertyLoginKeys.LOGIN_VALIDATION_YN, "N");// 로그인 validation
|
||||
String UUID_VALIDATION_YN = prop.getProperty(PropertyLoginKeys.UUID_VALIDATION_YN, "N");// uuid validation
|
||||
|
||||
if ("Y".equals(LOGIN_VALIDATION_YN)) {
|
||||
SessionVO sessionVo = SessionManager.getInstance().getLogin(userId);
|
||||
if(sessionVo == null) {
|
||||
return ResponseType_Phase1.LoginValidation;
|
||||
}
|
||||
}else if ("Y".equals(UUID_VALIDATION_YN)) {
|
||||
//UUID validation 추가
|
||||
SessionVO sessionVo = SessionManager.getInstance().getHttpLogin(uuid);
|
||||
if (sessionVo == null || !userId.equals(sessionVo.getLoginUserId())) {
|
||||
return ResponseType_Phase1.LoginUuidValidation;
|
||||
}
|
||||
} else {
|
||||
logger.error("HttpAdapterServiceChannel] NOTLOGIN OR EXPIRED SESSION userId[" + userId + "] - continue.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ResponseType_Phase1.Skip;
|
||||
}
|
||||
|
||||
public ResponseType_Phase2 dividePhase2() {
|
||||
|
||||
if (HttpAdapterServiceKey.RESPONSE_TYPE_ASYNC.equals(bundle.getResponseType())) {
|
||||
|
||||
return ResponseType_Phase2.Async;
|
||||
|
||||
} else {
|
||||
|
||||
if (bundle.getRequestBytes() != null) {
|
||||
if (loginTransaction) {
|
||||
|
||||
return ResponseType_Phase2.LoginTransaction;
|
||||
|
||||
} else if (logoutTransaction) {
|
||||
|
||||
return ResponseType_Phase2.LogoutTransaction;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ResponseType_Phase2.Skip;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.impl;
|
||||
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceSupport;
|
||||
import com.eactive.eai.common.property.PropManager;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class HttpAdapterServicePass extends HttpAdapterServiceSupport {
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
private static final int BUFFER_SIZE = 1024;
|
||||
private static final String FROM = "From";
|
||||
private static final String FILE_TYPE = "FileType";
|
||||
private static final String FILE_NAME = "FileName";
|
||||
private static final String USER_AGENT = "User-Agent";
|
||||
private static final String USER_HEADER = "User-Header";
|
||||
private static final String CONTENT_TYPE = "Content-Type";
|
||||
|
||||
public void service(String adptGrpName, String adptName, HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
PropManager manager = PropManager.getInstance();
|
||||
|
||||
Properties prop = manager.getProperties("BYPASS_REU");
|
||||
String url = prop.getProperty("URL", "http://192.168.12.224:8068/ForexWeb/ForexServiceServlet");
|
||||
int connectionTimeout = 30000;
|
||||
int timeout = 30000;
|
||||
|
||||
if (prop != null) {
|
||||
try {
|
||||
connectionTimeout = Integer.parseInt(prop.getProperty("CONNECTION_TIMEOUT", "30000"));
|
||||
} catch (NumberFormatException e) {
|
||||
connectionTimeout = 30000;
|
||||
}
|
||||
try {
|
||||
timeout = Integer.parseInt(prop.getProperty("TIMEOUT", "30000"));
|
||||
} catch (NumberFormatException e) {
|
||||
timeout = 30000;
|
||||
}
|
||||
}
|
||||
|
||||
String from = request.getHeader(FROM);
|
||||
String fileType = request.getHeader(FILE_TYPE);
|
||||
String fileName = request.getHeader(FILE_NAME);
|
||||
String userAgent = request.getHeader(USER_AGENT);// "Forex HTTP Connector"
|
||||
String userHeader = request.getHeader(USER_HEADER);
|
||||
String contentType = request.getHeader(CONTENT_TYPE);// "text/plain"
|
||||
ByteArrayOutputStream outStream = null;
|
||||
DataInputStream inStream = null;
|
||||
byte[] data = null;
|
||||
|
||||
try {
|
||||
int c = -1;
|
||||
byte[] bytes = new byte[BUFFER_SIZE];
|
||||
inStream = new DataInputStream(request.getInputStream());
|
||||
outStream = new ByteArrayOutputStream();
|
||||
|
||||
while ((c = inStream.read(bytes, 0, bytes.length)) != -1) {
|
||||
outStream.write(bytes, 0, c);
|
||||
}
|
||||
data = new byte[outStream.toByteArray().length];
|
||||
data = outStream.toByteArray();
|
||||
} catch (IOException e1) {
|
||||
if (logger.isError())
|
||||
logger.error("HttpPassAdapter] IOException ", e1);
|
||||
} finally {
|
||||
try {
|
||||
if (inStream != null) {
|
||||
inStream.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn(e);
|
||||
}
|
||||
try {
|
||||
if (outStream != null) {
|
||||
outStream.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn(e);
|
||||
}
|
||||
}
|
||||
|
||||
HttpClient httpClient = new HttpClient();
|
||||
|
||||
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
|
||||
httpClient.getHttpConnectionManager().getParams().setSoTimeout(timeout);
|
||||
|
||||
PostMethod post = new PostMethod(url);
|
||||
|
||||
post.setRequestHeader(FROM, from);
|
||||
post.setRequestHeader(FILE_TYPE, fileType);
|
||||
post.setRequestHeader(FILE_NAME, fileName);
|
||||
post.setRequestHeader(USER_AGENT, userAgent);
|
||||
post.setRequestHeader(USER_HEADER, userHeader);
|
||||
post.setRequestHeader(CONTENT_TYPE, contentType);
|
||||
|
||||
// post.setRequestBody(new ByteArrayInputStream(data));
|
||||
// if (data != null) {
|
||||
// post.setRequestContentLength(data.length);
|
||||
// }
|
||||
ByteArrayRequestEntity entity = new ByteArrayRequestEntity(data);
|
||||
post.setRequestEntity(entity);
|
||||
|
||||
int status = -1;
|
||||
|
||||
try {
|
||||
status = httpClient.executeMethod(post);
|
||||
} catch (SocketTimeoutException ste) {
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpPassAdapter] SocketTimeoutException : " + ste.toString(), ste);
|
||||
}
|
||||
} catch (ConnectException ce) {
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpPassAdapter] Connection Exception : " + ce.toString(), ce);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpPassAdapter] Exception : " + e.toString(), e);
|
||||
}
|
||||
} finally {
|
||||
post.releaseConnection();
|
||||
if (status != HttpStatus.SC_OK) {
|
||||
logger.error("Http error status=" + status);
|
||||
}
|
||||
}
|
||||
response.setContentType("text/html");
|
||||
PrintWriter writer = null;
|
||||
try {
|
||||
writer = response.getWriter();
|
||||
writer.print(post.getResponseBodyAsString());
|
||||
} catch (IOException e) {
|
||||
if (logger.isError()) {
|
||||
logger.error("HttpPassAdapter] IOException : " + e.toString(), e);
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
if (writer != null) {
|
||||
writer.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,783 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.impl;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.*;
|
||||
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.eactive.eai.adapter.http.client.HttpClientAdapterServiceKey;
|
||||
import com.eactive.eai.common.TransactionContextKeys;
|
||||
import com.eactive.eai.common.util.HttpAdapterExtraLogUtil;
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
import org.apache.mina.common.ByteBuffer;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterManager;
|
||||
import com.eactive.eai.adapter.AdapterPropManager;
|
||||
import com.eactive.eai.adapter.AdapterVO;
|
||||
import com.eactive.eai.adapter.Keys;
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpMethodType;
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceSupport;
|
||||
import com.eactive.eai.adapter.http.dynamic.filter.JwtAuthException;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.message.MessageType;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.common.util.MessageUtil;
|
||||
import com.eactive.eai.env.ElinkConfig;
|
||||
import com.eactive.eai.inbound.action.ActionFactory;
|
||||
import com.eactive.eai.inbound.action.RequestAction;
|
||||
import com.eactive.eai.inbound.processor.Processor;
|
||||
import com.eactive.eai.message.StandardMessageUtil;
|
||||
|
||||
public class HttpAdapterServiceRest extends HttpAdapterServiceSupport {
|
||||
public static final String HEADER_GROUP = "HEADER_GROUP";
|
||||
// HEADER_GROUP JSON에 추가할 항목 정의, 없으면 전체 header 추가
|
||||
public static final String HEADER_KEYS = "HEADER_KEYS";
|
||||
public static final String HTTP_STATUS = "HTTP_STATUS";
|
||||
public static final String PROPERTIES_NAME_HTTP_REQUEST_METHOD = "httpRequestMethod";
|
||||
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
private String logPrefix = "HttpAdapterServiceRest] ";
|
||||
|
||||
private static final String JSON_CONTENT_TYPE = "application/json";
|
||||
private static final String JSON_FIELD_NAME = "json-body";
|
||||
private static final String FILE_GROUP_NAME = "image-file";
|
||||
private static final String UPLOAD_ROOT_PATH = "UPLOAD_ROOT_PATH";
|
||||
|
||||
private String readMultipartBody(HttpServletRequest request) throws Exception {
|
||||
String jsonString = null;
|
||||
// Create a factory for disk-based file items
|
||||
DiskFileItemFactory factory = new DiskFileItemFactory();
|
||||
|
||||
// Set the maximum size of the files to be uploaded
|
||||
factory.setSizeThreshold(1024 * 1024);
|
||||
|
||||
// Set the temporary directory to store uploaded files
|
||||
File tempDir = (File) request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir");
|
||||
factory.setRepository(tempDir);
|
||||
|
||||
ServletFileUpload upload = new ServletFileUpload(factory);
|
||||
Map<String, String> fileMap = new HashMap<>();
|
||||
|
||||
InputStream fin = null;
|
||||
try {
|
||||
byte[] buffer = new byte[1024];
|
||||
int read = 0;
|
||||
|
||||
List<FileItem> items = upload.parseRequest(request);
|
||||
for (FileItem item : items) {
|
||||
if (!item.isFormField()) {
|
||||
// file
|
||||
String fieldName = item.getFieldName();
|
||||
String fileName = item.getName();
|
||||
fin = item.getInputStream();
|
||||
ByteArrayOutputStream fo = new ByteArrayOutputStream();
|
||||
while ((read = fin.read(buffer)) > 0) {
|
||||
fo.write(buffer, 0, read);
|
||||
}
|
||||
int fileSize = fo.size();
|
||||
byte[] fileBytes = fo.toByteArray();
|
||||
String fileContents = new String(fileBytes);
|
||||
if (logger.isInfo()) {
|
||||
logger.info("[FILE]-------------------------------------------------->");
|
||||
logger.info("Field name = " + fieldName);
|
||||
logger.info("File name = " + fileName + " contents length = " + fileSize);
|
||||
logger.info("File Contents [" + fileContents + "]");
|
||||
logger.info("[FILE]<--------------------------------------------------");
|
||||
}
|
||||
fileMap.put(fileName, fileContents);
|
||||
fin.close();
|
||||
} else {
|
||||
// regular form field
|
||||
String fieldName = item.getFieldName();
|
||||
String fieldValue = item.getString();
|
||||
if (logger.isInfo()) {
|
||||
logger.info("[FIELD] " + fieldName + " [" + fieldValue + "]");
|
||||
}
|
||||
if (JSON_CONTENT_TYPE.equalsIgnoreCase(item.getContentType())
|
||||
|| JSON_FIELD_NAME.equals(fieldName)) {
|
||||
jsonString = fieldValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isInfo()) {
|
||||
logger.info("Json body [" + jsonString + "]");
|
||||
}
|
||||
|
||||
if (jsonString == null) {
|
||||
jsonString = "{}";
|
||||
} else {
|
||||
// parsing json & add file contents
|
||||
JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString);
|
||||
if (jsonObject == null) {
|
||||
jsonString = "{}";
|
||||
} else {
|
||||
JSONObject fileGroup = new JSONObject();
|
||||
|
||||
for (Map.Entry<String, String> entry : fileMap.entrySet()) {
|
||||
fileGroup.put("fileName", entry.getKey());
|
||||
fileGroup.put("fileContents", entry.getValue());
|
||||
}
|
||||
jsonObject.put(FILE_GROUP_NAME, fileGroup);
|
||||
jsonString = jsonObject.toJSONString();
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isInfo()) {
|
||||
logger.info("Json with file [" + jsonString + "]");
|
||||
}
|
||||
return jsonString;
|
||||
} catch (Exception e) {
|
||||
logger.error("Read multipart body error.", e);
|
||||
throw e;
|
||||
} finally {
|
||||
if (fin != null) {
|
||||
try {
|
||||
fin.close();
|
||||
} catch (Exception ex) {
|
||||
// empty
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String checkRootPath(String path) {
|
||||
if (StringUtils.isEmpty(path)) {
|
||||
logger.info("upload dir not set(UPLOAD_ROOT_PATH), use system temp " + path);
|
||||
return System.getProperty("java.io.tmpdir");
|
||||
}
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
private String uploadMultipartBody(HttpServletRequest request, String uploadRootPath) throws Exception {
|
||||
String jsonString = null;
|
||||
// Create a factory for disk-based file items
|
||||
DiskFileItemFactory factory = new DiskFileItemFactory();
|
||||
|
||||
// Set the maximum size of the files to be uploaded
|
||||
factory.setSizeThreshold(1024 * 1024);
|
||||
|
||||
// Set the temporary directory to store uploaded files
|
||||
File tempDir = (File) request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir");
|
||||
factory.setRepository(tempDir);
|
||||
|
||||
ServletFileUpload upload = new ServletFileUpload(factory);
|
||||
try {
|
||||
// if not exist, create folders
|
||||
String uploadDir = checkRootPath(uploadRootPath);
|
||||
List<FileItem> items = upload.parseRequest(request);
|
||||
for (FileItem item : items) {
|
||||
if (!item.isFormField()) {
|
||||
// file
|
||||
String fieldName = item.getFieldName();
|
||||
String fileName = item.getName();
|
||||
String uploadFilePath = uploadDir + File.separator + fileName;
|
||||
File uploadFile = new File(uploadFilePath);
|
||||
item.write(uploadFile);
|
||||
|
||||
if (logger.isInfo()) {
|
||||
logger.info("[FILE]-------------------------------------------------->");
|
||||
logger.info("Field name = " + fieldName);
|
||||
logger.info("File name = " + fileName + " path = " + uploadFile.getAbsolutePath());
|
||||
logger.info("[FILE]<--------------------------------------------------");
|
||||
}
|
||||
} else {
|
||||
// regular form field
|
||||
String fieldName = item.getFieldName();
|
||||
String fieldValue = item.getString();
|
||||
if (logger.isInfo()) {
|
||||
logger.info("[FIELD] " + fieldName + " [" + fieldValue + "]");
|
||||
}
|
||||
if (JSON_CONTENT_TYPE.equalsIgnoreCase(item.getContentType())
|
||||
|| JSON_FIELD_NAME.equals(fieldName)) {
|
||||
jsonString = fieldValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isInfo()) {
|
||||
logger.info("Json body [" + jsonString + "]");
|
||||
}
|
||||
return jsonString;
|
||||
} catch (Exception e) {
|
||||
logger.error("Read multipart body error.", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "deprecation" })
|
||||
public void service(String adptGrpName, String adptName, HttpServletRequest request, HttpServletResponse response) {
|
||||
int traceLevel = 0;
|
||||
|
||||
AdapterVO adptVO = null;
|
||||
AdapterPropManager manager = null;
|
||||
|
||||
Properties httpProp = null;
|
||||
String responseType = null;
|
||||
String urlDecodeYn = null;
|
||||
String encode = null;
|
||||
|
||||
String traceLevelTemp = null;
|
||||
String relayRequestHeaderKeys = null;
|
||||
String headerGroupName = null;
|
||||
|
||||
boolean isParameterType = false;
|
||||
String message = null;
|
||||
|
||||
StopWatch stopWatch = null;
|
||||
Properties prop = null;
|
||||
String paramValue = null;
|
||||
String adptMsgType = null;
|
||||
String errorResponseFormat = null;
|
||||
String uploadRootPath = null;
|
||||
try {
|
||||
AdapterManager adapterManager = AdapterManager.getInstance();
|
||||
adptVO = adapterManager.getAdapterVO(adptGrpName, adptName);
|
||||
if (adptVO == null) {
|
||||
throw new Exception("Adapter not found error");
|
||||
}
|
||||
|
||||
manager = AdapterPropManager.getInstance();
|
||||
|
||||
httpProp = manager.getProperties(adptVO.getPropGroupName());
|
||||
responseType = httpProp.getProperty(RESPONSE_TYPE, "SYNC");
|
||||
urlDecodeYn = httpProp.getProperty(URL_DECODE_YN, "N");
|
||||
// encode = httpProp.getProperty(ENCODE, "UTF-8");
|
||||
encode = StringUtils.defaultIfBlank(adapterManager.getAdapterGroupVO(adptGrpName).getMessageEncode(),
|
||||
"UTF-8");
|
||||
traceLevelTemp = httpProp.getProperty(TRACE_LEVEL, "0");
|
||||
relayRequestHeaderKeys = httpProp.getProperty(HEADER_KEYS);
|
||||
headerGroupName = httpProp.getProperty(HEADER_GROUP);
|
||||
errorResponseFormat = httpProp.getProperty(ERROR_RESPONSE_FORMAT);
|
||||
uploadRootPath = httpProp.getProperty(UPLOAD_ROOT_PATH);
|
||||
prop = new Properties();
|
||||
prop.put(INBOUND_METHOD, request.getMethod());
|
||||
prop.put(INBOUND_URI, request.getRequestURI());
|
||||
prop.put(INBOUND_HEADER, getHeaders(request));
|
||||
if (StringUtils.equals(adptVO.getAdapterGroupVO().getType(), Keys.TYPE_REST)
|
||||
|| StringUtils.equals(adptVO.getAdapterGroupVO().getType(), Keys.TYPE_HTTP_CUSTOM)) {
|
||||
// /api/v1/public/getUserInfo.svc
|
||||
String extUrl = StringUtils.removeStart(request.getRequestURI(), request.getContextPath());
|
||||
prop.put(INBOUND_EXTURI, extUrl);
|
||||
} else {
|
||||
prop.put(INBOUND_EXTURI, getExtUri(request));
|
||||
}
|
||||
prop.put(Processor.REQUEST_ACTION, adptVO.getAdapterGroupVO().getRefClass());
|
||||
prop.put(API_PATH, httpProp.getProperty(API_PATH, ""));
|
||||
prop.put(PRE_FILTERS, httpProp.getProperty(PRE_FILTERS, ""));
|
||||
prop.put(POST_FILTERS, httpProp.getProperty(POST_FILTERS, ""));
|
||||
prop.put(PROPERTIES_NAME_HTTP_REQUEST_METHOD, request.getMethod());
|
||||
prop.put(ALLOW_IP, httpProp.getProperty(ALLOW_IP, ""));
|
||||
|
||||
isParameterType = false;
|
||||
try {
|
||||
traceLevel = Integer.parseInt(traceLevelTemp);
|
||||
} catch (Exception e) {
|
||||
traceLevel = 0;
|
||||
}
|
||||
|
||||
stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
logger.debug("시작 >> encode = [" + encode + "]");
|
||||
|
||||
switch (HttpMethodType.getValue(request.getMethod())) {
|
||||
case GET:
|
||||
case DELETE:
|
||||
isParameterType = true;
|
||||
break;
|
||||
case POST:
|
||||
case PUT:
|
||||
if (StringUtils.contains(request.getContentType(), "application/x-www-form-urlencoded")) {
|
||||
isParameterType = true;
|
||||
} else {
|
||||
isParameterType = false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (isParameterType) {
|
||||
paramValue = request.getQueryString();
|
||||
if (paramValue == null)
|
||||
paramValue = "";
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.txlog(adptGrpName + adptName,
|
||||
"RECV " + "[" + paramValue + "]" + CommonLib.getDumpMessage(paramValue));
|
||||
}
|
||||
|
||||
// json으로 변환
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{");
|
||||
Map<String, String[]> paramMap = assignParameterMap(request, adptGrpName, adptName, null, prop);
|
||||
int i = 0;
|
||||
for (Map.Entry<String, String[]> entry : paramMap.entrySet()) {
|
||||
if (i > 0) {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append("\"").append(entry.getKey()).append("\":");
|
||||
String[] values = entry.getValue();
|
||||
if (values.length > 1) {
|
||||
// ["111", "222"]
|
||||
sb.append("[");
|
||||
for (int j = 0; j < values.length; j++) {
|
||||
if (j > 0) {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append("\"").append(JSONValue.escape(values[j])).append("\"");
|
||||
}
|
||||
sb.append("]");
|
||||
} else {
|
||||
sb.append("\"").append(JSONValue.escape(values[0])).append("\"");
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
sb.append("}");
|
||||
|
||||
paramValue = sb.toString();
|
||||
} else {
|
||||
if (ServletFileUpload.isMultipartContent(request)) {
|
||||
// TODO : 아래의 로직은 업무에 맞게 수정이 필요함.
|
||||
// 불필요할 경우 제거
|
||||
// if(StringUtils.isEmpty(uploadRootPath)) {
|
||||
// uploadRootPath = System.getProperty("java.io.tmpdir");
|
||||
// }
|
||||
|
||||
// 임시로직 : UPLOAD_ROOT_PATH 가 없는 경우에는 JSON에 추가
|
||||
if (StringUtils.isEmpty(uploadRootPath)) {
|
||||
paramValue = readMultipartBody(request);
|
||||
} else {
|
||||
paramValue = uploadMultipartBody(request, uploadRootPath);
|
||||
}
|
||||
// TEST : 테스트용 임시코드
|
||||
// response.setCharacterEncoding(encode);
|
||||
// response.getWriter().print(paramValue);
|
||||
// return;
|
||||
} else {
|
||||
ServletInputStream sis = request.getInputStream();
|
||||
ByteBuffer bb = ByteBuffer.allocate(1024).setAutoExpand(true);
|
||||
int i = 0;
|
||||
byte[] cbuf = new byte[1024];
|
||||
while ((i = sis.read(cbuf, 0, 1024)) != -1) {
|
||||
if (i == 1024) {
|
||||
bb.put(cbuf);
|
||||
} else {
|
||||
byte[] tail = new byte[i];
|
||||
System.arraycopy(cbuf, 0, tail, 0, i);
|
||||
bb.put(tail);
|
||||
}
|
||||
}
|
||||
byte[] data = new byte[bb.position()];
|
||||
bb.position(0);
|
||||
bb.get(data);
|
||||
paramValue = new String(data, encode);
|
||||
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.txlog(adptGrpName + adptName,
|
||||
"RECV " + "[" + paramValue + "]" + CommonLib.getDumpMessage(paramValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (paramValue == null) { // parameter가 없는 경우때문에 처리
|
||||
paramValue = "";
|
||||
}
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpAdapterServiceRest] RECV (" + adptGrpName + ") = [" + paramValue + "]\n"
|
||||
+ CommonLib.getDumpMessage(paramValue));
|
||||
}
|
||||
|
||||
if ("Y".equals(urlDecodeYn) && isParameterType) {
|
||||
message = URLDecoder.decode(paramValue);
|
||||
} else {
|
||||
message = paramValue;
|
||||
}
|
||||
|
||||
if (logger.isDebug()) {
|
||||
String[] msgArgs = new String[2];
|
||||
msgArgs[0] = adptGrpName;
|
||||
msgArgs[1] = message;
|
||||
String resMsg = ExceptionUtil.make("RICEAIAHA005", msgArgs);
|
||||
logger.debug(logPrefix + resMsg);
|
||||
}
|
||||
|
||||
adptMsgType = adptVO.getAdapterGroupVO().getMessageType();
|
||||
if (StringUtils.equals(adptMsgType, MessageType.JSON)) {
|
||||
response.setContentType(JSON_CONTENT_TYPE+"; charset="+encode);
|
||||
}
|
||||
|
||||
// HEADER_GROUP 셋팅
|
||||
if (MessageType.JSON.equals(adptMsgType) && StringUtils.isNotBlank(headerGroupName)
|
||||
&& StringUtils.isNotBlank(relayRequestHeaderKeys)) {
|
||||
JSONObject jsonMessage = (JSONObject) JSONValue.parse(message);
|
||||
JSONObject headerJson = new JSONObject();
|
||||
if (StringUtils.equalsIgnoreCase(relayRequestHeaderKeys, "ALL")) {
|
||||
for (Enumeration<String> e = request.getHeaderNames(); e.hasMoreElements();) {
|
||||
String key = e.nextElement();
|
||||
headerJson.put(key, request.getHeader(key));
|
||||
}
|
||||
} else {
|
||||
String[] relayKeyArr = org.springframework.util.StringUtils
|
||||
.tokenizeToStringArray(relayRequestHeaderKeys, ",");
|
||||
|
||||
for (String key : relayKeyArr) {
|
||||
String headerValue = request.getHeader(key);
|
||||
if (StringUtils.isNotBlank(headerValue)) {
|
||||
headerJson.put(key, headerValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (headerJson.size() > 0) {
|
||||
jsonMessage.put(headerGroupName, headerJson);
|
||||
message = jsonMessage.toJSONString();
|
||||
}
|
||||
}
|
||||
|
||||
if (message == null) {
|
||||
message = "";
|
||||
}
|
||||
|
||||
// 로컬 서비스 호출 ,encoding 처리 추가
|
||||
String result = (String) service(adptGrpName, adptName, message, prop, request, response);
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpAdapterServiceRest] result " + encode + " (" + adptGrpName + ") = [" + result + "]");
|
||||
}
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
String responseData = "";
|
||||
if (RESPONSE_TYPE_ASYNC.equals(responseType)) {
|
||||
if (stopWatch.getTime() > slowTranTime && (logger.isInfo())) {
|
||||
logger.info("HttpAdapterServiceRest] dummy response time = " + stopWatch.toString() + ", message = "
|
||||
+ message);
|
||||
|
||||
}
|
||||
if (result == null) {
|
||||
responseData = ElinkConfig.getAsyncDummyDataForAdapterGroup(adptGrpName);
|
||||
} else {
|
||||
responseData = result;
|
||||
}
|
||||
response.setCharacterEncoding(encode);
|
||||
response.getWriter().print(responseData);
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.txlog(adptGrpName + adptName,
|
||||
"SEND " + "[" + responseData + "]" + CommonLib.getDumpMessage(responseData));
|
||||
}
|
||||
} else {
|
||||
responseData = result;
|
||||
logger.info("종료 >> encode = [" + encode + "]");
|
||||
|
||||
JSONObject dataObject = null;
|
||||
if (MessageType.JSON.equals(adptMsgType)) {
|
||||
dataObject = (JSONObject) JSONValue.parse(responseData);
|
||||
}
|
||||
|
||||
// HEADER_GROUP 하위 필드를 response Header에 세팅한다.
|
||||
HashMap<String, String> header = new HashMap<>();
|
||||
boolean redirect = assignHttpHeaders(header, dataObject, headerGroupName);
|
||||
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpAdapterServiceRest] response header field (" + adptGrpName + ") = ["
|
||||
+ header.toString() + "]");
|
||||
}
|
||||
|
||||
if (redirect) {
|
||||
response.setStatus(302);
|
||||
logger.debug("HttpAdapterServiceRest] set response status_code: 302");
|
||||
} else {
|
||||
String httpStatus = header.get(HTTP_STATUS);
|
||||
if (httpStatus != null && !"".equals(httpStatus))
|
||||
response.setStatus(Integer.parseInt(httpStatus));
|
||||
header.remove(HTTP_STATUS);
|
||||
}
|
||||
|
||||
// response header 셋팅
|
||||
for (Map.Entry<String, String> entry : header.entrySet()) {
|
||||
response.setHeader(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
if (dataObject != null) {
|
||||
responseData = dataObject.toJSONString();
|
||||
}
|
||||
|
||||
// UI와 통신시(UTF-8) 변환오류로 ENCODE 제거
|
||||
response.setCharacterEncoding(encode);
|
||||
response.getWriter().print(responseData);
|
||||
if (logger.isDebug()) {
|
||||
logger.debug("HttpAdapterServiceRest] SEND (" + adptGrpName + ") = [" + responseData + "]");
|
||||
logger.debug("HttpAdapterServiceRest] SEND (" + adptGrpName + ") = "
|
||||
+ CommonLib.getDumpMessage(responseData));
|
||||
}
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.txlog(adptGrpName + adptName,
|
||||
"SEND " + "[" + responseData + "]" + CommonLib.getDumpMessage(responseData));
|
||||
}
|
||||
}
|
||||
} catch (HttpStatusException e) {
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.error(adptGrpName + adptName, e.toString(), e);
|
||||
}
|
||||
logger.warn("HttpAdapter] " + adptGrpName + "-" + adptName + ">>" + e.getMessage());
|
||||
response.setStatus(e.getStatus());
|
||||
try {
|
||||
String errorMsg = MessageUtil.makeErrorMessageByMessageType(adptMsgType, encode, e.getCode(),
|
||||
e.getMessage(), errorResponseFormat);
|
||||
response.getWriter().println(errorMsg);
|
||||
} catch (Exception ex) {
|
||||
// IGNORE
|
||||
}
|
||||
} catch (JwtAuthException e) {
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.error(adptGrpName + adptName, e.toString(), e);
|
||||
}
|
||||
logger.error(logPrefix + adptGrpName + "-" + adptName + ">>" + e.getMessage(), e);
|
||||
response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
||||
try {
|
||||
String errorMsg = MessageUtil.makeErrorMessageByMessageType(adptMsgType, encode, e.getCode(),
|
||||
e.getMessage(), errorResponseFormat);
|
||||
response.getWriter().println(errorMsg);
|
||||
} catch (Exception ex) {
|
||||
// IGNORE
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.error(adptGrpName + adptName, e.toString(), e);
|
||||
}
|
||||
logger.error(logPrefix + adptGrpName + "-" + adptName + ">>" + e.getMessage(), e);
|
||||
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
||||
try {
|
||||
response.getWriter().println(e.getMessage());
|
||||
String errCode = ExceptionUtil.getErrorCode(e, "RECEAIAHA003");
|
||||
throw new Exception(errCode);
|
||||
} catch (Exception ex) {
|
||||
// IGNORE
|
||||
logger.warn(logPrefix + adptGrpName + "-" + adptName + ">>" + e.getMessage(), e);
|
||||
}
|
||||
} finally {
|
||||
String uuid = prop.getProperty(TransactionContextKeys.TRANSACTION_UUID);
|
||||
String url = prop.getProperty(HttpClientAdapterServiceKey.INBOUND_EXTURI);
|
||||
String method = prop.getProperty(HttpClientAdapterServiceKey.INBOUND_METHOD);
|
||||
String adapterGroupName = prop.getProperty(HttpClientAdapterServiceKey.ADAPTER_GROUP_NAME);
|
||||
String adapterName = prop.getProperty(HttpClientAdapterServiceKey.ADAPTER_NAME);
|
||||
int httpStatusCode = response.getStatus();
|
||||
HttpAdapterExtraLogUtil.insertHttpAdapterExtraLog(uuid, 400, adapterGroupName, adapterName, new HashMap<>(), url, method, httpStatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String[]> assignParameterMap(HttpServletRequest request, String adptGrpName, String adptName,
|
||||
Object requestBytes, Properties prop) {
|
||||
// PathVariable 체크
|
||||
if (StringUtils.equalsAnyIgnoreCase(request.getMethod(), HttpMethod.GET.name(), HttpMethod.DELETE.name())
|
||||
&& StringUtils.isBlank(request.getQueryString())) {
|
||||
try {
|
||||
String actionName = prop.getProperty(Processor.REQUEST_ACTION);
|
||||
RequestAction action = ActionFactory.createAction(actionName);
|
||||
action.setAdapterInfo(adptGrpName, adptName, prop);
|
||||
String[] keys = action.perform(requestBytes);
|
||||
String requestPath = keys[0];
|
||||
|
||||
// PathVariable 지원 추가
|
||||
String ruledPath = StandardMessageUtil.getMatchedKey(requestPath, actionName);
|
||||
if (!StringUtils.equals(requestPath, ruledPath) && StringUtils.contains(ruledPath, "{")) {
|
||||
Map<String, String> paramMap = new AntPathMatcher().extractUriTemplateVariables(ruledPath,
|
||||
requestPath);
|
||||
if (paramMap != null && paramMap.size() > 0) {
|
||||
Map<String, String[]> returnMap = new HashMap<>();
|
||||
for (String key : paramMap.keySet()) {
|
||||
if (StringUtils.equalsIgnoreCase(key, "method")) {
|
||||
continue;
|
||||
}
|
||||
returnMap.put(key, new String[] { paramMap.get(key) });
|
||||
}
|
||||
|
||||
return returnMap;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return request.getParameterMap();
|
||||
}
|
||||
|
||||
private void validateServiceAndAdapter(String adptGrpName, String adptName, byte[] requestBytes, Properties prop)
|
||||
throws JwtAuthException {
|
||||
// try {
|
||||
// String actionName = prop.getProperty(Processor.REQUEST_ACTION);
|
||||
// RequestAction action = ActionFactory.createAction(actionName);
|
||||
// action.setAdapterInfo(adptGrpName, adptName, prop);
|
||||
// String[] keys = action.perform(requestBytes);
|
||||
// String apiId = keys[0];
|
||||
//
|
||||
// // PathVariable 지원 추가
|
||||
// apiId = StandardMessageUtil.getMatchedKey(apiId, actionName);
|
||||
// if (apiId == null) {
|
||||
// throw new Exception("apiId");
|
||||
// }
|
||||
//
|
||||
//// STDMessageManager stdheaderManager = STDMessageManager.getInstance();
|
||||
//// STDMessage stdmessage = stdheaderManager.getSTDMessage(apiId);
|
||||
//// if (stdmessage == null) {
|
||||
//// throw new Exception("stdMsg");
|
||||
//// }
|
||||
//
|
||||
// long msgRcvTm = System.currentTimeMillis();
|
||||
// AdapterManager adapterManager = AdapterManager.getInstance();
|
||||
// AdapterGroupVO adptGrpVO = adapterManager.getAdapterGroupVO(adptGrpName);
|
||||
// ProcessVO processVO = setProcessVO(apiId, adptGrpVO.getOsidInstiNo(), msgRcvTm, adptGrpName, adptName);
|
||||
// ExtMessage extMessage = ExtMessageConverter.getExtMessageForNonStandard(requestBytes, requestBytes, processVO);
|
||||
//
|
||||
// EAIMessageManager eaiMsgManager = EAIMessageManager.getInstance();
|
||||
//// String eaiSvcCd = stdmessage.getEAITranName() + stdmessage.getSTDHeader().getMesgDvcd();
|
||||
// String eaiSvcCd = extMessage.getEAIServiceCode();
|
||||
// if (eaiSvcCd == null) {
|
||||
// eaiSvcCd = extMessage.getInterfaceId() + extMessage.getSendRecv() + extMessage.getDirection();
|
||||
// }
|
||||
// EAIMessage eaiMsg = eaiMsgManager.getEAIMessage(eaiSvcCd);
|
||||
// logger.warn("adptGrpName=[" + adptGrpName + "],eaiSvcCd=[" + eaiSvcCd + "],eaiMsg=[" + eaiMsg
|
||||
// + "],getSngSysItfTp=[" + "]");
|
||||
// if (eaiMsg == null) {
|
||||
// throw new Exception("eaiMsg");
|
||||
// }
|
||||
//
|
||||
// if (!StringUtils.equals(eaiMsg.getSngSysItfTp(), adptGrpName)) {
|
||||
// throw new Exception("adapter");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// String msg = String.format("The resource does not exist or does not have permissions(%s): %s",
|
||||
// e.getMessage(), prop.get(INBOUND_EXTURI));
|
||||
// throw new JwtAuthException(JwtAuthFilter.ERROR_AUTHORIZATION_FAIL, msg);
|
||||
// }
|
||||
}
|
||||
|
||||
// private ProcessVO setProcessVO(String apiId, String osidInstiNo, long msgRcvTm, String adptGrpName,
|
||||
// String adptName) {
|
||||
// ProcessVO vo = new ProcessVO();
|
||||
// vo.setSelectedKey(apiId);
|
||||
// vo.setAdapterGroupName(adptGrpName);
|
||||
// vo.setAdapterName(adptName);
|
||||
// vo.setInAdapterOsidInstiNo(osidInstiNo);
|
||||
// vo.setErrTm(msgRcvTm);
|
||||
// return vo;
|
||||
// }
|
||||
|
||||
/**
|
||||
* request header 를 hashmap으로 조립
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
private Properties getHeaders(HttpServletRequest request) {
|
||||
Properties prop = new Properties();
|
||||
|
||||
Enumeration<String> headerNames = request.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String key = headerNames.nextElement();
|
||||
String value = request.getHeader(key);
|
||||
prop.setProperty(key, value);
|
||||
}
|
||||
|
||||
return prop;
|
||||
}
|
||||
|
||||
/**
|
||||
* adapter property의 HEADER_GROUP으로 정의된 (MFE_HEADER) 그룹의 하위 필드를 http Header에
|
||||
* 세팅한다.
|
||||
*
|
||||
* @param header
|
||||
* @param object
|
||||
*/
|
||||
private boolean assignHttpHeaders(HashMap<String, String> header, Object msg, String headerGroupName) {
|
||||
boolean redirect = false;
|
||||
|
||||
if (msg == null || StringUtils.isBlank(headerGroupName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (msg instanceof JSONObject) {
|
||||
JSONObject headerObject = (JSONObject) ((JSONObject) msg).get(headerGroupName);
|
||||
|
||||
if (headerObject == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// List<String> headerKeys = new ArrayList<>();
|
||||
for (Object key : headerObject.keySet()) {
|
||||
|
||||
Object obj = headerObject.get(key);
|
||||
if ((obj instanceof JSONObject) || (obj instanceof JSONArray)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
header.put((String) key, (String) obj);
|
||||
|
||||
if (StringUtils.equalsIgnoreCase((String) key, "Location")) {
|
||||
redirect = true;
|
||||
}
|
||||
}
|
||||
|
||||
((JSONObject) msg).remove(headerGroupName);
|
||||
}
|
||||
|
||||
return redirect;
|
||||
}
|
||||
|
||||
/**
|
||||
* 어댑터 명 이후의 URI값을 가져온다.
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
private String getExtUri(HttpServletRequest request) {
|
||||
String orgUri = request.getRequestURI().replaceAll(request.getContextPath(), "");
|
||||
String uri = getExtUri(orgUri, 3);
|
||||
if (uri != null && uri.trim().length() > 0) {
|
||||
return "/" + uri;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static String getExtUri(String url, int length) {
|
||||
String[] urls = url.split("/");
|
||||
List<String> newUrls = new ArrayList<>();
|
||||
Collections.addAll(newUrls, urls);
|
||||
return StringUtils.join(newUrls.subList(length, urls.length).toArray(), "/");
|
||||
}
|
||||
|
||||
// public static void main(String[] args) throws Exception {
|
||||
// String orgUri = "/HTT/CbsInNetSys/abcd/123456";
|
||||
// String result = "";
|
||||
// result = getExtUri(orgUri, 3);
|
||||
// System.out.println(result);
|
||||
// }
|
||||
}
|
||||
+306
@@ -0,0 +1,306 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.impl;
|
||||
|
||||
import com.eactive.eai.adapter.AdapterManager;
|
||||
import com.eactive.eai.adapter.AdapterPropManager;
|
||||
import com.eactive.eai.adapter.AdapterVO;
|
||||
import com.eactive.eai.adapter.Keys;
|
||||
import com.eactive.eai.adapter.http.HttpMemoryLogger;
|
||||
import com.eactive.eai.adapter.http.HttpStatusException;
|
||||
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceSupport;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.util.CommonLib;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.common.util.MessageUtil;
|
||||
import com.eactive.eai.env.ElinkConfig;
|
||||
import com.eactive.eai.inbound.processor.Processor;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
import org.apache.mina.common.ByteBuffer;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
public class HttpAdapterServiceStandard extends HttpAdapterServiceSupport
|
||||
{
|
||||
public static final String PROPERTIES_NAME_HTTP_REQUEST_METHOD = "httpRequestMethod";
|
||||
/*
|
||||
* Adapter Logger
|
||||
*/
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
|
||||
/**
|
||||
* 1. 기능 : Http Servlet service 구현 메소드
|
||||
* 2. 처리 개요 :
|
||||
* - 로컬 서비스 호출
|
||||
* 3. 주의사항
|
||||
*
|
||||
* @param request Http Servlet Request
|
||||
* @return response Http Servlet Response
|
||||
* @exception Exception PARAMETER_MESSAGE가 null 이거나 로컬 서비스 호출 중 오류가 발생할 경우
|
||||
**/
|
||||
public void service(String adptGrpName, String adptName, HttpServletRequest request, HttpServletResponse response) {
|
||||
int traceLevel = 0;
|
||||
try {
|
||||
AdapterManager adapterManager = AdapterManager.getInstance();
|
||||
AdapterVO adptVO = adapterManager.getAdapterVO(adptGrpName,adptName);
|
||||
|
||||
AdapterPropManager manager = AdapterPropManager.getInstance();
|
||||
|
||||
if(adptVO == null) {
|
||||
throw new ServletException("Adapter not Found adptName="+adptName);
|
||||
}
|
||||
|
||||
Properties httpProp = manager.getProperties(adptVO.getPropGroupName());
|
||||
String responseType = httpProp.getProperty(RESPONSE_TYPE,"SYNC");
|
||||
String urlDecodeYn = httpProp.getProperty(URL_DECODE_YN,"N");
|
||||
String encode = StringUtils.defaultIfBlank(adapterManager.getAdapterGroupVO(adptGrpName).getMessageEncode(), "MS949");
|
||||
String messageType = adapterManager.getAdapterGroupVO(adptGrpName).getMessageType();
|
||||
String requestBodyYn = httpProp.getProperty(REQUEST_BODY_YN,"N");
|
||||
String parameterName = httpProp.getProperty(PARAMETER_NAME,PARAMETER_MESSAGE);
|
||||
String removeLength = httpProp.getProperty(REMOVE_LENGTH,"0");
|
||||
String addData = httpProp.getProperty(ADD_DATA,"");
|
||||
String traceLevelTemp = httpProp.getProperty(TRACE_LEVEL,"0");
|
||||
try{
|
||||
traceLevel = Integer.parseInt(traceLevelTemp);
|
||||
}catch(Exception e){
|
||||
traceLevel = 0;
|
||||
}
|
||||
|
||||
String paramValue = null;
|
||||
|
||||
int removeLen = 0;
|
||||
try{
|
||||
removeLen = Integer.parseInt(removeLength);
|
||||
}catch(Exception e){
|
||||
removeLen = 0;
|
||||
}
|
||||
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
logger.info("시작 >> encode = [" + encode + "]");
|
||||
|
||||
byte[] requestBytes = null;
|
||||
|
||||
if ("Y".equals(requestBodyYn)){
|
||||
ServletInputStream sis = request.getInputStream();
|
||||
ByteBuffer bb = ByteBuffer.allocate(1024).setAutoExpand(true);
|
||||
int i=0;
|
||||
byte[] cbuf = new byte[1024];
|
||||
while( ( i = sis.read(cbuf, 0, 1024) ) != -1 ) {
|
||||
if (i==1024){
|
||||
bb.put(cbuf);
|
||||
}else{
|
||||
byte[] tail = new byte[i];
|
||||
System.arraycopy(cbuf, 0, tail, 0, i);
|
||||
bb.put(tail);
|
||||
}
|
||||
}
|
||||
requestBytes = new byte[bb.position()];
|
||||
bb.position(0);
|
||||
bb.get(requestBytes);
|
||||
if (traceLevel >= 3){
|
||||
HttpMemoryLogger.txlog(adptGrpName+adptName, "RECV "+"["+new String(requestBytes, encode)+"]"+ CommonLib.getDumpMessage(requestBytes));
|
||||
}
|
||||
}else{
|
||||
paramValue = request.getParameter(parameterName);
|
||||
if (traceLevel >= 3){
|
||||
HttpMemoryLogger.txlog(adptGrpName+adptName, "RECV "+"["+paramValue+"]"+
|
||||
((paramValue == null)?"":CommonLib.getDumpMessage(paramValue))
|
||||
);
|
||||
}
|
||||
|
||||
if(paramValue==null)
|
||||
throw new ServletException("RECEAIAHA002");
|
||||
|
||||
if ("Y".equals(urlDecodeYn)){
|
||||
paramValue = URLDecoder.decode(paramValue);
|
||||
}
|
||||
|
||||
if (logger.isDebug()){
|
||||
logger.debug("HttpAdapter] RECV ("+adptGrpName+") = [" + paramValue + "]");
|
||||
logger.debug("HttpAdapter] RECV ("+adptGrpName+") = " +
|
||||
((paramValue == null)?"":CommonLib.getDumpMessage(paramValue))
|
||||
);
|
||||
}
|
||||
requestBytes = paramValue.getBytes(encode);
|
||||
}
|
||||
|
||||
if (removeLen > 0 ) {
|
||||
byte[] cuttedBytes = new byte[requestBytes.length-removeLen];
|
||||
System.arraycopy(requestBytes, removeLen, cuttedBytes, 0, cuttedBytes.length);
|
||||
requestBytes = cuttedBytes;
|
||||
if (logger.isDebug()){
|
||||
logger.debug("HttpAdapter] RECV after remove ("+adptGrpName+") = [" + new String(requestBytes, encode) + "]");
|
||||
logger.debug("HttpAdapter] RECV after remove ("+adptGrpName+") = " + CommonLib.getDumpMessage(requestBytes));
|
||||
}
|
||||
}
|
||||
|
||||
if(logger.isDebug()){
|
||||
String[] msgArgs = new String[2];
|
||||
msgArgs[0] = adptGrpName;
|
||||
msgArgs[1] = new String(requestBytes, encode);
|
||||
String resMsg = ExceptionUtil.make("RICEAIAHA005", msgArgs);
|
||||
logger.debug("HttpAdapter] " + resMsg);
|
||||
}
|
||||
|
||||
Properties prop = new Properties();
|
||||
prop.put(INBOUND_METHOD, request.getMethod());
|
||||
prop.put(INBOUND_URI, request.getRequestURI());
|
||||
prop.put(INBOUND_HEADER, getHeaders(request));
|
||||
if (StringUtils.equals(adptVO.getAdapterGroupVO().getType(), Keys.TYPE_REST)
|
||||
|| StringUtils.equals(adptVO.getAdapterGroupVO().getType(), Keys.TYPE_HTTP_CUSTOM)) {
|
||||
// /api/v1/public/getUserInfo.svc
|
||||
String extUrl = StringUtils.removeStart(request.getRequestURI(), request.getContextPath());
|
||||
prop.put(INBOUND_EXTURI, extUrl);
|
||||
} else {
|
||||
prop.put(INBOUND_EXTURI, getExtUri(request));
|
||||
}
|
||||
prop.put(Processor.REQUEST_ACTION, adptVO.getAdapterGroupVO().getRefClass());
|
||||
prop.put(API_PATH, httpProp.getProperty(API_PATH, ""));
|
||||
prop.put(PRE_FILTERS, httpProp.getProperty(PRE_FILTERS, ""));
|
||||
prop.put(POST_FILTERS, httpProp.getProperty(POST_FILTERS, ""));
|
||||
prop.put(PROPERTIES_NAME_HTTP_REQUEST_METHOD, request.getMethod());
|
||||
prop.put(ALLOW_IP, httpProp.getProperty(ALLOW_IP, ""));
|
||||
|
||||
// 로컬 서비스 호출
|
||||
Object result = null;
|
||||
if (MessageUtil.isBytesMessage(messageType)) {
|
||||
result = service(adptGrpName, adptName, requestBytes, prop, request, response);
|
||||
} else {
|
||||
result = service(adptGrpName, adptName, new String(requestBytes, encode), prop, request, response);
|
||||
}
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
String responseData = "";
|
||||
if (RESPONSE_TYPE_ASYNC.equals(responseType)){
|
||||
if (stopWatch.getTime() > slowTranTime){
|
||||
if (logger.isWarn()){
|
||||
logger.warn("HttpAdapter] lazy async response time = " + stopWatch.toString() + ", message = "+new String(requestBytes, encode));
|
||||
}
|
||||
}
|
||||
|
||||
if (result == null) {
|
||||
responseData = ElinkConfig.getAsyncDummyDataForAdapterGroup(adptGrpName);
|
||||
} else if (result instanceof byte[]) {
|
||||
responseData = new String((byte[]) result, encode);
|
||||
} else if (result instanceof String) {
|
||||
responseData = (String) result;
|
||||
}
|
||||
|
||||
response.setCharacterEncoding(encode);
|
||||
response.getWriter().print(responseData);
|
||||
if (traceLevel >= 3){
|
||||
HttpMemoryLogger.txlog(adptGrpName+adptName, "SEND "+"["+responseData+"]"+
|
||||
((responseData == null)?"":CommonLib.getDumpMessage(responseData))
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if(requestBytes != null) {
|
||||
if (result instanceof byte[]) {
|
||||
responseData = new String((byte[]) result, encode);
|
||||
} else if (result instanceof String) {
|
||||
responseData = (String) result;
|
||||
}
|
||||
|
||||
logger.info("종료 >> encode = [" + encode + "]");
|
||||
|
||||
if (addData.length() > 0){
|
||||
responseData = addData + responseData;
|
||||
}
|
||||
// UI와 통신시(UTF-8) 변환오류로 ENCODE 제거
|
||||
response.setContentType("text/plain");
|
||||
response.setCharacterEncoding(encode);
|
||||
response.getWriter().print(responseData);
|
||||
if (logger.isDebug()){
|
||||
logger.debug("HttpAdapter] SEND ("+adptGrpName+") = [" + responseData + "]");
|
||||
logger.debug("HttpAdapter] SEND ("+adptGrpName+") = " +
|
||||
((responseData == null)?"":CommonLib.getDumpMessage(responseData))
|
||||
);
|
||||
}
|
||||
if (traceLevel >= 3){
|
||||
HttpMemoryLogger.txlog(adptGrpName+adptName, "SEND "+"["+responseData+"]"+
|
||||
((responseData == null)?"":CommonLib.getDumpMessage(responseData))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (HttpStatusException e) {
|
||||
if (traceLevel >= 3) {
|
||||
HttpMemoryLogger.error(adptGrpName + adptName, e.toString(), e);
|
||||
}
|
||||
logger.warn("HttpAdapter] " + adptGrpName + "-" + adptName + ">>" + e.getMessage());
|
||||
response.setStatus(e.getStatus());
|
||||
try {
|
||||
String errorMsg = String.format("(%d) %s", e.getStatus(), e.getMessage());
|
||||
response.getWriter().println(errorMsg);
|
||||
} catch (Exception ex) {
|
||||
// IGNORE
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (traceLevel >= 3){
|
||||
HttpMemoryLogger.error(adptGrpName+adptName, e.toString(),e);
|
||||
}
|
||||
logger.error("HttpAdapter] "+adptGrpName+"-"+adptName, e);
|
||||
response.setStatus(500);
|
||||
try {
|
||||
response.getWriter().println(e.getMessage());
|
||||
String errCode = ExceptionUtil.getErrorCode(e, "RECEAIAHA003");
|
||||
throw new Exception(errCode);
|
||||
} catch (Exception ex) {
|
||||
logger.warn("error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 어댑터 명 이후의 URI값을 가져온다.
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
private String getExtUri(HttpServletRequest request) {
|
||||
String orgUri = request.getRequestURI().replaceAll(request.getContextPath(), "");
|
||||
String uri = getExtUri(orgUri, 3);
|
||||
if (uri != null && uri.trim().length() > 0) {
|
||||
return "/" + uri;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static String getExtUri(String url, int length) {
|
||||
String[] urls = url.split("/");
|
||||
List<String> newUrls = new ArrayList<>();
|
||||
Collections.addAll(newUrls, urls);
|
||||
return StringUtils.join(newUrls.subList(length, urls.length).toArray(), "/");
|
||||
}
|
||||
/**
|
||||
* request header 를 hashmap으로 조립
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
private Properties getHeaders(HttpServletRequest request) {
|
||||
Properties prop = new Properties();
|
||||
|
||||
Enumeration<String> headerNames = request.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String key = headerNames.nextElement();
|
||||
String value = request.getHeader(key);
|
||||
prop.setProperty(key, value);
|
||||
}
|
||||
|
||||
return prop;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.wrapper;
|
||||
|
||||
import io.undertow.server.HandlerWrapper;
|
||||
import io.undertow.server.HttpHandler;
|
||||
import io.undertow.server.HttpServerExchange;
|
||||
import io.undertow.util.Headers;
|
||||
|
||||
public class KeepAliveFalseWrapper implements HandlerWrapper {
|
||||
@Override
|
||||
public HttpHandler wrap(HttpHandler handler) {
|
||||
return new HttpHandler() {
|
||||
@Override
|
||||
public void handleRequest(HttpServerExchange exchange) throws Exception {
|
||||
handler.handleRequest(exchange);
|
||||
exchange.getResponseHeaders().put(Headers.CONNECTION, "close");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user