package com.icomsys; import com.icomsys.dsl.MessageSendServiceDsl; import com.icomsys.util.CommonBeanUtils; import com.icomsys.vo.*; import lombok.extern.slf4j.Slf4j; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; import java.time.LocalDate; import java.util.Map; import java.util.Objects; import java.util.UUID; @Service public class MessagingConnector { public RestTemplate restTemplate; public MessageSendServiceDsl messageSendServiceDsl; private String sms; private String lms; private String mms; private String kakao; private String apiKey; public MessagingConnector(String sms, String lms,String mms,String kakao,String apiKey){ this.sms = sms; this.lms = lms; this.mms = mms; this.kakao = kakao; this.apiKey = apiKey; } /** * @Name messageDiv * @Description 세종텔레콤 메신저 구분 API * @Author EunGu. Lee * @CreateDate 2024. 07. 22 * * @param messageSendRequest * @return messageSendResponse * * @ChangeDescription */ public MessageSendResponse messageDiv(MessageSendRequest messageSendRequest) throws UnsupportedEncodingException { String url = sms; String key = UUID.randomUUID().toString().substring(0, 6); LocalDate now = LocalDate.now(); messageSendRequest.setUserKey(now.toString().substring(2).replace("-", "")+key); // sendType(발송 타입)이 없는 경우 contents(메시지 내용)의 Byte를 체크하여 SMS, LMS로 전송 if (messageSendRequest.getSendType() == null || messageSendRequest.getSendType() == ""){ if (messageSendRequest.getContents().getBytes("euc-kr").length < 91) { messageSendRequest.setSendType("SMS"); return this.messageSend(messageSendRequest, url); } else { messageSendRequest.setSendType("LMS"); url = lms; return this.messageSend(messageSendRequest, url); } } // 발송 타입이 있는 경우 // 발송 타입이 SMS 일 때 else if (messageSendRequest.getSendType().equals("SMS")) { return this.messageSend(messageSendRequest, url); } // 발송 타입이 LMS 일 때 else if (messageSendRequest.getSendType().equals("LMS")) { url = lms; return this.messageSend(messageSendRequest, url); } // 발송 타입이 MMS 일 때 else if (messageSendRequest.getSendType().equals("MMS")) { url = mms; return this.messageSend(messageSendRequest, url); } // 발송 타입이 KAKAO(알림톡) 일 때 else if (messageSendRequest.getSendType().equals("KAKAO")) { url = kakao; return this.messageSend(messageSendRequest, url); } return null; } /** * @Name messageSend * @Description 세종텔레콤 메신저 전송 API * @Author EunGu. Lee * @CreateDate 2024. 07. 22 * * @param messageSendRequest * @return messageSendResponse * * @ChangeDescription */ public MessageSendResponse messageSend(MessageSendRequest messageSendRequest, String url) throws UnsupportedEncodingException { MessageSendResponse messageSendResponse = new MessageSendResponse(); SejongMessageSendResponse sejongMessageSendResponse = null; // Header 정보 입력 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); headers.add("sejongApiKey", apiKey); // Body MULTIPART_FORM_DATA로 변환 MultiValueMap body = new LinkedMultiValueMap<>(); ObjectMapper objectMapper = new ObjectMapper(); Map map = null; // SMS if (messageSendRequest.getSendType().equals("SMS")) { SejongSmsSendRequest smsSendRequest = CommonBeanUtils.convertType(messageSendRequest, SejongSmsSendRequest.class); map = objectMapper.convertValue(smsSendRequest, new TypeReference>() {}); } // LMS else if (messageSendRequest.getSendType().equals("LMS")) { SejongLmsSendRequest lmsSendRequest = CommonBeanUtils.convertType(messageSendRequest, SejongLmsSendRequest.class); map = objectMapper.convertValue(lmsSendRequest, new TypeReference>() {}); } // MMS else if (messageSendRequest.getSendType().equals("MMS")) { SejongMmsSendRequest mmsSendRequest = CommonBeanUtils.convertType(messageSendRequest, SejongMmsSendRequest.class); map = objectMapper.convertValue(mmsSendRequest, new TypeReference>() {}); } // KAKAO(알림톡) else if (messageSendRequest.getSendType().equals("KAKAO")) { SejongKakaoSendRequest kakaoSendRequest = CommonBeanUtils.convertType(messageSendRequest, SejongKakaoSendRequest.class); map = objectMapper.convertValue(kakaoSendRequest, new TypeReference>() {}); } body.setAll(map); System.out.println("Request URL: " + url); System.out.println("Request Headers: " + headers); System.out.println("Request Body: " + body); // 연동 API (메세지 전송) try{ HttpEntity request = new HttpEntity<>(body, headers); ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, request, new ParameterizedTypeReference() { @Override public Type getType() { return super.getType(); } }); System.out.println("response.getBody() : " + response.getBody()); if (response != null) { System.out.println("Status Code: " + response.getStatusCode()); if (response.hasBody()) { System.out.println("Response Body: " + response.getBody()); } else { System.out.println("Response Body is null"); } } else { System.out.println("Response is null"); } sejongMessageSendResponse = response.getBody(); } catch (Exception e) { e.printStackTrace(); messageSendResponse.setCode("500"); messageSendResponse.setMessage("서버 통신 오류"); } // Response 변환 messageSendResponse = this.responseChange(sejongMessageSendResponse, messageSendRequest); // 메시지 전송 Log Insert messageSendServiceDsl.insertSendLog(sejongMessageSendResponse, messageSendRequest, messageSendResponse); // log.info(String.valueOf(seq)); // 알림톡 발송 실패 시 SMS, LMS로 전송 if (messageSendRequest.getSendType().equals("KAKAO") && messageSendRequest.getOptionYn().equals("Y")) { if (!Objects.equals(sejongMessageSendResponse.getCode(), "200")) { messageSendRequest.setSendType(""); messageSendResponse = this.messageDiv(messageSendRequest); } } return messageSendResponse; } /** * @Name responseChange * @Description response 변환 * @Author EunGu. Lee * @CreateDate 2024. 07. 29 * * @param response, request * @return messageSendResponse * * @ChangeDescription */ public MessageSendResponse responseChange(SejongMessageSendResponse response, MessageSendRequest request) { MessageSendResponse messageSendResponse = new MessageSendResponse(); System.out.println("request : " + request); System.out.println("response : " + response); messageSendResponse.setSendType(request.getSendType()); messageSendResponse.setSendCode(response.getSendCode()); if (response.getCode().contains("S")) { if (response.getCode().equals("S405")) { messageSendResponse.setSendCode("300"); messageSendResponse.setMessage("Parameter 확인"); } messageSendResponse.setCode("500"); messageSendResponse.setMessage("서버 오류"); } else if (response.getCode().equals("200")) { messageSendResponse.setCode("200"); messageSendResponse.setMessage("메세지 전송 요청 성공"); } else if ((Integer.parseInt(response.getCode()) > 200 && Integer.parseInt(response.getCode()) <= 300)) { messageSendResponse.setCode("300"); messageSendResponse.setMessage("Parameter 확인"); } else if (Integer.parseInt(response.getCode()) > 400 && Integer.parseInt(response.getCode()) <= 500) { messageSendResponse.setCode("400"); messageSendResponse.setMessage("서버 통신에 실패했습니다."); } else if (Integer.parseInt(response.getCode()) > 500 && Integer.parseInt(response.getCode()) <= 600) { messageSendResponse.setCode("600"); messageSendResponse.setMessage("메세지 전송 오류"); } else { messageSendResponse.setCode("500"); messageSendResponse.setMessage("서버 오류"); } return messageSendResponse; } }