]> WPIA git - gigi.git/blob - src/club/wpia/gigi/api/APIPoint.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / club / wpia / gigi / api / APIPoint.java
1 package club.wpia.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 club.wpia.gigi.dbObjects.Certificate;
10 import club.wpia.gigi.dbObjects.CertificateOwner;
11 import club.wpia.gigi.dbObjects.User;
12 import club.wpia.gigi.pages.LoginPage;
13
14 public abstract class APIPoint {
15
16     public void process(HttpServletRequest req, HttpServletResponse resp) throws IOException {
17         X509Certificate cert = LoginPage.getCertificateFromRequest(req);
18         if (cert == null) {
19             resp.sendError(403, "Error, cert authing required. No cert found.");
20             return;
21         }
22         String serial = LoginPage.extractSerialFormCert(cert);
23         Certificate clientCert = Certificate.getBySerial(serial);
24         CertificateOwner u = CertificateOwner.getByEnabledSerial(serial);
25         if (u == null || clientCert == null) {
26             resp.sendError(403, "Error, cert authing required. Serial not found: " + serial);
27             return;
28         }
29         if (req.getMethod().equals("GET")) {
30             if (u instanceof User) {
31                 processGet(req, resp, (User) u);
32                 return;
33             } else {
34                 resp.sendError(500, "Error, requires a User certificate.");
35                 return;
36             }
37         }
38
39         if ( !req.getMethod().equals("POST")) {
40             resp.sendError(500, "Error, POST required.");
41             return;
42         }
43         if (req.getQueryString() != null) {
44             resp.sendError(500, "Error, no query String allowed.");
45             return;
46         }
47         process(req, resp, u, clientCert);
48     }
49
50     protected void process(HttpServletRequest req, HttpServletResponse resp, CertificateOwner u, Certificate clientCert) throws IOException {
51         process(req, resp, u);
52     }
53
54     protected void process(HttpServletRequest req, HttpServletResponse resp, CertificateOwner u) throws IOException {
55         if (u instanceof User) {
56             process(req, resp, (User) u);
57         } else {
58             resp.sendError(500, "Error, requires a User certificate.");
59             return;
60         }
61     }
62
63     protected void process(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
64         resp.sendError(500, "Error, Post not allowed.");
65     }
66
67     protected void processGet(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
68         resp.sendError(500, "Error, Get not allowed.");
69     }
70 }