1 package club.wpia.gigi.api;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.math.BigInteger;
6 import java.util.HashMap;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
11 import org.json.JSONWriter;
13 import club.wpia.gigi.dbObjects.Certificate;
14 import club.wpia.gigi.dbObjects.CertificateOwner;
15 import club.wpia.gigi.dbObjects.Group;
16 import club.wpia.gigi.dbObjects.Organisation;
17 import club.wpia.gigi.dbObjects.User;
18 import club.wpia.gigi.email.EmailProvider;
19 import club.wpia.gigi.pages.account.FindAgentAccess;
20 import club.wpia.gigi.util.ServerConstants;
21 import club.wpia.gigi.util.ServerConstants.Host;
23 public class FindAgent extends APIPoint {
25 public static final String PATH_RESOLVE = "/find-agent/resolve";
27 public static final String PATH_INFO = "/find-agent/info";
29 public static final String PATH_MAIL = "/find-agent/email";
33 public static void register(HashMap<String, APIPoint> api) {
34 APIPoint p = new FindAgent();
35 api.put(PATH_RESOLVE, p);
36 api.put(PATH_INFO, p);
37 api.put(PATH_MAIL, p);
41 public void process(HttpServletRequest req, HttpServletResponse resp, CertificateOwner u) throws IOException {
42 if ( !(u instanceof Organisation)) {
43 resp.sendError(500, "Error, invalid cert");
46 if ( !((Organisation) u).isSelfOrganisation()) {
47 resp.sendError(500, "Error, invalid cert");
50 String pi = req.getPathInfo();
51 if (pi.equals(PATH_RESOLVE)) {
52 String serial = req.getParameter("serial");
53 if (serial == null || serial.isEmpty()) {
54 resp.sendError(500, "Error, requires serial");
57 Certificate c = Certificate.getBySerial(new BigInteger(serial, 16));
59 resp.sendError(500, "Error, requires serial");
62 CertificateOwner co = c.getOwner();
63 if ( !(co instanceof User)) {
64 resp.sendError(500, "Error, requires serial");
68 if ( !us.isInGroup(Group.LOCATE_AGENT)) {
70 resp.setContentType("text/plain; charset=UTF-8");
71 resp.getWriter().println("https://" + ServerConstants.getHostNamePortSecure(Host.SECURE) + FindAgentAccess.PATH);
74 resp.setContentType("text/plain; charset=UTF-8");
75 resp.getWriter().print(us.getId());
76 } else if (pi.equals(PATH_INFO)) {
77 resp.setContentType("application/json; charset=UTF-8");
78 PrintWriter out = resp.getWriter();
79 String[] uids = req.getParameterValues("id");
80 JSONWriter jw = new JSONWriter(out);
82 for (String i : uids) {
83 User u1 = User.getById(Integer.parseInt(i));
84 if ( !u1.isInGroup(Group.LOCATE_AGENT)) {
87 // date, recheck(?), name
93 jw.value(u1.canVerify());
96 jw.value(u1.getPreferredName().toAbbreviatedString());
100 } else if (pi.equals(PATH_MAIL)) {
101 String id = req.getParameter("from");
102 String rid = req.getParameter("to");
103 String subject = req.getParameter("subject");
104 String body = req.getParameter("body");
105 if (id == null || rid == null || subject == null || body == null) {
106 resp.sendError(500, "Error, parameter missing");
109 User from = User.getById(Integer.parseInt(id));
110 User to = User.getById(Integer.parseInt(rid));
111 if (from == null || to == null) {
112 resp.sendError(500, "Error, user not found");
115 if ( !from.isInGroup(Group.LOCATE_AGENT) || !to.isInGroup(Group.LOCATE_AGENT)) {
116 resp.sendError(501, "Error, user needs to enable access");
120 EmailProvider.getInstance().sendMail(to.getEmail(), "[Find Agent] " + subject, body, null, null, null, null, false);