]> WPIA git - gigi.git/blob - src/org/cacert/gigi/api/Emails.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / org / cacert / gigi / api / Emails.java
1 package org.cacert.gigi.api;
2
3 import java.io.IOException;
4 import java.util.Date;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.dbObjects.EmailAddress;
11 import org.cacert.gigi.dbObjects.User;
12 import org.json.JSONWriter;
13
14 public class Emails extends APIPoint {
15
16     public static final String PATH = "/account/emails";
17
18     @Override
19     public void processGet(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
20         EmailAddress[] mails = u.getEmails();
21         resp.setContentType("application/json; charset=UTF-8");
22         JSONWriter jw = new JSONWriter(resp.getWriter());
23         jw.array();
24         for (EmailAddress emailAddress : mails) {
25             Date p = emailAddress.getLastPing(true);
26             jw.object();
27             jw.key("id");
28             jw.value(emailAddress.getId());
29             jw.key("lastPing");
30             jw.value((p == null ? 0 : p.getTime()));
31             jw.key("address");
32             jw.value(emailAddress.getAddress());
33             jw.endObject();
34         }
35         jw.endArray();
36     }
37
38     @Override
39     protected void process(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
40         try {
41             String email = req.getParameter("email");
42             if (email == null) {
43                 resp.sendError(500, "No parameter 'email'.");
44                 return;
45             }
46             new EmailAddress(u, email, u.getPreferredLocale());
47         } catch (IllegalArgumentException e) {
48             resp.sendError(500, "Invalid email");
49         } catch (GigiApiException e) {
50             resp.setStatus(500);
51             resp.setContentType("text/plain; charset=UTF-8");
52             e.formatPlain(resp.getWriter());
53         }
54     }
55
56 }