bsm-lab/dfxagent#3
ALIVE 메시지 수신 기능 완료
INFORMATION 메시지 수신 기능 완료
main
icksishu 8 months ago
parent 41782ce2cc
commit 30c4e43447

@ -43,7 +43,7 @@ import java.util.concurrent.Executor;
})
public class DfxAgentConfiguration {
// gradle bootRun 실행 설정
// bootRun --args=" --setting.file=D:\projects\bsm-lab\dfx\dfxagent\src\docs\settings-examples\dfxagent.json"
// bootRun D:\projects\bsm-lab\dfx\dfxagent\src\docs\settings-examples\dfxagent.json"
// command line java 실행 설정
// java -jar dfxagent.jar --setting.file=D:\projects\bsm-lab\dfx\dfxagent\src\docs\settings-examples\dfxagent.json

@ -50,7 +50,6 @@ public class ListenerController {
/**
* (AckDto)
* TODO
*/
@PostMapping(value = "/telegram")
public AckDto telegram(HttpServletRequest request) {
@ -63,4 +62,19 @@ public class ListenerController {
}
return ackDto;
}
/**
* agent command
*/
@PostMapping(value = "/command")
public AckDto command(HttpServletRequest request) {
AckDto ackDto;
try {
String bodyString= ServletUtils.getBodyString(request);
ackDto = listenerService.receiveAck(bodyString);
} catch (IOException e) {
ackDto = AckDto.builder().result(AckDto.ResultType.RECEIVE_FAIL).resultText(e.getLocalizedMessage()).messageUuid("").build();
}
return ackDto;
}
}

@ -7,7 +7,7 @@ import lombok.*;
@Builder
@AllArgsConstructor
@NoArgsConstructor
/**
/*
* Listen
* 1. listener
* 2. ( (DropBoxQueue) )

@ -0,0 +1,18 @@
package com.bsmlab.dfx.agent.listener.dto;
import lombok.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CommandDto {
private CommandType commandType;
private String messageUuid;
public static enum CommandType {
ALIVE,
INFORMATION,
}
}

@ -3,12 +3,15 @@ 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.CommandDto;
import com.bsmlab.dfx.agent.listener.dto.ReceiveMessageDto;
import com.bsmlab.dfx.agent.support.MessageUtils;
import com.bsmlab.dfx.agent.support.exception.IllegalMessageException;
import com.bsmlab.dfx.agent.support.exception.InCompleteMessageException;
import com.bsmlab.dfx.agent.support.exception.NullMessageException;
import com.bsmlab.dfx.agent.task.dropbox.DropBoxService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.Part;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -118,4 +121,30 @@ public class ListenerService {
}
return ackDto;
}
public AckDto receiveCommand(String messageJsonString) {
AckDto ackDto;
CommandDto commandDto;
try {
commandDto = MessageUtils.toCommandDto(messageJsonString);
String resultText = null;
if(CommandDto.CommandType.ALIVE == commandDto.getCommandType()) {
resultText = "ALIVE";
}
else if(CommandDto.CommandType.INFORMATION == commandDto.getCommandType()) {
AgentConfigDto agentConfigDto = this.agentConfigReader.getAgentConfigDto();
ObjectMapper objectMapper = new ObjectMapper();
try {
resultText = objectMapper.writeValueAsString(agentConfigDto);
} catch (JsonProcessingException e) {
resultText = "";
}
}
ackDto = AckDto.builder().result(AckDto.ResultType.PROCESS_SUCCESS).messageUuid(commandDto.getMessageUuid()).resultText(resultText).build();
}
catch (IllegalMessageException | NullMessageException e) {
ackDto = AckDto.builder().result(AckDto.ResultType.RECEIVE_FAIL).resultText(e.getLocalizedMessage()).messageUuid("").build();
}
return ackDto;
}
}

@ -2,6 +2,7 @@ package com.bsmlab.dfx.agent.support;
import com.bsmlab.dfx.agent.config.AgentConfigDto;
import com.bsmlab.dfx.agent.listener.dto.AckDto;
import com.bsmlab.dfx.agent.listener.dto.CommandDto;
import com.bsmlab.dfx.agent.listener.dto.ReceiveMessageDto;
import com.bsmlab.dfx.agent.support.exception.IllegalMessageException;
import com.bsmlab.dfx.agent.support.exception.InCompleteMessageException;
@ -12,6 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.h2.command.Command;
import java.util.*;
@ -163,4 +165,24 @@ public class MessageUtils {
}
return ackDto;
}
public static CommandDto toCommandDto(String messageJsonString) throws IllegalMessageException, NullMessageException {
CommandDto commandDto;
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> map = null;
try {
map = objectMapper.readValue(messageJsonString, new TypeReference<Map<String, Object>>() {});
if(map == null) {
throw new NullMessageException("command message json string is invalid");
}
else {
CommandDto.CommandType commandType = EnumUtils.getEnum(CommandDto.CommandType.class, String.valueOf(map.get("commandType")));
String messageUuid = String.valueOf(map.get("messageUuid"));
commandDto = CommandDto.builder().commandType(commandType).messageUuid(messageUuid).build();
}
} catch (JsonProcessingException e) {
throw new IllegalMessageException(e.getMessage());
}
return commandDto;
}
}

Loading…
Cancel
Save