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;
20 import club.wpia.gigi.util.ServerConstants.Host;
22 public class FindAgent extends APIPoint {
24 public static final String PATH_RESOLVE = "/find-agent/resolve";
26 public static final String PATH_INFO = "/find-agent/info";
28 public static final String PATH_MAIL = "/find-agent/email";
32 public static void register(HashMap<String, APIPoint> api) {
33 APIPoint p = new FindAgent();
34 api.put(PATH_RESOLVE, p);
35 api.put(PATH_INFO, p);
36 api.put(PATH_MAIL, p);
40 public void process(HttpServletRequest req, HttpServletResponse resp, CertificateOwner u) throws IOException {
41 if ( !(u instanceof Organisation)) {
42 resp.sendError(500, "Error, invalid cert");
45 if ( !((Organisation) u).isSelfOrganisation()) {
46 resp.sendError(500, "Error, invalid cert");
49 String pi = req.getPathInfo();
50 if (pi.equals(PATH_RESOLVE)) {
51 String serial = req.getParameter("serial");
53 resp.sendError(500, "Error, requires serial");
56 Certificate c = Certificate.getBySerial(serial);
58 resp.sendError(500, "Error, requires serial");
61 CertificateOwner co = c.getOwner();
62 if ( !(co instanceof User)) {
63 resp.sendError(500, "Error, requires serial");
67 if ( !us.isInGroup(Group.LOCATE_AGENT)) {
69 resp.setContentType("text/plain; charset=UTF-8");
70 resp.getWriter().println("https://" + ServerConstants.getHostNamePortSecure(Host.SECURE) + FindAgentAccess.PATH);
73 resp.setContentType("text/plain; charset=UTF-8");
74 resp.getWriter().print(us.getId());
75 } else if (pi.equals(PATH_INFO)) {
76 resp.setContentType("application/json; charset=UTF-8");
77 PrintWriter out = resp.getWriter();
78 String[] uids = req.getParameterValues("id");
79 JSONWriter jw = new JSONWriter(out);
81 for (String i : uids) {
82 User u1 = User.getById(Integer.parseInt(i));
83 if ( !u1.isInGroup(Group.LOCATE_AGENT)) {
86 // date, recheck(?), name
92 jw.value(u1.canVerify());
95 jw.value(u1.getPreferredName().toAbbreviatedString());
99 } else if (pi.equals(PATH_MAIL)) {
100 String id = req.getParameter("from");
101 String rid = req.getParameter("to");
102 String subject = req.getParameter("subject");
103 String body = req.getParameter("body");
104 if (id == null || rid == null || subject == null || body == null) {
105 resp.sendError(500, "Error, parameter missing");
108 User from = User.getById(Integer.parseInt(id));
109 User to = User.getById(Integer.parseInt(rid));
110 if (from == null || to == null) {
111 resp.sendError(500, "Error, user not found");
114 if ( !from.isInGroup(Group.LOCATE_AGENT) || !to.isInGroup(Group.LOCATE_AGENT)) {
115 resp.sendError(501, "Error, user needs to enable access");
119 EmailProvider.getInstance().sendMail(to.getEmail(), "[Find Agent] " + subject, body, null, null, null, null, false);