提交 84c60683 编写于 作者: hlwwx's avatar hlwwx

'add'

上级 e0fa54ba
package com.x.program.center.jaxrs;
import javax.servlet.annotation.WebFilter;
import com.x.base.core.project.jaxrs.CipherManagerJaxrsFilter;
@WebFilter(urlPatterns = "/jaxrs/command/*", asyncSupported = true)
public class CommandJaxrsFilter extends CipherManagerJaxrsFilter {
}
package com.x.program.center.jaxrs.command;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.JsonElement;
import com.x.base.core.project.annotation.FieldDescribe;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.gson.GsonPropertyObject;
import com.x.base.core.project.gson.XGsonBuilder;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.Crypto;
/*执行服务器命令*/
public class ActionCommand extends BaseAction {
private static Logger logger = LoggerFactory.getLogger(ActionCommand.class);
ActionResult<Wo> execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
ActionResult<Wo> result = new ActionResult<>();
Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
String ctl = wi.getCtl();
String nodeName = wi.getNodeName() ;
int nodePort =Integer.parseInt(wi.getNodePort());
Wo wo = executeCommand(ctl, nodeName, nodePort);
result.setData(wo);
return result;
}
synchronized private Wo executeCommand(String ctl , String nodeName ,int nodePort) throws Exception{
Wo wo = new Wo();
wo.setNode(nodeName);
wo.setStatus("success");
try (Socket socket = new Socket(nodeName, nodePort)) {
socket.setKeepAlive(true);
socket.setSoTimeout(5000);
try (DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
DataInputStream dis = new DataInputStream(socket.getInputStream())){
Map<String, Object> commandObject = new HashMap<>();
commandObject.put("command", "command:"+ ctl);
commandObject.put("credential", Crypto.rsaEncrypt("o2@", Config.publicKey()));
dos.writeUTF(XGsonBuilder.toJson(commandObject));
dos.flush();
}
} catch (Exception ex) {
wo.setStatus("fail");
logger.warn("socket dispatch executeCommand to {}:{} error={}", nodeName, nodePort, ex.getMessage());
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
wo.setTime(df.format(new Date()));
return wo;
}
public static class Wi extends GsonPropertyObject{
@FieldDescribe("命令名称")
private String ctl;
@FieldDescribe("服务器地址")
private String nodeName;
@FieldDescribe("服务端口")
private String nodePort;
public String getCtl() {
return ctl;
}
public void setCtl(String ctl) {
this.ctl = ctl;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public String getNodePort() {
return nodePort;
}
public void setNodePort(String nodePort) {
this.nodePort = nodePort;
}
}
public static class Wo extends GsonPropertyObject {
@FieldDescribe("执行时间")
private String time;
@FieldDescribe("执行结束")
private String status;
@FieldDescribe("执行服务器")
private String node;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getNode() {
return node;
}
public void setNode(String node) {
this.node = node;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
}
package com.x.program.center.jaxrs.command;
import java.util.ArrayList;
import java.util.List;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.config.Nodes;
import com.x.base.core.project.exception.ExceptionEntityNotExist;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.config.Node;
/*获取服器信息列表*/
class ActionGet extends BaseAction {
ActionResult<Wo> execute(EffectivePerson effectivePerson, String id) throws Exception {
ActionResult<Wo> result = new ActionResult<>();
Nodes nodes = Config.nodes();
if (null == nodes) {
throw new ExceptionEntityNotExist(id, "Nodes");
}
List<NodeInfo> nodeInfoList = new ArrayList<>();
for(String key:nodes.keySet()){
NodeInfo nodeInfo = new NodeInfo();
nodeInfo.setNodeAddress(key);
nodeInfo.setNode(nodes.get(key));
nodeInfoList.add(nodeInfo);
}
Wo wo = new Wo();
wo.setNodeList(nodeInfoList);
result.setData(wo);
return result;
}
public class NodeInfo{
private String nodeAddress;
private Node node;
public String getNodeAddress() {
return nodeAddress;
}
public void setNodeAddress(String nodeAddress) {
this.nodeAddress = nodeAddress;
}
public Node getNode() {
return node;
}
public void setNode(Node node) {
this.node = node;
}
}
public static class Wo {
private List<NodeInfo> nodeList;
public List<NodeInfo> getNodeList() {
return nodeList;
}
public void setNodeList(List<NodeInfo> nodeList) {
this.nodeList = nodeList;
}
}
}
package com.x.program.center.jaxrs.command;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import com.x.base.core.project.annotation.FieldDescribe;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.config.Nodes;
import com.x.base.core.project.gson.GsonPropertyObject;
import com.x.base.core.project.gson.XGsonBuilder;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.Crypto;
public class ActionUploadFile extends BaseAction {
private static Logger logger = LoggerFactory.getLogger(CommandAction.class);
ActionResult<Wo> execute(EffectivePerson effectivePerson, String ctl, String nodeName , String nodePort, InputStream fileInputStream, FormDataContentDisposition disposition) throws Exception {
ActionResult<Wo> result = new ActionResult<>();
Wo wo = null;
if(nodeName.equalsIgnoreCase("*")) {
Nodes nodes = Config.nodes();
for (String node : nodes.keySet()){
if(nodes.get(node).getApplication().getEnable() || nodes.get(node).getCenter().getEnable()){
wo = executeCommand( ctl, node , nodes.get(node).nodeAgentPort(), fileInputStream, disposition);
}
}
}else {
wo = executeCommand( ctl, nodeName , Integer.parseInt(nodePort), fileInputStream, disposition);
}
result.setData(wo);
return result;
}
synchronized private Wo executeCommand(String ctl , String nodeName ,int nodePort,InputStream fileInputStream, FormDataContentDisposition disposition) throws Exception{
Wo wo = new Wo();
wo.setNode(nodeName);
wo.setStatus("success");
try (Socket socket = new Socket(nodeName, nodePort)) {
socket.setKeepAlive(true);
socket.setSoTimeout(5000);
DataOutputStream dos = null;
DataInputStream dis = null;
try {
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
Map<String, Object> commandObject = new HashMap<>();
commandObject.put("command", "redeploy:"+ ctl);
commandObject.put("credential", Crypto.rsaEncrypt("o2@", Config.publicKey()));
dos.writeUTF(XGsonBuilder.toJson(commandObject));
dos.flush();
dos.writeUTF(disposition.getFileName());
dos.flush();
logger.info("发送文件starting.......");
byte[] bytes = new byte[1024];
int length =0;
while((length = fileInputStream.read(bytes, 0, bytes.length)) != -1) {
dos.write(bytes, 0, length);
dos.flush();
}
logger.info("发送文件end.......");
}finally {
dos.close();
dis.close();
fileInputStream.close();
}
} catch (Exception ex) {
wo.setStatus("fail");
//logger.warn("socket dispatch executeCommand to {}:{} error={}", nodeName, nodePort, ex.getMessage());
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
wo.setTime(df.format(new Date()));
return wo;
}
public static class Wi extends GsonPropertyObject{
private String ctl;
private String nodeName;
private String nodePort;
public String getCtl() {
return ctl;
}
public void setCtl(String ctl) {
this.ctl = ctl;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public String getNodePort() {
return nodePort;
}
public void setNodePort(String nodePort) {
this.nodePort = nodePort;
}
}
public static class Wo extends GsonPropertyObject {
@FieldDescribe("执行时间")
private String time;
@FieldDescribe("执行结束")
private String status;
@FieldDescribe("执行服务器")
private String node;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getNode() {
return node;
}
public void setNode(String node) {
this.node = node;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
}
package com.x.program.center.jaxrs.command;
import com.x.base.core.project.cache.ApplicationCache;
import com.x.base.core.project.gson.GsonPropertyObject;
import com.x.base.core.project.jaxrs.StandardJaxrsAction;
import net.sf.ehcache.Ehcache;
abstract class BaseAction extends StandardJaxrsAction {
public static Ehcache cacheLog = ApplicationCache.instance().getCache(CacheLogObject.class);
public static class CacheLogObject extends GsonPropertyObject {
private String userToken;
private String node;
private long lastPoint;
public long getLastPoint() {
return lastPoint;
}
public void setLastPoint(long lastPoint) {
this.lastPoint = lastPoint;
}
public String getNode() {
return node;
}
public void setNode(String node) {
this.node = node;
}
public String getUserToken() {
return userToken;
}
public void setUserToken(String userToken) {
this.userToken = userToken;
}
}
}
package com.x.program.center.jaxrs.command;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import com.google.gson.JsonElement;
import com.x.base.core.project.annotation.FieldDescribe;
import com.x.base.core.project.annotation.JaxrsDescribe;
import com.x.base.core.project.annotation.JaxrsMethodDescribe;
import com.x.base.core.project.annotation.JaxrsParameterDescribe;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.http.HttpMediaType;
import com.x.base.core.project.jaxrs.ResponseFactory;
import com.x.base.core.project.jaxrs.StandardJaxrsAction;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
@JaxrsDescribe("命令")
@Path("command")
public class CommandAction<Wo> extends StandardJaxrsAction {
private static Logger logger = LoggerFactory.getLogger(CommandAction.class);
@JaxrsMethodDescribe(value = "执行服务器命令", action = ActionCommand.class)
@POST
@Path("execute")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.APPLICATION_JSON)
public void executeCommand(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request, JsonElement jsonElement) {
EffectivePerson effectivePerson = this.effectivePerson(request);
ActionResult<ActionCommand.Wo> result = new ActionResult<>();
try {
result = new ActionCommand().execute(effectivePerson, jsonElement);
} catch (Exception e) {
e.printStackTrace();
result.error(e);
}
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
}
@JaxrsMethodDescribe(value = "获取所有服务器信息", action = ActionGet.class)
@GET
@Path("nodeInfoList")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.APPLICATION_JSON)
public void getNodeInfoList(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request) {
EffectivePerson effectivePerson = this.effectivePerson(request);
ActionResult<ActionGet.Wo> result = new ActionResult<>();
try {
result = (ActionResult<ActionGet.Wo>) new ActionGet().execute(effectivePerson, "");
} catch (Exception e) {
e.printStackTrace();
result.error(e);
}
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
}
@JaxrsMethodDescribe(value = "上传customJar,customWar,storeJar,storeWar包并自动部署", action = ActionUploadFile.class)
@POST
@Path("upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
public void upload(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
@JaxrsParameterDescribe("命令名称(customJar|customWar|storeJar|storeWar)") @FormDataParam("ctl") String ctl,
@JaxrsParameterDescribe("服务器地址(*代表多台应用服务器)") @FormDataParam("nodeName") String nodeName,
@JaxrsParameterDescribe("服务端口") @FormDataParam("nodePort") String nodePort,
@JaxrsParameterDescribe("附件名称") @FormDataParam(FILENAME_FIELD) String fileName,
@JaxrsParameterDescribe("文件内容") @FormDataParam(FILE_FIELD) InputStream fileInputStream,
@JaxrsParameterDescribe("上传文件") @FormDataParam(FILE_FIELD) final FormDataContentDisposition disposition) {
ActionResult<ActionUploadFile.Wo> result = new ActionResult<>();
EffectivePerson effectivePerson = this.effectivePerson(request);
try {
result = new ActionUploadFile().execute(effectivePerson, ctl, nodeName, nodePort, fileInputStream, disposition);
} catch (Exception e) {
logger.error(e, effectivePerson, request, null);
result.error(e);
}
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册