REM:
1. 统一所有接口参数校验方式
This commit is contained in:
HuangXin 2020-05-29 10:13:40 +08:00
parent 286d511469
commit 0145e57066
4 changed files with 164 additions and 39 deletions

View File

@ -21,7 +21,12 @@ public enum DeviceCapacity {
/**
* Detecive device capacity.
*/
DETECIVE(3, "检测能力");
DETECIVE(3, "检测能力"),
/**
* Allcapacity device capacity.
*/
ALLCAPACITY(-1, "所有能力");
private final int code;
private final String readme;

View File

@ -61,6 +61,16 @@ public class AuthController {
LoginReq reqInfo = mr.getRequestObject(LoginReq.class);
if (reqInfo.getUserName() == null || reqInfo.getUserName().length() == 0) {
log.error("Request username params error, error:{} --> {}", mr.getMsgContent(), reqInfo.getUserName());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
if (reqInfo.getPassword() == null || reqInfo.getPassword().length() == 0) {
log.error("Request password params error, error:{} --> {}", mr.getMsgContent(), reqInfo.getPassword());
return ProtocolRespDTO.result(ErrorCode.ERR_PASSWORD);
}
MulReturnType<ErrorCode, String> ret = userAccountService.loginService(reqInfo.getUserName(),
reqInfo.getPassword());
@ -106,6 +116,11 @@ public class AuthController {
LoginReq reqInfo = mr.getRequestObject(LoginReq.class);
if (reqInfo.getUserName() == null || reqInfo.getUserName().length() == 0) {
log.error("Request username params error, error:{} --> {}", mr.getMsgContent(), reqInfo.getUserName());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
err = userAccountService.logoutService(reqInfo.getUserName(), mr.getAuthToken());
if (err != ErrorCode.ERR_OK) {

View File

@ -28,7 +28,9 @@ import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* The type Dispose node manager controller.
@ -137,13 +139,20 @@ public class DisposeNodeManagerController {
}
IDArrayReq reqInfo = mr.getRequestObject(IDArrayReq.class);
List<IDReturnStatus> rspInfo = new ArrayList<>();
if (reqInfo == null || reqInfo.getId().length > 1) {
if (reqInfo.getId() == null || Arrays.stream(reqInfo.getId()).anyMatch(Objects::isNull)) {
log.error("Request params error, error:{}", mr.getMsgContent());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
if (reqInfo.getId().length > 1
|| Arrays.stream(reqInfo.getId()).anyMatch(v -> v.length() == 0)) {
log.error("Prohibit deleting multiple devices at the same time");
return ProtocolRespDTO.result(ErrorCode.ERR_REMOVEMORE);
}
List<IDReturnStatus> rspInfo = new ArrayList<>();
IDReturnStatus retItem = new IDReturnStatus();
retItem.setId(reqInfo.getId()[0]);

View File

@ -4,6 +4,7 @@ import com.dispose.common.DeviceCapacity;
import com.dispose.common.ErrorCode;
import com.dispose.common.FlowDirection;
import com.dispose.common.Helper;
import com.dispose.common.IPAddrType;
import com.dispose.manager.TaskCacheManager;
import com.dispose.pojo.dto.ProtocolReqDTO;
import com.dispose.pojo.dto.ProtocolRespDTO;
@ -43,6 +44,7 @@ import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
@ -69,6 +71,12 @@ public class DisposeTaskController {
@Resource
private UserAccountService userAccountService;
private static Long convertTimeToLong(String time) {
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse(time, ftf);
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* Task start protocol resp dto.
*
@ -92,6 +100,25 @@ public class DisposeTaskController {
StartTaskReq reqInfo = mr.getRequestObject(StartTaskReq.class);
if (reqInfo.getId() == null) {
log.error("Request id params error, error:{} --> {}", mr.getMsgContent(), reqInfo.getId());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
if (reqInfo.getType() == null
|| reqInfo.getType() < DeviceCapacity.ALLCAPACITY.getCode()
|| reqInfo.getType() > DeviceCapacity.DETECIVE.getCode()) {
log.error("Request type params error, error:{} --> {}", mr.getMsgContent(), reqInfo.getType());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
if (reqInfo.getDisposeIp() == null || reqInfo.getDisposeIp().length() == 0
|| !IPAddrType.isIpAddress(reqInfo.getDisposeIp())) {
log.error("Request dispose ip params error, error:{} --> {}", mr.getMsgContent(), reqInfo.getDisposeIp());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
StartTaskRsp rspInfo = new StartTaskRsp();
TaskInfoDetail task = TaskInfoDetail.builder()
@ -114,6 +141,7 @@ public class DisposeTaskController {
rspInfo.setExpireTime(convertTimeToLong(tf.getPlanEndTime()));
} else {
log.error("Failed to create task, error:{}", ret.getFirstParam().getMsg());
rspInfo.setId(String.valueOf(reqInfo.getId()));
}
rspInfo.setStatus(ret.getFirstParam().getCode());
@ -144,6 +172,12 @@ public class DisposeTaskController {
}
IDArrayReq reqInfo = mr.getRequestObject(IDArrayReq.class);
if (reqInfo.getTaskId() == null || Arrays.stream(reqInfo.getTaskId()).anyMatch(Objects::isNull)) {
log.error("Request params error, error:{}", mr.getMsgContent());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
TaskInfoRsp rspInfo = new TaskInfoRsp();
Arrays.stream(reqInfo.getTaskId()).forEach(v -> {
@ -173,6 +207,7 @@ public class DisposeTaskController {
rspInfo.getItems().add(taskData);
} else {
log.error("Stop task: taskId:{}, error:{}", v, k.getFirstParam().getMsg());
taskData.setTaskId(v);
taskData.setStatus(k.getFirstParam().getCode());
taskData.setMessage(k.getFirstParam().getMsg());
}
@ -206,6 +241,27 @@ public class DisposeTaskController {
}
StopTaskReq reqInfo = mr.getRequestObject(StopTaskReq.class);
if (reqInfo.getItems().stream().anyMatch(v -> v.getId() == null
|| v.getId().length() == 0)) {
log.error("Request id params error, error:{}", mr.getMsgContent());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
if (reqInfo.getItems().stream().anyMatch(v -> v.getType() == null
|| v.getType() < DeviceCapacity.ALLCAPACITY.getCode()
|| v.getType() > DeviceCapacity.DETECIVE.getCode())) {
log.error("Request type params error, error:{}", mr.getMsgContent());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
if (reqInfo.getItems().stream().anyMatch(v -> v.getDisposeIp() == null
|| v.getDisposeIp().length() == 0
|| !IPAddrType.isIpAddress(v.getDisposeIp()))) {
log.error("Request ip params error, error:{}", mr.getMsgContent());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
TaskInfoRsp rspInfo = new TaskInfoRsp();
reqInfo.getItems().forEach(v -> {
@ -234,6 +290,11 @@ public class DisposeTaskController {
} else {
log.error("Stop task: taskId:{}, type:{}, disposeIp:{}, error:{}", v.getId(), v.getType(), v.getDisposeIp(),
k.getFirstParam().getMsg());
if (k.getFirstParam() == ErrorCode.ERR_NOSUCHTASK) {
taskData.setDisposeIp(v.getDisposeIp());
} else if (k.getFirstParam() == ErrorCode.ERR_NOSUCHDEVICE) {
taskData.setId(v.getId());
}
taskData.setStatus(k.getFirstParam().getCode());
taskData.setMessage(k.getFirstParam().getMsg());
}
@ -270,6 +331,19 @@ public class DisposeTaskController {
StopTaskReq reqInfo = mr.getRequestObject(StopTaskReq.class);
TaskInfoRsp rspInfo = new TaskInfoRsp();
if (reqInfo.getItems().stream().anyMatch(v -> v.getId() == null
|| v.getId().length() == 0)) {
log.error("Request id params error, error:{}", mr.getMsgContent());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
if (reqInfo.getItems().stream().anyMatch(v -> v.getType() == null
|| v.getType() < DeviceCapacity.ALLCAPACITY.getCode()
|| v.getType() > DeviceCapacity.DETECIVE.getCode())) {
log.error("Request type params error, error:{}", mr.getMsgContent());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
reqInfo.getItems().forEach(v -> {
List<MulReturnType<ErrorCode, TaskInfoDetail>> ret = taskService.stopTaskByDisposeNode(Long.parseLong(v.getId()), v.getType());
@ -299,6 +373,7 @@ public class DisposeTaskController {
log.error("Stop task: deviceId:{}, type:{}, error:{}", v.getId(),
v.getType(), k.getFirstParam().getMsg());
TaskInfoData taskData = TaskInfoData.builder().type(v.getType()).build();
taskData.setId(v.getId());
taskData.setStatus(k.getFirstParam().getCode());
taskData.setMessage(k.getFirstParam().getMsg());
}
@ -332,6 +407,14 @@ public class DisposeTaskController {
}
StopTaskData reqInfo = mr.getRequestObject(StopTaskData.class);
if (reqInfo.getType() == null
|| reqInfo.getType() < DeviceCapacity.ALLCAPACITY.getCode()
|| reqInfo.getType() > DeviceCapacity.DETECIVE.getCode()) {
log.error("Request type params error, error:{}", mr.getMsgContent());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
TaskInfoRsp rspInfo = new TaskInfoRsp();
List<MulReturnType<ErrorCode, TaskInfoDetail>> ret = taskService.stopTaskByType(reqInfo.getType());
@ -361,6 +444,7 @@ public class DisposeTaskController {
} else {
log.error("No task to stop: error:{}", k.getFirstParam().getMsg());
TaskInfoData taskData = TaskInfoData.builder().type(reqInfo.getType()).build();
taskData.setType(reqInfo.getType());
taskData.setStatus(k.getFirstParam().getCode());
taskData.setMessage(k.getFirstParam().getMsg());
}
@ -392,15 +476,25 @@ public class DisposeTaskController {
}
GetTaskReq reqInfo = mr.getRequestObject(GetTaskReq.class);
NodeTaskRsp rspInfo = new NodeTaskRsp();
if (reqInfo.getId() == null || reqInfo.getType() == null) {
if (reqInfo.getId() == null || Arrays.stream(reqInfo.getId()).anyMatch(Objects::isNull)) {
log.error("Request id params error, error:{}", mr.getMsgContent());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
if (reqInfo.getType() == null
|| reqInfo.getType() < DeviceCapacity.ALLCAPACITY.getCode()
|| reqInfo.getType() > DeviceCapacity.DETECIVE.getCode()) {
log.error("Request type params error, error:{}", mr.getMsgContent());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
NodeTaskRsp rspInfo = new NodeTaskRsp();
List<DisposeDevice> valuableData = disposeNodeManager.getAllDisposeDevice()
.stream()
.filter(v -> reqInfo.getId().length == 0
|| reqInfo.getId()[0].length() == 0
|| Arrays.stream(reqInfo.getId()).anyMatch(s -> s.equals(v.getId().toString())))
.collect(Collectors.toList());
@ -411,7 +505,8 @@ public class DisposeTaskController {
AtomicBoolean bHasData = new AtomicBoolean(false);
taskService.getNodeAllRunningTask(v.getId()).forEach(k -> {
if (v.getType().equals(reqInfo.getType()) || reqInfo.getType() == -1) {
if (v.getType().equals(reqInfo.getType())
|| reqInfo.getType() == DeviceCapacity.ALLCAPACITY.getCode()) {
int diff = Helper.getTimestampDiff(k.getBeginTime(), k.getPlanEndTime());
TaskInfoData taskInfoData = TaskInfoData.builder()
.taskId(k.getId().toString())
@ -432,28 +527,28 @@ public class DisposeTaskController {
taskData.setStatus(ErrorCode.ERR_OK.getCode());
taskData.setMessage(ErrorCode.ERR_OK.getMsg());
} else {
log.error("No such task, deviceId:{}", v);
log.error("No such task, deviceId:{}", v.getId());
taskData.setId(v.getId().toString());
taskData.setStatus(ErrorCode.ERR_NOSUCHTASK.getCode());
taskData.setMessage(ErrorCode.ERR_NOSUCHTASK.getMsg());
taskData.setTaskArray(null);
}
});
if (reqInfo.getId().length != 0) {
List<String> unExists = Arrays.stream(reqInfo.getId())
.filter(v -> valuableData.stream().noneMatch(k -> k.getId() == Long.parseLong(v)))
.collect(Collectors.toList());
List<String> unExists = Arrays.stream(reqInfo.getId())
.filter(v -> v.length() > 0)
.filter(v -> valuableData.stream().noneMatch(k -> k.getId() == Long.parseLong(v)))
.collect(Collectors.toList());
unExists.forEach(v -> {
log.error("No such task, deviceId:{}", v);
NodeTaskData taskData = new NodeTaskData();
rspInfo.getItems().add(taskData);
taskData.setId(v);
taskData.setTaskArray(null);
taskData.setStatus(ErrorCode.ERR_NOSUCHTASK.getCode());
taskData.setMessage(ErrorCode.ERR_NOSUCHTASK.getMsg());
});
}
unExists.forEach(v -> {
log.error("No such task, deviceId:{}", v);
NodeTaskData taskData = new NodeTaskData();
rspInfo.getItems().add(taskData);
taskData.setId(v);
taskData.setTaskArray(null);
taskData.setStatus(ErrorCode.ERR_NOSUCHTASK.getCode());
taskData.setMessage(ErrorCode.ERR_NOSUCHTASK.getMsg());
});
return ProtocolRespDTO.result(err, rspInfo);
}
@ -480,11 +575,18 @@ public class DisposeTaskController {
}
IDArrayReq reqInfo = mr.getRequestObject(IDArrayReq.class);
if (reqInfo.getTaskId() == null || Arrays.stream(reqInfo.getTaskId()).anyMatch(Objects::isNull)) {
log.error("Request params error, error:{}", mr.getMsgContent());
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMS);
}
TaskInfoRsp rspInfo = new TaskInfoRsp();
List<TaskInfoDetail> valuableData = taskService.getAllTask()
.stream()
.filter(v -> reqInfo.getTaskId().length == 0
|| reqInfo.getTaskId()[0].length() == 0
|| Arrays.stream(reqInfo.getTaskId()).anyMatch(s -> s.equals(v.getId().toString())))
.collect(Collectors.toList());
@ -510,26 +612,20 @@ public class DisposeTaskController {
taskInfoData.setMessage(ErrorCode.ERR_OK.getMsg());
});
if (reqInfo.getTaskId().length != 0) {
List<String> unExists = Arrays.stream(reqInfo.getTaskId())
.filter(v -> valuableData.stream().noneMatch(k -> k.getId() == Long.parseLong(v)))
.collect(Collectors.toList());
List<String> unExists = Arrays.stream(reqInfo.getTaskId())
.filter(v -> v.length() > 0)
.filter(v -> valuableData.stream().noneMatch(k -> k.getId() == Long.parseLong(v)))
.collect(Collectors.toList());
unExists.forEach(v -> {
log.error("No such task, taskId:{}", v);
TaskInfoData taskData = new TaskInfoData();
rspInfo.getItems().add(taskData);
taskData.setStatus(ErrorCode.ERR_NOSUCHTASK.getCode());
taskData.setMessage(ErrorCode.ERR_NOSUCHTASK.getMsg());
});
}
unExists.forEach(v -> {
log.error("No such task, taskId:{}", v);
TaskInfoData taskData = new TaskInfoData();
rspInfo.getItems().add(taskData);
taskData.setTaskId(v);
taskData.setStatus(ErrorCode.ERR_NOSUCHTASK.getCode());
taskData.setMessage(ErrorCode.ERR_NOSUCHTASK.getMsg());
});
return ProtocolRespDTO.result(err, rspInfo);
}
public static Long convertTimeToLong(String time) {
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse(time, ftf);
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
}