Merge "Suggestions to enhance the SQL call pattern."
[gigi.git] / src / org / cacert / gigi / api / GigiAPI.java
1 package org.cacert.gigi.api;
2
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.util.HashMap;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.ServletInputStream;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import org.cacert.gigi.database.DatabaseConnection;
14 import org.cacert.gigi.database.DatabaseConnection.Link;
15
16 public class GigiAPI extends HttpServlet {
17
18     private static final long serialVersionUID = 659963677032635817L;
19
20     HashMap<String, APIPoint> api = new HashMap<>();
21
22     public GigiAPI() {
23         api.put(CreateCertificate.PATH, new CreateCertificate());
24         api.put(RevokeCertificate.PATH, new RevokeCertificate());
25         api.put(CATSImport.PATH, new CATSImport());
26         api.put(CATSResolve.PATH, new CATSResolve());
27     }
28
29     @Override
30     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
31         String pi = req.getPathInfo();
32         if (pi == null) {
33             return;
34         }
35         if (pi.equals("/security/csp/report")) {
36             ServletInputStream sis = req.getInputStream();
37             InputStreamReader isr = new InputStreamReader(sis, "UTF-8");
38             StringBuffer strB = new StringBuffer();
39             char[] buffer = new char[4 * 1024];
40             int len;
41             while ((len = isr.read(buffer)) > 0) {
42                 strB.append(buffer, 0, len);
43             }
44             System.out.println(strB);
45             return;
46         }
47
48         APIPoint p = api.get(pi);
49         try (Link l = DatabaseConnection.newLink(false)) {
50             if (p != null) {
51                 p.process(req, resp);
52             }
53         } catch (InterruptedException e) {
54             throw new Error(e);
55         }
56     }
57 }