add: enable support to search for certificates
[gigi.git] / src / org / cacert / gigi / pages / admin / support / FindCertForm.java
1 package org.cacert.gigi.pages.admin.support;
2
3 import java.io.PrintWriter;
4 import java.util.Map;
5
6 import javax.servlet.http.HttpServletRequest;
7
8 import org.cacert.gigi.GigiApiException;
9 import org.cacert.gigi.dbObjects.Certificate;
10 import org.cacert.gigi.dbObjects.Certificate.SANType;
11 import org.cacert.gigi.localisation.Language;
12 import org.cacert.gigi.output.template.Form;
13 import org.cacert.gigi.output.template.SprintfCommand;
14 import org.cacert.gigi.output.template.Template;
15
16 public class FindCertForm extends Form {
17
18     private static final Template t = new Template(FindCertForm.class.getResource("FindCertForm.templ"));
19
20     private final String SERIAL = "serial";
21
22     private String certType = SERIAL;
23
24     public Certificate certs[];
25
26     public FindCertForm(HttpServletRequest hsr) {
27         super(hsr);
28     }
29
30     @Override
31     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
32         this.certType = req.getParameter("certType");
33         String request = req.getParameter("cert").trim();
34
35         if ( !SERIAL.equals(certType) && !SANType.EMAIL.getOpensslName().equals(certType) && !SANType.DNS.getOpensslName().equals(certType)) {
36             throw new GigiApiException("Invalid search type.");
37         }
38
39         if (SERIAL.equals(certType)) {
40             certs = Certificate.findBySerialPattern(request);
41             if (certs.length <= 0) {
42                 throw new GigiApiException(SprintfCommand.createSimple("No certificate found matching serial number {0}", request));
43             }
44         }
45
46         if (SANType.EMAIL.getOpensslName().equals(certType) || SANType.DNS.getOpensslName().equals(certType)) {
47             SANType stype = SANType.valueOf(certType.toUpperCase());
48             certs = Certificate.findBySANPattern(request, stype);
49             if (certs.length <= 0) {
50                 throw new GigiApiException(SprintfCommand.createSimple("No certificate found matching {0}", request));
51             }
52         }
53         return true;
54     }
55
56     @Override
57     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
58         vars.put("serial", !SERIAL.equals(certType) ? "" : "checked");
59         vars.put("email", !SANType.EMAIL.getOpensslName().equals(certType) ? "" : "checked");
60         vars.put("dns", !SANType.DNS.getOpensslName().equals(certType) ? "" : "checked");
61
62         t.output(out, l, vars);
63     }
64
65     public Certificate[] getCerts() {
66         return certs;
67     }
68
69 }