]> WPIA git - gigi.git/commitdiff
add: mail templates support
authorFelix Dörre <felix@dogcraft.de>
Thu, 28 Jul 2016 16:31:51 +0000 (18:31 +0200)
committerFelix Dörre <felix@dogcraft.de>
Fri, 29 Jul 2016 08:23:13 +0000 (10:23 +0200)
Change-Id: I1cacfe75e6eec215fc3c664826d7c1db4892d2c9

src/org/cacert/gigi/output/template/MailTemplate.java [new file with mode: 0644]
src/org/cacert/gigi/output/template/Template.java
tests/org/cacert/gigi/template/TestTemplateMail.java [new file with mode: 0644]

diff --git a/src/org/cacert/gigi/output/template/MailTemplate.java b/src/org/cacert/gigi/output/template/MailTemplate.java
new file mode 100644 (file)
index 0000000..9c247e0
--- /dev/null
@@ -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<String, Object> 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<String, Object> 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<String> s) {
+        subjectBlock.addTranslations(s);
+        super.addTranslations(s);
+    }
+
+}
index 9dbe4dc76c720a19beaa4424ebe46db1988686f4..8ad47bc2876a271dea1cce221c4a732cc2907190 100644 (file)
@@ -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<String> splitted = new LinkedList<String>();
         LinkedList<Translatable> commands = new LinkedList<Translatable>();
         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<String, Object> 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<String, Object> 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 (file)
index 0000000..288f541
--- /dev/null
@@ -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<String, Object> vars, String input) throws IOException {
+        MailTemplate t = new MailTemplate(new StringReader(input));
+        t.sendMail(Language.getInstance(Locale.ENGLISH), vars, TEST_MAIL);
+    }
+
+    HashMap<String, Object> vars = new HashMap<>();
+
+    @Test
+    public void testSimple() throws IOException {
+        vars.put("var", "val");
+        testExecuteMail(vars, "Subject: subj\n\n<?=$var?>l");
+        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: a<?=$var?>b\n\n<?=$var2?>l");
+        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: a<?=_a<?>b\n\nc<?=_b\"?>l");
+        TestMail tm = getMailReceiver().receive();
+        assertEquals(MailTemplate.SUBJECT_TAG + "aa<b", tm.getSubject());
+        assertEquals("cb\"l", tm.getMessage());
+
+    }
+}