diff --git a/src/docs/agent-bsm-lab-postgres/conf/dfxagent-bsm-lab-postgres.json b/src/docs/agent-bsm-lab-postgres/conf/dfxagent-bsm-lab-postgres.json index dcd7495..b129cc6 100644 --- a/src/docs/agent-bsm-lab-postgres/conf/dfxagent-bsm-lab-postgres.json +++ b/src/docs/agent-bsm-lab-postgres/conf/dfxagent-bsm-lab-postgres.json @@ -5,6 +5,7 @@ "sslEnabled": true, "keyStorePath": "D:/projects/bsm-lab/dfx/dfxagent/src/docs/agent-bsm-lab-postgres/cert/dfxagent-kdn.p12", "keyStorePassword": "qortpals1!", + "keyStoreAlias": "dfxagent-local-01", "keyStoreType": "PKCS12", "trustStorePath": "D:/projects/bsm-lab/dfx/dfxagent/src/docs/agent-bsm-lab-postgres/cert/truststore-kdn.jks", "trustStorePassword": "qortpals1!", @@ -65,6 +66,7 @@ ] }, "dropBoxConfig": { + "sentMessageStorageRoot": "D:/projects/bsm-lab/dfx/dfxagent/src/docs/agent-bsm-lab-postgres/messages/sent", "receivedMessageStorageRoot": "D:/projects/bsm-lab/dfx/dfxagent/src/docs/agent-bsm-lab-postgres/messages/received", "processedMessageStorageRoot": "D:/projects/bsm-lab/dfx/dfxagent/src/docs/aagent-bsm-lab-postgres/messages/processed", "failureMessageStorageRoot": "D:/projects/bsm-lab/dfx/dfxagent/src/docs/agent-bsm-lab-postgres/messages/failure", diff --git a/src/main/java/com/bsmlab/dfx/agent/DfxAgentApplication.java b/src/main/java/com/bsmlab/dfx/agent/DfxAgentApplication.java index f8f27b6..6b59146 100644 --- a/src/main/java/com/bsmlab/dfx/agent/DfxAgentApplication.java +++ b/src/main/java/com/bsmlab/dfx/agent/DfxAgentApplication.java @@ -13,6 +13,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Scanner; @SpringBootApplication @@ -44,27 +45,37 @@ public class DfxAgentApplication { } } if(hasSettingFile && !hasSettingCheck && !hasParseMessageFile) { + Map props = new HashMap<>(); + boolean isCorrect = true; try { ObjectMapper objectMapper = new ObjectMapper(); AgentConfigDto agentConfigDto = objectMapper.readValue(new File(settingFilePath), AgentConfigDto.class); if(agentConfigDto.isSslEnabled()) { - System.setProperty("server.port", String.valueOf(agentConfigDto.getMyListenPort())); - System.setProperty("server.ssl.enabled", String.valueOf(agentConfigDto.isSslEnabled())); - System.setProperty("server.ssl.key-store", agentConfigDto.getKeyStorePath()); - System.setProperty("server.ssl.key-store-password", agentConfigDto.getKeyStorePassword()); - System.setProperty("server.ssl.key-store-type", "PKCS12"); - System.setProperty("server.ssl.trust-store", agentConfigDto.getTrustStorePath()); - System.setProperty("server.ssl.trust-store-password", agentConfigDto.getTrustStorePassword()); - System.setProperty("server.ssl.trust-store-type", "JKS"); + props.put("server.port", agentConfigDto.getMyListenPort()); + props.put("server.ssl.enabled", agentConfigDto.isSslEnabled()); + props.put("server.ssl.key-store", agentConfigDto.getKeyStorePath()); + props.put("server.ssl.key-store-password", agentConfigDto.getKeyStorePassword()); + props.put("server.ssl.key-store-type", "PKCS12"); + props.put("server.ssl.trust-store", agentConfigDto.getTrustStorePath()); + props.put("server.ssl.trust-store-password", agentConfigDto.getTrustStorePassword()); + props.put("server.ssl.trust-store-type", "JKS"); } } catch (DatabindException e) { System.out.println("cannot parse a setting file. " + settingFilePath); e.printStackTrace(System.out); + isCorrect = false; } catch (IOException e) { System.out.println("cannot read a setting file. " + settingFilePath); e.printStackTrace(System.out); + isCorrect = false; + } + if(isCorrect) { + SpringApplication springApplication = new SpringApplication(DfxAgentApplication.class); + if(!props.isEmpty()) { + springApplication.setDefaultProperties(props); + } + springApplication.run(args); } - SpringApplication.run(DfxAgentApplication.class, args); } } diff --git a/src/main/java/com/bsmlab/dfx/agent/config/AgentConfigDto.java b/src/main/java/com/bsmlab/dfx/agent/config/AgentConfigDto.java index 91bcd60..503a34b 100644 --- a/src/main/java/com/bsmlab/dfx/agent/config/AgentConfigDto.java +++ b/src/main/java/com/bsmlab/dfx/agent/config/AgentConfigDto.java @@ -60,6 +60,7 @@ public class AgentConfigDto { @Data public static class DropBoxConfig { + private String sentMessageStorageRoot; private String receivedMessageStorageRoot; private String processedMessageStorageRoot; private String failureMessageStorageRoot; diff --git a/src/main/java/com/bsmlab/dfx/agent/support/MessageUtils.java b/src/main/java/com/bsmlab/dfx/agent/support/MessageUtils.java index a8de12e..b5f57da 100644 --- a/src/main/java/com/bsmlab/dfx/agent/support/MessageUtils.java +++ b/src/main/java/com/bsmlab/dfx/agent/support/MessageUtils.java @@ -226,6 +226,7 @@ public class MessageUtils { AgentConfigDto.KnownAgent recipientAgent = agentConfigReader.getKnownAgent(receiveMessageDto.getRecipientHostId()); String targetHostId = recipientAgent.getRoutingHostIdList().get(0); AgentConfigDto.KnownAgent knownAgent = agentConfigReader.getKnownAgent(targetHostId); + String protocol = knownAgent.isSslEnabled() ? "https://" : "http://"; HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); httpHeaders.set("User-Agent", agentConfigReader.getApplicationName() + ", version: " + agentConfigReader.getApplicationVersion() + "(" + agentConfigReader.getApplicationCommitId() + ")" @@ -234,7 +235,7 @@ public class MessageUtils { ); HttpEntity bodyEntity = new HttpEntity<>(receiveMessageDto, httpHeaders); RestTemplate restTemplate = new RestTemplate(); - String url = "http://" + knownAgent.getHostName() + ":" + knownAgent.getListenPort() + "/listen"; + String url = protocol + knownAgent.getHostName() + ":" + knownAgent.getListenPort() + "/listen"; String response = restTemplate.postForObject(url, bodyEntity, String.class); AckDto ackDto = objectMapper.readValue(response, new TypeReference() {}); return ackDto; @@ -254,6 +255,7 @@ public class MessageUtils { } } AgentConfigDto.KnownAgent knownAgent = agentConfigReader.getKnownAgent(targetHostId); + String protocol = knownAgent.isSslEnabled() ? "https://" : "http://"; HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); httpHeaders.set("User-Agent", agentConfigReader.getApplicationName() + ", version: " + agentConfigReader.getApplicationVersion() + "(" + agentConfigReader.getApplicationCommitId() + ")" @@ -262,7 +264,7 @@ public class MessageUtils { ); HttpEntity bodyEntity = new HttpEntity<>(receiveMessageDto, httpHeaders); RestTemplate restTemplate = new RestTemplate(); - String url = "http://" + knownAgent.getHostName() + ":" + knownAgent.getListenPort() + "/listen"; + String url = protocol + knownAgent.getHostName() + ":" + knownAgent.getListenPort() + "/listen"; String response = restTemplate.postForObject(url, bodyEntity, String.class); AckDto ackDto = objectMapper.readValue(response, new TypeReference() {}); return ackDto; @@ -271,6 +273,7 @@ public class MessageUtils { public static AckDto alive(AgentConfigReader agentConfigReader, AgentConfigDto.KnownAgent knownAgent) throws JsonProcessingException { String messageUuid = UUID.randomUUID().toString(); CommandDto commandDto = CommandDto.builder().commandType(CommandDto.CommandType.ALIVE).messageUuid(messageUuid).build(); + String protocol = knownAgent.isSslEnabled() ? "https://" : "http://"; HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); httpHeaders.set("User-Agent", agentConfigReader.getApplicationName() + ", version: " + agentConfigReader.getApplicationVersion() + "(" + agentConfigReader.getApplicationCommitId() + ")" @@ -279,7 +282,7 @@ public class MessageUtils { ); HttpEntity bodyEntity = new HttpEntity<>(commandDto, httpHeaders); RestTemplate restTemplate = new RestTemplate(); - String url = "http://" + knownAgent.getHostName() + ":" + knownAgent.getListenPort() + "/command"; + String url = protocol + knownAgent.getHostName() + ":" + knownAgent.getListenPort() + "/command"; log.debug("StatusChecker to {} send a message UUID {}", knownAgent.getHostName(), messageUuid); String response = ""; AckDto ackDto = null; diff --git a/src/main/java/com/bsmlab/dfx/agent/task/postman/PostmanSchedulerService.java b/src/main/java/com/bsmlab/dfx/agent/task/postman/PostmanSchedulerService.java index 02fde64..f76a93e 100644 --- a/src/main/java/com/bsmlab/dfx/agent/task/postman/PostmanSchedulerService.java +++ b/src/main/java/com/bsmlab/dfx/agent/task/postman/PostmanSchedulerService.java @@ -25,8 +25,7 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.text.SimpleDateFormat; @@ -70,6 +69,24 @@ public class PostmanSchedulerService { } } + private void writeSentMessage(ReceiveMessageDto receiveMessageDto) { + File root = new File(agentConfigReader.getAgentConfigDto().getDropBoxConfig().getSentMessageStorageRoot()); + Date today = new Date(System.currentTimeMillis()); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd/HH"); + String targetDirectoryString = root.getAbsolutePath() + "/" + dateFormat.format(today); + File targetDirectory = new File(targetDirectoryString); + if(!targetDirectory.exists()) { + targetDirectory.mkdirs(); + } + String targetFilePath = targetDirectoryString + "/" + receiveMessageDto.getMessageUuid(); + try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(targetFilePath)))) { + objectOutputStream.writeObject(receiveMessageDto); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + public void run(AgentConfigDto.Postman postman) { AgentConfigDto.KnownAgent knownAgent = agentConfigReader.getKnownAgent(postman.getRecipientHostId()); if("ALIVE".equals(agentConfigReader.getKnownAgentStatus(knownAgent.getHostId()))) { @@ -96,6 +113,7 @@ public class PostmanSchedulerService { .recipientHostId(postman.getRecipientHostId()).recipientDropBoxId(postman.getRecipientDropBoxId()) .routingHostList(routingHostList) .data(dataString).processStatus(ReceiveMessageDto.ProcessStatus.PROCESS_SEND).build(); + this.writeSentMessage(receiveMessageDto); log.debug("postman to {} send a message UUID {} (data count: {})", receiveMessageDto.getRecipientHostId(), receiveMessageDto.getMessageUuid(), dataMapList.size()); AckDto ackDto = MessageUtils.send(this.agentConfigReader, postman.getPostmanId(), receiveMessageDto); log.debug("postman received ack from {} ack: {}", receiveMessageDto.getRecipientHostId(), ackDto); @@ -175,6 +193,7 @@ public class PostmanSchedulerService { .messageUuid(messageUuid).messageType(AgentConfigDto.MessageType.TRANSFER_DB_TO_DB) .recipientHostId(postman.getRecipientHostId()).recipientDropBoxId(postman.getRecipientDropBoxId()) .data(dataString).build(); + this.writeSentMessage(receiveMessageDto); String messageString = objectMapper.writeValueAsString(receiveMessageDto); // http 준비 HttpHeaders httpHeaders = new HttpHeaders(); diff --git a/src/main/java/com/bsmlab/dfx/agent/task/status/FileCleanerSchedulerService.java b/src/main/java/com/bsmlab/dfx/agent/task/status/FileCleanerSchedulerService.java index 21cbec7..cbbc25f 100644 --- a/src/main/java/com/bsmlab/dfx/agent/task/status/FileCleanerSchedulerService.java +++ b/src/main/java/com/bsmlab/dfx/agent/task/status/FileCleanerSchedulerService.java @@ -27,17 +27,20 @@ public class FileCleanerSchedulerService { } public void run() { - String processMesssageStorageRoot = agentConfigReader.getAgentConfigDto().getDropBoxConfig().getProcessedMessageStorageRoot(); - File storageRoot = new File(processMesssageStorageRoot); + String processedMesssageStorageRoot = agentConfigReader.getAgentConfigDto().getDropBoxConfig().getProcessedMessageStorageRoot(); + File processedMessageStorage = new File(processedMesssageStorageRoot); + String sentMesssageStorageRoot = agentConfigReader.getAgentConfigDto().getDropBoxConfig().getSentMessageStorageRoot(); + File sentMessageStorage = new File(sentMesssageStorageRoot); List allDirectoryList = new ArrayList<>(); - this.findDirectory(storageRoot, allDirectoryList); + this.findDirectory(processedMessageStorage, allDirectoryList); + this.findDirectory(sentMessageStorage, allDirectoryList); Calendar calendar = Calendar.getInstance(); int toDateCount = agentConfigReader.getAgentConfigDto().getDropBoxConfig().getRetentionDaysOfProcessedMessage() * -1; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); for(int i = 0; i < toDateCount; i++) { calendar.roll(Calendar.DATE, (i * -1)); Date currentDate = calendar.getTime(); - String retentionDirectoryString = processMesssageStorageRoot + "/" + dateFormat.format(currentDate); + String retentionDirectoryString = processedMesssageStorageRoot + "/" + dateFormat.format(currentDate); Iterator iterator = allDirectoryList.iterator(); while(iterator.hasNext()) { File file = iterator.next();