VIVO-12 Permit multi-language support in E-mails to users

The Freemarker environment must have the request as a special attribute, so the I18nBundle can find the preferred languages.
This commit is contained in:
j2blake 2013-04-23 17:18:39 -04:00
parent 556af80432
commit a0e7d70fd3
2 changed files with 23 additions and 13 deletions

View file

@ -61,8 +61,8 @@ public class FreemarkerEmailFactory {
FreemarkerEmailFactory factory = getFactory(vreq);
FreemarkerConfiguration fConfig = FreemarkerConfigurationLoader
.getConfig(vreq);
return new FreemarkerEmailMessage(fConfig, factory.getEmailSession(),
factory.getReplyToAddress());
return new FreemarkerEmailMessage(vreq, fConfig,
factory.getEmailSession(), factory.getReplyToAddress());
}
/**

View file

@ -27,8 +27,10 @@ import javax.mail.internet.MimeMultipart;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerConfiguration;
import edu.cornell.mannlib.vitro.webapp.web.directives.EmailDirective;
import freemarker.core.Environment;
import freemarker.template.Template;
import freemarker.template.TemplateException;
@ -47,7 +49,8 @@ public class FreemarkerEmailMessage {
private static final Log log = LogFactory
.getLog(FreemarkerEmailMessage.class);
private final Session session;
private final VitroRequest vreq;
private final Session mailSession;
private final FreemarkerConfiguration config;
private final List<Recipient> recipients = new ArrayList<Recipient>();
@ -63,9 +66,10 @@ public class FreemarkerEmailMessage {
/**
* Package access - should only be created by the factory.
*/
FreemarkerEmailMessage(FreemarkerConfiguration fConfig, Session session,
InternetAddress replyToAddress) {
this.session = session;
FreemarkerEmailMessage(VitroRequest vreq, FreemarkerConfiguration fConfig,
Session mailSession, InternetAddress replyToAddress) {
this.vreq = vreq;
this.mailSession = mailSession;
this.replyToAddress = replyToAddress;
this.config = fConfig;
}
@ -141,7 +145,13 @@ public class FreemarkerEmailMessage {
try {
Template template = config.getTemplate(templateName);
template.process(bodyMap, new StringWriter());
Environment env = template.createProcessingEnvironment(bodyMap,
new StringWriter());
env.setCustomAttribute("request", vreq);
env.setCustomAttribute("context", vreq.getSession()
.getServletContext());
env.process();
} catch (TemplateException e) {
log.error(e, e);
} catch (IOException e) {
@ -151,7 +161,7 @@ public class FreemarkerEmailMessage {
public boolean send() {
try {
MimeMessage msg = new MimeMessage(session);
MimeMessage msg = new MimeMessage(mailSession);
msg.setReplyTo(new Address[] { replyToAddress });
if (fromAddress == null) {
@ -199,11 +209,11 @@ public class FreemarkerEmailMessage {
bodyPart.setContent(textBody, type);
content.addBodyPart(bodyPart);
}
public String getReplyToAddress() {
return replyToAddress.getAddress();
}
public String getReplyToAddress() {
return replyToAddress.getAddress();
}
private <T> T nonNull(T value, T defaultValue) {
return (value == null) ? defaultValue : value;
}