1 package club.wpia.gigi.api;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
10 import org.json.JSONWriter;
12 import club.wpia.gigi.dbObjects.Certificate;
13 import club.wpia.gigi.dbObjects.CertificateOwner;
14 import club.wpia.gigi.dbObjects.Group;
15 import club.wpia.gigi.dbObjects.Organisation;
16 import club.wpia.gigi.dbObjects.User;
17 import club.wpia.gigi.email.EmailProvider;
18 import club.wpia.gigi.pages.account.FindAgentAccess;
19 import club.wpia.gigi.util.ServerConstants;
21 public class FindAgent extends APIPoint {
23 public static final String PATH_RESOLVE = "/find-agent/resolve";
25 public static final String PATH_INFO = "/find-agent/info";
27 public static final String PATH_MAIL = "/find-agent/email";
31 public static void register(HashMap<String, APIPoint> api) {
32 APIPoint p = new FindAgent();
33 api.put(PATH_RESOLVE, p);
34 api.put(PATH_INFO, p);
35 api.put(PATH_MAIL, p);
39 public void process(HttpServletRequest req, HttpServletResponse resp, CertificateOwner u) throws IOException {
40 if ( !(u instanceof Organisation)) {
41 resp.sendError(500, "Error, invalid cert");
44 if ( !((Organisation) u).isSelfOrganisation()) {
45 resp.sendError(500, "Error, invalid cert");
48 String pi = req.getPathInfo();
49 if (pi.equals(PATH_RESOLVE)) {
50 String serial = req.getParameter("serial");
52 resp.sendError(500, "Error, requires serial");
55 Certificate c = Certificate.getBySerial(serial);
57 resp.sendError(500, "Error, requires serial");
60 CertificateOwner co = c.getOwner();
61 if ( !(co instanceof User)) {
62 resp.sendError(500, "Error, requires serial");
66 if ( !us.isInGroup(Group.LOCATE_AGENT)) {
68 resp.setContentType("text/plain; charset=UTF-8");
69 resp.getWriter().println("https://" + ServerConstants.getSecureHostNamePortSecure() + FindAgentAccess.PATH);
72 resp.setContentType("text/plain; charset=UTF-8");
73 resp.getWriter().print(us.getId());
74 } else if (pi.equals(PATH_INFO)) {
75 resp.setContentType("application/json; charset=UTF-8");
76 PrintWriter out = resp.getWriter();
77 String[] uids = req.getParameterValues("id");
78 JSONWriter jw = new JSONWriter(out);
80 for (String i : uids) {
81 User u1 = User.getById(Integer.parseInt(i));
82 if ( !u1.isInGroup(Group.LOCATE_AGENT)) {
85 // date, recheck(?), name
91 jw.value(u1.canAssure());
94 jw.value(u1.getPreferredName().toAbbreviatedString());
98 } else if (pi.equals(PATH_MAIL)) {
99 String id = req.getParameter("from");
100 String rid = req.getParameter("to");
101 String subject = req.getParameter("subject");
102 String body = req.getParameter("body");
103 if (id == null || rid == null || subject == null || body == null) {
104 resp.sendError(500, "Error, parameter missing");
107 User from = User.getById(Integer.parseInt(id));
108 User to = User.getById(Integer.parseInt(rid));
109 if (from == null || to == null) {
110 resp.sendError(500, "Error, user not found");
113 if ( !from.isInGroup(Group.LOCATE_AGENT) || !to.isInGroup(Group.LOCATE_AGENT)) {
114 resp.sendError(501, "Error, user needs to enable access");
118 EmailProvider.getInstance().sendMail(to.getEmail(), "[Find Agent] " + subject, body, null, null, null, null, false);