Files
eapim-admin/src/main/java/com/eactive/ext/kjb/ums/KjbEmailService.java
T
2025-11-05 12:24:55 +09:00

143 lines
5.8 KiB
Java

package com.eactive.ext.kjb.ums;
import com.eactive.apim.portal.template.entity.MessageCode;
import com.eactive.apim.portal.template.entity.MessageRequest;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.man.monitoringProperty.service.MonitoringPropertyService;
import com.eactive.ext.kjb.common.KjbProperty;
import com.eactive.ext.kjb.safedb.KjbSafedbWrapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.UnsupportedEncodingException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
@Service
@Transactional(transactionManager = "transactionManagerForEMS")
@Slf4j
public class KjbEmailService extends BaseService {
public final static int[] ALLOW_CHANNELS = { MessageCode.Channels.EMAIL.getValue() };
public static final String PROP_GORUP_ID = "Monitoring";
public static final String PROP_EAI_BATCH_URL = "kjb.ums.eai_batch.url";
public static final String PROP_EAI_BATCH_SEND_DIR = "kjb.ums.eai_batch.send_dir";
private final KjbSafedbWrapper kjbSafedbWrapper;
private final KjbProperty prop;
@Autowired
public KjbEmailService(MonitoringPropertyService monitoringPropertyService) {
this.kjbSafedbWrapper = KjbSafedbWrapper.getInstance();
KjbProperty prop = new KjbProperty();
prop.setEaiBatchUrl(monitoringPropertyService.getPrpty2ValById(PROP_GORUP_ID, PROP_EAI_BATCH_URL));
prop.setUmsEaiBatchSendDir(monitoringPropertyService.getPrpty2ValById(PROP_GORUP_ID, PROP_EAI_BATCH_SEND_DIR));
prop.setUmsEaiBatchBackupDir(monitoringPropertyService.getPrpty2ValById(PROP_GORUP_ID, "kjb.ums.eai_batch.backup_dir"));
prop.setUmsEaiBatchInterfaceId(monitoringPropertyService.getPrpty2ValById(PROP_GORUP_ID, "kjb.ums.eai_batch.interface_id"));
prop.setUmsHostUrl(monitoringPropertyService.getPrpty2ValById(PROP_GORUP_ID, "kjb.ums.host_url"));
prop.setUmsApiKey(monitoringPropertyService.getPrpty2ValById(PROP_GORUP_ID, "kjb.ums.api_key"));
this.prop = prop;
}
public static boolean isAllowChannel(MessageRequest message) {
for (int channel : ALLOW_CHANNELS) {
if ((message.getMessageCode().getChannels() & channel) == channel) {
return true;
}
}
return false;
}
public KjbEmailService(KjbSafedbWrapper kjbSafedbWrapper,
KjbProperty property) {
this.kjbSafedbWrapper = kjbSafedbWrapper;
this.prop = property;
init();
}
private void init() {
if (prop.getEaiBatchUrl().isEmpty()) {
log.warn("{} 가 설정되지 않음. 실제 EAI 배치 연동 없이 동작함", PROP_EAI_BATCH_URL);
}
log.info("EAI Batch URL: {}", prop.getEaiBatchUrl());
log.info("EAI Batch Send Dir: {}", prop.getUmsEaiBatchSendDir());
// 디렉토리 검사(쓰기 권한 포함)
Path sendDirPath = Paths.get(prop.getUmsEaiBatchSendDir());
if (!sendDirPath.toFile().exists()) {
// 생성 시도
boolean created = sendDirPath.toFile().mkdirs();
if (!created) {
log.error("EAI_BATCH_SEND_DIR 디렉토리를 생성할 수 없음: {}", prop.getUmsEaiBatchSendDir());
throw new IllegalStateException("EAI_BATCH_SEND_DIR 디렉토리를 생성할 수 없음: " + prop.getUmsEaiBatchSendDir());
}
}
if (!sendDirPath.toFile().isDirectory()) {
log.error("EAI_BATCH_SEND_DIR 가 디렉토리가 아님: {}", prop.getUmsEaiBatchSendDir());
throw new IllegalStateException("EAI_BATCH_SEND_DIR 가 디렉토리가 아님: " + prop.getUmsEaiBatchSendDir());
}
if (!sendDirPath.toFile().canWrite()) {
log.error("EAI_BATCH_SEND_DIR 에 쓰기 권한이 없음: {}", prop.getUmsEaiBatchSendDir());
throw new IllegalStateException("EAI_BATCH_SEND_DIR 에 쓰기 권한이 없음: " + prop.getUmsEaiBatchSendDir());
}
}
public void sendEmail(MessageRequest messageRequest) {
log.debug("sendEmail() called - EAI_BATCH_URL: {}, EAI_BATCH_SEND_DIR: {}", prop.getEaiBatchUrl(), prop.getUmsEaiBatchSendDir());
log.debug("MessageRequest ID: {}, Email: {}", messageRequest.getId(), messageRequest.getEmail());
if (StringUtils.isEmpty(messageRequest.getId())) {
log.warn("MessageRequest ID is empty");
throw new IllegalArgumentException("MessageRequest ID is required");
}
if (StringUtils.isEmpty(messageRequest.getEmail())) {
log.warn("MessageRequest Email is empty for ID: {}", messageRequest.getId());
throw new IllegalArgumentException("MessageRequest Email is required for ID: " + messageRequest.getId());
}
String filename = "EMAIL_" + messageRequest.getId() + ".json";
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("EMAIL", kjbSafedbWrapper.encryptNotRnnoString(messageRequest.getEmail()));
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonString = gson.toJson(jsonObject);
byte[] jsonBytes = null;
try {
jsonBytes = jsonString.getBytes("euc-kr");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
log.debug("Generated JSON({} bytes): {}", jsonBytes.length, jsonString);
log.debug("euc-kr first 10 bytes: {}...", Arrays.toString(Arrays.copyOfRange(jsonBytes, 0, 10)));
}
}