From 4c0f5eb8aa744649102aa30b32db4d275de9d214 Mon Sep 17 00:00:00 2001 From: smallbun <2689170096@qq.com> Date: Sat, 26 Aug 2023 18:12:05 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E4=BC=98=E5=8C=96=E7=9F=AD=E4=BF=A1/?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=E5=8F=91=E9=80=81=E6=9C=AA=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/MailProviderException.java | 2 +- .../exception/SmsMessageSendException.java | 58 ++++++++++ .../SmsProviderException.java | 5 +- .../common/message/SendMessageException.java | 105 ------------------ .../mail/DefaultMailProviderSendImpl.java | 5 +- .../core/mq/AbstractMessageListener.java | 3 +- .../core/mq/NoticeMessageListener.java | 15 ++- .../employee/core/mq/UserMessageListener.java | 2 +- 8 files changed, 79 insertions(+), 116 deletions(-) create mode 100644 eiam-common/src/main/java/cn/topiam/employee/common/exception/SmsMessageSendException.java rename eiam-common/src/main/java/cn/topiam/employee/common/{message => exception}/SmsProviderException.java (97%) delete mode 100644 eiam-common/src/main/java/cn/topiam/employee/common/message/SendMessageException.java diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/exception/MailProviderException.java b/eiam-common/src/main/java/cn/topiam/employee/common/exception/MailProviderException.java index 3f5b0939..28e3593a 100644 --- a/eiam-common/src/main/java/cn/topiam/employee/common/exception/MailProviderException.java +++ b/eiam-common/src/main/java/cn/topiam/employee/common/exception/MailProviderException.java @@ -24,7 +24,7 @@ import org.springframework.http.HttpStatus; import cn.topiam.employee.support.exception.TopIamException; /** - * 邮件服务异常 + * 邮件提供商异常 * * @author TopIAM * Created by support@topiam.cn on 2020/8/19 22:53 diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/exception/SmsMessageSendException.java b/eiam-common/src/main/java/cn/topiam/employee/common/exception/SmsMessageSendException.java new file mode 100644 index 00000000..16b0281c --- /dev/null +++ b/eiam-common/src/main/java/cn/topiam/employee/common/exception/SmsMessageSendException.java @@ -0,0 +1,58 @@ +/* + * eiam-common - Employee Identity and Access Management + * Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package cn.topiam.employee.common.exception; + +import cn.topiam.employee.support.exception.TopIamException; + +/** + * 短信消息发送失败 + * + * @author TopIAM + * Created by support@topiam.cn on 2023/8/26 18:01 + */ +public class SmsMessageSendException extends TopIamException { + /** + * Constructs a new runtime exception with the specified detail message. + * The cause is not initialized, and may subsequently be initialized by a + * call to {@link #initCause}. + * + * @param message the detail message. The detail message is saved for + * later retrieval by the {@link #getMessage()} method. + */ + public SmsMessageSendException(String message) { + super(message); + } + + /** + * Constructs a new runtime exception with the specified detail message and + * cause.

Note that the detail message associated with + * {@code cause} is not automatically incorporated in + * this runtime exception's detail message. + * + * @param message the detail message (which is saved for later retrieval + * by the {@link #getMessage()} method). + * @param cause the cause (which is saved for later retrieval by the + * {@link #getCause()} method). (A null value is + * permitted, and indicates that the cause is nonexistent or + * unknown.) + * @since 1.4 + */ + public SmsMessageSendException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/message/SmsProviderException.java b/eiam-common/src/main/java/cn/topiam/employee/common/exception/SmsProviderException.java similarity index 97% rename from eiam-common/src/main/java/cn/topiam/employee/common/message/SmsProviderException.java rename to eiam-common/src/main/java/cn/topiam/employee/common/exception/SmsProviderException.java index 90efb51e..0e9eec3d 100644 --- a/eiam-common/src/main/java/cn/topiam/employee/common/message/SmsProviderException.java +++ b/eiam-common/src/main/java/cn/topiam/employee/common/exception/SmsProviderException.java @@ -15,7 +15,9 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -package cn.topiam.employee.common.message; +package cn.topiam.employee.common.exception; + +import java.io.Serial; /** * 邮件服务异常 @@ -24,6 +26,7 @@ package cn.topiam.employee.common.message; * Created by support@topiam.cn on 2020/8/19 22:53 */ public class SmsProviderException extends RuntimeException { + @Serial private static final long serialVersionUID = 6249098979022610064L; /** diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/message/SendMessageException.java b/eiam-common/src/main/java/cn/topiam/employee/common/message/SendMessageException.java deleted file mode 100644 index c79e0db0..00000000 --- a/eiam-common/src/main/java/cn/topiam/employee/common/message/SendMessageException.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * eiam-common - Employee Identity and Access Management - * Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package cn.topiam.employee.common.message; - -import java.io.Serial; - -import cn.topiam.employee.common.message.enums.MessageType; - -/** - * 消息发送异常 - * - * @author TopIAM - * Created by support@topiam.cn on 2020/8/19 22:53 - */ -public class SendMessageException extends RuntimeException { - @Serial - private static final long serialVersionUID = 6249098979022610064L; - private MessageType messageType; - - /** - * Constructs a new runtime exception with the specified detail message. - * The cause is not initialized, and may subsequently be initialized by a - * call to {@link #initCause}. - * - * @param message the detail message. The detail message is saved for - * later retrieval by the {@link #getMessage()} method. - */ - public SendMessageException(MessageType messageType, String message) { - super(message); - this.messageType = messageType; - } - - public MessageType getMessageType() { - return messageType; - } - - /** - * Constructs a new runtime exception with the specified detail message and - * cause.

Note that the detail message associated with - * {@code cause} is not automatically incorporated in - * this runtime exception's detail message. - * - * @param message the detail message (which is saved for later retrieval - * by the {@link #getMessage()} method). - * @param cause the cause (which is saved for later retrieval by the - * {@link #getCause()} method). (A null value is - * permitted, and indicates that the cause is nonexistent or - * unknown.) - * @since 1.4 - */ - public SendMessageException(String message, Throwable cause) { - super(message, cause); - } - - /** - * Constructs a new runtime exception with the specified cause and a - * detail message of (cause==null ? null : cause.toString()) - * (which typically contains the class and detail message of - * cause). This constructor is useful for runtime exceptions - * that are little more than wrappers for other throwables. - * - * @param cause the cause (which is saved for later retrieval by the - * {@link #getCause()} method). (A null value is - * permitted, and indicates that the cause is nonexistent or - * unknown.) - * @since 1.4 - */ - public SendMessageException(Throwable cause) { - super(cause); - } - - /** - * Constructs a new runtime exception with the specified detail - * message, cause, suppression enabled or disabled, and writable - * stack trace enabled or disabled. - * - * @param message the detail message. - * @param cause the cause. (A {@code null} value is permitted, - * and indicates that the cause is nonexistent or unknown.) - * @param enableSuppression whether or not suppression is enabled - * or disabled - * @param writableStackTrace whether or not the stack trace should - * be writable - * @since 1.7 - */ - public SendMessageException(String message, Throwable cause, boolean enableSuppression, - boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } -} diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/message/mail/DefaultMailProviderSendImpl.java b/eiam-common/src/main/java/cn/topiam/employee/common/message/mail/DefaultMailProviderSendImpl.java index 8af0d25d..f5c58e21 100644 --- a/eiam-common/src/main/java/cn/topiam/employee/common/message/mail/DefaultMailProviderSendImpl.java +++ b/eiam-common/src/main/java/cn/topiam/employee/common/message/mail/DefaultMailProviderSendImpl.java @@ -27,10 +27,9 @@ import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; -import cn.topiam.employee.common.message.SendMessageException; +import cn.topiam.employee.common.exception.MailMessageSendException; import cn.topiam.employee.common.message.enums.MailProvider; import cn.topiam.employee.common.message.enums.MailSafetyType; -import cn.topiam.employee.common.message.enums.MessageType; import jakarta.mail.internet.MimeMessage; import static org.apache.commons.codec.CharEncoding.UTF_8; @@ -120,7 +119,7 @@ public class DefaultMailProviderSendImpl implements MailProviderSend { taskExecutor.execute(() -> javaMailSender.send(mimeMessage)); } catch (Exception e) { log.info("发送邮件消息失败: {}", e.getMessage()); - throw new SendMessageException(MessageType.MAIL, e.getMessage()); + throw new MailMessageSendException(e.getMessage(), e); } } diff --git a/eiam-core/src/main/java/cn/topiam/employee/core/mq/AbstractMessageListener.java b/eiam-core/src/main/java/cn/topiam/employee/core/mq/AbstractMessageListener.java index 209b9327..3993a515 100644 --- a/eiam-core/src/main/java/cn/topiam/employee/core/mq/AbstractMessageListener.java +++ b/eiam-core/src/main/java/cn/topiam/employee/core/mq/AbstractMessageListener.java @@ -17,6 +17,7 @@ */ package cn.topiam.employee.core.mq; +import java.io.IOException; import java.util.Map; import org.springframework.amqp.core.Message; @@ -50,7 +51,7 @@ public abstract class AbstractMessageListener { * @param headers {@link Map} */ public void onMessage(Message message, Channel channel, @Payload String body, - @Headers Map headers) { + @Headers Map headers) throws IOException { // 设置TraceId TraceUtils.put(String.valueOf(headers.get(TRACE_ID))); } diff --git a/eiam-core/src/main/java/cn/topiam/employee/core/mq/NoticeMessageListener.java b/eiam-core/src/main/java/cn/topiam/employee/core/mq/NoticeMessageListener.java index af4ee75c..3da89a1a 100644 --- a/eiam-core/src/main/java/cn/topiam/employee/core/mq/NoticeMessageListener.java +++ b/eiam-core/src/main/java/cn/topiam/employee/core/mq/NoticeMessageListener.java @@ -43,9 +43,12 @@ import cn.topiam.employee.common.enums.MailType; import cn.topiam.employee.common.enums.MessageCategory; import cn.topiam.employee.common.exception.MailMessageSendException; import cn.topiam.employee.common.exception.MessageSendException; +import cn.topiam.employee.common.exception.SmsMessageSendException; +import cn.topiam.employee.common.message.mail.MailNoneProviderSend; import cn.topiam.employee.common.message.mail.MailProviderSend; import cn.topiam.employee.common.message.mail.SendMailRequest; import cn.topiam.employee.common.message.sms.SendSmsRequest; +import cn.topiam.employee.common.message.sms.SmsNoneProviderSend; import cn.topiam.employee.common.message.sms.SmsProviderSend; import cn.topiam.employee.common.message.sms.SmsResponse; import cn.topiam.employee.common.repository.message.MailSendRecordRepository; @@ -58,7 +61,6 @@ import cn.topiam.employee.core.message.sms.SmsMsgEventPublish; import cn.topiam.employee.core.setting.constant.MessageSettingConstants; import cn.topiam.employee.support.context.ApplicationContextHelp; -import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import freemarker.cache.StringTemplateLoader; @@ -94,7 +96,7 @@ public class NoticeMessageListener extends AbstractMessageListener { @RabbitListener(queues = { NOTICE_SMS, NOTICE_MAIL }, ackMode = "MANUAL") @RabbitHandler() public void onMessage(Message message, Channel channel, @Payload String body, - @Headers Map headers) { + @Headers Map headers) throws IOException { super.onMessage(message, channel, body, headers); log.info("异步接收ES用户信息入参: [{}]", message); sendNotice(message, channel, body); @@ -107,8 +109,7 @@ public class NoticeMessageListener extends AbstractMessageListener { * @param channel {@link Channel} * @param body {@link String} */ - @SneakyThrows - private String sendNotice(Message message, Channel channel, String body) { + private String sendNotice(Message message, Channel channel, String body) throws IOException { try { String queueName = message.getMessageProperties().getConsumerQueue(); if (Objects.isNull(body)) { @@ -139,6 +140,9 @@ public class NoticeMessageListener extends AbstractMessageListener { * @param smsMessage {@link SmsMessage} */ private void sendSms(@NonNull SmsMessage smsMessage) { + if (smsProviderSend instanceof SmsNoneProviderSend) { + throw new SmsMessageSendException("暂未配置短信服务"); + } SendSmsRequest smsParam = new SendSmsRequest(); try { //@formatter:off @@ -189,6 +193,9 @@ public class NoticeMessageListener extends AbstractMessageListener { * @param mailMessage {@link MailMessage} */ private void sendMail(@NonNull MailMessage mailMessage) { + if (mailProviderSend instanceof MailNoneProviderSend) { + throw new MailMessageSendException("暂未配置邮件服务"); + } // 邮件通知类型 MailType type = mailMessage.getType(); String content = htmlUnescape(MailUtils.readEmailContent(type.getContent())); diff --git a/eiam-core/src/main/java/cn/topiam/employee/core/mq/UserMessageListener.java b/eiam-core/src/main/java/cn/topiam/employee/core/mq/UserMessageListener.java index 398c50b3..ee95cfbf 100644 --- a/eiam-core/src/main/java/cn/topiam/employee/core/mq/UserMessageListener.java +++ b/eiam-core/src/main/java/cn/topiam/employee/core/mq/UserMessageListener.java @@ -64,7 +64,7 @@ public class UserMessageListener extends AbstractMessageListener { @RabbitListener(queues = { USER_SAVE, USER_DELETE }, ackMode = "MANUAL") @RabbitHandler() public void onMessage(Message message, Channel channel, @Payload String body, - @Headers Map headers) { + @Headers Map headers) throws IOException { super.onMessage(message, channel, body, headers); log.info("异步接收ES用户信息入参: [{}]", message); syncUser(message, channel, body);