refactor: 更改获取邮件附件方式

This commit is contained in:
椰子 2021-07-09 09:21:01 +08:00
parent aa1cbbdd93
commit cb84a0f86a
2 changed files with 27 additions and 51 deletions

View File

@ -41,36 +41,17 @@
<version>1.6.7</version> <version>1.6.7</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
<version>5.2.10.RELEASE</version> <version>5.2.10.RELEASE</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.3.11.RELEASE</version>
</dependency>
<dependency> <dependency>
<groupId>org.jsoup</groupId> <groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId> <artifactId>jsoup</artifactId>

View File

@ -6,25 +6,26 @@ import com.cicdi.notify.email.EmailProvider;
import com.cicdi.notify.email.EmailTemplate; import com.cicdi.notify.email.EmailTemplate;
import com.cicdi.notify.email.EmailTemplateParsed; import com.cicdi.notify.email.EmailTemplateParsed;
import com.cicdi.notify.template.TemplateManager; import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.util.ExpressionUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.InputStreamSource; import org.springframework.core.io.InputStreamSource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.client.WebClient;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility; import javax.mail.internet.MimeUtility;
import java.io.FileInputStream; import java.io.*;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
@ -69,11 +70,7 @@ public class DefaultEmailNotifier extends AbstractNotifier<EmailTemplate> {
public DefaultEmailNotifier(NotifierProperties properties, TemplateManager templateManager) { public DefaultEmailNotifier(NotifierProperties properties, TemplateManager templateManager) {
super(templateManager); super(templateManager);
notifierId = properties.getId(); notifierId = properties.getId();
DefaultEmailProperties emailProperties = new JSONObject(properties.getConfiguration()).toJavaObject(DefaultEmailProperties.class);
DefaultEmailProperties emailProperties = new JSONObject(properties.getConfiguration())
.toJavaObject(DefaultEmailProperties.class);
// ValidatorUtils.tryValidate(emailProperties);
JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(emailProperties.getHost()); mailSender.setHost(emailProperties.getHost());
mailSender.setPort(emailProperties.getPort()); mailSender.setPort(emailProperties.getPort());
@ -123,25 +120,24 @@ public class DefaultEmailNotifier extends AbstractNotifier<EmailTemplate> {
} }
Set<Map.Entry<String, String>> imageEntries = template.getImages().entrySet(); Set<Map.Entry<String, String>> imageEntries = template.getImages().entrySet();
for (Map.Entry<String, String> image : imageEntries) { for (Map.Entry<String, String> image : imageEntries) {
helper.addInline(image.getKey(), convertResource(image.getValue()), MediaType.APPLICATION_OCTET_STREAM_VALUE); helper.addInline(image.getKey(), convertResource(image.getValue()), "application/octet-stream");
} }
this.javaMailSender.send(mimeMessage); this.javaMailSender.send(mimeMessage);
} }
protected InputStreamSource convertResource(String resource) { protected InputStreamSource convertResource(String resource) {
if (resource.startsWith("http")) { if (resource.startsWith("http://") || resource.startsWith("https://")) {
// Mono<Resource> resourceMono = HttpClient httpClient = HttpClientBuilder.create().build();
// InputStream is = new FileInputStream(); HttpGet httpGet = new HttpGet(resource);
// InputStreamSource iss = new InputStreamResource(is, null); httpGet.setHeader(HttpHeaders.ACCEPT, "application/octet-stream");
// TODO 使用其他工具实现不用反应式框架 try {
WebClient.create() HttpResponse response = httpClient.execute(httpGet);
.get() InputStream inputStream = response.getEntity().getContent();
.uri(resource) new InputStreamResource(inputStream);
.accept(MediaType.APPLICATION_OCTET_STREAM) } catch (IOException e) {
.exchange() e.printStackTrace();
.flatMap(rep -> rep.bodyToMono(Resource.class)) }
;
} else { } else {
try { try {
return new InputStreamResource(new FileInputStream(resource)); return new InputStreamResource(new FileInputStream(resource));
@ -157,7 +153,7 @@ public class DefaultEmailNotifier extends AbstractNotifier<EmailTemplate> {
String subject = template.getSubject(); String subject = template.getSubject();
String text = template.getText(); String text = template.getText();
if (StringUtils.isEmpty(subject) || StringUtils.isEmpty(text)) { if (StringUtils.isEmpty(subject) || StringUtils.isEmpty(text)) {
// throw new BusinessException("模板内容错误text 或者 subject 不能为空."); throw new RuntimeException("模板内容错误text 或者 subject 不能为空.");
} }
String sendText = render(text, context); String sendText = render(text, context);
List<EmailTemplate.Attachment> tempAttachments = template.getAttachments(); List<EmailTemplate.Attachment> tempAttachments = template.getAttachments();
@ -192,8 +188,7 @@ public class DefaultEmailNotifier extends AbstractNotifier<EmailTemplate> {
} }
private String render(String str, Map<String, Object> context) { private String render(String str, Map<String, Object> context) {
return str; return ExpressionUtils.analytical(str, context);
// return ExpressionUtils.analytical(str, context, "spel");
} }
} }