]> WPIA git - gigi.git/commitdiff
add: email-management-api
authorFelix Dörre <felix@dogcraft.de>
Mon, 3 Oct 2016 12:03:38 +0000 (14:03 +0200)
committerFelix Dörre <felix@dogcraft.de>
Sun, 16 Oct 2016 13:23:21 +0000 (15:23 +0200)
Change-Id: I4f7ca7b68e9222520738fb329ba390b07fd74b10

src/org/cacert/gigi/api/APIPoint.java
src/org/cacert/gigi/api/EmailReping.java [new file with mode: 0644]
src/org/cacert/gigi/api/Emails.java [new file with mode: 0644]
src/org/cacert/gigi/api/GigiAPI.java

index 684729123a8ed4b9230cd6c4810a305d3756201e..8987afdb4bf5f628e61c49c4448cdbfaebb7d32c 100644 (file)
@@ -24,6 +24,15 @@ public abstract class APIPoint {
             resp.sendError(403, "Error, cert authing required. Serial not found: " + serial);
             return;
         }
+        if (req.getMethod().equals("GET")) {
+            if (u instanceof User) {
+                processGet(req, resp, (User) u);
+                return;
+            } else {
+                resp.sendError(500, "Error, requires a User certificate.");
+                return;
+            }
+        }
 
         if ( !req.getMethod().equals("POST")) {
             resp.sendError(500, "Error, POST required.");
@@ -46,6 +55,10 @@ public abstract class APIPoint {
     }
 
     protected void process(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
+        resp.sendError(500, "Error, Post not allowed.");
+    }
 
+    protected void processGet(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
+        resp.sendError(500, "Error, Get not allowed.");
     }
 }
diff --git a/src/org/cacert/gigi/api/EmailReping.java b/src/org/cacert/gigi/api/EmailReping.java
new file mode 100644 (file)
index 0000000..e2c3383
--- /dev/null
@@ -0,0 +1,41 @@
+package org.cacert.gigi.api;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.cacert.gigi.GigiApiException;
+import org.cacert.gigi.dbObjects.EmailAddress;
+import org.cacert.gigi.dbObjects.User;
+import org.cacert.gigi.localisation.Language;
+
+public class EmailReping extends APIPoint {
+
+    public static final String PATH = "/account/emails/reping";
+
+    @Override
+    protected void process(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
+        try {
+            String email = req.getParameter("email");
+            if (email == null) {
+                resp.sendError(500, "No parameter 'email'.");
+                return;
+            }
+            for (EmailAddress e : u.getEmails()) {
+                if (e.getAddress().equals(email)) {
+                    e.requestReping(Language.getInstance(u.getPreferredLocale()));
+                    resp.setContentType("text/plain; charset=UTF-8");
+                    return;
+                }
+            }
+            resp.sendError(500, "Error, Email address not found.");
+        } catch (IllegalArgumentException e) {
+            resp.sendError(500, "Invalid email");
+        } catch (GigiApiException e) {
+            resp.setStatus(500);
+            resp.setContentType("text/plain; charset=UTF-8");
+            e.formatPlain(resp.getWriter());
+        }
+    }
+}
diff --git a/src/org/cacert/gigi/api/Emails.java b/src/org/cacert/gigi/api/Emails.java
new file mode 100644 (file)
index 0000000..6641a8e
--- /dev/null
@@ -0,0 +1,56 @@
+package org.cacert.gigi.api;
+
+import java.io.IOException;
+import java.util.Date;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.cacert.gigi.GigiApiException;
+import org.cacert.gigi.dbObjects.EmailAddress;
+import org.cacert.gigi.dbObjects.User;
+import org.json.JSONWriter;
+
+public class Emails extends APIPoint {
+
+    public static final String PATH = "/account/emails";
+
+    @Override
+    public void processGet(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
+        EmailAddress[] mails = u.getEmails();
+        resp.setContentType("application/json; charset=UTF-8");
+        JSONWriter jw = new JSONWriter(resp.getWriter());
+        jw.array();
+        for (EmailAddress emailAddress : mails) {
+            Date p = emailAddress.getLastPing(true);
+            jw.object();
+            jw.key("id");
+            jw.value(emailAddress.getId());
+            jw.key("lastPing");
+            jw.value((p == null ? 0 : p.getTime()));
+            jw.key("address");
+            jw.value(emailAddress.getAddress());
+            jw.endObject();
+        }
+        jw.endArray();
+    }
+
+    @Override
+    protected void process(HttpServletRequest req, HttpServletResponse resp, User u) throws IOException {
+        try {
+            String email = req.getParameter("email");
+            if (email == null) {
+                resp.sendError(500, "No parameter 'email'.");
+                return;
+            }
+            new EmailAddress(u, email, u.getPreferredLocale());
+        } catch (IllegalArgumentException e) {
+            resp.sendError(500, "Invalid email");
+        } catch (GigiApiException e) {
+            resp.setStatus(500);
+            resp.setContentType("text/plain; charset=UTF-8");
+            e.formatPlain(resp.getWriter());
+        }
+    }
+
+}
index 2f5e922c8e146aca32a348bfe880020b1e95e4c5..bd6884f9855827f123d0f03c507e99f1ae6cdb1c 100644 (file)
@@ -21,6 +21,8 @@ public class GigiAPI extends HttpServlet {
 
     public GigiAPI() {
         api.put(CreateCertificate.PATH, new CreateCertificate());
+        api.put(Emails.PATH, new Emails());
+        api.put(EmailReping.PATH, new EmailReping());
         api.put(RevokeCertificate.PATH, new RevokeCertificate());
         api.put(CATSImport.PATH, new CATSImport());
         api.put(CATSResolve.PATH, new CATSResolve());