88e1264b7ed34cb2e0107e4726dc2f2d60abc6bc
[gigi.git] / src / club / wpia / gigi / pages / main / CertStatusRequestForm.java
1 package club.wpia.gigi.pages.main;
2
3 import java.io.PrintWriter;
4 import java.util.Map;
5
6 import javax.servlet.http.HttpServletRequest;
7
8 import club.wpia.gigi.GigiApiException;
9 import club.wpia.gigi.dbObjects.Certificate;
10 import club.wpia.gigi.dbObjects.Certificate.CertificateStatus;
11 import club.wpia.gigi.localisation.Language;
12 import club.wpia.gigi.output.template.Form;
13 import club.wpia.gigi.output.template.SprintfCommand;
14 import club.wpia.gigi.output.template.Template;
15 import club.wpia.gigi.output.template.TranslateCommand;
16 import club.wpia.gigi.util.RateLimit;
17 import club.wpia.gigi.util.RateLimit.RateLimitException;
18
19 public class CertStatusRequestForm extends Form {
20
21     private static final Template t = new Template(CertStatusRequestForm.class.getResource("CertStatusForm.templ"));
22
23     // 50 per 5 min
24     public static final RateLimit RATE_LIMIT = new RateLimit(50, 5 * 60 * 1000);
25
26     public static final TranslateCommand NOT_FOUND = new TranslateCommand("Certificate to check not found. Maybe it was issued by a different CA.");
27
28     public CertStatusRequestForm(HttpServletRequest hsr) {
29         super(hsr);
30
31     }
32
33     @Override
34     public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
35         if (RATE_LIMIT.isLimitExceeded(req.getRemoteAddr())) {
36             throw new RateLimitException();
37         }
38         Certificate c = null;
39         try {
40             c = Certificate.locateCertificate(req.getParameter("serial"), req.getParameter("cert"));
41             if (c == null) {
42                 throw new GigiApiException(NOT_FOUND);
43             }
44         } catch (GigiApiException e) {
45             throw new PermamentFormException(e);
46         }
47
48         if (c.getStatus() == CertificateStatus.REVOKED) {
49             java.util.Date revocationDate = c.getRevocationDate();
50             throw new PermamentFormException(new GigiApiException(SprintfCommand.createSimple("Certificate has been revoked on {0}.", revocationDate)));
51         }
52
53         return new SuccessMessageResult(new TranslateCommand("Certificate is valid."));
54     }
55
56     @Override
57     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
58         t.output(out, l, vars);
59     }
60
61 }