作者 王勇

对发送的消息的格式与数据进行校验

正在显示 37 个修改的文件 包含 825 行增加65 行删除
... ... @@ -115,6 +115,10 @@ management:
shutdown:
enabled: true
path:
# 文件夹的位置,就一个文件夹
dir: xml
# 基础信息配置
info:
version: 1.0
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sunyo.wlpt.message.bus.service</groupId>
<artifactId>message_bus_service</artifactId>
... ... @@ -138,6 +138,13 @@
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- dom4j解析xml -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
... ...
package com.sunyo.wlpt.message.bus.service.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author 子诚
* Description:
* 时间:2020/7/28 15:18
*/
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Value("${path.dir}")
private String dir;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/bus/**").addResourceLocations("file:" + dir + "/");
WebMvcConfigurer.super.addResourceHandlers(registry);
}
}
... ...
... ... @@ -4,12 +4,12 @@ import com.github.pagehelper.PageInfo;
import com.sunyo.wlpt.message.bus.service.domain.*;
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
import com.sunyo.wlpt.message.bus.service.service.*;
import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;
/**
... ... @@ -71,7 +71,8 @@ public class MessageNoteController {
@DateTimeFormat(pattern = "yyyy-MM-dd")
@RequestParam(value = "receiveTime", required = false) Date receiveTime,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize)
{
// 获取查询参数
MessageNote messageNote = MessageNote.builder()
.username(username)
... ... @@ -97,8 +98,8 @@ public class MessageNoteController {
* @return {@link ResultJson}
*/
@DeleteMapping("/delete")
public ResultJson deleteMessageNote(@RequestBody MessageNote messageNote) {
public ResultJson deleteMessageNote(@RequestBody MessageNote messageNote)
{
return messageNoteService.deleteByPrimaryKey(messageNote.getId()) > 0
? new ResultJson<>("200", "删除-消息收发记录,成功")
: new ResultJson<>("500", "删除-消息收发记录,失败");
... ... @@ -111,8 +112,8 @@ public class MessageNoteController {
* @return {@link ResultJson}
*/
@GetMapping("/batchRemove")
public ResultJson batchRemoveMessageNote(String ids) {
public ResultJson batchRemoveMessageNote(String ids)
{
return messageNoteService.deleteByPrimaryKey(ids) > 0
? new ResultJson<>("200", "删除-消息收发记录,成功")
: new ResultJson<>("500", "删除-消息收发记录,失败");
... ... @@ -125,8 +126,8 @@ public class MessageNoteController {
* @return {@link ResultJson}
*/
@PutMapping("/update")
public ResultJson updateMessageNote(@RequestBody MessageNote messageNote) {
public ResultJson updateMessageNote(@RequestBody MessageNote messageNote)
{
return messageNoteService.updateByPrimaryKeySelective(note_fillName(messageNote)) > 0
? new ResultJson<>("200", "编辑-消息收发记录,成功")
: new ResultJson<>("500", "编辑-消息收发记录,失败");
... ... @@ -139,10 +140,9 @@ public class MessageNoteController {
* @return {@link ResultJson}
*/
@PostMapping("/insert")
public ResultJson insertMessageNote(@RequestBody MessageNote messageNote) {
// 设置id
messageNote.setId(IdUtils.generateId());
return messageNoteService.insertSelective(note_fillName(messageNote)) > 0
public ResultJson insertMessageNote(@RequestBody @NotNull MessageNote messageNote)
{
return messageNoteService.insertSelective(messageNote) > 0
? new ResultJson<>("200", "编辑-消息收发记录,成功")
: new ResultJson<>("500", "编辑-消息收发记录,失败");
}
... ... @@ -153,10 +153,11 @@ public class MessageNoteController {
*/
@Scheduled(cron = "0 0 4 * * ? ")
@GetMapping("/autoDelete")
public void autoDeleteMessageNote() {
public void autoDeleteMessageNote()
{
// 类型
String deleteType = "message_note";
// 首先获取可自主设置的默认时间
// 获取时间
SchedulingDelete schedulingDelete = schedulingDeleteService.selectByType(deleteType);
Integer deleteTime = schedulingDelete.getDeleteTime();
// 定时自动删除
... ... @@ -169,7 +170,9 @@ public class MessageNoteController {
* @param messageNote {@link MessageNote}
* @return {@link MessageNote}
*/
public MessageNote note_fillName(MessageNote messageNote) {
public MessageNote note_fillName(MessageNote messageNote)
{
// 填充,发送内容(编辑 or 新增)
messageNote.setSendContent(messageNote.getAlias_sendContent().getBytes());
... ... @@ -186,8 +189,8 @@ public class MessageNoteController {
messageNote.setExchangeName(busExchange.getExchangeName());
// 填充,队列名称
BusQueue busQueue = busQueueService.selectByPrimaryKey(messageNote.getQueueId());
messageNote.setQueueName(busQueue.getQueueName());
// BusQueue busQueue = busQueueService.selectByPrimaryKey(messageNote.getQueueId());
// messageNote.setQueueName(busQueue.getQueueName());
// 填充,路由键名称
RoutingKey routingKey = routingKeyService.selectByPrimaryKey(messageNote.getRoutingKeyId());
... ...
package com.sunyo.wlpt.message.bus.service.controller;
import com.sunyo.wlpt.message.bus.service.rabbit.test.TestProduct;
import com.sunyo.wlpt.message.bus.service.domain.XmlData;
import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType;
import com.sunyo.wlpt.message.bus.service.rabbit.utils.DirectUtils;
import com.sunyo.wlpt.message.bus.service.rabbit.utils.RabbitUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
import com.sunyo.wlpt.message.bus.service.service.UserMessageBindingService;
import com.sunyo.wlpt.message.bus.service.utils.XmlUtils;
import org.dom4j.DocumentException;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeoutException;
/**
... ... @@ -21,9 +25,11 @@ import java.util.concurrent.TimeoutException;
@RequestMapping("bus/rabbit")
@RestController
public class RabbitController {
@Resource
private UserMessageBindingService userMessageBindingService;
@Resource
private TestProduct testProduct;
private XmlUtils xmlUtils;
@Resource
private RabbitUtils rabbitUtils;
... ... @@ -31,18 +37,70 @@ public class RabbitController {
@Resource
private DirectUtils directUtils;
@GetMapping("/test/product")
public void product() throws IOException, TimeoutException
{
rabbitUtils.createExchange("E_zicheng");
rabbitUtils.createQueue("Q_zicheng");
rabbitUtils.createBing("E_zicheng","Q_zicheng","R_zicheng");
directUtils.directProducer("E_zicheng", "R_zicheng", "2020-7-21,进行测试->" + Math.random() * 100);
}
@GetMapping("/test/consumer")
public void consumer() throws IOException, TimeoutException
{
directUtils.directConsumer("Q_zicheng", "E_zicheng", "R_zicheng");
}
@PostMapping("/product/old")
public ResultJson productOld(String xmlStr) throws DocumentException
{
// 1、解析 xml 文件,获取数据
XmlData xmlData = xmlUtils.parsingMessage(xmlStr);
// 2、校验格式、数据
ResultJson resultJson = xmlUtils.checkFormatAndData(xmlData);
if (!"200".equals(resultJson.getCode())) {
return resultJson;
}
// 3、通过格式校验之后,进行配置校验
Boolean binding = userMessageBindingService.validateXmlBinding(xmlData);
if (!binding) {
return ResultJson.error(CustomExceptionType.BINDING_ERROR);
}
// 4、发送消息,并讲消息存放到数据库中
return ResultJson.success("发送成功");
}
@PostMapping("/product/new")
public ResultJson productNew(String SNDR,
@RequestParam(value = "queueName", required = false) String RCVR,
@DateTimeFormat(pattern = "yyyyMMddHHmmss") Date DDTM,
String TYPE, String STYP,
String TOKN, String SEQN,
String VSHT, String SERV,
String content)
{
// 1、获取数据
XmlData xmlData = XmlData.builder()
.sender(SNDR)
.queueName(RCVR)
.sendDateTime(DDTM)
.exchangeName(TYPE)
.routingKeyName(STYP)
.token(TOKN)
.sequence(SEQN)
.virtualHostName(VSHT)
.serverName(SERV)
.sendContent(content)
.build();
// 2、校验格式、数据
ResultJson resultJson = xmlUtils.checkFormatAndData(xmlData);
if (!"200".equals(resultJson.getCode())) {
return resultJson;
}
// 3、通过格式校验之后,进行配置校验
Boolean binding = userMessageBindingService.validateXmlBinding(xmlData);
if (!binding) {
return ResultJson.error(CustomExceptionType.BINDING_ERROR);
}
// 4、mq发送消息,数据库中保存消息
ResultJson result = directUtils.sendMessage();
return ResultJson.success("发送成功", xmlData);
}
}
... ...
package com.sunyo.wlpt.message.bus.service.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
// 报文模板,如下
/*
<?xml version="1.0" encoding="UTF-8"?>
<MSG>
<META>
<SNDR> 消息发送者账号 </SNDR>
<RCVR> 指定接受者 </RCVR>
<DDTM> 发送报文时间 </DDTM>
<TYPE> 大类型--交换机名称 </TYPE>
<STYP> 子类型--路由键名称 </STYP>
<SEQN> 序列号(唯一) </SEQN>
<TOKN> token值 </TOKN>
<VSHT> 虚拟机名称 </VSHT>
<SERV> MQ服务器名称 </SERV>
</META>
<BODY>
发送内容
</BODY>
</MSG>
*/
/**
* @author 子诚
* Description:
* 时间:2020/7/28 10:30
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class XmlData implements Serializable {
private static final long serialVersionUID = -2539052741259114774L;
/**
* 对应:根标签 MSG ->报文
*/
private String message;
/**
* 对应:标签 BODY -> 发送内容
*/
private String sendContent;
/**
* 对应:标签 META -> 报文的头部信息
*/
private String meta;
/**
* 对应:标签 SNDR -> 报文发送者(用户)
*/
private String sender;
/**
* 对应:标签 RCVR -> 指定接收者(队列名称)
*/
private String queueName;
/**
* 对应:标签 DDTM -> 发送报文时间
*/
private String sendTime;
private Date sendDateTime;
/**
* 对应:标签 TYPE -> (一级)交换机名称
*/
private String exchangeName;
/**
* 对应:标签 STYP -> (二级)路由键名称
*/
private String routingKeyName;
/**
* 对应:标签 SEQN -> 序列(唯一)
*/
private String sequence;
/**
* 对应:标签 TOKN -> JWT生成的 token 值
*/
private String token;
/**
* 对应:标签 VSHT -> 指定的虚拟主机
*/
private String virtualHostName;
/**
* 对应:标签 SERV -> 指定的服务器
*/
private String serverName;
}
... ...
... ... @@ -7,6 +7,22 @@ package com.sunyo.wlpt.message.bus.service.exception;
*/
public enum CustomExceptionType {
BINDING_ERROR("10501", "配置信息,未进行绑定!"),
SENDER_ERROR("10401", "报文格式错误,发送者不能为空!"),
CONTENT_ERROR("10402", "报文格式错误,消息内容不能为空!"),
SERVER_ERROR("10403", "报文格式错误,服务器名称不能为空!"),
HOST_ERROR("10404", "报文格式错误,虚拟主机名称不能为空!"),
EXCHANGE_ERROR("10405", "报文格式错误,交换机名称不能为空!"),
ROUTING_KEY_ERROR("10406", "报文格式错误,路由键名称不能为空!"),
SEQUENCE_ERROR("10407", "报文格式错误,序列不能为空!"),
TOKEN_ERROR("10408", "报文格式错误,token不能为空!"),
SENDER_NO_EXIST("20401", "报文数据错误,发送者不存在!"),
SERVER_NO_EXIST("20403", "报文数据错误,服务器不存在!"),
HOST_NO_EXIST("20404", "报文数据错误,虚拟主机不存在!"),
EXCHANGE_NO_EXIST("20405", "报文数据错误,交换机不存在!"),
ROUTING_KEY_NO_EXIST("20406", "报文数据错误,路由键不存在!"),
CLIENT_ERROR("400", "客户端异常"),
SYSTEM_ERROR("500", "系统服务异常"),
... ... @@ -21,16 +37,19 @@ public enum CustomExceptionType {
*/
private String msg;
CustomExceptionType(String code, String msg) {
CustomExceptionType(String code, String msg)
{
this.code = code;
this.msg = msg;
}
public String getCode() {
public String getCode()
{
return code;
}
public String getMsg() {
public String getMsg()
{
return msg;
}
... ...
... ... @@ -84,4 +84,12 @@ public interface BusExchangeMapper {
* @return List<BusExchange>
*/
List<BusExchange> getExchangeList(BusExchange busExchange);
/**
* 检验交换机是否存在
*
* @param exchange {@link BusExchange}
* @return List<BusExchange>
*/
List<BusExchange> selectExchangeExist(BusExchange exchange);
}
... ...
... ... @@ -2,6 +2,7 @@ package com.sunyo.wlpt.message.bus.service.mapper;
import com.sunyo.wlpt.message.bus.service.domain.BusServer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
... ... @@ -107,4 +108,12 @@ public interface BusServerMapper {
* @return List<BusServer>
*/
List<BusServer> getServerAndHostAndExchangeList();
/**
* 检验用户名是否存在
*
* @param serverName 服务器名称
* @return List<BusServer>
*/
List<BusServer> selectServerExist(@Param("serverName") String serverName);
}
... ...
... ... @@ -84,4 +84,12 @@ public interface RoutingKeyMapper {
* @return List<RoutingKey>
*/
List<RoutingKey> getRoutingKeyList(RoutingKey routingKey);
/**
* 检验路由键是否存在,根据交换机id和路由键名称
*
* @param routingKey 路由键
* @return List<RoutingKey>
*/
List<RoutingKey> selectRoutingKeyExist(RoutingKey routingKey);
}
... ...
package com.sunyo.wlpt.message.bus.service.mapper;
import com.sunyo.wlpt.message.bus.service.domain.UserInfo;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author 子诚
... ... @@ -64,4 +67,12 @@ public interface UserInfoMapper {
* @return 用户信息集合
*/
List<UserInfo> getUserInfoList();
/**
* 判断该用户名是否存在
*
* @param username 用户登录名
* @return 用户列表
*/
List<UserInfo> selectUserExist(@Param("username") String username);
}
\ No newline at end of file
... ...
package com.sunyo.wlpt.message.bus.service.mapper;
import com.sunyo.wlpt.message.bus.service.domain.MessageNote;
import com.sunyo.wlpt.message.bus.service.domain.UserMessageBinding;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
import com.sunyo.wlpt.message.bus.service.domain.XmlData;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author 子诚
... ... @@ -73,4 +77,21 @@ public interface UserMessageBindingMapper {
* @return 判断校验是否通过
*/
List<UserMessageBinding> validateBinding(UserMessageBinding userMessageBinding);
/**
* 发送信息之前的检验配置是否存在
*
* @param messageNote {@link MessageNote}
* @return 判断校验是否通过
*/
List<UserMessageBinding> validateNote(MessageNote messageNote);
/**
* 校验报文配置与否?
*
* @param xmlData {@link XmlData}
* @return 判断校验是否通过
*/
List<UserMessageBinding> validateXmlBinding(XmlData xmlData);
}
\ No newline at end of file
... ...
... ... @@ -84,4 +84,11 @@ public interface VirtualHostMapper {
* @return 虚拟主机列表
*/
List<VirtualHost> getVirtualHostList(VirtualHost virtualHost);
/**
* 检验是否存在
* @param virtualHost {@link VirtualHost}
* @return List<VirtualHost>
*/
List<VirtualHost> selectVirtualHostExist(VirtualHost virtualHost);
}
... ...
package com.sunyo.wlpt.message.bus.service.rabbit.utils;
import com.rabbitmq.client.*;
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -87,8 +88,7 @@ public class DirectUtils {
factory.setUsername(userName);
factory.setPassword(password);
// 通过工程获取连接
Connection connection = factory.newConnection();
return connection;
return factory.newConnection();
}
/**
... ... @@ -219,6 +219,11 @@ public class DirectUtils {
// 消费消息
channel.basicConsume(queueName, false, consumer);
}
public ResultJson sendMessage()
{
return new ResultJson<>();
}
}
... ...
... ... @@ -44,7 +44,8 @@ public class ResultJson<T> implements Serializable {
/**
* 无参,构造方法
*/
public ResultJson() {
public ResultJson()
{
}
/**
... ... @@ -53,7 +54,8 @@ public class ResultJson<T> implements Serializable {
* @param code 响应状态
* @param msg 响应消息
*/
public ResultJson(String code, String msg) {
public ResultJson(String code, String msg)
{
this.code = code;
this.msg = msg;
}
... ... @@ -65,7 +67,8 @@ public class ResultJson<T> implements Serializable {
* @param msg 响应消息
* @param data 响应数据
*/
public ResultJson(String code, String msg, T data) {
public ResultJson(String code, String msg, T data)
{
this.code = code;
this.msg = msg;
this.data = data;
... ... @@ -76,7 +79,8 @@ public class ResultJson<T> implements Serializable {
*
* @return 成功(没有响应数据)
*/
public static ResultJson success() {
public static ResultJson success()
{
return new ResultJson<>("200", "success");
}
... ... @@ -85,17 +89,29 @@ public class ResultJson<T> implements Serializable {
*
* @return 成功(响应数据)
*/
public static ResultJson success(Object data) {
public static ResultJson success(Object data)
{
return new ResultJson<>("200", "success", data);
}
/**
* 定义静态、成功方法(重载)
*
* @return 成功(响应数据)
*/
public static ResultJson success(String message, Object data)
{
return new ResultJson<>("200", message, data);
}
/**
* 请求出现异常时的响应数据封装
*
* @param e 自定义异常类
* @return 返回异常信息
*/
public static ResultJson error(CustomException e) {
public static ResultJson error(CustomException e)
{
ResultJson result = new ResultJson<>();
result.setCode(e.getCode());
if (e.getCode() == CustomExceptionType.CLIENT_ERROR.getCode()) {
... ... @@ -109,15 +125,13 @@ public class ResultJson<T> implements Serializable {
return result;
}
/**
* 请求出现异常时的响应数据封装
*
* @param customExceptionType 自定义异常枚举中的异常
* @param errorMessage 错误信息
* @return 返回异常信息
*/
public static ResultJson error(CustomExceptionType customExceptionType, String errorMessage) {
public static ResultJson error(CustomExceptionType customExceptionType, String errorMessage)
{
return new ResultJson<>(customExceptionType.getCode(), errorMessage);
}
public static ResultJson error(CustomExceptionType customExceptionType)
{
return new ResultJson<>(customExceptionType.getCode(), customExceptionType.getMsg());
}
}
... ...
... ... @@ -2,7 +2,6 @@ package com.sunyo.wlpt.message.bus.service.service;
import com.github.pagehelper.PageInfo;
import com.sunyo.wlpt.message.bus.service.domain.BusExchange;
import com.sunyo.wlpt.message.bus.service.domain.VirtualHost;
import java.util.List;
... ... @@ -87,4 +86,12 @@ public interface BusExchangeService {
* @return List<BusExchange>
*/
List<BusExchange> getExchangeList(BusExchange busExchange);
/**
* 检验交换机是否存在
*
* @param exchange {@link BusExchange}
* @return List<BusExchange>
*/
List<BusExchange> selectExchangeExist(BusExchange exchange);
}
... ...
... ... @@ -109,5 +109,10 @@ public interface BusServerService {
*/
List<BusServer> getServerAndHostAndExchangeList();
/**
* 查询服务器名称是否存在
* @param serverName 服务器名称
* @return true or false
*/
List<BusServer> selectServerExist(String serverName);
}
... ...
package com.sunyo.wlpt.message.bus.service.service;
import com.github.pagehelper.PageInfo;
import com.sunyo.wlpt.message.bus.service.domain.BusExchange;
import com.sunyo.wlpt.message.bus.service.domain.RoutingKey;
import java.util.List;
... ... @@ -86,4 +85,12 @@ public interface RoutingKeyService {
* @return List<RoutingKey>
*/
List<RoutingKey> getRoutingKeyList(RoutingKey routingKey);
/**
* 检验路由键是否存在,根据交换机id和路由键名称
*
* @param routingKey 路由键
* @return List<RoutingKey>
*/
List<RoutingKey> selectRoutingKeyExist(RoutingKey routingKey);
}
... ...
... ... @@ -65,6 +65,14 @@ public interface UserInfoService {
* @return 用户信息集合
*/
List<UserInfo> getUserInfoList();
/**
* 判断该用户名是否存在
*
* @param username 用户登录名
* @return true or false
*/
Boolean selectUserExist(String username);
}
... ...
... ... @@ -2,8 +2,7 @@ package com.sunyo.wlpt.message.bus.service.service;
import com.github.pagehelper.PageInfo;
import com.sunyo.wlpt.message.bus.service.domain.UserMessageBinding;
import java.util.List;
import com.sunyo.wlpt.message.bus.service.domain.XmlData;
/**
* @author 子诚
... ... @@ -69,6 +68,13 @@ public interface UserMessageBindingService {
*/
PageInfo selectUserMessageBindingList(UserMessageBinding userMessageBinding, Integer pageNum, Integer pageSize);
/**
* 校验 报文 是否已经配置
*
* @param xmlData {@link XmlData} 解析之后的数据
* @return true or false
*/
public Boolean validateXmlBinding(XmlData xmlData);
}
... ...
package com.sunyo.wlpt.message.bus.service.service;
import com.github.pagehelper.PageInfo;
import com.sunyo.wlpt.message.bus.service.domain.BusServer;
import com.sunyo.wlpt.message.bus.service.domain.VirtualHost;
import java.util.List;
... ... @@ -86,4 +85,11 @@ public interface VirtualHostService {
* @return 虚拟主机列表
*/
List<VirtualHost> getVirtualHostList(VirtualHost virtualHost);
/**
* 检验是否存在
* @param virtualHost {@link VirtualHost}
* @return List<VirtualHost>
*/
List<VirtualHost> selectVirtualHostExist(VirtualHost virtualHost);
}
... ...
... ... @@ -100,4 +100,10 @@ public class BusExchangeServiceImpl implements BusExchangeService {
return busExchangeMapper.getExchangeList(busExchange);
}
@Override
public List<BusExchange> selectExchangeExist(BusExchange exchange)
{
return busExchangeMapper.selectExchangeExist(exchange);
}
}
... ...
... ... @@ -118,4 +118,10 @@ public class BusServerServiceImpl implements BusServerService {
{
return busServerMapper.getServerAndHostAndExchangeList();
}
@Override
public List<BusServer> selectServerExist(String serverName)
{
return busServerMapper.selectServerExist(serverName);
}
}
... ...
... ... @@ -2,9 +2,12 @@ package com.sunyo.wlpt.message.bus.service.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunyo.wlpt.message.bus.service.domain.MessageNote;
import com.sunyo.wlpt.message.bus.service.domain.*;
import com.sunyo.wlpt.message.bus.service.mapper.MessageNoteMapper;
import com.sunyo.wlpt.message.bus.service.service.MessageNoteService;
import com.sunyo.wlpt.message.bus.service.mapper.UserMessageBindingMapper;
import com.sunyo.wlpt.message.bus.service.service.*;
import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
... ... @@ -17,8 +20,29 @@ import java.util.List;
* Description:
* 时间:2020/6/29 17:46
*/
@Slf4j
@Service
public class MessageNoteServiceImpl implements MessageNoteService {
@Resource
private UserInfoService userInfoService;
@Resource
private BusServerService busServerService;
@Resource
private VirtualHostService virtualHostService;
@Resource
private BusExchangeService busExchangeService;
@Resource
private BusQueueService busQueueService;
@Resource
private UserMessageBindingMapper userMessageBindingMapper;
@Resource
private RoutingKeyService routingKeyService;
@Resource
private MessageNoteMapper messageNoteMapper;
... ... @@ -58,7 +82,7 @@ public class MessageNoteServiceImpl implements MessageNoteService {
@Override
public int insertSelective(MessageNote record)
{
return messageNoteMapper.insertSelective(record);
return validateNoteAndFill(record);
}
@Override
... ... @@ -99,6 +123,64 @@ public class MessageNoteServiceImpl implements MessageNoteService {
messageNoteMapper.autoDelete(deleteTime);
}
/**
* 填充名称(使用get方法,如果不存在就会报空指针异常)
*
* @param messageNote {@link MessageNote}
* @return {@link MessageNote}
*/
public MessageNote note_fillName(MessageNote messageNote)
{
// 设置id
messageNote.setId(IdUtils.generateId());
// 填充,用户名称
UserInfo userInfo = userInfoService.selectByPrimaryKey(messageNote.getUserId());
messageNote.setUsername(userInfo.getUsername());
// 填充,发送内容(编辑 or 新增)
messageNote.setSendContent(messageNote.getAlias_sendContent().getBytes());
// 填充,服务器名称
BusServer busServer = busServerService.selectByPrimaryKey(messageNote.getServerId());
messageNote.setServerName(busServer.getServerName());
// 填充,虚拟主机名称
VirtualHost virtualHost = virtualHostService.selectByPrimaryKey(messageNote.getVirtualHostId());
messageNote.setVirtualHostName(virtualHost.getVirtualHostName());
// 填充,交换机名称
BusExchange busExchange = busExchangeService.selectByPrimaryKey(messageNote.getExchangeId());
messageNote.setExchangeName(busExchange.getExchangeName());
// 填充,路由键名称
RoutingKey routingKey = routingKeyService.selectByPrimaryKey(messageNote.getRoutingKeyId());
messageNote.setRoutingKeyName(routingKey.getRoutingKeyName());
return messageNote;
}
/**
* 校验,messageNote是否已经存在于配置中
*
* @param messageNote {@link MessageNote}
*/
public Boolean validateNote(MessageNote messageNote)
{
// 用户id, 服务器id,虚拟主机id,交换机id,路由键id
return userMessageBindingMapper.validateNote(messageNote).size() > 0;
}
/**
* 检验、填充、添加
*
* @param messageNote {@link MessageNote}
* @return 发送信息记录的条数
*/
public int validateNoteAndFill(MessageNote messageNote)
{
return validateNote(messageNote) ? messageNoteMapper.insertSelective(note_fillName(messageNote)) : 0;
}
}
... ...
... ... @@ -114,4 +114,10 @@ public class RoutingKeyServiceImpl implements RoutingKeyService {
}
return routingKeyMapper.getRoutingKeyList(routingKey);
}
@Override
public List<RoutingKey> selectRoutingKeyExist(RoutingKey routingKey)
{
return routingKeyMapper.selectRoutingKeyExist(routingKey);
}
}
... ...
... ... @@ -61,6 +61,13 @@ public class UserInfoServiceImpl implements UserInfoService {
return userInfoMapper.getUserInfoList();
}
@Override
public Boolean selectUserExist(String username)
{
List<UserInfo> list = userInfoMapper.selectUserExist(username);
return list.size() > 0;
}
}
... ...
... ... @@ -326,6 +326,7 @@ public class UserMessageBindingServiceImpl implements UserMessageBindingService
return pageInfo;
}
/**
* 编辑 or 新增方法,根绝前端传递来的id值,来填充对应的name
* 服务器id,虚拟主机id,交换机id,队列id,路由键id
... ... @@ -394,8 +395,16 @@ public class UserMessageBindingServiceImpl implements UserMessageBindingService
return 0;
}
}
@Override
public Boolean validateXmlBinding(XmlData xmlData)
{
List<UserMessageBinding> list = userMessageBindingMapper.validateXmlBinding(xmlData);
return list.size() > 0;
}
}
... ...
... ... @@ -102,4 +102,10 @@ public class VirtualHostServiceImpl implements VirtualHostService {
return virtualHostMapper.getVirtualHostList(virtualHost);
}
@Override
public List<VirtualHost> selectVirtualHostExist(VirtualHost virtualHost)
{
return virtualHostMapper.selectVirtualHostExist(virtualHost);
}
}
... ...
package com.sunyo.wlpt.message.bus.service.utils;
import com.sunyo.wlpt.message.bus.service.domain.*;
import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType;
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
import com.sunyo.wlpt.message.bus.service.service.*;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 子诚
* Description:Dom4j 解析 xml的工具类
* 时间:2020/7/28 10:29
*/
@Slf4j
@Component
public class XmlUtils {
@Resource
private UserInfoService userInfoService;
@Resource
private BusServerService busServerService;
@Resource
private VirtualHostService virtualHostService;
@Resource
private BusExchangeService busExchangeService;
private RoutingKeyService routingKeyService;
@Value("${path.dir}")
private String dir;
/**
* 解析字符串类型的 xml
*
* @return {@link XmlData}
*/
public XmlData parsingMessage(String xmlStr) throws DocumentException
{
// 设置文件名
String title = "test.xml";
String filePath = dir + "/" + title;
SAXReader reader = new SAXReader();
Document document = reader.read(filePath);
Element msg = document.getRootElement();
Element meta = msg.element("META");
XmlData xmlData = XmlData.builder()
.sendContent(xmlStr)
.sender(meta.elementText("SNDR"))
.queueName(meta.elementText("RCVR"))
.sendTime(meta.elementText("DDTM"))
.exchangeName(meta.elementText("TYPE"))
.routingKeyName(meta.elementText("STYP"))
.sequence(meta.elementText("SEQN"))
.token(meta.elementText("TOKN"))
.virtualHostName(meta.elementText("VSHT"))
.serverName(meta.elementText("SERV"))
.build();
return xmlData;
}
/**
* @param xmlData {@link XmlData} 需要被检查的 xml 报文数据与格式
* @return
*/
public ResultJson checkFormatAndData(XmlData xmlData)
{
if (StringUtil.isNullOrEmpty(xmlData.getSendContent())) {
// 消息内容为空
return ResultJson.error(CustomExceptionType.CONTENT_ERROR);
} else if (StringUtil.isNullOrEmpty(xmlData.getSender())) {
// 发送者名称为空
return ResultJson.error(CustomExceptionType.SERVER_ERROR);
} else if (StringUtil.isNullOrEmpty(xmlData.getServerName())) {
// 服务器名称为空
return ResultJson.error(CustomExceptionType.SERVER_ERROR);
} else if (StringUtil.isNullOrEmpty(xmlData.getVirtualHostName())) {
// 虚拟主机为空
return ResultJson.error(CustomExceptionType.HOST_ERROR);
} else if (StringUtil.isNullOrEmpty(xmlData.getExchangeName())) {
// 交换机为空
return ResultJson.error(CustomExceptionType.EXCHANGE_ERROR);
} else if (StringUtil.isNullOrEmpty(xmlData.getRoutingKeyName())) {
// 路由键为空
return ResultJson.error(CustomExceptionType.ROUTING_KEY_ERROR);
} else if (StringUtil.isNullOrEmpty(xmlData.getSequence())) {
// 序列为空
return ResultJson.error(CustomExceptionType.SEQUENCE_ERROR);
} else if (StringUtil.isNullOrEmpty(xmlData.getToken())) {
// token为空
return ResultJson.error(CustomExceptionType.TOKEN_ERROR);
}
if (!userInfoService.selectUserExist(xmlData.getSender())) {
// 发送者不存在
return ResultJson.error(CustomExceptionType.SENDER_NO_EXIST);
}
List<BusServer> serverList = busServerService.selectServerExist(xmlData.getServerName());
if (serverList.size() == 0) {
// 服务器不存在
return ResultJson.error(CustomExceptionType.SERVER_NO_EXIST);
}
VirtualHost virtualHost = VirtualHost.builder()
.serverId(serverList.get(0).getId())
.virtualHostName(xmlData.getVirtualHostName()).build();
List<VirtualHost> virtualHostList = virtualHostService.selectVirtualHostExist(virtualHost);
if (virtualHostList.size() == 0) {
// 虚拟主机不存在
return ResultJson.error(CustomExceptionType.HOST_NO_EXIST);
}
BusExchange exchange = BusExchange.builder()
.virtualHostId(virtualHostList.get(0).getId())
.exchangeName(xmlData.getExchangeName()).build();
List<BusExchange> exchangeList = busExchangeService.selectExchangeExist(exchange);
if (exchangeList.size() == 0) {
// 交换机不存在
return ResultJson.error(CustomExceptionType.EXCHANGE_NO_EXIST);
}
RoutingKey routingKey = RoutingKey.builder()
.exchangeId(exchangeList.get(0).getId())
.routingKeyName(xmlData.getRoutingKeyName()).build();
List<RoutingKey> routingKeyList = routingKeyService.selectRoutingKeyExist(routingKey);
if (routingKeyList.size() == 0) {
// 路由键不存在
return ResultJson.error(CustomExceptionType.ROUTING_KEY_NO_EXIST);
}
return ResultJson.success();
}
}
... ...
... ... @@ -238,4 +238,21 @@
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=VARCHAR}
</update>
<select id="selectExchangeExist" parameterType="com.sunyo.wlpt.message.bus.service.domain.BusExchange"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from bus_exchange
<where>
<!-- 交换机名称 -->
<if test="exchangeName != null and exchangeName != ''">
exchange_name = #{exchangeName,jdbcType=VARCHAR}
</if>
<!-- 所属虚拟主机ID -->
<if test="virtualHostId != null and virtualHostId != ''">
and virtual_host_id = #{virtualHostId,jdbcType=VARCHAR}
</if>
</where>
</select>
</mapper>
... ...
... ... @@ -229,4 +229,13 @@
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=VARCHAR}
</update>
<select id="selectServerExist" parameterType="java.lang.String" resultMap="BaseResultMap">
<!--@mbg.generated-->
select
<include refid="Base_Column_List"/>
from bus_server
where server_name = #{serverName,jdbcType=VARCHAR}
</select>
</mapper>
... ...
... ... @@ -85,6 +85,22 @@
</where>
</select>
<select id="selectRoutingKeyExist" parameterType="com.sunyo.wlpt.message.bus.service.domain.RoutingKey"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from routing_key
<where>
<!-- 所属交换机ID -->
<if test="exchangeId != null and exchangeId != ''">
exchange_id = #{exchangeId,jdbcType=VARCHAR}
</if>
<!-- 路由键名称 -->
<if test="routingKeyName != null and routingKeyName != ''">
and routing_key_name = #{routingKeyName,jdbcType=VARCHAR}
</if>
</where>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
<!--@mbg.generated-->
delete
... ...
... ... @@ -122,4 +122,12 @@
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=VARCHAR}
</update>
<select id="selectUserExist" parameterType="java.lang.String" resultMap="BaseResultMap">
<!--@mbg.generated-->
select
<include refid="Base_Column_List"/>
from user_info
where username = #{username,jdbcType=VARCHAR}
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -323,4 +323,61 @@
</if>
</where>
</select>
<select id="validateNote" parameterType="com.sunyo.wlpt.message.bus.service.domain.MessageNote"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user_message_binding
<where>
<!-- 所属用户id -->
<if test="userId != null and userId != ''">
user_id = #{userId,jdbcType=VARCHAR}
</if>
<!-- 所属服务器id -->
<if test="serverId != null and serverId != ''">
AND server_id = #{serverId,jdbcType=VARCHAR}
</if>
<!-- 所属虚拟主机id -->
<if test="virtualHostId != null and virtualHostId != ''">
AND virtual_host_id = #{virtualHostId,jdbcType=VARCHAR}
</if>
<!-- 所属交换机id -->
<if test="exchangeId != null and exchangeId != ''">
AND exchange_id = #{exchangeId,jdbcType=VARCHAR}
</if>
<!-- 所属路由键id -->
<if test="routingKeyId != null and routingKeyId != ''">
AND routing_key_id = #{routingKeyId,jdbcType=VARCHAR}
</if>
</where>
</select>
<select id="validateXmlBinding" parameterType="com.sunyo.wlpt.message.bus.service.domain.XmlData"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user_message_binding
<where>
<!-- 用户 -->
<if test="sender != null and sender != ''">
username = #{sender,jdbcType=VARCHAR}
</if>
<!-- 服务器名称 -->
<if test="serverName != null and serverName != ''">
AND server_name = #{serverName,jdbcType=VARCHAR}
</if>
<!-- 虚拟主机名称 -->
<if test="virtualHostName != null and virtualHostName != ''">
AND virtual_host_name = #{virtualHostName,jdbcType=VARCHAR}
</if>
<!-- 交换机名称 -->
<if test="exchangeName != null and exchangeName != ''">
AND exchange_name = #{exchangeName,jdbcType=VARCHAR}
</if>
<!-- 路由键名称 -->
<if test="routingKeyName != null and routingKeyName != ''">
AND routing_key_name = #{routingKeyName,jdbcType=VARCHAR}
</if>
</where>
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -191,4 +191,21 @@
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=VARCHAR}
</update>
<select id="selectVirtualHostExist" parameterType="com.sunyo.wlpt.message.bus.service.domain.VirtualHost"
resultMap="HostAndServerMap">
select
<include refid="Base_Column_List"/>
from virtual_host
<where>
<!-- 所属服务器ID -->
<if test="serverId != null and serverId != ''">
server_id = #{serverId,jdbcType=VARCHAR}
</if>
<!-- 虚拟主机名称 -->
<if test="virtualHostName != null and virtualHostName != ''">
and virtual_host_name = #{virtualHostName,jdbcType=VARCHAR}
</if>
</where>
</select>
</mapper>
... ...
<?xml version="1.0" encoding="UTF-8"?>
<MSG>
<META>
<SNDR>zicheng</SNDR>
<RCVR></RCVR>
<DDTM>2020728151024</DDTM>
<TYPE>E_zicheng</TYPE>
<STYP>R_zicheng</STYP>
<SEQN>12345</SEQN>
<TOKN>token</TOKN>
<VSHT>V_zicheng</VSHT>
<SERV>S_server</SERV>
</META>
<BODY>测试一下,发送xml,进行解析</BODY>
</MSG><!-- 根标签,结束 -->
... ...
<?xml version="1.0" encoding="UTF-8"?>
<MSG><!-- 根标签,开始 -->
<META><!-- 头标签,开始 -->
<SNDR>消息发送者(用户名称)</SNDR>
<RCVR>指定接收者(队列名称)</RCVR><!-- 指定接受者为空,则代表发送给 预配置的接收者 -->
<DDTM>发送报文时间</DDTM><!-- 发送时间,格式为yyyyMMddHHmmss -->
<TYPE>交换机名称</TYPE><!-- 消息大类型:交换机名称 -->
<STYP>路由键名称</STYP><!-- 消息子类型:路由键名称 -->
<SEQN>序列号(唯一)</SEQN><!-- 该消息的序列,消息唯一标识 -->
<TOKN>token值</TOKN><!-- token -->
<VSHT>虚拟机名称</VSHT><!-- 虚拟主机名称 -->
<SERV>MQ服务器名称</SERV><!-- 服务器名称 -->
</META><!-- 头标签,结束 -->
<BODY><!-- 内容标签,开始 -->
发送内容
</BODY><!-- 内容标签,结束 -->
</MSG><!-- 根标签,结束 -->
... ...