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

@ -79,13 +79,13 @@ public class MailService implements Serializable {
public String sendTestHtmlEmail(String recipient) throws MailException {
Date now = new Date();
AccountEntity account = new AccountEntity();
account.setId (UUID.randomUUID());
account.setFirstname("Jörn");
account.setLastname ("Mühlencord");
account.setId(UUID.randomUUID());
account.setFirstname("Jörn");
account.setLastname("Mühlencord");
AccountLoginEntity accountLogin = new AccountLoginEntity();
accountLogin.setAccountPassword("secret");
accountLogin.setAccount(account);
account.setAccountLogin(accountLogin);
accountLogin.setAccountPassword("secret");
accountLogin.setAccount(account);
account.setAccountLogin(accountLogin);
MailDatamodel dataModel = new MailDatamodel(account);
dataModel.addParameter("url", "http://url.de");
dataModel.addParameter("resetUrl", "http://reseturl.de");
@ -119,6 +119,22 @@ public class MailService implements Serializable {
public String sendHTMLMail(String recipient, String ccRecipient, String bccRecipient, String subject,
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 {
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(); // use default from
@ -131,22 +147,19 @@ public class MailService implements Serializable {
}
message.setSubject(subject, encoding);
Multipart contentMultiPart = new MimeMultipart("alternative");
String htmlBody = mailTemplateService.getStringFromTemplate(htmlTemplateName, dataModel);
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(htmlBody, "text/html; charset=UTF-8");
contentMultiPart.addBodyPart(htmlBodyPart);
if (plainTemplateName != null) {
String plainBody = mailTemplateService.getStringFromTemplate(plainTemplateName, dataModel);
if (plainBody != null) {
MimeBodyPart plainBodyPart = new MimeBodyPart();
plainBodyPart.setText(plainBody, "UTF-8");
contentMultiPart.addBodyPart(plainBodyPart);
}
if ((attachments == null) || (attachments.isEmpty())) {
message.setContent(contentMultiPart);
} else {
@ -154,7 +167,7 @@ public class MailService implements Serializable {
contentBodyPart.setContent(contentMultiPart);
MimeMultipart messageMultipart = new MimeMultipart("related");
messageMultipart.addBodyPart(contentBodyPart);
messageMultipart.addBodyPart(contentBodyPart);
for (File attachment : attachments) {
try {
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
@ -168,12 +181,10 @@ public class MailService implements Serializable {
throw new MailException("Cannot attach " + attachment.toString() + " to email. Reason: " + ex.toString(), ex);
}
}
message.setContent (messageMultipart);
message.setContent(messageMultipart);
}
return transportMail(message);
} catch (MessagingException | MailTemplateException ex) {
} catch (MessagingException 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.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.Writer;
@ -57,22 +59,8 @@ public class MailTemplateService implements Serializable {
LOGGER.error("Tempate with name " + templateName + " not found");
return null;
}
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(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) {
return fillTemplate(templateName, templateEntity.getTemplateValue(), dataModel);
} catch (MailTemplateException ex) {
String hint = "Error while processing template with name " + templateName + ".";
LOGGER.error(hint + " " + ex.toString());
if (LOGGER.isDebugEnabled()) {
@ -81,6 +69,30 @@ public class MailTemplateService implements Serializable {
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);
}
}
}