20250109_2

main
seokjun jeon 2 weeks ago
parent e74f90168d
commit e7cfe445b3

@ -38,7 +38,7 @@ public class MessagingConnector {
public MessageSendResponse messageDiv(MessageSendRequest messageSendRequest) throws IOException, InterruptedException { public MessageSendResponse messageDiv(MessageSendRequest messageSendRequest) throws IOException, InterruptedException {
String url = sms; String url = sms;
String key = UUID.randomUUID().toString().substring(0, 7); String key = UUID.randomUUID().toString().substring(0, 6);
LocalDate now = LocalDate.now(); LocalDate now = LocalDate.now();
messageSendRequest.setUserKey(now.toString().substring(2).replace("-", "") + key); messageSendRequest.setUserKey(now.toString().substring(2).replace("-", "") + key);
@ -76,20 +76,20 @@ public class MessagingConnector {
try { try {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
Map<String, String> body = prepareRequestBody(messageSendRequest, objectMapper); Map<String, String> body = this.prepareRequestBody(messageSendRequest, objectMapper);
System.out.println("body : " + body); String boundary = "----WebKitFormBoundary" + System.currentTimeMillis();
String jsonBody = objectMapper.writeValueAsString(body); // Multipart body 생성
System.out.println("jsonbody : " + jsonBody); HttpRequest.BodyPublisher bodyPublisher = createMultipartBody(body, boundary);
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url)) .uri(URI.create(url))
.header("Content-Type", "application/json") .header("Content-Type", "multipart/form-data; boundary=" + boundary)
.header("sejongApiKey", apiKey) .header("sejongApiKey", this.apiKey)
.POST(HttpRequest.BodyPublishers.ofString(jsonBody)) .POST(bodyPublisher)
.build(); .build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("responseCode : " + response.statusCode()); System.out.println("responseCode : " + response.statusCode());
System.out.println("responseBody : " + response.body()); System.out.println("responseBody : " + response.body());
System.out.println("responseFull : " + response); System.out.println("responseFull : " + response);
@ -101,18 +101,18 @@ public class MessagingConnector {
messageSendResponse.setMessage("서버 통신 오류"); messageSendResponse.setMessage("서버 통신 오류");
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
messageSendResponse.setCode("500"); messageSendResponse.setCode("500");
messageSendResponse.setMessage("서버 통신 오류"); messageSendResponse.setMessage("서버 통신 오류");
} }
messageSendResponse = this.responseChange(sejongMessageSendResponse, messageSendRequest); 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 ("KAKAO".equals(messageSendRequest.getSendType()) && "Y".equals(messageSendRequest.getOptionYn()) &&
if (!Objects.equals(sejongMessageSendResponse.getCode(), "200")) { !Objects.equals(sejongMessageSendResponse.getCode(), "200")) {
messageSendRequest.setSendType(""); messageSendRequest.setSendType("");
messageSendResponse = this.messageDiv(messageSendRequest); messageSendResponse = this.messageDiv(messageSendRequest);
}
} }
return messageSendResponse; return messageSendResponse;
@ -133,6 +133,21 @@ public class MessagingConnector {
} }
} }
private HttpRequest.BodyPublisher createMultipartBody(Map<String, String> body, String boundary) {
StringBuilder multipartBody = new StringBuilder();
for (Map.Entry<String, String> 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) { public MessageSendResponse responseChange(SejongMessageSendResponse response, MessageSendRequest request) {
MessageSendResponse messageSendResponse = new MessageSendResponse(); MessageSendResponse messageSendResponse = new MessageSendResponse();
messageSendResponse.setSendType(request.getSendType()); messageSendResponse.setSendType(request.getSendType());

Loading…
Cancel
Save