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:
parent
556af80432
commit
a0e7d70fd3
2 changed files with 23 additions and 13 deletions
|
@ -61,8 +61,8 @@ public class FreemarkerEmailFactory {
|
||||||
FreemarkerEmailFactory factory = getFactory(vreq);
|
FreemarkerEmailFactory factory = getFactory(vreq);
|
||||||
FreemarkerConfiguration fConfig = FreemarkerConfigurationLoader
|
FreemarkerConfiguration fConfig = FreemarkerConfigurationLoader
|
||||||
.getConfig(vreq);
|
.getConfig(vreq);
|
||||||
return new FreemarkerEmailMessage(fConfig, factory.getEmailSession(),
|
return new FreemarkerEmailMessage(vreq, fConfig,
|
||||||
factory.getReplyToAddress());
|
factory.getEmailSession(), factory.getReplyToAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -27,8 +27,10 @@ import javax.mail.internet.MimeMultipart;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
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.controller.freemarker.FreemarkerConfiguration;
|
||||||
import edu.cornell.mannlib.vitro.webapp.web.directives.EmailDirective;
|
import edu.cornell.mannlib.vitro.webapp.web.directives.EmailDirective;
|
||||||
|
import freemarker.core.Environment;
|
||||||
import freemarker.template.Template;
|
import freemarker.template.Template;
|
||||||
import freemarker.template.TemplateException;
|
import freemarker.template.TemplateException;
|
||||||
|
|
||||||
|
@ -47,7 +49,8 @@ public class FreemarkerEmailMessage {
|
||||||
private static final Log log = LogFactory
|
private static final Log log = LogFactory
|
||||||
.getLog(FreemarkerEmailMessage.class);
|
.getLog(FreemarkerEmailMessage.class);
|
||||||
|
|
||||||
private final Session session;
|
private final VitroRequest vreq;
|
||||||
|
private final Session mailSession;
|
||||||
private final FreemarkerConfiguration config;
|
private final FreemarkerConfiguration config;
|
||||||
|
|
||||||
private final List<Recipient> recipients = new ArrayList<Recipient>();
|
private final List<Recipient> recipients = new ArrayList<Recipient>();
|
||||||
|
@ -63,9 +66,10 @@ public class FreemarkerEmailMessage {
|
||||||
/**
|
/**
|
||||||
* Package access - should only be created by the factory.
|
* Package access - should only be created by the factory.
|
||||||
*/
|
*/
|
||||||
FreemarkerEmailMessage(FreemarkerConfiguration fConfig, Session session,
|
FreemarkerEmailMessage(VitroRequest vreq, FreemarkerConfiguration fConfig,
|
||||||
InternetAddress replyToAddress) {
|
Session mailSession, InternetAddress replyToAddress) {
|
||||||
this.session = session;
|
this.vreq = vreq;
|
||||||
|
this.mailSession = mailSession;
|
||||||
this.replyToAddress = replyToAddress;
|
this.replyToAddress = replyToAddress;
|
||||||
this.config = fConfig;
|
this.config = fConfig;
|
||||||
}
|
}
|
||||||
|
@ -141,7 +145,13 @@ public class FreemarkerEmailMessage {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Template template = config.getTemplate(templateName);
|
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) {
|
} catch (TemplateException e) {
|
||||||
log.error(e, e);
|
log.error(e, e);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -151,7 +161,7 @@ public class FreemarkerEmailMessage {
|
||||||
|
|
||||||
public boolean send() {
|
public boolean send() {
|
||||||
try {
|
try {
|
||||||
MimeMessage msg = new MimeMessage(session);
|
MimeMessage msg = new MimeMessage(mailSession);
|
||||||
msg.setReplyTo(new Address[] { replyToAddress });
|
msg.setReplyTo(new Address[] { replyToAddress });
|
||||||
|
|
||||||
if (fromAddress == null) {
|
if (fromAddress == null) {
|
||||||
|
@ -199,11 +209,11 @@ public class FreemarkerEmailMessage {
|
||||||
bodyPart.setContent(textBody, type);
|
bodyPart.setContent(textBody, type);
|
||||||
content.addBodyPart(bodyPart);
|
content.addBodyPart(bodyPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getReplyToAddress() {
|
public String getReplyToAddress() {
|
||||||
return replyToAddress.getAddress();
|
return replyToAddress.getAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> T nonNull(T value, T defaultValue) {
|
private <T> T nonNull(T value, T defaultValue) {
|
||||||
return (value == null) ? defaultValue : value;
|
return (value == null) ? defaultValue : value;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue