333 lines
11 KiB
Java
333 lines
11 KiB
Java
package com.eactive.ext.djb.ums;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStream;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.nio.charset.Charset;
|
|
import java.security.SecureRandom;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.hc.client5.http.config.RequestConfig;
|
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
|
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
|
import org.apache.hc.core5.util.Timeout;
|
|
|
|
import com.eactive.apim.portal.template.entity.MessageCode;
|
|
import com.eactive.apim.portal.template.entity.MessageRequest;
|
|
import com.eactive.ext.kjb.common.KjbProperty;
|
|
import com.eactive.ext.kjb.common.KjbPropertyHolder;
|
|
import com.google.gson.Gson;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
|
public class DjbMessengerService {
|
|
public final static int[] ALLOW_CHANNELS = { MessageCode.Channels.MESSENGER.getValue() };
|
|
|
|
private static final Gson gson = new Gson();
|
|
|
|
private static volatile DjbMessengerService instance;
|
|
|
|
|
|
private DjbMessengerService() {
|
|
// 싱글톤
|
|
}
|
|
|
|
public static DjbMessengerService getInstance() {
|
|
if (instance == null) {
|
|
synchronized (DjbMessengerService.class) {
|
|
if (instance == null) {
|
|
instance = new DjbMessengerService();
|
|
}
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
private KjbProperty getProperty() {
|
|
return KjbPropertyHolder.get();
|
|
}
|
|
|
|
private boolean isRealMode() {
|
|
String url = getProperty().getUmsHostUrl();
|
|
return url != null && StringUtils.isNotEmpty(url.trim());
|
|
}
|
|
|
|
private CloseableHttpClient createHttpClient() {
|
|
return HttpClients.createDefault();
|
|
}
|
|
|
|
private RequestConfig createRequestConfig() {
|
|
KjbProperty prop = getProperty();
|
|
return RequestConfig.custom()
|
|
.setConnectTimeout(Timeout.ofSeconds(prop.getUmsConnectionTimeout()))
|
|
.setResponseTimeout(Timeout.ofSeconds(prop.getUmsResponseTimeout()))
|
|
.build();
|
|
}
|
|
|
|
public static boolean isAllowChannel(MessageRequest message) {
|
|
for (int channel : ALLOW_CHANNELS) {
|
|
if ((message.getMessageCode().getChannels() & channel) == channel) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
public boolean send(MessageRequest messageRequest) throws Exception {
|
|
if (messageRequest == null) {
|
|
throw new IllegalArgumentException("messageRequest is null");
|
|
}
|
|
|
|
if (MessageCode.Channels.MESSENGER.getValue() != (messageRequest.getMessageCode().getChannels() & MessageCode.Channels.MESSENGER.getValue())) {
|
|
log.error("지원하지 않는 발송 채널 - {}", messageRequest.getMessageCode().toString());
|
|
throw new IllegalArgumentException("지원하지 않는 발송 채널 - " + messageRequest.getMessageCode().toString());
|
|
}
|
|
|
|
this.httpConnection(messageRequest);
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
private void httpConnection(MessageRequest messageRequest) throws Exception {
|
|
|
|
KjbProperty prop = getProperty();
|
|
|
|
URL url = new URL(prop.getSwingUrl());
|
|
int timeoutValue = 5 * 1000; // 타임아웃 설정을 위한 값 (단위: ms)
|
|
// Charset charset = Charset.forName("UTF-8");
|
|
Charset charset = Charset.forName("MS949");
|
|
HttpURLConnection conn = null;
|
|
BufferedReader br = null;
|
|
StringBuffer sb = null;
|
|
String bodyData = this.makeBodyData(messageRequest);
|
|
|
|
String responseData = "";
|
|
String method = bodyData != "" ? "POST" : "GET";
|
|
conn = (HttpURLConnection) url.openConnection();
|
|
conn.setRequestMethod(method); // 전송방식
|
|
// conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
|
conn.setRequestProperty("Content-Type", "application/json; charset=MS949");
|
|
conn.setConnectTimeout(timeoutValue); // 연결 타임아웃 설정(5초)
|
|
conn.setReadTimeout(timeoutValue); // 읽기 타임아웃 설정(5초)
|
|
conn.setDoOutput(true);
|
|
|
|
// POst 방식인 경우에만
|
|
if (method.equals("POST")) {
|
|
OutputStream os = conn.getOutputStream();
|
|
byte requestData[] = bodyData.getBytes(charset);
|
|
os.write(requestData);
|
|
os.close();
|
|
}
|
|
// System.out.println("getContentType():"t conn.getContentType());
|
|
// System,out.println("getResponseCode();" t conn.getResponseCode());
|
|
// System.out.println("getResponseMessage();" + conn.getResponseMessage());
|
|
if (log.isDebugEnabled()) {
|
|
log.debug("getContentType();" + conn.getContentType()); // 응답 콘텐츠 유형 구하기
|
|
log.debug("getResponsecode();" + conn.getResponseCode()); // 응답 코드 구하기
|
|
log.debug("getResponseMessage():" + conn.getResponseMessage()); // 응답 메세지 구하기
|
|
}
|
|
|
|
|
|
// http 요청 후 응답 받은 데이타를 버퍼에 쌓는다
|
|
if (conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300) {
|
|
br = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
|
|
} else {
|
|
br = new BufferedReader(new InputStreamReader(conn.getErrorStream(), charset));
|
|
}
|
|
|
|
String inputLine;
|
|
sb = new StringBuffer();
|
|
while ((inputLine = br.readLine()) != null) {
|
|
sb.append(inputLine);
|
|
}
|
|
|
|
responseData = sb.toString();
|
|
|
|
// http 요청 및 응답 완료 후 BufferedReader를 닫는다
|
|
if (br != null) {
|
|
br.close();
|
|
conn.disconnect();
|
|
}
|
|
}
|
|
|
|
|
|
private String makeBodyData(MessageRequest messageRequest) {
|
|
|
|
KjbProperty prop = getProperty();
|
|
|
|
// 리턴값
|
|
String rtnVal = "";
|
|
|
|
// 스윙챗 알림 헤더
|
|
SwingHeadVO swiHeadVO = new SwingHeadVO();
|
|
|
|
// 스윙챗 알림 공용
|
|
SwingCommonVO swiComVO = new SwingCommonVO();
|
|
|
|
// 스윙챗 알림 데이터
|
|
SwingDataVO swiDataVO = new SwingDataVO();
|
|
|
|
// 스윙챗 알림 데이터
|
|
List<SwingReceiverDataVO> swiReDataVOList = new ArrayList<SwingReceiverDataVO>();
|
|
|
|
// guID 및 날짜 시간 정보
|
|
String jsonData[] = this.makeJsonDataInfo();
|
|
|
|
// 스윙챗 알림 헤더 설정
|
|
swiHeadVO.setNxgn_stnd_idfr("JERA");
|
|
swiHeadVO.setGuid(jsonData[2]);
|
|
swiHeadVO.setGuid_prgs_no("1");
|
|
swiHeadVO.setStnd_mesg_ver("R10");
|
|
swiHeadVO.setOrtr_guid(jsonData[2]);
|
|
swiHeadVO.setSect_ecrpt_yn("N");
|
|
swiHeadVO.setFrst_trnm_ipad(prop.getSwingWasIpAddress());
|
|
swiHeadVO.setFrst_trnm_ipv4_addr(prop.getSwingWasIpAddress());
|
|
swiHeadVO.setFrst_trnm_mac(prop.getSwingMacIpAddress());
|
|
swiHeadVO.setFrst_mesg_dman_dt(jsonData[0]);
|
|
swiHeadVO.setFrst_mesg_dman_time(jsonData[1]);
|
|
swiHeadVO.setFrst_trnm_sys_dvcd("EBG");
|
|
swiHeadVO.setTrnm_sys_dvcd("FEP");
|
|
swiHeadVO.setDman_rspn_dvcd("S");
|
|
swiHeadVO.setSys_env_dvcd("TODO: 설정값 확인 할것!!!"); //TODO: EgovPropertiesUtils.getOptionalProp("SYS_ENV_DVCD"));
|
|
swiHeadVO.setTx_tycd("0");
|
|
swiHeadVO.setSynz_dvcd("S");
|
|
swiHeadVO.setTx_id("MSGSENDMSG1S");
|
|
swiHeadVO.setXtis_cd(null);
|
|
swiHeadVO.setOsdch_dvcd(null);
|
|
swiHeadVO.setChnl_tycd("FEP");
|
|
swiHeadVO.setIf_id("F2HMPSHGMSGSENDMSG1S");
|
|
swiHeadVO.setHmab_dvcd("2");
|
|
swiHeadVO.setTx_brcd("157");
|
|
swiHeadVO.setTlrno("ET0001");
|
|
swiHeadVO.setInsl_brcd("157");
|
|
swiHeadVO.setBlng_brcd("157");
|
|
swiHeadVO.setMgmt_brcd("157");
|
|
swiComVO.setClientld(prop.getSwingClientId());
|
|
swiComVO.setClientSecret(prop.getSwingClientSecret());
|
|
swiComVO.setCompanyCode("CB");
|
|
|
|
// 랜던값(26)
|
|
String random26 = this.genrateRandomStringNumber26();
|
|
swiComVO.setRequestUniqueKey(random26);
|
|
swiDataVO.setNotiCode("CNO_FEP_HMP_0002");
|
|
swiDataVO.setNotiTitle("알림");
|
|
swiDataVO.setNotiContent(messageRequest.getMessage());
|
|
|
|
SwingReceiverDataVO receiver = new SwingReceiverDataVO();
|
|
receiver.setCode(messageRequest.getMessengerId());
|
|
swiReDataVOList.add(receiver);
|
|
|
|
swiDataVO.setReceiverInfo(swiReDataVOList);
|
|
|
|
String header = gson.toJson(swiHeadVO);
|
|
String common = gson.toJson(swiComVO);
|
|
String data = gson.toJson(swiDataVO);
|
|
|
|
// 헤더 + 데이터
|
|
rtnVal = "{\"HEAD\":" + header + " ,\r\n" + "\"DATA\":\r\n" + " [" + "{\r\n"
|
|
+ " \"data_dvcd\":\"IO\", \r\n" + " \"BIZ_DATA\":\r\n" + " {\r\n"
|
|
+ " \"common\":\r\n" + common + ",\r\n" + " \"data\":" + data
|
|
+ " }\r\n" + " }\r\n" + " ]\r\n" + "}";
|
|
|
|
log.debug(rtnVal);
|
|
|
|
return rtnVal;
|
|
}
|
|
|
|
private String[] makeJsonDataInfo() {
|
|
// 리턴값
|
|
String rtnVal[] = new String[3];
|
|
|
|
// 현재시간
|
|
Date currentDate = new Date();
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
|
String formattedDate = sdf.format(currentDate);
|
|
|
|
rtnVal[0] = formattedDate.substring(0,8);
|
|
rtnVal[1] = formattedDate.substring(8);
|
|
|
|
// 랜던값
|
|
String random20 = this.genrateRandomStringNumber20();
|
|
|
|
rtnVal[2] = formattedDate + "HMP" + random20;
|
|
|
|
return rtnVal;
|
|
}
|
|
|
|
|
|
private String genrateRandomStringNumber20() {
|
|
String rtnVal= "";
|
|
|
|
StringBuilder buffer= new StringBuilder(10);
|
|
|
|
// 랜덤값
|
|
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
// 랜덤객체 생성
|
|
SecureRandom random = new SecureRandom();
|
|
|
|
// 반복문
|
|
for (int i= 0;i< 10;i++) {
|
|
// 랜덤수
|
|
int randomIndex = random.nextInt(characters.length());
|
|
|
|
// 생성된 랜덤값
|
|
char randomChar = characters.charAt(randomIndex);
|
|
|
|
// 랜덤값 저장
|
|
buffer.append(randomChar);
|
|
}
|
|
|
|
// 랜덤객체 재생성
|
|
random = new SecureRandom();
|
|
|
|
// 난수 10개 생성
|
|
int random10 = (int) (Math.pow(10, 9) + random.nextDouble() * Math.pow(10, 9));
|
|
|
|
// 문자 숫자 조합(10) + 숫자(10)
|
|
rtnVal = buffer.toString() + Integer.toString(random10);
|
|
|
|
//리턴
|
|
return rtnVal;
|
|
}
|
|
|
|
private String genrateRandomStringNumber26() {
|
|
String rtnVal = "";
|
|
|
|
// 리턴값
|
|
StringBuilder buffer = new StringBuilder(26);
|
|
|
|
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
// 랜덤객체 생성
|
|
Random random = new Random();
|
|
|
|
//반복문
|
|
for (int i= 0; i < 26; i++) {
|
|
// 랜덤수
|
|
int randomIndex = random.nextInt(characters.length());
|
|
// 생성된 랜덤값
|
|
char randomChar = characters.charAt(randomIndex);
|
|
// 랜덤값 저장
|
|
buffer.append(randomChar);
|
|
}
|
|
|
|
// 문자 숫자 조합(26)
|
|
rtnVal= buffer.toString();
|
|
|
|
// 리턴
|
|
return rtnVal;
|
|
}
|
|
}
|