File 수신 로직 진행중

main
semin.baek 10 months ago
parent 82ad55fc03
commit 47b8376508

@ -0,0 +1,22 @@
{
"sender": {
"host-id": "sam",
"timestamp": 1750643945523
},
"message-uuid": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6e",
"message-type": "SAVE_FILE",
"recipient": {
"host-id": "defree",
"drop-box-id": "receive-work-image-file"
},
"data": [
{
"file-name": "v1.jpg",
"meta-data": {
"FILE_ID": "F1V1JPG",
"ORIGINAL_FILE_NAME": "현장사진1.jpg"
},
"meta-drop-box-id": "save-work-image-file-info"
}
]
}

@ -47,6 +47,12 @@
"drop-box-id": "receive-work-image-file", "drop-box-id": "receive-work-image-file",
"task-type": "RECEIVE_FILE", "task-type": "RECEIVE_FILE",
"save-directory-root": "D:\\projects\\bsm-lab\\dfx\\run\\receive_file" "save-directory-root": "D:\\projects\\bsm-lab\\dfx\\run\\receive_file"
},
{
"drop-box-id": "save-work-image-file-info",
"task-type": "SAVE_DATA_TABLE",
"dataSourceId": "dfcms",
"sql-id": "dfcms.file.insertFileInfo"
} }
] ]
}, },

@ -3,13 +3,18 @@ package com.bsmlab.dfx.agent.listener;
import com.bsmlab.dfx.agent.listener.dto.AckDto; import com.bsmlab.dfx.agent.listener.dto.AckDto;
import com.bsmlab.dfx.agent.listener.service.ListenerService; import com.bsmlab.dfx.agent.listener.service.ListenerService;
import com.bsmlab.dfx.agent.support.ServletUtils; import com.bsmlab.dfx.agent.support.ServletUtils;
import com.bsmlab.dfx.agent.support.exception.IllegalMessageException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.Part;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException; import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
@Slf4j @Slf4j
@RestController @RestController
@ -26,9 +31,17 @@ public class ListenerController {
public AckDto listener(HttpServletRequest request) { public AckDto listener(HttpServletRequest request) {
AckDto ackDto = AckDto.builder().build(); AckDto ackDto = AckDto.builder().build();
try { try {
String contentType = request.getContentType();
if(contentType != null && contentType.contains("application/json")) {
String bodyString = ServletUtils.getBodyString(request); String bodyString = ServletUtils.getBodyString(request);
ackDto = listenerService.receiveMessage(bodyString); ackDto = listenerService.receiveMessage(bodyString);
} catch (IOException e) { }
else if(contentType != null && contentType.contains("multipart/form-data")) {
//TODO 파일 전송인 경우 Multipart로 전송하며 파일의 메타데이터인 application/json 과 n개의 파일 본문인 File Multipart 를 합쳐서 전송한다.
Collection<Part> parts = request.getParts();
ackDto = listenerService.receiveFiles(parts);
}
} catch (IOException | ServletException e) {
ackDto.setResult(AckDto.ResultType.RECEIVE_FAIL); ackDto.setResult(AckDto.ResultType.RECEIVE_FAIL);
ackDto.setResultText(e.getMessage()); ackDto.setResultText(e.getMessage());
} }
@ -36,7 +49,8 @@ public class ListenerController {
} }
/** /**
* * (AckDto)
* TODO
*/ */
@PostMapping(value = "/telegram") @PostMapping(value = "/telegram")
public AckDto telegram(HttpServletRequest request) { public AckDto telegram(HttpServletRequest request) {

@ -1,21 +1,31 @@
package com.bsmlab.dfx.agent.listener.service; package com.bsmlab.dfx.agent.listener.service;
import com.bsmlab.dfx.agent.config.AgentConfigDto;
import com.bsmlab.dfx.agent.config.AgentConfigReader;
import com.bsmlab.dfx.agent.listener.dto.AckDto; import com.bsmlab.dfx.agent.listener.dto.AckDto;
import com.bsmlab.dfx.agent.listener.dto.ReceiveMessageDto; import com.bsmlab.dfx.agent.listener.dto.ReceiveMessageDto;
import com.bsmlab.dfx.agent.support.MessageUtils; import com.bsmlab.dfx.agent.support.MessageUtils;
import com.bsmlab.dfx.agent.support.exception.IllegalMessageException; import com.bsmlab.dfx.agent.support.exception.IllegalMessageException;
import com.bsmlab.dfx.agent.support.exception.InCompleteMessageException; import com.bsmlab.dfx.agent.support.exception.InCompleteMessageException;
import com.bsmlab.dfx.agent.support.exception.NullMessageException; import com.bsmlab.dfx.agent.support.exception.NullMessageException;
import com.bsmlab.dfx.agent.task.dropbox.DropBoxDto;
import com.bsmlab.dfx.agent.task.dropbox.DropBoxService; import com.bsmlab.dfx.agent.task.dropbox.DropBoxService;
import jakarta.servlet.http.Part;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Iterator;
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j @Slf4j
@Service @Service
public class ListenerService { public class ListenerService {
private final DropBoxService dropBoxService; private final DropBoxService dropBoxService;
private final AgentConfigReader agentConfigReader;
public AckDto receiveMessage(String messageJsonString) { public AckDto receiveMessage(String messageJsonString) {
AckDto ackDto = null; AckDto ackDto = null;
@ -35,4 +45,40 @@ public class ListenerService {
} }
return ackDto; return ackDto;
} }
public AckDto receiveFiles(Collection<Part> parts) {
AckDto ackDto = null;
try {
if(parts == null || parts.size() < 2) {
throw new IllegalMessageException("파일 전송은 파일의 메타데이터인 application/json 과 n개의 파일 본문인 File Multipart 를 합쳐서 전송하여야 합니다.");
}
else {
Iterator<Part> iterator = parts.iterator();
Part firstPart = iterator.next();
if(firstPart.getContentType() == null || !firstPart.getContentType().contains("application/json")) {
throw new IllegalMessageException("Multipart의 첫 번째 데이터는 application/json 이어야 합니다.");
}
while(iterator.hasNext()) {
Part filePart = iterator.next();
if(filePart.getSubmittedFileName() == null) {
throw new IllegalMessageException("Multipart의 두 번째 데이터 및 그 이후 데이터는 File 이어야 합니다.");
}
}
firstPart = iterator.next();
String messageJsonString = new String(firstPart.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
ReceiveMessageDto receiveMessageDto = MessageUtils.toReceiveMessageDto(messageJsonString);
AgentConfigDto.DropBox dropBox = agentConfigReader.getDropBox(receiveMessageDto.getRecipientDropBoxId());
String saveDirectoryRoot = dropBox.getSaveDirectoryRoot();
while(iterator.hasNext()) {
Part part = iterator.next();
}
}
}
catch(IllegalMessageException | IOException | InCompleteMessageException | NullMessageException e) {
log.error("{}", e, e);
ackDto = AckDto.builder().result(AckDto.ResultType.RECEIVE_FAIL).resultText(e.getLocalizedMessage()).build();
}
return ackDto;
}
} }

Loading…
Cancel
Save