From ad8de0f6c0adb5a7bfe07b4fb76eaf962a42d10c Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Fri, 25 Mar 2016 22:10:41 +0100 Subject: [PATCH] Spring emails: using Thymeleaf and Velocity --- SpringJava/Emails/spring-emails-web-client/pom.xml | 9 ++++ .../services/impl/EmailMakerServiceImpl.java | 48 ---------------------- .../impl/EmailMakerThymeleafServiceImpl.java | 44 ++++++++++++++++++++ .../impl/EmailMakerVelocityServiceImpl.java | 48 ++++++++++++++++++++++ .../rest/controller/EmailController.java | 18 ++++---- .../resources/email/email-template-thymeleaf.html | 35 ++++++++++++++++ .../src/main/resources/email/email-template.vm | 11 ++++- .../spring-configuration/spring-configuration.xml | 47 ++++++++++++++------- SpringJava/Emails/spring-emails/pom.xml | 14 ++++++- 9 files changed, 202 insertions(+), 72 deletions(-) delete mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerServiceImpl.java create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerThymeleafServiceImpl.java create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerVelocityServiceImpl.java create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template-thymeleaf.html diff --git a/SpringJava/Emails/spring-emails-web-client/pom.xml b/SpringJava/Emails/spring-emails-web-client/pom.xml index bf5ac7e..5dd948c 100644 --- a/SpringJava/Emails/spring-emails-web-client/pom.xml +++ b/SpringJava/Emails/spring-emails-web-client/pom.xml @@ -86,6 +86,15 @@ hibernate-validator + + + org.thymeleaf + thymeleaf + + + org.thymeleaf + thymeleaf-spring4 + diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerServiceImpl.java b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerServiceImpl.java deleted file mode 100644 index da9f4bf..0000000 --- a/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerServiceImpl.java +++ /dev/null @@ -1,48 +0,0 @@ -package de.spring.emails.services.impl; - -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - -import org.apache.velocity.app.VelocityEngine; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.MessageSource; -import org.springframework.stereotype.Service; -import org.springframework.ui.velocity.VelocityEngineUtils; - -import de.spring.emails.services.EmailMakerService; - - -@Service("emailMakerService") -public class EmailMakerServiceImpl implements EmailMakerService { - private static final String TEMPLATES_DEFAULT_EXTENSION = ".vm"; - private static final String TEMPLATES_DEFAULT_PATH = "email/"; - private static final String EMAIL_CONTENT_ENCODING = "UTF-8"; - - private final VelocityEngine velocityEngine; - private final MessageSource messageSource; - - @Autowired - public EmailMakerServiceImpl(VelocityEngine velocityEngine, MessageSource messageSource) { - this.velocityEngine = velocityEngine; - this.messageSource = messageSource; - } - - @Override - public String emailMaker(Map text, String templateName, Locale locale) { - final String templateLocation = TEMPLATES_DEFAULT_PATH + templateName + TEMPLATES_DEFAULT_EXTENSION; - final Map model = new HashMap<>(); - model.put("text", text); - model.put("messageSource", messageSource); - model.put("locale", locale); - - return VelocityEngineUtils.mergeTemplateIntoString( - velocityEngine, templateLocation, EMAIL_CONTENT_ENCODING, model); - } - - @Override - public String getSubject(String code, Locale locale, Object... args) { - return messageSource.getMessage(code, args, locale); - } - -} diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerThymeleafServiceImpl.java b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerThymeleafServiceImpl.java new file mode 100644 index 0000000..97f710f --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerThymeleafServiceImpl.java @@ -0,0 +1,44 @@ +package de.spring.emails.services.impl; + +import java.util.Arrays; +import java.util.Date; +import java.util.Locale; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.MessageSource; +import org.springframework.stereotype.Service; +import org.thymeleaf.TemplateEngine; +import org.thymeleaf.context.Context; + +import de.spring.emails.services.EmailMakerService; + +@Service("emailMakerThymeleafService") +public class EmailMakerThymeleafServiceImpl implements EmailMakerService { + private final TemplateEngine templateEngine; + private final MessageSource messageSource; + + @Autowired + public EmailMakerThymeleafServiceImpl(TemplateEngine templateEngine, MessageSource messageSource) { + this.templateEngine = templateEngine; + this.messageSource = messageSource; + } + + @Override + public String emailMaker(Map text, String templateLocation, Locale locale) { + final Context ctx = new Context(locale); + ctx.setVariable("name", "Gustavo Martin Morcuende"); + ctx.setVariable("subscriptionDate", new Date()); + ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music")); + ctx.setVariable("imageResourceName", "imageResourceName"); + + return this.templateEngine.process("email-inlineimage.html", ctx); + } + + @Override + public String getSubject(String code, Locale locale, Object... args) { + return messageSource.getMessage(code, args, locale); + } + +} + diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerVelocityServiceImpl.java b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerVelocityServiceImpl.java new file mode 100644 index 0000000..aedb305 --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerVelocityServiceImpl.java @@ -0,0 +1,48 @@ +package de.spring.emails.services.impl; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import org.apache.velocity.app.VelocityEngine; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.MessageSource; +import org.springframework.stereotype.Service; +import org.springframework.ui.velocity.VelocityEngineUtils; + +import de.spring.emails.services.EmailMakerService; + + +@Service("emailMakerVelocityService") +public class EmailMakerVelocityServiceImpl implements EmailMakerService { + private static final String TEMPLATES_DEFAULT_EXTENSION = ".vm"; + private static final String TEMPLATES_DEFAULT_PATH = "email/"; + private static final String EMAIL_CONTENT_ENCODING = "UTF-8"; + + private final VelocityEngine velocityEngine; + private final MessageSource messageSource; + + @Autowired + public EmailMakerVelocityServiceImpl(VelocityEngine velocityEngine, MessageSource messageSource) { + this.velocityEngine = velocityEngine; + this.messageSource = messageSource; + } + + @Override + public String emailMaker(Map text, String templateName, Locale locale) { + final String templateLocation = TEMPLATES_DEFAULT_PATH + templateName + TEMPLATES_DEFAULT_EXTENSION; + final Map model = new HashMap<>(); + model.put("text", text); + model.put("messageSource", messageSource); + model.put("locale", locale); + + return VelocityEngineUtils.mergeTemplateIntoString( + velocityEngine, templateLocation, EMAIL_CONTENT_ENCODING, model); + } + + @Override + public String getSubject(String code, Locale locale, Object... args) { + return messageSource.getMessage(code, args, locale); + } + +} diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/webservices/rest/controller/EmailController.java b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/webservices/rest/controller/EmailController.java index 4bcc5b2..25bebd5 100644 --- a/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/webservices/rest/controller/EmailController.java +++ b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/webservices/rest/controller/EmailController.java @@ -29,25 +29,28 @@ public class EmailController { private static final String USER = "Gustavo Martin Morcuende"; private static final String USER_ADDRESS = "noemail@gumartinm.name"; private static final String TEMPLATE = "email-template"; + private static final String LOGO = "logo"; + private static final String LOGO_RESOURCE = "email/logo.png"; private static final String SUBJECT_MESSAGE_KEY = "email.subject"; private final EmailService emailService; - private final EmailMakerService emailMakerService; + private final EmailMakerService emailMakerVelocityService; @Autowired - public EmailController(EmailService emailService, EmailMakerService emailMakerService) { + public EmailController(EmailService emailService, EmailMakerService emailMakerVelocityService) { this.emailService = emailService; - this.emailMakerService = emailMakerService; + this.emailMakerVelocityService = emailMakerVelocityService; } @RequestMapping(method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public void emails() throws MessagingException { - final String emailSubject = emailMakerService.getSubject(SUBJECT_MESSAGE_KEY, Locale.getDefault()); + final String emailSubject = emailMakerVelocityService.getSubject(SUBJECT_MESSAGE_KEY, Locale.getDefault()); final String emailText = doEmailText(); - final String[] to = { USER_ADDRESS }; final Map inline = new HashMap<>(); - inline.put("cid:mainlogo", new ClassPathResource("email/logo.png")); + inline.put(LOGO, new ClassPathResource(LOGO_RESOURCE)); + final String[] to = { USER_ADDRESS }; + try { emailService.sendEmailAsync(to, emailSubject, emailText, true, null, inline); } catch (MessagingException ex) { @@ -60,6 +63,7 @@ public class EmailController { final Map text = new HashMap<>(); text.put("user", USER); text.put("date", isoDateTime); - return emailMakerService.emailMaker(text, TEMPLATE, Locale.getDefault()); + text.put(LOGO, LOGO); + return emailMakerVelocityService.emailMaker(text, TEMPLATE, Locale.getDefault()); } } diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template-thymeleaf.html b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template-thymeleaf.html new file mode 100644 index 0000000..360148b --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template-thymeleaf.html @@ -0,0 +1,35 @@ + + + + Thymeleaf template for HTML email + + + +

+ Hello, Peter Static! +

+

+ Wow! You've got a long name (more than 10 chars)! +

+

+ You have been successfully subscribed to the Fake newsletter on + 28-12-2012 +

+

Your hobbies are:

+
    +
  • Reading
  • +
  • Writing
  • +
  • Bowling
  • +
+

+ You can find your inlined image just below this text. +

+

+ +

+

+ Regards,
+ The Thymeleaf Team +

+ + \ No newline at end of file diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template.vm b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template.vm index a5fc478..c432394 100644 --- a/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template.vm +++ b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template.vm @@ -1,4 +1,8 @@ + + Velocity template for HTML email + +

#msg("email.header1")

#msg("email.header2")/h2> @@ -11,7 +15,10 @@

${text.date}

- + +

+ Regards,
+ Gustavo Martin Morcuende +

- \ No newline at end of file diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/spring-configuration.xml b/SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/spring-configuration.xml index fa22293..5bd90ec 100644 --- a/SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/spring-configuration.xml +++ b/SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/spring-configuration.xml @@ -33,6 +33,20 @@ + + + + email.email-messages + + + + + + + + + @@ -45,23 +59,28 @@ + + + + - - - email.email-messages - - + + + + + + + - - - + + - - - + + - + diff --git a/SpringJava/Emails/spring-emails/pom.xml b/SpringJava/Emails/spring-emails/pom.xml index bf9c1a3..7f8db54 100644 --- a/SpringJava/Emails/spring-emails/pom.xml +++ b/SpringJava/Emails/spring-emails/pom.xml @@ -173,7 +173,7 @@ - + org.springframework spring-context-support @@ -199,6 +199,18 @@ velocity-tools 2.0-beta1 + + + + org.thymeleaf + thymeleaf + 3.0.0.BETA02 + + + org.thymeleaf + thymeleaf-spring4 + 3.0.0.BETA02 + -- 2.1.4