improved templating service

This commit is contained in:
2019-03-16 12:44:13 +01:00
parent e75523e9dd
commit b49fb0e19d
2 changed files with 58 additions and 35 deletions

View File

@ -119,6 +119,22 @@ public class MailService implements Serializable {
public String sendHTMLMail(String recipient, String ccRecipient, String bccRecipient, String subject, public String sendHTMLMail(String recipient, String ccRecipient, String bccRecipient, String subject,
MailDatamodel dataModel, String htmlTemplateName, String plainTemplateName, String encoding, List<File> attachments) throws MailException { MailDatamodel dataModel, String htmlTemplateName, String plainTemplateName, String encoding, List<File> attachments) throws MailException {
try {
String htmlBody = mailTemplateService.getStringFromTemplate(htmlTemplateName, dataModel);
String plainBody;
if (plainTemplateName != null) {
plainBody = mailTemplateService.getStringFromTemplate(plainTemplateName, dataModel);
} else {
plainBody = null;
}
return sendHTMLMail(recipient, ccRecipient, bccRecipient, subject, htmlBody, plainBody, encoding, attachments);
} catch (MailTemplateException ex) {
throw new MailException("Error while sending email.", ex);
}
}
public String sendHTMLMail(String recipient, String ccRecipient, String bccRecipient, String subject, String htmlBody, String plainBody, String encoding, List<File> attachments) throws MailException {
try { try {
MimeMessage message = new MimeMessage(mailSession); MimeMessage message = new MimeMessage(mailSession);
message.setFrom(); // use default from message.setFrom(); // use default from
@ -134,19 +150,16 @@ public class MailService implements Serializable {
Multipart contentMultiPart = new MimeMultipart("alternative"); Multipart contentMultiPart = new MimeMultipart("alternative");
String htmlBody = mailTemplateService.getStringFromTemplate(htmlTemplateName, dataModel);
MimeBodyPart htmlBodyPart = new MimeBodyPart(); MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(htmlBody, "text/html; charset=UTF-8"); htmlBodyPart.setContent(htmlBody, "text/html; charset=UTF-8");
contentMultiPart.addBodyPart(htmlBodyPart); contentMultiPart.addBodyPart(htmlBodyPart);
if (plainTemplateName != null) { if (plainBody != null) {
String plainBody = mailTemplateService.getStringFromTemplate(plainTemplateName, dataModel);
MimeBodyPart plainBodyPart = new MimeBodyPart(); MimeBodyPart plainBodyPart = new MimeBodyPart();
plainBodyPart.setText(plainBody, "UTF-8"); plainBodyPart.setText(plainBody, "UTF-8");
contentMultiPart.addBodyPart(plainBodyPart); contentMultiPart.addBodyPart(plainBodyPart);
} }
if ((attachments == null) || (attachments.isEmpty())) { if ((attachments == null) || (attachments.isEmpty())) {
message.setContent(contentMultiPart); message.setContent(contentMultiPart);
} else { } else {
@ -168,12 +181,10 @@ public class MailService implements Serializable {
throw new MailException("Cannot attach " + attachment.toString() + " to email. Reason: " + ex.toString(), ex); throw new MailException("Cannot attach " + attachment.toString() + " to email. Reason: " + ex.toString(), ex);
} }
} }
message.setContent(messageMultipart); message.setContent(messageMultipart);
} }
return transportMail(message); return transportMail(message);
} catch (MessagingException | MailTemplateException ex) { } catch (MessagingException ex) {
throw new MailException("Error while sending email.", ex); throw new MailException("Error while sending email.", ex);
} }
} }

View File

@ -22,7 +22,9 @@ import de.muehlencord.shared.account.util.AccountPU;
import freemarker.cache.StringTemplateLoader; import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration; import freemarker.template.Configuration;
import freemarker.template.Template; import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler; import freemarker.template.TemplateExceptionHandler;
import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
@ -57,22 +59,8 @@ public class MailTemplateService implements Serializable {
LOGGER.error("Tempate with name " + templateName + " not found"); LOGGER.error("Tempate with name " + templateName + " not found");
return null; return null;
} }
return fillTemplate(templateName, templateEntity.getTemplateValue(), dataModel);
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23); } catch (MailTemplateException ex) {
configuration.setDefaultEncoding("UTF-8"); // FIXME - make encoding configurable
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate(templateEntity.getTemplateName(), templateEntity.getTemplateValue());
configuration.setTemplateLoader(stringLoader);
Template template = configuration.getTemplate(templateEntity.getTemplateName());
Writer out = new StringWriter();
template.process(dataModel, out);
String templateString = out.toString();
return templateString;
} catch (Exception ex) {
String hint = "Error while processing template with name " + templateName + "."; String hint = "Error while processing template with name " + templateName + ".";
LOGGER.error(hint + " " + ex.toString()); LOGGER.error(hint + " " + ex.toString());
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
@ -81,6 +69,30 @@ public class MailTemplateService implements Serializable {
throw new MailTemplateException(hint, ex); throw new MailTemplateException(hint, ex);
} }
}
public String fillTemplate(String templateName, String templateString, MailDatamodel dataModel) throws MailTemplateException {
try {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
configuration.setDefaultEncoding("UTF-8"); // FIXME - make encoding configurable
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate(templateName, templateString);
configuration.setTemplateLoader(stringLoader);
Template template = configuration.getTemplate(templateName);
Writer out = new StringWriter();
template.process(dataModel, out);
String resultString = out.toString();
return resultString;
} catch (TemplateException | IOException ex) {
String hint = "Error while processing template with name " + templateName + ".";
LOGGER.error(hint + " " + ex.toString());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(hint, ex);
}
throw new MailTemplateException(hint, ex);
}
} }
} }