package com.eactive.ext.kjb.eai; import com.eactive.ext.kjb.common.KjbProperty; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.hc.client5.http.classic.methods.HttpGet; 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.http.io.entity.EntityUtils; import org.apache.hc.core5.net.URIBuilder; import org.apache.hc.core5.util.Timeout; import javax.validation.constraints.NotNull; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @Slf4j public class KjbEaiBatchModule { private final KjbProperty property; private final CloseableHttpClient httpClient; private final RequestConfig requestConfig; public KjbEaiBatchModule(@NotNull KjbProperty property) { if (property == null) { throw new IllegalArgumentException("KjbProperty cannot be null"); } this.property = property; requestConfig = RequestConfig.custom() .setConnectTimeout(Timeout.ofSeconds(property.getEaiBatchConnectionTimeout())) .setResponseTimeout(Timeout.ofSeconds(property.getEaiBatchResponseTimeout())) .build(); if (StringUtils.isNotEmpty(property.getEaiBatchUrl())) { this.httpClient = HttpClients.createDefault(); } else { this.httpClient = null; log.warn("EAI_BATCH_URL 가 설정되지 않음. 실제 EAI 배치 연동 없이 동작함"); } } public EaiBatchMessage call(String interfaceId, String filename) throws IOException { EaiBatchMessage reqMessage = EaiBatchMessage.createRequest(interfaceId, filename); log.debug("EAI 배치 호출: {}", reqMessage); if (this.httpClient != null) { URI uri = null; try { uri = new URIBuilder(property.getEaiBatchUrl()) .addParameter("eaibatMsg", reqMessage.toString()) .build(); } catch (URISyntaxException e) { throw new RuntimeException(e); } log.debug("EAI Batch call - {}", uri.toASCIIString()); final HttpGet httpGet = new HttpGet(uri); httpGet.setConfig(requestConfig); String message = httpClient.execute(httpGet, response -> EntityUtils.toString(response.getEntity(), "EUC-KR")); EaiBatchMessage resMessage = EaiBatchMessage.fromResponse(message); log.debug("EAI 배치 응답: {}", resMessage); return resMessage; } else { log.info("EAI_BATCH_URL 이 설정되지 않아 실제 호출하지 않음. 요청 메시지: {}", reqMessage); // 정상응답 리턴 EaiBatchMessage resMessage = EaiBatchMessage.fromResponse(String.format("%-20s%-8s%-10s%-1s%s@@", interfaceId, "", "0000000000", "1", "S:" + filename)); log.debug("EAI 배치 응답(모의): {}", resMessage); return resMessage; } } }