diff --git a/notify-email/pom.xml b/notify-email/pom.xml index f2eff15..383742e 100644 --- a/notify-email/pom.xml +++ b/notify-email/pom.xml @@ -41,36 +41,17 @@ 1.6.7 + + org.apache.httpcomponents + httpclient + + org.springframework spring-web 5.2.10.RELEASE - - org.springframework.boot - spring-boot - 2.3.11.RELEASE - - - - org.springframework.boot - spring-boot-starter-web - 2.3.5.RELEASE - - - - org.springframework.boot - spring-boot-starter-webflux - 2.3.5.RELEASE - - - - org.springframework.boot - spring-boot-autoconfigure - 2.3.11.RELEASE - - org.jsoup jsoup diff --git a/notify-email/src/main/java/com/cicdi/notify/email/embedded/DefaultEmailNotifier.java b/notify-email/src/main/java/com/cicdi/notify/email/embedded/DefaultEmailNotifier.java index 74ba6ed..2fa4cee 100644 --- a/notify-email/src/main/java/com/cicdi/notify/email/embedded/DefaultEmailNotifier.java +++ b/notify-email/src/main/java/com/cicdi/notify/email/embedded/DefaultEmailNotifier.java @@ -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 { 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 { } Set> imageEntries = template.getImages().entrySet(); for (Map.Entry 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 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 { 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 tempAttachments = template.getAttachments(); @@ -192,8 +188,7 @@ public class DefaultEmailNotifier extends AbstractNotifier { } private String render(String str, Map context) { - return str; -// return ExpressionUtils.analytical(str, context, "spel"); + return ExpressionUtils.analytical(str, context); } }