diff --git a/src/main/java/com/icomsys/MessagingConnector.java b/src/main/java/com/icomsys/MessagingConnector.java index bcecfe1..3b68a93 100644 --- a/src/main/java/com/icomsys/MessagingConnector.java +++ b/src/main/java/com/icomsys/MessagingConnector.java @@ -38,7 +38,7 @@ public class MessagingConnector { public MessageSendResponse messageDiv(MessageSendRequest messageSendRequest) throws IOException, InterruptedException { String url = sms; - String key = UUID.randomUUID().toString().substring(0, 7); + String key = UUID.randomUUID().toString().substring(0, 6); LocalDate now = LocalDate.now(); messageSendRequest.setUserKey(now.toString().substring(2).replace("-", "") + key); @@ -76,20 +76,20 @@ public class MessagingConnector { try { ObjectMapper objectMapper = new ObjectMapper(); - Map body = prepareRequestBody(messageSendRequest, objectMapper); - System.out.println("body : " + body); + Map body = this.prepareRequestBody(messageSendRequest, objectMapper); + String boundary = "----WebKitFormBoundary" + System.currentTimeMillis(); - String jsonBody = objectMapper.writeValueAsString(body); - System.out.println("jsonbody : " + jsonBody); + // Multipart body 생성 + HttpRequest.BodyPublisher bodyPublisher = createMultipartBody(body, boundary); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(url)) - .header("Content-Type", "application/json") - .header("sejongApiKey", apiKey) - .POST(HttpRequest.BodyPublishers.ofString(jsonBody)) + .header("Content-Type", "multipart/form-data; boundary=" + boundary) + .header("sejongApiKey", this.apiKey) + .POST(bodyPublisher) .build(); - HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + HttpResponse response = this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println("responseCode : " + response.statusCode()); System.out.println("responseBody : " + response.body()); System.out.println("responseFull : " + response); @@ -101,18 +101,18 @@ public class MessagingConnector { messageSendResponse.setMessage("서버 통신 오류"); } } catch (Exception e) { + e.printStackTrace(); messageSendResponse.setCode("500"); messageSendResponse.setMessage("서버 통신 오류"); } messageSendResponse = this.responseChange(sejongMessageSendResponse, messageSendRequest); - messageSendServiceDsl.insertSendLog(sejongMessageSendResponse, messageSendRequest, messageSendResponse); + this.messageSendServiceDsl.insertSendLog(sejongMessageSendResponse, messageSendRequest, messageSendResponse); - if ("KAKAO".equals(messageSendRequest.getSendType()) && "Y".equals(messageSendRequest.getOptionYn())) { - if (!Objects.equals(sejongMessageSendResponse.getCode(), "200")) { - messageSendRequest.setSendType(""); - messageSendResponse = this.messageDiv(messageSendRequest); - } + if ("KAKAO".equals(messageSendRequest.getSendType()) && "Y".equals(messageSendRequest.getOptionYn()) && + !Objects.equals(sejongMessageSendResponse.getCode(), "200")) { + messageSendRequest.setSendType(""); + messageSendResponse = this.messageDiv(messageSendRequest); } return messageSendResponse; @@ -133,6 +133,21 @@ public class MessagingConnector { } } + private HttpRequest.BodyPublisher createMultipartBody(Map body, String boundary) { + StringBuilder multipartBody = new StringBuilder(); + + for (Map.Entry entry : body.entrySet()) { + multipartBody.append("--").append(boundary).append("\r\n"); + multipartBody.append("Content-Disposition: form-data; name=\"").append(entry.getKey()).append("\"\r\n\r\n"); + multipartBody.append(entry.getValue()).append("\r\n"); + } + + // 마지막 바운더리 추가 + multipartBody.append("--").append(boundary).append("--\r\n"); + + return HttpRequest.BodyPublishers.ofString(multipartBody.toString()); + } + public MessageSendResponse responseChange(SejongMessageSendResponse response, MessageSendRequest request) { MessageSendResponse messageSendResponse = new MessageSendResponse(); messageSendResponse.setSendType(request.getSendType());