improved templating service
This commit is contained in:
@ -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
|
||||
@ -134,19 +150,16 @@ public class MailService implements Serializable {
|
||||
|
||||
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 {
|
||||
@ -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);
|
||||
}
|
||||
|
||||
return transportMail(message);
|
||||
} catch (MessagingException | MailTemplateException ex) {
|
||||
} catch (MessagingException ex) {
|
||||
throw new MailException("Error while sending email.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user