]> WPIA git - gigi.git/blob - src/org/cacert/gigi/api/APIPoint.java
add: email-management-api
[gigi.git] / src / org / cacert / gigi / api / APIPoint.java
1 package org.cacert.gigi.api;
2
3 import java.io.IOException;
4 import java.security.cert.X509Certificate;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import org.cacert.gigi.dbObjects.CertificateOwner;
10 import org.cacert.gigi.dbObjects.User;
11 import org.cacert.gigi.pages.LoginPage;
12
13 public abstract class APIPoint {
14
15     public void process(HttpServletRequest req, HttpServletResponse resp) throws IOException {
16         X509Certificate cert = LoginPage.getCertificateFromRequest(req);
17         if (cert == null) {
18             resp.sendError(403, "Error, cert authing required. No cert found.");
19             return;
20         }
21         String serial = LoginPage.extractSerialFormCert(cert);
22         CertificateOwner u = CertificateOwner.getByEnabledSerial(serial);
23         if (u == null) {
24             resp.sendError(403, "Error, cert authing required. Serial not found: " + serial);
25             return;
26         }
27         if (req.getMethod().equals("GET")) {
28             if (u instanceof User) {
29                 processGet(req, resp, (User) u);
30                 return;
31             } else {
32                 resp.sendError(500, "Error, requires a User certificate.");
33                 return;
34             }
35         }
36
37         if ( !req.getMethod().equals("POST")) {
38             resp.sendError(500, "Error, POST required.");
39             return;
40         }
41         if (req.getQueryString() != null) {
42             resp.sendError(500, "Error, no query String allowed.");
43             return;
44         }
45         process(req, resp, u);
46     }
47
48     protected void process(HttpServletRequest req, HttpServletResponse resp, CertificateOwner u) throws IOException {
49         if (u instanceof User) {
50             process(req, resp, (User) u);
51         } else {
52             resp.sendError(500, "Error, requires a User certificate.");
53             return;
54         }
55     }
56
57     protected void process(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
58         resp.sendError(500, "Error, Post not allowed.");
59     }
60
61     protected void processGet(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
62         resp.sendError(500, "Error, Get not allowed.");
63     }
64 }