Skip to content

Commit

Permalink
fix: mail from address property, jdbc url check
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardShan committed Nov 6, 2019
1 parent 869bff6 commit d947f52
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 17 deletions.
1 change: 1 addition & 0 deletions config/application.yml.example
Expand Up @@ -99,6 +99,7 @@ spring:
host:
port:
username:
fromAddress:
password:
nickname:

Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Expand Up @@ -37,10 +37,12 @@
</properties>

<repositories>
<!--
<repository>
<id>alimaven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
-->
<repository>
<id>central</id>
<name>Maven Repository</name>
Expand Down Expand Up @@ -208,5 +210,4 @@
</plugins>
</build>


</project>
Expand Up @@ -63,6 +63,8 @@ public void execute(JobExecutionContext jobExecutionContext) {
try {
scheduleService.execute(scheduleJob.getId());
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
scheduleLogger.error(e.getMessage());
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions server/src/main/java/edp/core/consts/Consts.java
Expand Up @@ -31,6 +31,8 @@ public class Consts {

public static final String SLASH = "/";

public static final String DOUBLE_SLASH = "//";

public static final String SPACE = " ";

public static final String EMPTY = "";
Expand Down
13 changes: 13 additions & 0 deletions server/src/main/java/edp/core/model/MailContent.java
Expand Up @@ -29,6 +29,7 @@
import lombok.Setter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand All @@ -52,6 +53,18 @@ public class MailContent {
private Map<String, Object> templateContent;
private List<MailAttachment> attachments;

@Override
public String toString() {
return "MailContent{" +
"from='" + from + '\'' +
", subject='" + subject + '\'' +
", to=" + Arrays.toString(to) +
", cc=" + Arrays.toString(cc) +
", bcc=" + Arrays.toString(bcc) +
", attachments.size=" + (CollectionUtils.isEmpty(attachments) ? 0 : attachments.size()) +
'}';
}

public static final class MailContentBuilder {
private String from;
private String nickName;
Expand Down
35 changes: 28 additions & 7 deletions server/src/main/java/edp/core/utils/MailUtils.java
Expand Up @@ -20,11 +20,11 @@
package edp.core.utils;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Stopwatch;
import edp.core.exception.ServerException;
import edp.core.model.MailContent;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
Expand Down Expand Up @@ -54,14 +54,14 @@ public class MailUtils {
@Value("${spring.mail.username}")
private String mailUsername;

@Value("{spring.mail.fromAddress:}")
@Value("${spring.mail.fromAddress:}")
private String fromAddress;

@Value("${spring.mail.nickname}")
private String nickName;


public void sendMail(MailContent mailContent) throws ServerException {
public void sendMail(MailContent mailContent, Logger customLogger) throws ServerException {
Stopwatch watch = Stopwatch.createStarted();
if (mailContent == null) {
throw new ServerException("Mail content is null");
Expand All @@ -73,7 +73,10 @@ public void sendMail(MailContent mailContent) throws ServerException {
if (!StringUtils.isEmpty(mailContent.getFrom())) {
Matcher matcher = PATTERN_EMAIL_FORMAT.matcher(mailContent.getFrom());
if (!matcher.find()) {
log.info("Unknown email sending address: {}", mailContent.getFrom());
log.error("Unknown email sending address: {}", mailContent.getFrom());
if (customLogger != null) {
customLogger.error("Unknown email sending address: {}", mailContent.getFrom());
}
throw new ServerException("Unknown email sending address: " + mailContent.getFrom());
}
from = mailContent.getFrom();
Expand All @@ -84,12 +87,18 @@ public void sendMail(MailContent mailContent) throws ServerException {
}

if (StringUtils.isEmpty(mailContent.getSubject())) {
log.info("Email subject cannot be EMPTY");
log.error("Email subject cannot be EMPTY");
if (customLogger != null) {
customLogger.error("Email subject cannot be EMPTY");
}
throw new ServerException("Email subject cannot be EMPTY");
}

if (null == mailContent.getTo() || mailContent.getTo().length < 1) {
log.info("Email receiving address(to) cannot be EMPTY");
log.error("Email receiving address(to) cannot be EMPTY");
if (customLogger != null) {
log.error("Email receiving address(to) cannot be EMPTY");
}
throw new ServerException("Email receiving address cannot be EMPTY");
}

Expand Down Expand Up @@ -156,18 +165,30 @@ public void sendMail(MailContent mailContent) throws ServerException {
}
} catch (MessagingException e) {
log.warn(e.getMessage());
if (customLogger != null) {
log.warn(e.getMessage());
}
}
});
}

javaMailSender.send(message);
log.info("MailUtil.sendMail sending: MailContent: {}, cost: {}", JSONObject.toJSONString(mailContent), watch.elapsed(TimeUnit.MILLISECONDS));
log.info("Email sending --- content: {}, cost: {}", mailContent.toString(), watch.elapsed(TimeUnit.MILLISECONDS));
if (customLogger != null) {
customLogger.info("Email sending --- content: {}, cost: {}", mailContent.toString(), watch.elapsed(TimeUnit.MILLISECONDS));
}
} catch (MessagingException e) {
log.error("Send mail failed, {}\n", e.getMessage());
if (customLogger != null) {
customLogger.error("Send mail failed, {}\n", e.getMessage());
}
e.printStackTrace();
throw new ServerException(e.getMessage());
} catch (UnsupportedEncodingException e) {
log.error("Send mail failed, {}\n", e.getMessage());
if (customLogger != null) {
customLogger.error("Send mail failed, {}\n", e.getMessage());
}
e.printStackTrace();
}
}
Expand Down
7 changes: 7 additions & 0 deletions server/src/main/java/edp/core/utils/SourceUtils.java
Expand Up @@ -177,6 +177,13 @@ public static String isSupportedDatasource(String jdbcUrl) {
if (!LoadSupportDataSourceRunner.getSupportDatasourceMap().containsKey(dataSourceName)) {
throw new SourceException("Not supported data type: jdbcUrl=" + jdbcUrl);
}

String urlPrefix = String.format(JDBC_PREFIX_FORMATER, dataSourceName);
String checkUrl = jdbcUrl.replaceFirst(DOUBLE_SLASH, EMPTY).replaceFirst(AT_SYMBOL, EMPTY);
if (urlPrefix.equals(checkUrl)) {
throw new SourceException("Communications link failure");
}

return dataSourceName;
}

Expand Down
Expand Up @@ -131,7 +131,7 @@ public void execute(long jobId) throws Exception {
return;
}

if (null == cronJobConfig || !StringUtils.isEmpty(cronJobConfig.getType())) {
if (null == cronJobConfig || StringUtils.isEmpty(cronJobConfig.getType())) {
log.warn("cron job config is not expected format: {}", cronJob.getConfig());
scheduleLogger.warn("cron job config is not expected format: {}", cronJob.getConfig());
return;
Expand All @@ -156,11 +156,6 @@ public void execute(long jobId) throws Exception {
excels = generateExcels(jobId, cronJobConfig, creater);
}

if (CollectionUtils.isEmpty(excels) && CollectionUtils.isEmpty(images)) {
log.warn("CronJob (:{}) Email content is empty", jobId);
return;
}

List<MailAttachment> attachmentList = new ArrayList<>();

if (!CollectionUtils.isEmpty(excels)) {
Expand All @@ -173,6 +168,12 @@ public void execute(long jobId) throws Exception {
});
}

if (CollectionUtils.isEmpty(attachmentList)) {
log.warn("CronJob (:{}) Email content is empty", jobId);
scheduleLogger.warn("CronJob (:{}) Email content is empty", jobId);
return;
}

MailContent mailContent = null;
try {
mailContent = MailContent.MailContentBuilder.builder()
Expand All @@ -189,7 +190,7 @@ public void execute(long jobId) throws Exception {
log.error("EmailScheduleServiceImpl.execute, build MailContent error: {}", e.getMessage());
scheduleLogger.error("EmailScheduleServiceImpl.execute, build MailContent error: {}", e.getMessage());
}
mailUtils.sendMail(mailContent);
mailUtils.sendMail(mailContent, null);
scheduleLogger.info("CronJob (:{}) is finish! --------------", jobId);
}

Expand Down
Expand Up @@ -383,7 +383,7 @@ public void inviteMember(Long orgId, Long memId, User user) throws NotFoundExcep
.withTemplateContent(content)
.build();

mailUtils.sendMail(mailContent);
mailUtils.sendMail(mailContent, null);
} catch (ServerException e) {
log.info(e.getMessage());
e.printStackTrace();
Expand Down
Expand Up @@ -349,7 +349,7 @@ public boolean sendMail(String email, User user) throws ServerException {
.withTemplateContent(content)
.build();

mailUtils.sendMail(mailContent);
mailUtils.sendMail(mailContent, null);
return true;
}

Expand Down

0 comments on commit d947f52

Please sign in to comment.