Search in sources :

Example 41 with Configuration

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);
    }
}
Also used : SmtpConfiguration(com.linkedin.thirdeye.anomaly.SmtpConfiguration) ThirdEyeAnomalyConfiguration(com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration) Configuration(freemarker.template.Configuration) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 42 with Configuration

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;
}
Also used : Configuration(freemarker.template.Configuration) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper)

Example 43 with Configuration

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();
    }
}
Also used : SmtpConfiguration(com.linkedin.thirdeye.anomaly.SmtpConfiguration) ThirdEyeAnomalyConfiguration(com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration) Configuration(freemarker.template.Configuration) HtmlEmail(org.apache.commons.mail.HtmlEmail) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) Template(freemarker.template.Template)

Example 44 with Configuration

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();
}
Also used : Configuration(freemarker.template.Configuration) Template(freemarker.template.Template) StringWriter(java.io.StringWriter) Version(freemarker.template.Version) Component(org.deeplearning4j.ui.api.Component) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 45 with Configuration

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()));
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) FreeMarkerConfigurationFactoryBean(org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) FileSystemResource(org.springframework.core.io.FileSystemResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Properties(java.util.Properties) Template(freemarker.template.Template) Test(org.junit.Test)

Aggregations

Configuration (freemarker.template.Configuration)72 Template (freemarker.template.Template)33 HashMap (java.util.HashMap)23 Writer (java.io.Writer)17 File (java.io.File)15 IOException (java.io.IOException)15 StringWriter (java.io.StringWriter)15 User (org.vcell.util.document.User)11 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)10 TemplateRepresentation (org.restlet.ext.freemarker.TemplateRepresentation)10 Representation (org.restlet.representation.Representation)10 ClientResource (org.restlet.resource.ClientResource)10 VCellApiApplication (org.vcell.rest.VCellApiApplication)10 Gson (com.google.gson.Gson)9 TemplateException (freemarker.template.TemplateException)8 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)7 OutputStreamWriter (java.io.OutputStreamWriter)7 StringTemplateLoader (freemarker.cache.StringTemplateLoader)5 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)4 FileTemplateLoader (freemarker.cache.FileTemplateLoader)4