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