OCT
REM: 1. 更新测试环境配置文件 2. 删除冗余的全局配置项 3. 任务管理定时器轮询时间改为1s 4. 增加核心业务注释 5. 增加关键点log日志
This commit is contained in:
parent
f2b93005e3
commit
afae01470e
|
@ -52,6 +52,6 @@ phoenix.response-enc-switch=false
|
|||
phoenix.aes-key=Wt4EJu6Rrq5udd/42bNpCQ==
|
||||
#====custom config,begin with phoenix====
|
||||
#调试配置
|
||||
dispose.check-protocol-timeout=false
|
||||
dispose.check-protocol-timeout=true
|
||||
dispose.check-request-token=true
|
||||
dispose.check-admin-permission=true
|
||||
|
|
|
@ -30,9 +30,4 @@ public class GlobalVar {
|
|||
* The constant IS_CHECK_REQUEST_TIMEOUT.
|
||||
*/
|
||||
public static volatile boolean IS_CHECK_REQUEST_TIMEOUT = false;
|
||||
|
||||
/**
|
||||
* The constant IS_VERIFY_TOKEN.
|
||||
*/
|
||||
public static volatile boolean IS_VERIFY_TOKEN = true;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ public class EncodingFilterConfig {
|
|||
*/
|
||||
@Bean
|
||||
public FilterRegistrationBean<CharacterEncodingFilter> filterRegistrationBean() {
|
||||
// 设置控制器返回HTTP头编码格式为UTF8
|
||||
FilterRegistrationBean<CharacterEncodingFilter> registrationBean = new FilterRegistrationBean<>();
|
||||
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
|
||||
characterEncodingFilter.setForceEncoding(true);
|
||||
|
|
|
@ -39,9 +39,8 @@ public class SetupInit implements CommandLineRunner {
|
|||
*/
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
// TODO Auto-generated method stub
|
||||
// 系统初始化入口
|
||||
GlobalVar.IS_CHECK_REQUEST_TIMEOUT = Boolean.parseBoolean(disposeConfigure.getCheckProtocolTimeout());
|
||||
log.info("System Setup................................................");
|
||||
disposeNodeManager.loadDisposeNodeFromDB();
|
||||
taskService.loadTaskFromDatabase();
|
||||
}
|
||||
|
|
|
@ -39,20 +39,25 @@ public class DeviceRouter {
|
|||
public static DisposeEntryManager deviceRouterFactory(int devType, String ipAddr, IPAddrType ipType) {
|
||||
String mapKey = getMapKey(devType, ipAddr);
|
||||
|
||||
// 判断缓存中是否创建过对象,如果创建过直接从缓存中获取
|
||||
if (DEVICE_CACHE_MAP.containsKey(mapKey)) {
|
||||
return DEVICE_CACHE_MAP.get(mapKey);
|
||||
} else {
|
||||
// 第一次访问创建新对象并缓存
|
||||
if (GlobalVar.USED_VIRTUAL_DISPOSE_MODE) {
|
||||
// 虚拟设备,供调试业务使用
|
||||
DisposeEntryManager dev = new VirtualDeviceImpl(ipAddr);
|
||||
DEVICE_CACHE_MAP.put(mapKey, dev);
|
||||
return dev;
|
||||
} else if (devType == DisposeDeviceType.DPTECH_UMC.getCode()) {
|
||||
// 迪普设备
|
||||
DisposeEntryManager dev = new DPTechImpl(ipAddr);
|
||||
DEVICE_CACHE_MAP.put(mapKey, dev);
|
||||
return dev;
|
||||
}
|
||||
}
|
||||
|
||||
// 始终返回一个虚拟设备作为默认设备
|
||||
DisposeEntryManager dev = new VirtualDeviceImpl(ipAddr);
|
||||
DEVICE_CACHE_MAP.put(mapKey, dev);
|
||||
return dev;
|
||||
|
|
|
@ -16,6 +16,7 @@ public class SoapPasswordCallbackHandler implements CallbackHandler {
|
|||
*/
|
||||
@Override
|
||||
public void handle(Callback[] callbacks) {
|
||||
// DPTech SOAP接口WSSecure头部密码填充
|
||||
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
|
||||
pc.setPassword(ConstValue.SOAPWrapperConst.PASSWORD);
|
||||
}
|
||||
|
|
|
@ -48,17 +48,19 @@ public class TokenInterceptor implements HandlerInterceptor {
|
|||
response.setContentType("application/json;charset=UTF-8");
|
||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
response.getWriter().write(new ObjectMapper().writeValueAsString(ProtocolRespDTO.result(err)));
|
||||
log.error("Http request token [{}] is error: {}", token, err);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// 缺少必要的认证头部
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
||||
response.getWriter().write(new ObjectMapper().writeValueAsString(ProtocolRespDTO.result(ErrorCode.ERR_MISSAUTHHEAD)));
|
||||
log.error("Http request head miss \"Authorization\" item");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@ public class TaskCacheManagerImpl implements TaskCacheManager {
|
|||
*/
|
||||
@Override
|
||||
public ErrorCode addTask(TaskInfoDetail taskData) {
|
||||
// 输入参数验证
|
||||
if (taskData == null
|
||||
|| taskData.getDisposeIp() == null
|
||||
|| taskData.getDisposeIp().length() == 0
|
||||
|
@ -59,11 +60,13 @@ public class TaskCacheManagerImpl implements TaskCacheManager {
|
|||
return ErrorCode.ERR_INPUTMISS;
|
||||
}
|
||||
|
||||
// 判断任务是否已经存在
|
||||
if (taskCacheMap.containsKey(taskData.getId())) {
|
||||
return ErrorCode.ERR_DEVICEEXISTS;
|
||||
}
|
||||
|
||||
taskData.setRetryTimes(0);
|
||||
// 添加到Map缓存中
|
||||
taskData.setRetryTimes(0); // 保留字段
|
||||
taskCacheMap.put(taskData.getId(), taskData);
|
||||
return ErrorCode.ERR_OK;
|
||||
}
|
||||
|
@ -91,6 +94,7 @@ public class TaskCacheManagerImpl implements TaskCacheManager {
|
|||
return ErrorCode.ERR_NOSUCHDEVICE;
|
||||
}
|
||||
|
||||
// 更新状态
|
||||
taskCacheMap.get(id).setCurrentStatus(status);
|
||||
|
||||
return ErrorCode.ERR_OK;
|
||||
|
|
|
@ -57,9 +57,10 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
|
|||
|
||||
String username = getUsernameByToken(token);
|
||||
|
||||
if (username == null) {
|
||||
if (username == null || username.length() == 0) {
|
||||
return ErrorCode.ERR_LOGOUT;
|
||||
} else if ("admin".equals(username)) {
|
||||
// admin 用户具有操作权限
|
||||
return ErrorCode.ERR_OK;
|
||||
}
|
||||
|
||||
|
@ -89,16 +90,20 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
|
|||
*/
|
||||
@Override
|
||||
public ErrorCode verifyToken(String token) {
|
||||
//userAccountMap
|
||||
|
||||
// 判断当前token是否存在,如果不存在则用户没有登录
|
||||
if (!userAccountCache.containsKey(token)) {
|
||||
return ErrorCode.ERR_LOGOUT;
|
||||
} else {
|
||||
UserAccountCache uc = userAccountCache.get(token);
|
||||
|
||||
// 根据token提取用户信息,判断当前token是否超时
|
||||
if ((System.currentTimeMillis() - uc.getLastAccess())
|
||||
>= ConstValue.GlobalConfigure.TOKEN_TIMEOUT_MS) {
|
||||
log.error("User {} token timeout before {}", uc.getUsername(), uc.getLastAccess());
|
||||
return ErrorCode.ERR_TOKENTIMEOUT;
|
||||
} else {
|
||||
// 更新用户最后一次访问时间戳
|
||||
uc.setLastAccess(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
return ErrorCode.ERR_OK;
|
||||
|
@ -114,25 +119,30 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
|
|||
*/
|
||||
@Override
|
||||
public ErrorCode verifyUserLogin(String username, String token) {
|
||||
// 缓存中通过用户名查找当前用户
|
||||
Optional<UserAccountCache> findRet = userAccountCache.values().stream()
|
||||
.filter(userAccountCache -> username.equals(userAccountCache.getUsername()))
|
||||
.findFirst();
|
||||
|
||||
// 用户不存在
|
||||
if (!findRet.isPresent()) {
|
||||
return ErrorCode.ERR_LOGOUT;
|
||||
}
|
||||
|
||||
UserAccountCache uc = findRet.get();
|
||||
|
||||
// 如果token为空说明用户没有登录
|
||||
if (uc.getToken().length() == 0) {
|
||||
return ErrorCode.ERR_LOGOUT;
|
||||
}
|
||||
|
||||
// 判断用户访问时间间隔是否超时
|
||||
if ((System.currentTimeMillis() - uc.getLastAccess())
|
||||
>= ConstValue.GlobalConfigure.TOKEN_TIMEOUT_MS) {
|
||||
return ErrorCode.ERR_TOKENTIMEOUT;
|
||||
}
|
||||
|
||||
// 判断token是否正确
|
||||
if (!uc.getToken().equals(token)) {
|
||||
return ErrorCode.ERR_TOKENNOTFOUND;
|
||||
}
|
||||
|
@ -158,12 +168,14 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
|
|||
*/
|
||||
@Override
|
||||
public void cleanUserToken(String username) {
|
||||
// 根据用户名在缓存中查找用户
|
||||
Optional<UserAccountCache> findRet = userAccountCache.values().stream()
|
||||
.filter(userAccountCache -> username.equals(userAccountCache.getUsername()))
|
||||
.findFirst();
|
||||
|
||||
if (findRet.isPresent()) {
|
||||
UserAccountCache uc = findRet.get();
|
||||
// 缓存中删除当前用户
|
||||
userAccountCache.remove(uc.getToken());
|
||||
}
|
||||
}
|
||||
|
@ -184,6 +196,7 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
|
|||
UserAccountCache uc = findRet.get();
|
||||
return uc.getPwdErrTimes();
|
||||
} else {
|
||||
// 缓存不存在,说明用户未登录过或者已经过期,创建一个新用户缓存
|
||||
UserAccountCache uc = UserAccountCache.builder()
|
||||
.username(username)
|
||||
.token("")
|
||||
|
@ -223,7 +236,7 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
|
|||
*/
|
||||
@Override
|
||||
public String getUserToken(String username) throws NoSuchAlgorithmException {
|
||||
|
||||
// 根据用户名在缓存中查找用户
|
||||
Optional<UserAccountCache> findRet = userAccountCache.values().stream()
|
||||
.filter(userAccountCache -> username.equals(userAccountCache.getUsername()))
|
||||
.findFirst();
|
||||
|
@ -231,6 +244,7 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
|
|||
if (findRet.isPresent()) {
|
||||
UserAccountCache uc = findRet.get();
|
||||
|
||||
// token过期获取以前没有token,创建一个新token
|
||||
if ((System.currentTimeMillis() - uc.getLastAccess())
|
||||
>= ConstValue.GlobalConfigure.TOKEN_TIMEOUT_MS
|
||||
|| uc.getToken().length() == 0) {
|
||||
|
@ -244,6 +258,7 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
|
|||
|
||||
return uc.getToken();
|
||||
} else {
|
||||
// 用户不存在说明没有登陆过,创建一个新的用户缓存
|
||||
UserAccountCache uc = UserAccountCache.builder()
|
||||
.username(username)
|
||||
.token(createUserToken(username))
|
||||
|
@ -268,7 +283,7 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
|
|||
*/
|
||||
private String createUserToken(String username) throws NoSuchAlgorithmException {
|
||||
// 获取指定摘要算法的messageDigest对象
|
||||
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); // 此处的sha代表sha1
|
||||
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
|
||||
|
||||
String tokenKey = username +
|
||||
Convert.toStr(RANDOM_GEN.nextInt()) +
|
||||
|
|
|
@ -39,6 +39,7 @@ public class ProtocolReqDTO extends ProtocolDTO {
|
|||
* @throws JsonProcessingException the json processing exception
|
||||
*/
|
||||
public <T> T getRequestObject(Class<T> objType) throws JsonProcessingException {
|
||||
// 返回协议 MsgContent 字段内容
|
||||
return OBJECT_MAPPER.readValue(this.getMsgContent(), objType);
|
||||
}
|
||||
|
||||
|
@ -49,7 +50,6 @@ public class ProtocolReqDTO extends ProtocolDTO {
|
|||
*/
|
||||
@JsonIgnore
|
||||
public String getAuthToken() {
|
||||
|
||||
return ProtocolReqDTO.token;
|
||||
}
|
||||
|
||||
|
@ -67,26 +67,19 @@ public class ProtocolReqDTO extends ProtocolDTO {
|
|||
return err;
|
||||
}
|
||||
|
||||
// 是否有必要的HTTP Head字段
|
||||
if (headers == null) {
|
||||
return ErrorCode.ERR_MISSAUTHHEAD;
|
||||
}
|
||||
|
||||
try {
|
||||
ProtocolReqDTO.token = Objects.
|
||||
requireNonNull(headers.get("Authorization"))
|
||||
// 保持当前请求token内容
|
||||
ProtocolReqDTO.token = Objects.requireNonNull(headers.get("Authorization"))
|
||||
.get(0).replaceFirst("Bearer ", "");
|
||||
} catch (Exception ex) {
|
||||
return ErrorCode.ERR_MISSAUTHHEAD;
|
||||
}
|
||||
|
||||
if (ProtocolReqDTO.token.length() == 0) {
|
||||
return ErrorCode.ERR_MISSAUTHHEAD;
|
||||
}
|
||||
|
||||
if (this.getVer() < ConstValue.Protocol.VERSION) {
|
||||
return ErrorCode.ERR_VERSION;
|
||||
}
|
||||
|
||||
return ErrorCode.ERR_OK;
|
||||
}
|
||||
|
||||
|
@ -96,13 +89,14 @@ public class ProtocolReqDTO extends ProtocolDTO {
|
|||
* @return the error code
|
||||
*/
|
||||
public ErrorCode verifyRequest() {
|
||||
|
||||
// 校验版本
|
||||
if (this.getVer() < ConstValue.Protocol.VERSION) {
|
||||
return ErrorCode.ERR_VERSION;
|
||||
}
|
||||
|
||||
// 校验时间错
|
||||
if (GlobalVar.IS_CHECK_REQUEST_TIMEOUT
|
||||
&& System.currentTimeMillis() - this.getTimeStamp() >= ConstValue.Protocol.REQUEST_TIMEOUT_MS) {
|
||||
&& Math.abs(System.currentTimeMillis() - this.getTimeStamp()) >= ConstValue.Protocol.REQUEST_TIMEOUT_MS) {
|
||||
return ErrorCode.ERR_REQUESTTIMEOUT;
|
||||
}
|
||||
|
||||
|
|
|
@ -18,12 +18,15 @@ import java.util.Optional;
|
|||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* The type Dispose node manager.
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class DisposeNodeManagerImpl implements DisposeNodeManager {
|
||||
/**
|
||||
* The Dispose dev map.
|
||||
|
@ -50,8 +53,10 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager {
|
|||
*/
|
||||
@Override
|
||||
public void loadDisposeNodeFromDB() {
|
||||
// 清理缓存中的所有内容
|
||||
disposeDevMap.clear();
|
||||
|
||||
// 从数据库中加载所有设备
|
||||
List<DisposeDevice> devList = disposeDeviceMapper.selectAll();
|
||||
|
||||
if (devList == null) {
|
||||
|
@ -59,13 +64,17 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager {
|
|||
}
|
||||
|
||||
devList.forEach(v -> {
|
||||
// 对每个设备进行初始化
|
||||
DisposeEntryManager dp = DeviceRouter.deviceRouterFactory(v.getType(),
|
||||
v.getIpAddr(),
|
||||
IPAddrType.getIpAddrType(v.getIpAddr()));
|
||||
v.getIpAddr(), IPAddrType.getIpAddrType(v.getIpAddr()));
|
||||
|
||||
// 链接状态
|
||||
v.setLinkStatus(dp.getDeviceLinkStatus() ? 1 : 0);
|
||||
// 版本信息
|
||||
v.setVersion(dp.getVersion());
|
||||
// 设备信息
|
||||
v.setDevInfo(dp.getDeviceInfo());
|
||||
// 设备能力信息
|
||||
v.setDevCaps(dp.getDeviceCapacity());
|
||||
|
||||
disposeDevMap.put(v.getIpAddr(), v);
|
||||
|
@ -97,17 +106,20 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager {
|
|||
*/
|
||||
@Override
|
||||
public ErrorCode delDisposeDeviceByIp(String ipAddr) {
|
||||
// 判断该IP是否存在
|
||||
if (disposeDeviceMapper.isDeviceExistsByIp(ipAddr) == 0
|
||||
|| !disposeDevMap.containsKey(ipAddr)) {
|
||||
return ErrorCode.ERR_NOSUCHDEVICE;
|
||||
}
|
||||
|
||||
// 缓存中取出设备
|
||||
DisposeDevice dev = disposeDevMap.get(ipAddr);
|
||||
|
||||
if (dev == null) {
|
||||
return ErrorCode.ERR_NOSUCHDEVICE;
|
||||
}
|
||||
|
||||
// 删除数据库和缓存中内容
|
||||
disposeDeviceMapper.delDisposeDeviceByIp(ipAddr);
|
||||
disposeDevMap.remove(ipAddr);
|
||||
|
||||
|
@ -125,9 +137,11 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager {
|
|||
DisposeEntryManager dp;
|
||||
|
||||
try {
|
||||
// 获取处置设备硬件访问接口
|
||||
dp = DeviceRouter.deviceRouterFactory(dev.getType(),
|
||||
dev.getIpAddr(), IPAddrType.getIpAddrType(dev.getIpAddr()));
|
||||
|
||||
// 判断设备是否在线
|
||||
if (!dp.getDeviceLinkStatus()) {
|
||||
return new MReturnType<>(ErrorCode.ERR_NOSUCHDEVICE, String.valueOf(-1));
|
||||
}
|
||||
|
@ -135,26 +149,29 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager {
|
|||
return new MReturnType<>(ErrorCode.ERR_NOSUCHDEVICE, String.valueOf(-1));
|
||||
}
|
||||
|
||||
// 从缓存中获取设备信息
|
||||
DisposeDevice dbDev = disposeDeviceMapper.getDeviceByIp(dev.getIpAddr());
|
||||
|
||||
// 设备已经存在
|
||||
if (dbDev != null) {
|
||||
return new MReturnType<>(ErrorCode.ERR_DEVICEEXISTS, String.valueOf(dbDev.getId()));
|
||||
}
|
||||
|
||||
if (!dp.getDeviceLinkStatus()) {
|
||||
return new MReturnType<>(ErrorCode.ERR_NOSUCHDEVICE, String.valueOf(-1));
|
||||
}
|
||||
|
||||
// 获取设备能力
|
||||
if (dev.getDevCaps() == null) {
|
||||
// 更新能力
|
||||
dev.setDevCaps(dp.getDeviceCapacity());
|
||||
} else {
|
||||
// 添加能力
|
||||
dev.getDevCaps().addAll(dp.getDeviceCapacity());
|
||||
}
|
||||
|
||||
dev.setDevInfo(dp.getDeviceInfo());
|
||||
|
||||
// 保存设备信息到数据库中
|
||||
disposeDeviceMapper.addNewDisposeDevice(dev);
|
||||
|
||||
// 保存设备信息到缓存中
|
||||
disposeDevMap.put(dev.getIpAddr(), dev);
|
||||
return new MReturnType<>(ErrorCode.ERR_OK, dev.getId().toString());
|
||||
}
|
||||
|
@ -195,34 +212,6 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager {
|
|||
return findRet.orElse(null);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Is dispose device exists boolean.
|
||||
// *
|
||||
// * @param ipAddr the ip addr
|
||||
// * @return the boolean
|
||||
// */
|
||||
// private Boolean isDisposeDeviceExists(String ipAddr) {
|
||||
// return disposeDevMap.containsKey(ipAddr);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Add dispose device to cache.
|
||||
// *
|
||||
// * @param dev the dev
|
||||
// */
|
||||
// private void addDisposeDeviceToCache(DisposeDevice dev) {
|
||||
// disposeDevMap.put(dev.getIpAddr(), dev);
|
||||
// }
|
||||
//
|
||||
// private boolean devSupportCapacity(DisposeDevice dev, DeviceCapacity capacity) {
|
||||
// List<DisposeDeviceCapacity> capList = dev.getDevCaps()
|
||||
// .stream()
|
||||
// .filter(f -> f.getCapacity() == capacity.getCode())
|
||||
// .collect(Collectors.toList());
|
||||
//
|
||||
// return capList.size() > 0;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Is ip in range boolean.
|
||||
*
|
||||
|
@ -262,16 +251,20 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager {
|
|||
@Override
|
||||
public DisposeDevice getDisposeDevice(String ipAddr, DeviceCapacity capacity) {
|
||||
for (DisposeDevice dev : disposeDevMap.values()) {
|
||||
// 判断当前设备是否支持处置能力
|
||||
List<DisposeDeviceCapacity> capList = dev.getDevCaps()
|
||||
.stream()
|
||||
.filter(f -> f.getCapacity() == capacity.getCode())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 设备是否支持处置该IP的
|
||||
if(capList.stream().anyMatch(k -> isIpInRange(k, ipAddr))) {
|
||||
log.info("{} with {} used device {}:{}", ipAddr, capacity.getReadme(), dev.getId(), dev.getIpAddr());
|
||||
return dev;
|
||||
}
|
||||
}
|
||||
|
||||
log.error("No such device to process {} of {}", ipAddr, capacity.getReadme());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -115,7 +115,6 @@ public class TaskServiceImpl implements TaskService {
|
|||
try {
|
||||
log.error("load error:\n" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(v));
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
@ -141,6 +140,7 @@ public class TaskServiceImpl implements TaskService {
|
|||
}
|
||||
|
||||
if (disposeDevice == null) {
|
||||
log.error("No such device to dispose this task: devId:{}, disposeIp:{}, type:{}", task.getDeviceId(), task.getDisposeIp(), task.getType());
|
||||
return MReturnType.<ErrorCode, Long>builder()
|
||||
.firstParam(ErrorCode.ERR_NOSUCHDEVICE)
|
||||
.secondParam(-1L)
|
||||
|
@ -160,6 +160,7 @@ public class TaskServiceImpl implements TaskService {
|
|||
.collect(Collectors.toList());
|
||||
|
||||
if (taskList.size() > 0) {
|
||||
log.error("Same dispose task is running: devId:{}, disposeIp:{}, type:{}", task.getDeviceId(), task.getDisposeIp(), task.getType());
|
||||
return MReturnType.<ErrorCode, Long>builder()
|
||||
.firstParam(ErrorCode.ERR_TASKRUNNING)
|
||||
.secondParam(taskList.get(0).getId())
|
||||
|
@ -175,10 +176,10 @@ public class TaskServiceImpl implements TaskService {
|
|||
|
||||
ErrorCode err = taskCacheManager.addTask(cacheTask);
|
||||
|
||||
return MReturnType.<ErrorCode, Long>builder()
|
||||
.firstParam(err)
|
||||
.secondParam(task.getId())
|
||||
.build();
|
||||
log.info("Create new dispose task is running: devId:{}, disposeIp:{}, type:{} result {}",
|
||||
task.getDeviceId(), task.getDisposeIp(), task.getType(), err.getMsg());
|
||||
|
||||
return MReturnType.<ErrorCode, Long>builder().firstParam(err).secondParam(task.getId()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -195,6 +196,7 @@ public class TaskServiceImpl implements TaskService {
|
|||
TaskInfoDetail task = taskCacheManager.getTaskById(taskId);
|
||||
|
||||
if (task == null) {
|
||||
log.error("No such task: taskId:{}", taskId);
|
||||
return ErrorCode.ERR_NOSUCHDEVICE;
|
||||
}
|
||||
|
||||
|
@ -202,6 +204,7 @@ public class TaskServiceImpl implements TaskService {
|
|||
DisposeEntryManager dp = getDisposeDeviceHandle(task.getDisposeIp(), task.getType());
|
||||
|
||||
if (dp == null) {
|
||||
log.error("No such device to dispose this task: disposeId:{}, type:{}", task.getDisposeIp(), task.getType());
|
||||
return ErrorCode.ERR_NOSUCHDEVICE;
|
||||
}
|
||||
|
||||
|
@ -214,7 +217,7 @@ public class TaskServiceImpl implements TaskService {
|
|||
CompletableFuture.supplyAsync(() -> dp.runDispose(task.getDisposeIp(), DeviceCapacity.values()[task.getType()]))
|
||||
.whenComplete((v, ex) -> {
|
||||
if (ex != null) {
|
||||
log.error(ex.getMessage());
|
||||
log.error("Start task: taskId:{}, error:{}", taskId, ex.getMessage());
|
||||
// 执行任务失败恢复缓存中的任务状态
|
||||
taskCacheManager.upgradeTaskStatus(taskId, DisposeTaskStatus.TASK_NEW.getCode());
|
||||
} else {
|
||||
|
@ -225,6 +228,7 @@ public class TaskServiceImpl implements TaskService {
|
|||
} else {
|
||||
// 任务执行完成后更新数据库处置任务状态
|
||||
disposeTaskMapper.changeTaskCurrentStatus(taskId, DisposeTaskStatus.TASK_RUNNING.getCode());
|
||||
log.info("Start task finished: taskId:{}, disposeId:{}, type:{}", taskId, task.getDisposeIp(), task.getType());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -248,6 +252,7 @@ public class TaskServiceImpl implements TaskService {
|
|||
DisposeEntryManager dp = getDisposeDeviceHandle(task.getDisposeIp(), task.getType());
|
||||
|
||||
if (dp == null) {
|
||||
log.error("No such device to dispose this task: disposeId:{}, type:{}", task.getDisposeIp(), task.getType());
|
||||
return ErrorCode.ERR_NOSUCHDEVICE;
|
||||
}
|
||||
|
||||
|
@ -265,7 +270,7 @@ public class TaskServiceImpl implements TaskService {
|
|||
if (ex != null) {
|
||||
// 恢复缓存中任务状态到先前状态
|
||||
taskCacheManager.upgradeTaskStatus(taskId, prdStatus);
|
||||
log.error(ex.getMessage());
|
||||
log.error("Stop task: taskId:{}, error:{}", taskId, ex.getMessage());
|
||||
} else {
|
||||
if (v != ErrorCode.ERR_OK) {
|
||||
// 恢复缓存中任务状态到先前状态
|
||||
|
@ -274,10 +279,12 @@ public class TaskServiceImpl implements TaskService {
|
|||
} else {
|
||||
// 任务执行完成后更新数据库处置任务状态
|
||||
finishTask(taskId);
|
||||
log.info("Stop task finished: taskId:{}, disposeId:{}, type:{}", taskId, task.getDisposeIp(), task.getType());
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
log.error("No such task: taskId:{}", taskId);
|
||||
return ErrorCode.ERR_NOSUCHTASK;
|
||||
}
|
||||
|
||||
|
@ -292,12 +299,16 @@ public class TaskServiceImpl implements TaskService {
|
|||
*/
|
||||
@Override
|
||||
public ErrorCode finishTask(Long taskId) {
|
||||
// 设置缓存任务状态为完成状态
|
||||
ErrorCode err = taskCacheManager.upgradeTaskStatus(taskId, DisposeTaskStatus.TASK_FINISH.getCode());
|
||||
|
||||
if (err == ErrorCode.ERR_OK) {
|
||||
// 设置数据库任务状态为完成状态
|
||||
disposeTaskMapper.changeTaskCurrentStatus(taskId, DisposeTaskStatus.TASK_FINISH.getCode());
|
||||
// 移除缓存中的任务信息
|
||||
taskCacheManager.removeTask(taskId);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.dispose.service.impl;
|
|||
|
||||
import com.dispose.common.ConstValue;
|
||||
import com.dispose.common.ErrorCode;
|
||||
import com.dispose.common.GlobalVar;
|
||||
import com.dispose.config.DisposeConfigure;
|
||||
import com.dispose.manager.UserAccountCacheManager;
|
||||
import com.dispose.mapper.UserAccountMapper;
|
||||
import com.dispose.pojo.entity.UserAccount;
|
||||
|
@ -32,6 +32,12 @@ public class UserAccountServiceImpl implements UserAccountService {
|
|||
@Resource
|
||||
private UserAccountMapper userAccountMapper;
|
||||
|
||||
/**
|
||||
* The Dispose configure.
|
||||
*/
|
||||
@Resource
|
||||
private DisposeConfigure disposeConfigure;
|
||||
|
||||
/**
|
||||
* Auth token check error code.
|
||||
*
|
||||
|
@ -40,12 +46,12 @@ public class UserAccountServiceImpl implements UserAccountService {
|
|||
*/
|
||||
@Override
|
||||
public ErrorCode authTokenCheck(String token) {
|
||||
|
||||
if (GlobalVar.IS_VERIFY_TOKEN) {
|
||||
return userAccountCacheManager.verifyToken(token);
|
||||
// 判断当前配置是否需要校验 token
|
||||
if ("false".equals(disposeConfigure.getCheckRequestToken())) {
|
||||
return ErrorCode.ERR_OK;
|
||||
}
|
||||
|
||||
return ErrorCode.ERR_OK;
|
||||
return userAccountCacheManager.verifyToken(token);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -61,19 +67,21 @@ public class UserAccountServiceImpl implements UserAccountService {
|
|||
userAccountMapper.refreshLoginTime(username);
|
||||
UserAccount loginUser = userAccountMapper.getUserByName(username);
|
||||
|
||||
// 该用户是否存在
|
||||
if (loginUser == null) {
|
||||
return MReturnType.<ErrorCode, String>builder()
|
||||
.firstParam(ErrorCode.ERR_USERNOTFOUND)
|
||||
.build();
|
||||
log.error("User {} not exists", username);
|
||||
return MReturnType.<ErrorCode, String>builder().firstParam(ErrorCode.ERR_USERNOTFOUND).build();
|
||||
}
|
||||
|
||||
// 用户是否被锁定
|
||||
if (loginUser.getStatus() == ConstValue.UserAccountStatus.LOCKED) {
|
||||
return MReturnType.<ErrorCode, String>builder()
|
||||
.firstParam(ErrorCode.ERR_USERLOCK)
|
||||
.build();
|
||||
log.error("User {} is locked", username);
|
||||
return MReturnType.<ErrorCode, String>builder().firstParam(ErrorCode.ERR_USERLOCK).build();
|
||||
}
|
||||
|
||||
// 用户密码错误
|
||||
if (!loginUser.getPassword().equals(password)) {
|
||||
log.error("User {} password [{}] error", username, password);
|
||||
// 密码错误
|
||||
int errTimes = userAccountCacheManager.getUsrPwdErrTimes(username) + 1;
|
||||
|
||||
|
@ -82,26 +90,19 @@ public class UserAccountServiceImpl implements UserAccountService {
|
|||
|
||||
if (errTimes == ConstValue.GlobalConfigure.ALLOW_PWD_ERR_TIMES - 1) {
|
||||
// 提示用户即将锁定账户
|
||||
return MReturnType.<ErrorCode, String>builder()
|
||||
.firstParam(ErrorCode.ERR_PASSWORDMORE)
|
||||
.build();
|
||||
return MReturnType.<ErrorCode, String>builder().firstParam(ErrorCode.ERR_PASSWORDMORE).build();
|
||||
} else if (errTimes >= ConstValue.GlobalConfigure.ALLOW_PWD_ERR_TIMES) {
|
||||
// 锁定账户
|
||||
userAccountMapper.lockUserAccount(username);
|
||||
return MReturnType.<ErrorCode, String>builder()
|
||||
.firstParam(ErrorCode.ERR_USERLOCK)
|
||||
.build();
|
||||
return MReturnType.<ErrorCode, String>builder().firstParam(ErrorCode.ERR_USERLOCK).build();
|
||||
} else {
|
||||
return MReturnType.<ErrorCode, String>builder()
|
||||
.firstParam(ErrorCode.ERR_PASSWORD)
|
||||
.build();
|
||||
return MReturnType.<ErrorCode, String>builder().firstParam(ErrorCode.ERR_PASSWORD).build();
|
||||
}
|
||||
}
|
||||
|
||||
return MReturnType.<ErrorCode, String>builder()
|
||||
.firstParam(ErrorCode.ERR_OK)
|
||||
.secondParam(userAccountCacheManager.getUserToken(username))
|
||||
.build();
|
||||
.firstParam(ErrorCode.ERR_OK)
|
||||
.secondParam(userAccountCacheManager.getUserToken(username)).build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -119,6 +120,7 @@ public class UserAccountServiceImpl implements UserAccountService {
|
|||
return ErrorCode.ERR_USERNOTFOUND;
|
||||
}
|
||||
|
||||
// 注销
|
||||
ErrorCode err = userAccountCacheManager.verifyUserLogin(username, token);
|
||||
|
||||
if (err == ErrorCode.ERR_OK) {
|
||||
|
@ -139,7 +141,7 @@ public class UserAccountServiceImpl implements UserAccountService {
|
|||
|
||||
String username = userAccountCacheManager.getUsernameByToken(token);
|
||||
|
||||
if (username != null) {
|
||||
if (username != null && username.length() > 0) {
|
||||
return userAccountMapper.getUserByName(username);
|
||||
}
|
||||
|
||||
|
|
|
@ -34,9 +34,11 @@ public class DeviceManagerTask {
|
|||
DisposeEntryManager dp = DeviceRouter.deviceRouterFactory(v.getType(),
|
||||
v.getIpAddr(),
|
||||
IPAddrType.getIpAddrType(v.getIpAddr()));
|
||||
|
||||
// 自动更新设备心跳状态
|
||||
v.setLinkStatus(dp.getDeviceLinkStatus() ? 1 : 0);
|
||||
// 自动读取设备系统版本信息
|
||||
v.setVersion(dp.getVersion());
|
||||
// 自动更新系统信息
|
||||
v.setDevInfo(dp.getDeviceInfo());
|
||||
});
|
||||
}
|
||||
|
|
|
@ -35,20 +35,24 @@ public class TaskManagerTask {
|
|||
* Task runtime manager.
|
||||
*/
|
||||
@Async("bizExecutor")
|
||||
@Scheduled(fixedDelay = 500)
|
||||
@Scheduled(fixedDelay = 1000)
|
||||
public void taskRuntimeManager() {
|
||||
Iterator it = taskCacheManager.getAllTask().iterator();
|
||||
Iterator<TaskInfoDetail> it = taskCacheManager.getAllTask().iterator();
|
||||
|
||||
// 由于可能删除列表中的项目,所以使用迭代器
|
||||
while (it.hasNext()) {
|
||||
TaskInfoDetail taskData = (TaskInfoDetail) it.next();
|
||||
TaskInfoDetail taskData = it.next();
|
||||
|
||||
// 判断是否存在正在运行的过期的任务,如果存在则结束该任务
|
||||
if(taskService.taskIsExpired(taskData) && taskService.taskIsRunning(taskData)) {
|
||||
log.info("Finish expired task {}:{} begin at {}",
|
||||
taskData.getId(), taskData.getDisposeIp(), taskData.getBeginTime());
|
||||
taskService.stopTask(taskData.getId());
|
||||
it.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
// 判断是否有新建任务,如果有的话启动新建的处置任务
|
||||
if (taskData.getCurrentStatus() == DisposeTaskStatus.TASK_NEW.getCode()) {
|
||||
log.info("Start task {}:{} of {}",
|
||||
taskData.getId(), taskData.getDisposeIp(), taskData.getBeginTime());
|
||||
|
|
|
@ -53,7 +53,6 @@ public class InitTestEnvironment {
|
|||
public static void initVirtualDevice() {
|
||||
GlobalVar.USED_VIRTUAL_DISPOSE_MODE = true;
|
||||
GlobalVar.IS_CHECK_REQUEST_TIMEOUT = true;
|
||||
GlobalVar.IS_VERIFY_TOKEN = true;
|
||||
|
||||
log.warn("Current Used Virtual Dispose Device");
|
||||
}
|
||||
|
@ -91,11 +90,11 @@ public class InitTestEnvironment {
|
|||
ErrorCode err = ErrorCode.ERR_OK;
|
||||
|
||||
JSONObject jsonObject = new JSONObject(data);
|
||||
Iterator it = jsonObject.keys();
|
||||
Iterator<String> it = jsonObject.keys();
|
||||
System.out.println();
|
||||
while (it.hasNext()) {
|
||||
// 获得key
|
||||
String key = (String) it.next();
|
||||
String key = it.next();
|
||||
String value = jsonObject.getString(key);
|
||||
System.out.println("key: " + key + ",value:" + value);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue