add: revocation state in the result list of support cert search
[gigi.git] / src / club / wpia / gigi / pages / admin / support / FindCertPage.java
1 package club.wpia.gigi.pages.admin.support;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import club.wpia.gigi.dbObjects.Certificate;
12 import club.wpia.gigi.dbObjects.Certificate.CertificateStatus;
13 import club.wpia.gigi.dbObjects.Certificate.SubjectAlternateName;
14 import club.wpia.gigi.localisation.Language;
15 import club.wpia.gigi.output.ArrayIterable;
16 import club.wpia.gigi.output.template.Form;
17 import club.wpia.gigi.output.template.IterableDataset;
18 import club.wpia.gigi.output.template.SprintfCommand;
19 import club.wpia.gigi.pages.LoginPage;
20 import club.wpia.gigi.pages.Page;
21 import club.wpia.gigi.pages.account.certs.Certificates;
22 import club.wpia.gigi.util.AuthorizationContext;
23
24 public class FindCertPage extends Page {
25
26     public static final String PATH = "/support/find/certs";
27
28     public FindCertPage() {
29         super("Find Certificate");
30     }
31
32     @Override
33     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
34         new FindCertForm(req).output(resp.getWriter(), Page.getLanguage(req), new HashMap<String, Object>());
35     }
36
37     @Override
38     public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
39         return Form.getForm(req, FindCertForm.class).submitExceptionProtected(req, resp);
40     }
41
42     @Override
43     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
44         if ( !Form.printFormErrors(req, resp.getWriter())) {
45             final Certificate[] certs = ((FindCertForm.FindResult) req.getAttribute(Form.SUBMIT_RESULT)).getCerts();
46             if (certs.length == 1) {
47                 resp.sendRedirect(Certificates.SUPPORT_PATH + "/" + certs[0].getSerial());
48             } else {
49                 HashMap<String, Object> vars = new HashMap<String, Object>();
50                 Language l = LoginPage.getLanguage(req);
51                 if (certs.length >= 100) {
52                     vars.put("limit", l.getTranslation("100 or more entries available, only the first 100 are displayed."));
53                 } else {
54                     vars.put("limit", SprintfCommand.createSimple("{0} entries found", certs.length));
55                 }
56                 vars.put("certtable", new ArrayIterable<Certificate>(certs) {
57
58                     @Override
59                     public void apply(Certificate t, Language l, Map<String, Object> vars) {
60                         vars.put("id", t.getId());
61                         vars.put("serial", t.getSerial());
62
63                         if (t.getStatus() == CertificateStatus.REVOKED) {
64                             vars.put("revoked", t.getRevocationDate());
65                         } else {
66                             vars.put("revoked", l.getTranslation("N/A"));
67                         }
68
69                         final List<SubjectAlternateName> san = t.getSANs();
70                         vars.put("san", new IterableDataset() {
71
72                             int j = 0;
73
74                             @Override
75                             public boolean next(Language l, Map<String, Object> vars) {
76                                 if (j == san.size()) {
77                                     return false;
78                                 }
79                                 vars.put("entry", san.get(j).getName() + (j < san.size() - 1 ? ", " : ""));
80                                 j++;
81                                 return true;
82                             }
83
84                         });
85                     }
86                 });
87                 getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
88             }
89         }
90     }
91
92     @Override
93     public boolean isPermitted(AuthorizationContext ac) {
94         return ac != null && ac.canSupport();
95     }
96 }