use of freemarker.template.Configuration in project pinot by linkedin.
the class AnomalyReportGenerator method buildEmailTemplateAndSendAlert.
void buildEmailTemplateAndSendAlert(Map<String, Object> paramMap, SmtpConfiguration smtpConfiguration, String subject, String emailRecipients, String fromEmail, boolean isSingleAnomalyEmail, HtmlEmail email) {
if (Strings.isNullOrEmpty(fromEmail)) {
throw new IllegalArgumentException("Invalid sender's email");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (Writer out = new OutputStreamWriter(baos, AlertTaskRunner.CHARSET)) {
Configuration freemarkerConfig = new Configuration(Configuration.VERSION_2_3_21);
freemarkerConfig.setClassForTemplateLoading(getClass(), "/com/linkedin/thirdeye/detector");
freemarkerConfig.setDefaultEncoding(AlertTaskRunner.CHARSET);
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
Template template = null;
if (isSingleAnomalyEmail) {
template = freemarkerConfig.getTemplate(SINGLE_ANOMALY_EMAIL_TEMPLATE);
} else {
template = freemarkerConfig.getTemplate(MULTIPLE_ANOMALIES_EMAIL_TEMPLATE);
}
template.process(paramMap, out);
String alertEmailHtml = new String(baos.toByteArray(), AlertTaskRunner.CHARSET);
EmailHelper.sendEmailWithHtml(email, smtpConfiguration, subject, alertEmailHtml, fromEmail, emailRecipients);
} catch (Exception e) {
Throwables.propagate(e);
}
}
use of freemarker.template.Configuration in project head by mifos.
the class TallyXMLGenerator method buildFreemarkerConfiguration.
private static Configuration buildFreemarkerConfiguration() {
if (freemarkerConfiguration == null) {
freemarkerConfiguration = new Configuration();
freemarkerConfiguration.setClassForTemplateLoading(TallyXMLGenerator.class, "");
freemarkerConfiguration.setObjectWrapper(new DefaultObjectWrapper());
}
return freemarkerConfiguration;
}
use of freemarker.template.Configuration in project pinot by linkedin.
the class GenerateAnomalyReport method buildEmailTemplateAndSendAlert.
void buildEmailTemplateAndSendAlert(Map<String, Object> paramMap) {
HtmlEmail email = new HtmlEmail();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (Writer out = new OutputStreamWriter(baos, AlertTaskRunner.CHARSET)) {
Configuration freemarkerConfig = new Configuration(Configuration.VERSION_2_3_21);
freemarkerConfig.setClassForTemplateLoading(getClass(), "/com/linkedin/thirdeye/detector");
freemarkerConfig.setDefaultEncoding(AlertTaskRunner.CHARSET);
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
Template template = freemarkerConfig.getTemplate("custom-anomaly-report.ftl");
template.process(paramMap, out);
String alertEmailSubject = "Thirdeye : Daily anomaly report";
String alertEmailHtml = new String(baos.toByteArray(), AlertTaskRunner.CHARSET);
EmailHelper.sendEmailWithHtml(email, smtpConfiguration, alertEmailSubject, alertEmailHtml, "thirdeye-dev@linkedin.com", emailRecipients);
} catch (Exception e) {
e.printStackTrace();
}
}
use of freemarker.template.Configuration in project deeplearning4j by deeplearning4j.
the class StaticPageUtil method renderHTMLContent.
public static String renderHTMLContent(Component... components) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Configuration cfg = new Configuration(new Version(2, 3, 23));
// Where do we load the templates from:
cfg.setClassForTemplateLoading(StaticPageUtil.class, "");
// Some other recommended settings:
cfg.setIncompatibleImprovements(new Version(2, 3, 23));
cfg.setDefaultEncoding("UTF-8");
cfg.setLocale(Locale.US);
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
ClassPathResource cpr = new ClassPathResource("assets/dl4j-ui.js");
String scriptContents = IOUtils.toString(cpr.getInputStream(), "UTF-8");
Map<String, Object> pageElements = new HashMap<>();
List<ComponentObject> list = new ArrayList<>();
int i = 0;
for (Component c : components) {
list.add(new ComponentObject(String.valueOf(i), mapper.writeValueAsString(c)));
i++;
}
pageElements.put("components", list);
pageElements.put("scriptcontent", scriptContents);
Template template = cfg.getTemplate("staticpage.ftl");
Writer stringWriter = new StringWriter();
template.process(pageElements, stringWriter);
return stringWriter.toString();
}
use of freemarker.template.Configuration in project spring-framework by spring-projects.
the class FreeMarkerConfigurerTests method freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath.
@Test
@SuppressWarnings("rawtypes")
public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
fcfb.setTemplateLoaderPath("file:/mydir");
Properties settings = new Properties();
settings.setProperty("localized_lookup", "false");
fcfb.setFreemarkerSettings(settings);
fcfb.setResourceLoader(new ResourceLoader() {
@Override
public Resource getResource(String location) {
if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
throw new IllegalArgumentException(location);
}
return new ByteArrayResource("test".getBytes(), "test");
}
@Override
public ClassLoader getClassLoader() {
return getClass().getClassLoader();
}
});
fcfb.afterPropertiesSet();
assertThat(fcfb.getObject(), instanceOf(Configuration.class));
Configuration fc = fcfb.getObject();
Template ft = fc.getTemplate("test");
assertEquals("test", FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap()));
}
Aggregations