EmailTemplate { public Map<String, String> processTemplate(ActionMessage msg) throws Exception { Map<String, String> emailProcessed = new HashMap<>(); PluginMessageDescription pmDesc = new PluginMessageDescription(msg); emailProcessed.put("emailSubject", pmDesc.getEmailSubject()); String plain; String html; String templateLocale = pmDesc.getProps() != null ? pmDesc.getProps().get(EmailPlugin.PROP_TEMPLATE_LOCALE) : null; if (templateLocale != null) { plain = pmDesc.getProps().get(EmailPlugin.PROP_TEMPLATE_PLAIN + "." + templateLocale); html = pmDesc.getProps().get(EmailPlugin.PROP_TEMPLATE_HTML + "." + templateLocale); } else { plain = pmDesc.getProps() != null ? pmDesc.getProps().get(EmailPlugin.PROP_TEMPLATE_PLAIN) : null; html = pmDesc.getProps() != null ? pmDesc.getProps().get(EmailPlugin.PROP_TEMPLATE_HTML) : null; } StringWriter writerPlain = new StringWriter(); StringWriter writerHtml = new StringWriter(); if (!isEmpty(plain)) { StringReader plainReader = new StringReader(plain); ftlTemplate = new Template("plainTemplate", plainReader, ftlCfg); ftlTemplate.process(pmDesc, writerPlain); } else { ftlTemplatePlain.process(pmDesc, writerPlain); } if (!isEmpty(html)) { StringReader htmlReader = new StringReader(html); ftlTemplate = new Template("htmlTemplate", htmlReader, ftlCfg); ftlTemplate.process(pmDesc, writerHtml); } else { ftlTemplateHtml.process(pmDesc, writerHtml); } writerPlain.flush(); writerPlain.close(); emailProcessed.put("emailBodyPlain", writerPlain.toString()); writerHtml.flush(); writerHtml.close(); emailProcessed.put("emailBodyHtml", writerHtml.toString()); return emailProcessed; } EmailTemplate(); Map<String, String> processTemplate(ActionMessage msg); static final String DEFAULT_TEMPLATE_PLAIN; static final String DEFAULT_TEMPLATE_HTML; static final Locale DEFAULT_LOCALE; static final String FREEMARKER_VERSION; }
@Test public void loadTemplatesFromPropertiesTest() throws Exception { Alert openAlert = JvmGarbageCollectionData.getOpenAlert(); Action openAction = new Action(openAlert.getTriggerId(), "email", "email-to-test", openAlert); openAction.setProperties(properties); ActionMessage openMessage = new TestActionMessage(openAction); properties.put("template.plain", "Tiny template: ${emailSubject}"); EmailTemplate template = new EmailTemplate(); Map<String, String> processed = template.processTemplate(openMessage); assertNotNull(processed.get("emailBodyPlain")); assertEquals("Processed template", processed.get("emailBodyPlain"), "Tiny template: Alert [open] message: GC " + "Duration greater than 1000.0 ms for App Server thevault~Local"); properties.remove("template.plain"); }
@Test public void loadTemplatesFromPropertiesWithLocaleTest() throws Exception { Alert openAlert = JvmGarbageCollectionData.getOpenAlert(); Action openAction = new Action(openAlert.getTriggerId(), "email", "email-to-test", openAlert); openAction.setProperties(properties); ActionMessage openMessage = new TestActionMessage(openAction); properties.put("template.plain", "Tiny template: ${emailSubject}"); properties.put("template.plain.es", "ES Tiny template: ${emailSubject}"); properties.put("template.plain.fr", "FR Tiny template: ${emailSubject}"); properties.put("template.locale", "es"); EmailTemplate template = new EmailTemplate(); Map<String, String> processed = template.processTemplate(openMessage); assertNotNull(processed.get("emailBodyPlain")); assertEquals("Processed template", processed.get("emailBodyPlain"), "ES Tiny template: Alert [open] message: " + "GC Duration greater than 1000.0 ms for App Server thevault~Local"); properties.put("template.locale", "fr"); processed = template.processTemplate(openMessage); assertNotNull(processed.get("emailBodyPlain")); assertEquals("Processed template", processed.get("emailBodyPlain"), "FR Tiny template: Alert [open] message: " + "GC Duration greater than 1000.0 ms for App Server thevault~Local"); properties.remove("template.plain"); properties.remove("template.locale"); }