로컬 테스트 완료

This commit is contained in:
Rinjae
2025-10-21 17:53:50 +09:00
parent f5e689fe51
commit 798f4e7ace
14 changed files with 421 additions and 22 deletions
@@ -0,0 +1,167 @@
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.ext.kjb.common.KjbProperty;
import com.eactive.ext.kjb.ums.gson.IUmsGsonObject;
import com.eactive.ext.kjb.ums.gson.UmsRequestPayload;
import com.eactive.ext.kjb.ums.gson.VerifyPhoneUmsTemplate;
import com.eactive.ext.kjb.utils.ValidationUtil;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.entity.EntityBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.util.Timeout;
import java.io.IOException;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
@Slf4j
public class KjbUmsService {
private final static String SEND_URL = "/ums/openapi/send";
private final static String CONTENT_TYPE = "application/json; charset=utf-8";
private final static String AUTHORIZATION_FMT = "Bearer %s";
private final KjbProperty property;
private final boolean isRealMode;
private final CloseableHttpClient httpClient;
private final RequestConfig requestConfig;
public KjbUmsService(KjbProperty property) {
if (property == null) {
log.error("[KJB-UMS] 파라미터 설정 되지 않음");
throw new IllegalArgumentException("property is null");
}
isRealMode = property.getUmsUrl() != null && StringUtils.isNotEmpty(property.getUmsUrl().trim());
if (isRealMode) {
ValidationUtil.validateOrThrow(property);
httpClient = HttpClients.createDefault();
requestConfig = RequestConfig.custom()
.setConnectTimeout(Timeout.ofSeconds(property.getEaiBatchConnectionTimeout()))
.setResponseTimeout(Timeout.ofSeconds(property.getEaiBatchResponseTimeout()))
.build();
} else {
httpClient = HttpClients.createDefault();
requestConfig = null;
}
this.property = property;
}
public boolean send(MessageRequest messageRequest) {
if (messageRequest == null) {
throw new IllegalArgumentException("messageRequest is null");
}
if (MessageCode.Channels.KAKAO_ALIMTALK.getValue() != (messageRequest.getMessageCode().getChannels() & MessageCode.Channels.KAKAO_ALIMTALK.getValue())) {
log.error("지원하지 않는 발송 채널 - {}", messageRequest.getMessageCode().toString());
throw new IllegalArgumentException("지원하지 않는 발송 채널 - " + messageRequest.getMessageCode().toString());
}
IUmsGsonObject content = null;
content = VerifyPhoneUmsTemplate.builder().authNumber(messageRequest.getMessage()).build();
String umsMessageId = this.sendKakaoAlimTalk(messageRequest.getId(),
UmzBizWorkCode.VERIFY_PHONE,
messageRequest.getPhone(),
content
);
log.info("MessageRequest sent - {}", umsMessageId);
return true;
}
public String sendKakaoAlimTalk(String guid, UmzBizWorkCode bizWorkCode, String cpno, IUmsGsonObject content) {
if (content == null) {
throw new IllegalArgumentException("content is null");
}
if (StringUtils.isEmpty(guid)) {
guid = String.format("OBP-%s", DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").format(LocalDateTime.now()));
}
UmsRequestPayload payload = UmsRequestPayload.builder()
.messageIdentityNo(guid)
.msgBizWorkCode(bizWorkCode.getCode())
.content(content)
.cellphone(cpno)
.msgSndChnlCd(UmsRequestPayload.ChannelCode.KM)
.build();
return callUms(SEND_URL, payload, (messageId, ex) -> {
if (ex != null) {
log.error("UMS Call error", ex);
throw new RuntimeException(ex);
}
log.info("UMS Call success. ({})", messageId);
}).join().getUmsMessageId();
}
private CompletableFuture<UmsCallResult> callUms(@SuppressWarnings("SameParameterValue") String url, UmsRequestPayload requestPayload,
BiConsumer<UmsCallResult, Throwable> callback) {
return CompletableFuture
.supplyAsync(() -> {
Gson gson = new Gson();
String json = gson.toJson(requestPayload);
HttpPost httpPost = new HttpPost(property.getUmsUrl() + url);
httpPost.setHeader("Content-Type", CONTENT_TYPE);
httpPost.setHeader("Authorization", String.format(AUTHORIZATION_FMT, property.getUmsApiKey()));
httpPost.setEntity(EntityBuilder.create().setText(json).setContentType(ContentType.APPLICATION_JSON).build());
httpPost.setConfig(requestConfig);
AtomicReference<UmsCallResult> result = new AtomicReference<>();
if (isRealMode) {
try {
httpClient.execute(httpPost, res -> {
String body = EntityUtils.toString(res.getEntity());
result.set(gson.fromJson(body, UmsCallResult.class));
result.get().setUmsMessageId(requestPayload.getMessageIdentityNo());
result.get().setSuccess(true);
return result;
});
} catch (IOException e) {
log.error("UMS Call error", e);
result.set(
UmsCallResult.builder()
.umsMessageId(requestPayload.getMessageIdentityNo())
.isSuccess(false)
.throwable(e)
.build()
);
}
} else {
log.warn("UMS URL 미지정으로 실제 호출을 진행하지 않음.");
result.set(UmsCallResult.builder().isSuccess(true).build());
}
try {
log.debug("UMS Call URI : {}", httpPost.getUri().toASCIIString());
log.debug("UMS Call Request payload : {}", json);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
return result.get();
})
.whenComplete(callback);
}
}