Implement a Template-Foreach (and use in "new email certificate")
[gigi.git] / src / org / cacert / gigi / pages / account / MailCertificateAdd.java
1 package org.cacert.gigi.pages.account;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import org.cacert.gigi.Certificate;
15 import org.cacert.gigi.User;
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.output.ClientCSRGenerate;
18 import org.cacert.gigi.output.template.IterableDataset;
19 import org.cacert.gigi.pages.LoginPage;
20 import org.cacert.gigi.pages.Page;
21
22 public class MailCertificateAdd extends Page {
23         public static final String PATH = "/account/certs/email/new";
24
25         public MailCertificateAdd() {
26                 super("Create Email certificate");
27         }
28
29         @Override
30         public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
31                 HashMap<String, Object> vars = new HashMap<String, Object>();
32                 vars.put("CCA", "<a href='/policy/CAcertCommunityAgreement.html'>CCA</a>");
33
34                 User u = LoginPage.getUser(req);
35                 try {
36                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
37                                 "SELECT `id`,`email` from `email` WHERE `memid`=? AND `deleted`=0");
38                         ps.setInt(1, u.getId());
39                         final ResultSet rs = ps.executeQuery();
40                         vars.put("emails", new IterableDataset() {
41
42                                 @Override
43                                 public boolean next(Map<String, Object> vars) {
44                                         try {
45                                                 if (!rs.next()) {
46                                                         return false;
47                                                 }
48                                                 vars.put("id", rs.getString(1));
49                                                 vars.put("value", rs.getString(2));
50                                                 return true;
51                                         } catch (SQLException e) {
52                                                 e.printStackTrace();
53                                         }
54                                         return false;
55                                 }
56                         });
57                         getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
58                         rs.close();
59                 } catch (SQLException e) {
60                         e.printStackTrace();
61                 }
62         }
63
64         @Override
65         public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
66                 PrintWriter out = resp.getWriter();
67                 if (req.getParameter("optionalCSR") == null || req.getParameter("optionalCSR").equals("")) {
68                         out.println("csr missing");
69                         ClientCSRGenerate.output(req, resp);
70                 }
71                 String csr = req.getParameter("optionalCSR");
72                 if (!"on".equals(req.getParameter("CCA"))) {
73                         // Error.
74                         return;
75                 }
76                 Certificate c = new Certificate(LoginPage.getUser(req).getId(), "/commonName=CAcert WoT User", "sha256", csr);
77                 c.issue();
78                 try {
79                         c.waitFor(60000);
80                         resp.sendRedirect(MailCertificates.PATH + "/" + c.getSerial());
81                 } catch (SQLException e) {
82                         e.printStackTrace();
83                 } catch (InterruptedException e) {
84                         e.printStackTrace();
85                 }
86
87         }
88
89 }