Merge "upd: Reduce Boilerplate in translated SprintfCommands"
[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 public class GigiAPI extends HttpServlet {
14
15     private static final long serialVersionUID = 659963677032635817L;
16
17     HashMap<String, APIPoint> api = new HashMap<>();
18
19     public GigiAPI() {
20         api.put(CreateCertificate.PATH, new CreateCertificate());
21         api.put(RevokeCertificate.PATH, new RevokeCertificate());
22         api.put(CATSImport.PATH, new CATSImport());
23         api.put(CATSResolve.PATH, new CATSResolve());
24     }
25
26     @Override
27     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
28         String pi = req.getPathInfo();
29         if (pi == null) {
30             return;
31         }
32         if (pi.equals("/security/csp/report")) {
33             ServletInputStream sis = req.getInputStream();
34             InputStreamReader isr = new InputStreamReader(sis, "UTF-8");
35             StringBuffer strB = new StringBuffer();
36             char[] buffer = new char[4 * 1024];
37             int len;
38             while ((len = isr.read(buffer)) > 0) {
39                 strB.append(buffer, 0, len);
40             }
41             System.out.println(strB);
42             return;
43         }
44
45         APIPoint p = api.get(pi);
46         if (p != null) {
47             p.process(req, resp);
48         }
49     }
50 }