Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor email send server config #1015

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import java.time.LocalDateTime;
Expand All @@ -21,7 +20,7 @@
import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE;

/**
* 消息通知服务端配置实体
* 公共配置实体
* @author zqr10159
*/
@Entity
Expand All @@ -30,26 +29,20 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "Message notification server config entity | 消息通知服务端配置实体")
@Schema(description = "Common server config entity | 公共服务端配置实体")
@EntityListeners(AuditingEntityListener.class)
public class GeneralConfig {

@Id
@Schema(title = "Config type: 1-SMS 2-Email, primary key ", description = "配置类型: 1-短信 2-邮件, 主键",
@Schema(title = "Config type: email sms, primary key ", description = "配置类型: email sms, 主键",
accessMode = READ_WRITE)
@Min(1)
@NotNull
private Byte type;
private String type;

@Schema(title = "Config content", description = "配置内容,格式为json", accessMode = READ_WRITE)
@Column(length = 4096)
@Column(length = 8192)
private String content;

@Schema(title = "Whether to enable this policy",
description = "是否启用此配置",
example = "true", accessMode = READ_WRITE)
private boolean enable = true;

@Schema(title = "The creator of this record", description = "此条记录创建者", example = "tom", accessMode = READ_ONLY)
@CreatedBy
private String creator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.dromara.hertzbeat.manager.component.alerter.AlertNotifyHandler;
import org.dromara.hertzbeat.manager.config.MailConfigProperties;
import org.dromara.hertzbeat.manager.dao.GeneralConfigDao;
import org.dromara.hertzbeat.manager.pojo.dto.NoticeSender;
import org.dromara.hertzbeat.manager.pojo.dto.EmailNoticeSender;
import org.dromara.hertzbeat.manager.service.MailService;
import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -63,7 +63,7 @@ final class EmailAlertNotifyHandlerImpl implements AlertNotifyHandler {

private final ObjectMapper objectMapper;

private static final Byte TYPE = 2;
private static final String TYPE = "email";

private final ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");

Expand All @@ -73,19 +73,24 @@ public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeExcepti
//获取sender
JavaMailSenderImpl sender = (JavaMailSenderImpl) javaMailSender;
try {
boolean useDatabase = false;
GeneralConfig emailConfig = generalConfigDao.findByType(TYPE);
if (emailConfig != null && emailConfig.isEnable() && emailConfig.getContent() != null) {
if (emailConfig != null && emailConfig.getContent() != null) {
// 若启用数据库配置
String content = emailConfig.getContent();
NoticeSender noticeSenderConfig = objectMapper.readValue(content, NoticeSender.class);
sender.setHost(noticeSenderConfig.getEmailHost());
sender.setPort(noticeSenderConfig.getEmailPort());
sender.setUsername(noticeSenderConfig.getEmailUsername());
sender.setPassword(noticeSenderConfig.getEmailPassword());
Properties props = sender.getJavaMailProperties();
props.put("spring.mail.smtp.ssl.enable", noticeSenderConfig.isEmailSsl());
emailFromUser = noticeSenderConfig.getEmailUsername();
} else {
EmailNoticeSender emailNoticeSenderConfig = objectMapper.readValue(content, EmailNoticeSender.class);
if (emailNoticeSenderConfig.isEnable()) {
sender.setHost(emailNoticeSenderConfig.getEmailHost());
sender.setPort(emailNoticeSenderConfig.getEmailPort());
sender.setUsername(emailNoticeSenderConfig.getEmailUsername());
sender.setPassword(emailNoticeSenderConfig.getEmailPassword());
Properties props = sender.getJavaMailProperties();
props.put("spring.mail.smtp.ssl.enable", emailNoticeSenderConfig.isEmailSsl());
emailFromUser = emailNoticeSenderConfig.getEmailUsername();
useDatabase = true;
}
}
if (!useDatabase) {
// 若数据库未配置则启用yml配置
sender.setHost(mailConfigProperties.getHost());
sender.setPort(mailConfigProperties.getPort());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.dromara.hertzbeat.manager.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hertzbeat.common.entity.dto.Message;
import org.dromara.hertzbeat.manager.service.GeneralConfigService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
* Alert sender Configuration API
* 告警发送端配置API
*
* @author zqr10159
*
*/
@RestController
@RequestMapping(value = "/api/config", produces = {APPLICATION_JSON_VALUE})
@Tag(name = "Alert sender Configuration API | 告警发送端配置API")
@Slf4j
public class GeneralConfigController {
private Map<String, GeneralConfigService> configServiceMap;

public GeneralConfigController(List<GeneralConfigService> generalConfigServices) {
configServiceMap = new HashMap<>(8);
if (generalConfigServices != null) {
generalConfigServices.forEach(config -> configServiceMap.put(config.type(), config));
}
}

@PostMapping(path = "/{type}")
@Operation(summary = "Save the sender config", description = "保存公共配置")
public ResponseEntity<Message<String>> saveOrUpdateConfig(
@Parameter(description = "Config Type", example = "email")
@PathVariable("type") @NotNull final String type,
@RequestBody Object config) {
GeneralConfigService configService = configServiceMap.get(type);
if (configService == null) {
throw new IllegalArgumentException("Not supported this config type: " + type);
}
configService.saveConfig(config);
return ResponseEntity.ok(new Message<>("Update config success"));
}

@GetMapping(path = "/{type}")
@Operation(summary = "Get the sender config", description = "获取发送端配置")
public ResponseEntity<Message<Object>> getConfig(
@Parameter(description = "Config Type", example = "email")
@PathVariable("type") @NotNull final String type){
GeneralConfigService configService = configServiceMap.get(type);
if (configService == null) {
throw new IllegalArgumentException("Not supported this config type: " + type);
}
return ResponseEntity.ok(new Message<>(configService.getConfig()));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.stereotype.Component;

/**
* 消息通知服务端配置Dao
* 公共服务端配置Dao
*
* <p>该接口继承了JpaRepository和JpaSpecificationExecutor两个接口,提供基本的CRUD操作和规范查询能力。</p>
*
Expand All @@ -16,20 +16,12 @@
*/
@Component
public interface GeneralConfigDao extends JpaRepository<GeneralConfig, Long>, JpaSpecificationExecutor<GeneralConfig> {

/**
* 通过类型删除
*
* @param type 类型
* @return 返回受影响的行数
*/
int deleteByType(Byte type);


/**
* 通过类型查询
*
* @param type 类型
* @return 返回查询到的配置信息
*/
GeneralConfig findByType(Byte type);
}
GeneralConfig findByType(String type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author zqr
*/
@Data
public class NoticeSender {
public class EmailNoticeSender {

@NotNull(message = "类型不能为空|Type cannot be empty")
private Integer type;
Expand All @@ -30,7 +30,7 @@ public class NoticeSender {
@Min(message = "邮件端口不得小于1|Mail port must be greater than or equal to 1", value = 1)
private Integer emailPort;

private boolean emailSsl;
private boolean emailSsl = true;

private boolean emailEnable;
private boolean enable = true;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.dromara.hertzbeat.manager.service;

import java.util.List;

/**
* ConfigService接口,提供配置的增删查改操作。
*
Expand All @@ -11,31 +9,25 @@
* @version 1.0
*/
public interface GeneralConfigService<T> {



/**
* 保存配置。
*
* @param config 需要保存的配置
* @param enabled 是否启用
* config type: email, sms
* @return type string
*/
void saveConfig(T config, boolean enabled);

String type();
/**
* 删除配置。
* 保存更新配置
*
* @param config 需要保存的配置
*/
void deleteConfig();
void saveConfig(T config);

/**
* 获取配置
* 获取配置
*
* @return 查询到的配置
*/
T getConfig();

/**
* 获取所有配置。
*
* @return 查询到的所有配置集合
*/
List<T> getConfigs();
}
}
Loading