From 4cb0252dc568fd6c56e986289a2c45e4a8884fb3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Thu, 28 Jul 2016 18:31:51 +0200 Subject: [PATCH] add: mail templates support Change-Id: I1cacfe75e6eec215fc3c664826d7c1db4892d2c9 --- .../gigi/output/template/MailTemplate.java | 85 +++++++++++++++++++ .../cacert/gigi/output/template/Template.java | 18 ++-- .../gigi/template/TestTemplateMail.java | 55 ++++++++++++ 3 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 src/org/cacert/gigi/output/template/MailTemplate.java create mode 100644 tests/org/cacert/gigi/template/TestTemplateMail.java diff --git a/src/org/cacert/gigi/output/template/MailTemplate.java b/src/org/cacert/gigi/output/template/MailTemplate.java new file mode 100644 index 00000000..9c247e00 --- /dev/null +++ b/src/org/cacert/gigi/output/template/MailTemplate.java @@ -0,0 +1,85 @@ +package org.cacert.gigi.output.template; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.net.URL; +import java.util.Collection; +import java.util.Map; + +import org.cacert.gigi.email.SendMail; +import org.cacert.gigi.localisation.Language; + +public class MailTemplate extends Template { + + public static final String SUBJECT_TAG = "[SomeCA.org] "; + + private TemplateBlock subjectBlock; + + public MailTemplate(URL u) { + super(u); + } + + public MailTemplate(Reader r) { + super(r); + } + + @Override + protected ParseResult parse(Reader r) throws IOException { + StringBuilder strb = new StringBuilder(); + int ct = 0; + int c; + while ((c = r.read()) > 0) { + if (c == '\n') { + ct++; + if (ct == 2) { + break; + } + } else { + ct = 0; + } + strb.append((char) c); + } + String[] lines = strb.toString().split("\n"); + for (int i = 0; i < lines.length; i++) { + String[] lineParts = lines[i].split(": ", 2); + if (lineParts.length != 2) { + throw new IOException("Mail template header malformed."); + } + if (lineParts[0].equals("Subject")) { + subjectBlock = parseContent(new StringReader(lineParts[1])).getBlock(null); + } + } + if (subjectBlock == null) { + throw new IOException("Mail template without subject line."); + } + return parseContent(r); + } + + public void sendMail(Language l, Map vars, String to) throws IOException { + tryReload(); + vars.put(Outputable.OUT_KEY_PLAIN, true); + + String body = runTemplate(this, l, vars); + String subject = runTemplate(subjectBlock, l, vars); + + SendMail.getInstance().sendMail(to, SUBJECT_TAG + subject, body, "support@cacert.org", null, null, null, null, false); + } + + private static String runTemplate(Outputable toRun, Language l, Map vars) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + toRun.output(pw, l, vars); + pw.close(); + return sw.toString(); + } + + @Override + public void addTranslations(Collection s) { + subjectBlock.addTranslations(s); + super.addTranslations(s); + } + +} diff --git a/src/org/cacert/gigi/output/template/Template.java b/src/org/cacert/gigi/output/template/Template.java index 9dbe4dc7..8ad47bc2 100644 --- a/src/org/cacert/gigi/output/template/Template.java +++ b/src/org/cacert/gigi/output/template/Template.java @@ -27,7 +27,7 @@ import org.cacert.gigi.util.HTMLEncoder; */ public class Template implements Outputable { - private static class ParseResult { + protected static class ParseResult { TemplateBlock block; @@ -106,7 +106,11 @@ public class Template implements Outputable { } } - private ParseResult parse(Reader r) throws IOException { + protected ParseResult parse(Reader r) throws IOException { + return parseContent(r); + } + + protected ParseResult parseContent(Reader r) throws IOException { LinkedList splitted = new LinkedList(); LinkedList commands = new LinkedList(); StringBuffer buf = new StringBuffer(); @@ -137,10 +141,10 @@ public class Template implements Outputable { if (m.matches()) { String type = m.group(1); String variable = m.group(2); - ParseResult body = parse(r); + ParseResult body = parseContent(r); if (type.equals("if")) { if ("else".equals(body.getEndType())) { - commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}"))); + commands.add(new IfStatement(variable, body.getBlock("else"), parseContent(r).getBlock("}"))); } else { commands.add(new IfStatement(variable, body.getBlock("}"))); } @@ -186,6 +190,11 @@ public class Template implements Outputable { @Override public void output(PrintWriter out, Language l, Map vars) { + tryReload(); + data.output(out, l, vars); + } + + protected void tryReload() { if (source != null && lastLoaded < source.lastModified()) { try { System.out.println("Reloading template.... " + source); @@ -197,7 +206,6 @@ public class Template implements Outputable { e.printStackTrace(); } } - data.output(out, l, vars); } protected static void outputVar(PrintWriter out, Language l, Map vars, String varname, boolean unescaped) { diff --git a/tests/org/cacert/gigi/template/TestTemplateMail.java b/tests/org/cacert/gigi/template/TestTemplateMail.java new file mode 100644 index 00000000..288f541e --- /dev/null +++ b/tests/org/cacert/gigi/template/TestTemplateMail.java @@ -0,0 +1,55 @@ +package org.cacert.gigi.template; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.io.StringReader; +import java.util.HashMap; +import java.util.Locale; + +import org.cacert.gigi.localisation.Language; +import org.cacert.gigi.output.template.MailTemplate; +import org.cacert.gigi.testUtils.BusinessTest; +import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail; +import org.junit.Test; + +public class TestTemplateMail extends BusinessTest { + + private static final String TEST_MAIL = "test@mail.com"; + + private void testExecuteMail(HashMap vars, String input) throws IOException { + MailTemplate t = new MailTemplate(new StringReader(input)); + t.sendMail(Language.getInstance(Locale.ENGLISH), vars, TEST_MAIL); + } + + HashMap vars = new HashMap<>(); + + @Test + public void testSimple() throws IOException { + vars.put("var", "val"); + testExecuteMail(vars, "Subject: subj\n\nl"); + TestMail tm = getMailReceiver().receive(); + assertEquals(MailTemplate.SUBJECT_TAG + "subj", tm.getSubject()); + assertEquals("vall", tm.getMessage()); + } + + @Test + public void testVarNoEscape() throws IOException { + vars.put("var", "val\">"); + vars.put("var2", "sl\">"); + testExecuteMail(vars, "Subject: ab\n\nl"); + TestMail tm = getMailReceiver().receive(); + assertEquals(MailTemplate.SUBJECT_TAG + "aval\">b", tm.getSubject()); + assertEquals("sl\">l", tm.getMessage()); + + } + + @Test + public void testTranslate() throws IOException { + testExecuteMail(vars, "Subject: ab\n\ncl"); + TestMail tm = getMailReceiver().receive(); + assertEquals(MailTemplate.SUBJECT_TAG + "aa