From 24227e62d41100e339506f284034bc5ac3a0a0de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marten=20Prie=C3=9F?= Date: Mon, 25 Jun 2018 14:27:31 +0200 Subject: [PATCH] replaced custom email-template-builder by separated project --- commons-auth-core/pom.xml | 5 + .../commons/service/email/EmailService.java | 11 +- .../service/email/EmailTemplateService.java | 52 ----- .../service/email/MailContentConfig.java | 5 +- .../email/SimpleMailContentConfig.java | 43 ++-- .../service/email/TemplateConfigBuilder.java | 106 ---------- .../main/resources/templates/email/base.html | 190 ------------------ .../main/resources/templates/email/base.txt | 21 -- .../email/EmailTemplateServiceTest.java | 52 ----- pom.xml | 1 + 10 files changed, 36 insertions(+), 450 deletions(-) delete mode 100644 commons-auth-core/src/main/java/io/rocketbase/commons/service/email/EmailTemplateService.java delete mode 100644 commons-auth-core/src/main/java/io/rocketbase/commons/service/email/TemplateConfigBuilder.java delete mode 100644 commons-auth-core/src/main/resources/templates/email/base.html delete mode 100644 commons-auth-core/src/main/resources/templates/email/base.txt delete mode 100644 commons-auth-core/src/test/java/io/rocketbase/commons/service/email/EmailTemplateServiceTest.java diff --git a/commons-auth-core/pom.xml b/commons-auth-core/pom.xml index 439eba85..ef4915c1 100755 --- a/commons-auth-core/pom.xml +++ b/commons-auth-core/pom.xml @@ -47,6 +47,11 @@ SOFTWARE. commons-rest-server ${commons-rest.version} + + io.rocketbase.commons + email-template-builder + ${email-template-builder.version} + com.google.guava diff --git a/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/EmailService.java b/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/EmailService.java index 6792f0ea..bfc3bd55 100644 --- a/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/EmailService.java +++ b/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/EmailService.java @@ -1,7 +1,7 @@ package io.rocketbase.commons.service.email; +import io.rocketbase.commons.email.model.HtmlTextEmail; import io.rocketbase.commons.model.AppUser; -import io.rocketbase.commons.service.email.EmailTemplateService.HtmlTextEmail; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; @@ -25,21 +25,16 @@ public class EmailService { @Resource private MailContentConfig mailContentConfig; - @Resource - private EmailTemplateService emailTemplateService; - @SneakyThrows public void sentRegistrationEmail(AppUser user, String verificationUrl) { - TemplateConfigBuilder register = mailContentConfig.register(user, verificationUrl); - HtmlTextEmail htmlTextEmail = emailTemplateService.buildHtmlTextTemplate(register); + HtmlTextEmail htmlTextEmail = mailContentConfig.register(user, verificationUrl); sentEmail(new InternetAddress(user.getEmail()), mailContentConfig.registerSubject(user), htmlTextEmail); } @SneakyThrows public void sentForgotPasswordEmail(AppUser user, String verificationUrl) { - TemplateConfigBuilder forgotPassword = mailContentConfig.forgotPassword(user, verificationUrl); - HtmlTextEmail htmlTextEmail = emailTemplateService.buildHtmlTextTemplate(forgotPassword); + HtmlTextEmail htmlTextEmail = mailContentConfig.forgotPassword(user, verificationUrl); sentEmail(new InternetAddress(user.getEmail()), mailContentConfig.forgotPasswordSubject(user), htmlTextEmail); } diff --git a/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/EmailTemplateService.java b/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/EmailTemplateService.java deleted file mode 100644 index 01022eed..00000000 --- a/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/EmailTemplateService.java +++ /dev/null @@ -1,52 +0,0 @@ -package io.rocketbase.commons.service.email; - -import com.mitchellbosecke.pebble.PebbleEngine; -import com.mitchellbosecke.pebble.error.PebbleException; -import com.mitchellbosecke.pebble.template.PebbleTemplate; -import io.rocketbase.commons.service.email.TemplateConfigBuilder; -import lombok.Data; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; - -import java.io.StringWriter; -import java.io.Writer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -@Slf4j -@Service -public class EmailTemplateService { - - private PebbleEngine engine = new PebbleEngine.Builder().build(); - - @SneakyThrows - public HtmlTextEmail buildHtmlTextTemplate(TemplateConfigBuilder configBuilder) { - try { - PebbleTemplate htmlTemplate = engine.getTemplate("templates/email/base.html"); - PebbleTemplate textTemplate = engine.getTemplate("templates/email/base.txt"); - - Writer htmlWriter = new StringWriter(); - htmlTemplate.evaluate(htmlWriter, configBuilder.getTemplate()); - - Writer textWrite = new StringWriter(); - textTemplate.evaluate(textWrite, configBuilder.getTemplate()); - - return new HtmlTextEmail(htmlWriter.toString(), textWrite.toString()); - } catch (PebbleException e) { - log.error("processing template error: {}", e.getPebbleMessage()); - throw new RuntimeException("processing template error"); - } - } - - @RequiredArgsConstructor - @Data - public static class HtmlTextEmail { - private final String html; - private final String text; - } -} diff --git a/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/MailContentConfig.java b/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/MailContentConfig.java index d89486f0..6cb88483 100644 --- a/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/MailContentConfig.java +++ b/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/MailContentConfig.java @@ -1,14 +1,15 @@ package io.rocketbase.commons.service.email; +import io.rocketbase.commons.email.model.HtmlTextEmail; import io.rocketbase.commons.model.AppUser; public interface MailContentConfig { - TemplateConfigBuilder register(AppUser user, String actionUrl); + HtmlTextEmail register(AppUser user, String actionUrl); String registerSubject(AppUser user); - TemplateConfigBuilder forgotPassword(AppUser user, String actionUrl); + HtmlTextEmail forgotPassword(AppUser user, String actionUrl); String forgotPasswordSubject(AppUser user); diff --git a/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/SimpleMailContentConfig.java b/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/SimpleMailContentConfig.java index db692e0d..61d5e8ef 100644 --- a/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/SimpleMailContentConfig.java +++ b/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/SimpleMailContentConfig.java @@ -1,6 +1,9 @@ package io.rocketbase.commons.service.email; import io.rocketbase.commons.config.EmailProperties; +import io.rocketbase.commons.email.EmailTemplateBuilder; +import io.rocketbase.commons.email.model.HtmlTextEmail; +import io.rocketbase.commons.email.template.ColorStyle; import io.rocketbase.commons.model.AppUser; import lombok.RequiredArgsConstructor; @@ -10,16 +13,17 @@ public class SimpleMailContentConfig implements MailContentConfig { final EmailProperties emailProperties; @Override - public TemplateConfigBuilder register(AppUser user, String actionUrl) { - return TemplateConfigBuilder.build() - .title("Please Verify Your Account") + public HtmlTextEmail register(AppUser user, String actionUrl) { + return EmailTemplateBuilder.builder() .header("Verify Your Account") - .addLine(String.format("Hi %s,", user.getUsername())) - .addLine("please verify your account by clicking the button") - .action(actionUrl, "verify your account") - .addGreeting(String.format("- %s", emailProperties.getServiceName())) - .receiveNote(emailProperties.getServiceName(), emailProperties.getSupportEmail()) - .copyright(emailProperties.getCopyrightUrl(), emailProperties.getCopyrightName()); + .addText(String.format("Hi %s,", user.getUsername())) + .addText("please verify your account by clicking the button") + .addButton("verify your account", actionUrl) + .addText(String.format("- %s", emailProperties.getServiceName())) + .addFooter(String.format("You’re receiving this email because you have an account in %s.
" + + "If you are not sure why you’re receiving this, please contact us %s", emailProperties.getServiceName(), emailProperties.getSupportEmail()), true) + .copyright(emailProperties.getCopyrightUrl(), emailProperties.getCopyrightName()) + .build(); } @Override @@ -28,16 +32,17 @@ public String registerSubject(AppUser user) { } @Override - public TemplateConfigBuilder forgotPassword(AppUser user, String actionUrl) { - return TemplateConfigBuilder.build() - .title("Reset Password") - .headerWithStyling("You have submitted a password change request!", "fff", "E63946") - .addLine(String.format("Hi %s,", user.getUsername())) - .addLine("if it was you, confirm the password change by clicking the button") - .actionWithStyling(actionUrl, "confirm password change", "fff", "E63946") - .addGreeting(String.format("- %s", emailProperties.getServiceName())) - .receiveNote(emailProperties.getServiceName(), emailProperties.getSupportEmail()) - .copyright(emailProperties.getCopyrightUrl(), emailProperties.getCopyrightName()); + public HtmlTextEmail forgotPassword(AppUser user, String actionUrl) { + return EmailTemplateBuilder.builder() + .header("You have submitted a password change request!", new ColorStyle("fff", "E63946")) + .addText(String.format("Hi %s,", user.getUsername())) + .addText("if it was you, confirm the password change by clicking the button") + .addButton("confirm password change", actionUrl, new ColorStyle("fff", "E63946")) + .addText(String.format("- %s", emailProperties.getServiceName())) + .addFooter(String.format("You’re receiving this email because you have an account in %s.
" + + "If you are not sure why you’re receiving this, please contact us %s", emailProperties.getServiceName(), emailProperties.getSupportEmail()), true) + .copyright(emailProperties.getCopyrightUrl(), emailProperties.getCopyrightName()) + .build(); } @Override diff --git a/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/TemplateConfigBuilder.java b/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/TemplateConfigBuilder.java deleted file mode 100644 index 8d5eae68..00000000 --- a/commons-auth-core/src/main/java/io/rocketbase/commons/service/email/TemplateConfigBuilder.java +++ /dev/null @@ -1,106 +0,0 @@ -package io.rocketbase.commons.service.email; - -import lombok.Data; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class TemplateConfigBuilder { - - private static ColorStyle BASE_STYLE = new ColorStyle("ffffff", "457B9D"); - @Getter - private Map template = new HashMap<>(); - - private TemplateConfigBuilder() { - } - - public static TemplateConfigBuilder build() { - return new TemplateConfigBuilder(); - } - - public TemplateConfigBuilder header(String title) { - template.put("header", new Header(title, BASE_STYLE)); - return this; - } - - public TemplateConfigBuilder headerWithStyling(String title, String colorText, String colorBg) { - template.put("header", new Header(title, new ColorStyle(colorText, colorBg))); - return this; - } - - public TemplateConfigBuilder title(String title) { - template.put("title", title); - return this; - } - - public TemplateConfigBuilder action(String url, String text) { - template.put("action", new Action(url, text, BASE_STYLE)); - return this; - } - - public TemplateConfigBuilder actionWithStyling(String url, String text, String colorText, String colorBg) { - template.put("action", new Action(url, text, new ColorStyle(colorText, colorBg))); - return this; - } - - public TemplateConfigBuilder copyright(String url, String name) { - template.put("copyright", new Copyright(url, name)); - return this; - } - - public TemplateConfigBuilder receiveNote(String serviceName, String supportEmail) { - template.put("receiveNote", true); - template.put("serviceName", serviceName); - template.put("supportEmail", supportEmail); - return this; - } - - public TemplateConfigBuilder addLine(String line) { - return addToList("lines", line); - } - - public TemplateConfigBuilder addGreeting(String greeting) { - return addToList("greetings", greeting); - } - - private TemplateConfigBuilder addToList(String key, String text) { - if (!template.containsKey(key)) { - template.put(key, new ArrayList<>()); - } - ((List) template.get(key)).add(text); - return this; - } - - @RequiredArgsConstructor - @Data - private static class ColorStyle { - private final String text; - private final String bg; - } - - @RequiredArgsConstructor - @Data - private static class Header { - private final String title; - private final ColorStyle color; - } - - @RequiredArgsConstructor - @Data - private static class Action { - private final String url; - private final String text; - private final ColorStyle color; - } - - @RequiredArgsConstructor - @Data - private static class Copyright { - private final String url; - private final String name; - } -} diff --git a/commons-auth-core/src/main/resources/templates/email/base.html b/commons-auth-core/src/main/resources/templates/email/base.html deleted file mode 100644 index 959ad9d9..00000000 --- a/commons-auth-core/src/main/resources/templates/email/base.html +++ /dev/null @@ -1,190 +0,0 @@ - - - - - - {{ title }} - - - - - - - - - - - - -
-
- - - - - - - -
- {{ header.title }} -
- - - {% for line in lines %} - - - - {% endfor %} - - {% if action != null %} - - - - {% endif %} - - {% for greet in greetings %} - - - - {% endfor %} -
- {{ line | raw}} -
- {{ - action.text }} -
- {{ greet | raw}} -
-
- {% if receiveNote != null or copyright != null %} - - {% endif %} -
-
- - \ No newline at end of file diff --git a/commons-auth-core/src/main/resources/templates/email/base.txt b/commons-auth-core/src/main/resources/templates/email/base.txt deleted file mode 100644 index 3d14415f..00000000 --- a/commons-auth-core/src/main/resources/templates/email/base.txt +++ /dev/null @@ -1,21 +0,0 @@ -{{ header.title }} - -{% for line in lines %} -{{ line | raw}} -{% endfor %} - -{% if action != null %} -{{ action.text }} -> {{ action.url }} -{% endif %} - -{% for greet in greetings %} -{{ greet | raw}} -{% endfor %} - -{% if receiveNote != null %} -You’re receiving this email because you have an account in {{ serviceName }}. -If you are not sure why you’re receiving this, please contact us {{ supportEmail }}. -{% endif %} -{% if copyright != null%} -{{ copyright.date | date("yyyy") }} {{ copyright.name }} -{% endif %} \ No newline at end of file diff --git a/commons-auth-core/src/test/java/io/rocketbase/commons/service/email/EmailTemplateServiceTest.java b/commons-auth-core/src/test/java/io/rocketbase/commons/service/email/EmailTemplateServiceTest.java deleted file mode 100644 index 9ca1c573..00000000 --- a/commons-auth-core/src/test/java/io/rocketbase/commons/service/email/EmailTemplateServiceTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package io.rocketbase.commons.service.email; - -import org.junit.Test; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; - -public class EmailTemplateServiceTest { - - @Test - public void buildHtmlTextTemplate() { - // given - String title = "Please Verify Your Account"; - String header = "Verify Your Account"; - String firstLine = "first line"; - String second = "second"; - String url = "http://localhost/?verification=test123"; - String clickHere = "click here"; - String greeting = "- cheers"; - TemplateConfigBuilder templateConfigBuilder = TemplateConfigBuilder.build() - .title(title) - .header(header) - .addLine(firstLine) - .addLine(second) - .action(url, clickHere) - .addGreeting(greeting) - .receiveNote("commons-auth", "support@rocketbase.io") - .copyright("https://www.rocketbase.io", "rocketbase.io"); - - EmailTemplateService emailTemplateService = new EmailTemplateService(); - // when - - EmailTemplateService.HtmlTextEmail htmlTextEmail = emailTemplateService.buildHtmlTextTemplate(templateConfigBuilder); - - // then - - assertThat(htmlTextEmail, notNullValue()); - assertThat(htmlTextEmail.getHtml(), notNullValue()); - assertThat(htmlTextEmail.getHtml(), containsString(title)); - assertThat(htmlTextEmail.getHtml(), containsString(header)); - assertThat(htmlTextEmail.getHtml(), containsString(firstLine)); - assertThat(htmlTextEmail.getHtml(), containsString(second)); - assertThat(htmlTextEmail.getHtml(), containsString(url)); - assertThat(htmlTextEmail.getHtml(), containsString(clickHere)); - assertThat(htmlTextEmail.getHtml(), containsString(greeting)); - assertThat(htmlTextEmail.getText(), notNullValue()); - assertThat(htmlTextEmail.getText(), startsWith("Verify Your Account\n" + - "first linesecond\n" + - "click here -> http://localhost/?verification=test123\n" + - "- cheers\n")); - } -} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 24aab24c..811066cf 100755 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,7 @@ SOFTWARE. 2.6.0 25.1-jre 1.0 + 0.1.0