package ca.intelliware.ihtsdo.mlds.config;
import org.apache.commons.lang.CharEncoding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
@Configuration
public class ThymeleafConfiguration {
private final Logger log = LoggerFactory.getLogger(ThymeleafConfiguration.class);
public ThymeleafConfiguration() {
}
@Bean
@Description("Thymeleaf view resolver")
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setViewNames(new String[]{"error", "/tl/*"});
thymeleafViewResolver.setTemplateEngine(templateEngine());
thymeleafViewResolver.setCharacterEncoding(CharEncoding.UTF_8);
return thymeleafViewResolver;
}
@Bean
@Description("Thymeleaf template resolver serving HTML 5 emails")
public ClassLoaderTemplateResolver emailTemplateResolver() {
ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
emailTemplateResolver.setPrefix("mails/");
emailTemplateResolver.setSuffix(".html");
emailTemplateResolver.setTemplateMode("HTML5");
emailTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
emailTemplateResolver.setOrder(1);
// TODO MLDS-20 MB push this into yml config?
emailTemplateResolver.setCacheTTLMs(2000L);
return emailTemplateResolver;
}
@Bean
@Description("Thymeleaf template resolver serving HTML 5")
public ClassLoaderTemplateResolver webTemplateResolver() {
ClassLoaderTemplateResolver webTemplateResolver = new ClassLoaderTemplateResolver();
webTemplateResolver.setPrefix("templates/");
webTemplateResolver.setSuffix(".html");
webTemplateResolver.setTemplateMode("HTML5");
webTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
webTemplateResolver.setOrder(2);
return webTemplateResolver;
}
@Bean
@Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(emailTemplateResolver());
templateEngine.addTemplateResolver(webTemplateResolver());
return templateEngine;
}
@Bean
@Description("Spring mail message resolver")
public MessageSource messageSource() {
log.info("loading non-reloadable mail messages resources");
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/mails/messages/messages");
messageSource.setDefaultEncoding(CharEncoding.UTF_8);
// TODO MLDS-20 MB push this into yml config?
messageSource.setCacheSeconds(1);
return messageSource;
}
}