use of cn.jiangzeyin.common.validator.ValidatorItem in project Jpom by dromara.
the class OutGivingProjectController method getItemData.
@RequestMapping(value = "getItemData.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String getItemData(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "id error") String id) {
HttpServletRequest request = getRequest();
String workspaceId = outGivingServer.getCheckUserWorkspace(request);
OutGivingModel outGivingServerItem = outGivingServer.getByKey(id, request);
Objects.requireNonNull(outGivingServerItem, "没有数据");
List<OutGivingNodeProject> outGivingNodeProjectList = outGivingServerItem.outGivingNodeProjectList();
List<JSONObject> collect = outGivingNodeProjectList.stream().map(outGivingNodeProject -> {
NodeModel nodeModel = nodeService.getByKey(outGivingNodeProject.getNodeId());
JSONObject jsonObject = new JSONObject();
jsonObject.put("nodeId", outGivingNodeProject.getNodeId());
jsonObject.put("projectId", outGivingNodeProject.getProjectId());
jsonObject.put("nodeName", nodeModel.getName());
jsonObject.put("id", BaseNodeModel.fullId(workspaceId, outGivingNodeProject.getNodeId(), outGivingNodeProject.getProjectId()));
// set projectStatus property
// NodeModel node = nodeService.getItem(outGivingNodeProject.getNodeId());
// Project Status: data.pid > 0 means running
JSONObject projectStatus = JsonMessage.toJson(200, "success");
if (nodeModel.isOpenStatus()) {
JSONObject projectInfo = null;
try {
projectInfo = projectInfoCacheService.getItem(nodeModel, outGivingNodeProject.getProjectId());
projectStatus = NodeForward.requestBySys(nodeModel, NodeUrl.Manage_GetProjectStatus, "id", outGivingNodeProject.getProjectId()).toJson();
} catch (Exception e) {
jsonObject.put("errorMsg", "error " + e.getMessage());
}
if (projectInfo != null) {
jsonObject.put("projectName", projectInfo.getString("name"));
}
} else {
jsonObject.put("errorMsg", "节点未启用");
}
JSONObject data = projectStatus.getJSONObject("data");
if (data != null && data.getInteger("pId") != null) {
jsonObject.put("projectStatus", data.getIntValue("pId") > 0);
} else {
jsonObject.put("projectStatus", false);
}
jsonObject.put("outGivingStatus", outGivingNodeProject.getStatusMsg());
jsonObject.put("outGivingResult", outGivingNodeProject.getResult());
jsonObject.put("lastTime", outGivingNodeProject.getLastOutGivingTime());
return jsonObject;
}).collect(Collectors.toList());
return JsonMessage.getString(200, "", collect);
}
use of cn.jiangzeyin.common.validator.ValidatorItem in project Jpom by dromara.
the class NodeUpdateController method confirmFastInstall.
@GetMapping(value = "confirm_fast_install.json", produces = MediaType.APPLICATION_JSON_VALUE)
public String confirmFastInstall(@ValidatorItem String id, @ValidatorItem String ip, int port) {
JSONObject receiveCache = NodeInfoController.getReceiveCache(id);
Assert.notNull(receiveCache, "没有对应的缓存信息");
JSONArray jsonArray = receiveCache.getJSONArray("canUseNode");
Assert.notEmpty(jsonArray, "没有对应的缓存信息:-1");
Optional<NodeModel> any = jsonArray.stream().map(o -> {
if (o instanceof NodeModel) {
return (NodeModel) o;
}
JSONObject jsonObject = (JSONObject) o;
return jsonObject.toJavaObject(NodeModel.class);
}).filter(nodeModel -> StrUtil.equals(nodeModel.getUrl(), StrUtil.format("{}:{}", ip, port))).findAny();
Assert.state(any.isPresent(), "ip 地址信息不正确");
NodeModel nodeModel = any.get();
try {
nodeService.testNode(nodeModel);
} catch (Exception e) {
DefaultSystemLog.getLog().warn("测试结果:{} {}", nodeModel.getUrl(), e.getMessage());
return JsonMessage.getString(500, "节点连接失败:" + e.getMessage());
}
// 插入
boolean exists = nodeService.existsByUrl(nodeModel.getUrl(), nodeModel.getWorkspaceId(), null);
Assert.state(!exists, "对应的节点已经存在拉:" + nodeModel.getUrl());
nodeService.insert(nodeModel);
// 更新结果
receiveCache.put("type", "success");
return JsonMessage.getString(200, "安装成功", NodeInfoController.listReceiveCache(null));
}
use of cn.jiangzeyin.common.validator.ValidatorItem in project Jpom by dromara.
the class NodeInfoController method receivePush.
/**
* 接收节点推送的信息
* <p>
* yum install -y wget && wget -O install.sh https://dromara.gitee.io/jpom/docs/install.sh && bash install.sh Agent jdk
* --auto-push-to-server http://127.0.0.1:3000/api/node/receive_push?token=462a47b8fba8da1f824370bb9fcdc01aa1a0fe20&workspaceId=DEFAULT
*
* @return json
*/
@RequestMapping(value = ServerOpenApi.RECEIVE_PUSH, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@NotLogin
public String receivePush(@ValidatorItem(msg = "token empty") String token, @ValidatorItem(msg = "ips empty") String ips, @ValidatorItem(msg = "loginName empty") String loginName, @ValidatorItem(msg = "loginPwd empty") String loginPwd, @ValidatorItem(msg = "workspaceId empty") String workspaceId, @ValidatorItem(value = ValidatorRule.NUMBERS, msg = "port error") int port, String ping) {
Assert.state(StrUtil.equals(token, JpomManifest.getInstance().randomIdSign()), "token error");
boolean exists = workspaceService.exists(new WorkspaceModel(workspaceId));
Assert.state(exists, "workspaceId error");
String sha1Id = SecureUtil.sha1(ips);
//
List<String> ipsList = StrUtil.split(ips, StrUtil.COMMA);
String clientIp = getClientIP();
if (!ipsList.contains(clientIp)) {
ipsList.add(clientIp);
}
List<String> canUseIps = ipsList.stream().filter(s -> this.testIpProt(s, ping)).collect(Collectors.toList());
List<NodeModel> canUseNode = canUseIps.stream().map(s -> {
NodeModel model = NodeInfoController.this.createModel(s, loginName, loginPwd, port, workspaceId);
try {
nodeService.testNode(model);
} catch (Exception e) {
DefaultSystemLog.getLog().warn("测试结果:{} {}", model.getUrl(), e.getMessage());
return null;
}
return model;
}).filter(Objects::nonNull).collect(Collectors.toList());
// 只返回能通的 IP
canUseIps = canUseNode.stream().map(NodeModel::getName).collect(Collectors.toList());
int size1 = CollUtil.size(canUseNode);
//
JSONObject jsonObject = new JSONObject();
jsonObject.put("allIp", ipsList);
jsonObject.put("canUseIp", canUseIps);
jsonObject.put("port", port);
jsonObject.put("id", sha1Id);
jsonObject.put("canUseNode", canUseNode);
//
exists = false;
for (NodeModel nodeModel : canUseNode) {
if (nodeService.existsByUrl(nodeModel.getUrl(), nodeModel.getWorkspaceId(), null)) {
// 存在
jsonObject.put("type", "exists");
exists = true;
break;
}
}
if (!exists) {
if (size1 == 1) {
// 只有一个 ip 可以使用
// 添加插件端
NodeModel first = CollUtil.getFirst(canUseNode);
nodeService.insertNotFill(first);
jsonObject.put("type", "success");
} else {
jsonObject.put("type", size1 == 0 ? "canUseIpEmpty" : "multiIp");
}
}
CACHE_RECEIVE_PUSH.put(sha1Id, jsonObject);
return JsonMessage.getString(200, "done", jsonObject);
}
Aggregations