优化短信/邮件发送未配置提示

pull/37/head
smallbun 2023-08-26 18:12:05 +08:00
parent 2da0ddec6a
commit 4c0f5eb8aa
8 changed files with 79 additions and 116 deletions

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> 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 <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public SmsMessageSendException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -15,7 +15,9 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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;
/**

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> 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 <tt>null</tt> 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 <tt>(cause==null ? null : cause.toString())</tt>
* (which typically contains the class and detail message of
* <tt>cause</tt>). 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 <tt>null</tt> 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);
}
}

View File

@ -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);
}
}

View File

@ -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<String, Object> headers) {
@Headers Map<String, Object> headers) throws IOException {
// 设置TraceId
TraceUtils.put(String.valueOf(headers.get(TRACE_ID)));
}

View File

@ -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<String, Object> headers) {
@Headers Map<String, Object> 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()));

View File

@ -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<String, Object> headers) {
@Headers Map<String, Object> headers) throws IOException {
super.onMessage(message, channel, body, headers);
log.info("异步接收ES用户信息入参: [{}]", message);
syncUser(message, channel, body);