![JAR search and dependency download from the Maven repository](/logo.png)
org.dromara.jpom.socket.AgentWebSocketConsoleHandle Maven / Gradle / Ivy
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Code Technology Studio
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.dromara.jpom.socket;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.keepbx.jpom.model.JsonMessage;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.dromara.jpom.common.Const;
import org.dromara.jpom.common.commander.CommandOpResult;
import org.dromara.jpom.model.data.NodeProjectInfoModel;
import org.dromara.jpom.service.manage.ConsoleService;
import org.dromara.jpom.service.manage.ProjectInfoService;
import org.dromara.jpom.system.AgentConfig;
import org.dromara.jpom.util.FileSearchUtil;
import org.dromara.jpom.util.SocketSessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
/**
* 插件端,控制台socket
*
* @author bwcx_jzy
* @since 2019/4/16
*/
@ServerEndpoint(value = "/console")
@Component
@Slf4j
public class AgentWebSocketConsoleHandle extends BaseAgentWebSocketHandle {
private static ProjectInfoService projectInfoService;
private static ConsoleService consoleService;
private static AgentConfig.ProjectConfig.LogConfig logConfig;
@Autowired
public void init(ProjectInfoService projectInfoService,
ConsoleService consoleService,
AgentConfig agentConfig) {
AgentWebSocketConsoleHandle.projectInfoService = projectInfoService;
AgentWebSocketConsoleHandle.consoleService = consoleService;
AgentWebSocketConsoleHandle.logConfig = agentConfig.getProject().getLog();
}
@OnOpen
public void onOpen(Session session) {
try {
if (super.checkAuthorize(session)) {
return;
}
String projectId = super.getParameters(session, "projectId");
String copyId = super.getParameters(session, "copyId");
copyId = StrUtil.nullToDefault(copyId, StrUtil.EMPTY);
// 判断项目
if (!Const.SYSTEM_ID.equals(projectId)) {
NodeProjectInfoModel nodeProjectInfoModel = this.checkProject(projectId, copyId, session);
if (nodeProjectInfoModel == null) {
return;
}
//
SocketSessionUtil.send(session, "连接成功:" + nodeProjectInfoModel.getName() + (StrUtil.isEmpty(copyId) ? StrUtil.EMPTY : StrUtil.AT + copyId));
}
} catch (Exception e) {
log.error("socket 错误", e);
try {
SocketSessionUtil.send(session, JsonMessage.getString(500, "系统错误!"));
session.close();
} catch (IOException e1) {
log.error(e1.getMessage(), e1);
}
}
}
/**
* 静默消息不做过多处理
*
* @param consoleCommandOp 操作
* @param session 回话
* @return true
*/
private boolean silentMsg(ConsoleCommandOp consoleCommandOp, Session session) {
if (consoleCommandOp == ConsoleCommandOp.heart) {
return true;
}
// if (consoleCommandOp == ConsoleCommandOp.top) {
// TopManager.addMonitor(session);
// return true;
// }
return false;
}
private NodeProjectInfoModel checkProject(String projectId, String copyId, Session session) throws IOException {
NodeProjectInfoModel nodeProjectInfoModel = projectInfoService.getItem(projectId);
if (nodeProjectInfoModel == null) {
SocketSessionUtil.send(session, "没有对应项目:" + projectId);
session.close();
return null;
}
// 判断副本集
if (StrUtil.isNotEmpty(copyId)) {
NodeProjectInfoModel.JavaCopyItem copyItem = nodeProjectInfoModel.findCopyItem(copyId);
if (copyItem == null) {
SocketSessionUtil.send(session, "获取项目信息错误,没有对应副本:" + copyId);
session.close();
return null;
}
}
return nodeProjectInfoModel;
}
@OnMessage
public void onMessage(String message, Session session) throws Exception {
JSONObject json = JSONObject.parseObject(message);
String op = json.getString("op");
ConsoleCommandOp consoleCommandOp = ConsoleCommandOp.valueOf(op);
if (silentMsg(consoleCommandOp, session)) {
return;
}
String projectId = json.getString("projectId");
String copyId = json.getString("copyId");
NodeProjectInfoModel nodeProjectInfoModel = this.checkProject(projectId, copyId, session);
if (nodeProjectInfoModel == null) {
return;
}
runMsg(consoleCommandOp, session, nodeProjectInfoModel, copyId, json);
}
private void runMsg(ConsoleCommandOp consoleCommandOp, Session session, NodeProjectInfoModel nodeProjectInfoModel, String copyId, JSONObject reqJson) throws Exception {
//
NodeProjectInfoModel.JavaCopyItem copyItem = nodeProjectInfoModel.findCopyItem(copyId);
JsonMessage
© 2015 - 2025 Weber Informatics LLC | Privacy Policy