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