1 package org.cacert.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.cacert.gigi.dbObjects.Certificate;
11 import org.cacert.gigi.dbObjects.CertificateOwner;
12 import org.cacert.gigi.dbObjects.Group;
13 import org.cacert.gigi.dbObjects.Organisation;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.email.EmailProvider;
16 import org.cacert.gigi.pages.account.FindAgentAccess;
17 import org.cacert.gigi.util.ServerConstants;
18 import org.json.JSONWriter;
20 public class FindAgent extends APIPoint {
22 public static final String PATH_RESOLVE = "/find-agent/resolve";
24 public static final String PATH_INFO = "/find-agent/info";
26 public static final String PATH_MAIL = "/find-agent/email";
30 public static void register(HashMap<String, APIPoint> api) {
31 APIPoint p = new FindAgent();
32 api.put(PATH_RESOLVE, p);
33 api.put(PATH_INFO, p);
34 api.put(PATH_MAIL, p);
38 public void process(HttpServletRequest req, HttpServletResponse resp, CertificateOwner u) throws IOException {
39 if ( !(u instanceof Organisation)) {
40 resp.sendError(500, "Error, invalid cert");
43 if ( !((Organisation) u).isSelfOrganisation()) {
44 resp.sendError(500, "Error, invalid cert");
47 String pi = req.getPathInfo();
48 if (pi.equals(PATH_RESOLVE)) {
49 String serial = req.getParameter("serial");
51 resp.sendError(500, "Error, requires serial");
54 Certificate c = Certificate.getBySerial(serial);
56 resp.sendError(500, "Error, requires serial");
59 CertificateOwner co = c.getOwner();
60 if ( !(co instanceof User)) {
61 resp.sendError(500, "Error, requires serial");
65 if ( !us.isInGroup(Group.LOCATE_AGENT)) {
67 resp.setContentType("text/plain; charset=UTF-8");
68 resp.getWriter().println("https://" + ServerConstants.getSecureHostNamePort() + FindAgentAccess.PATH);
71 resp.setContentType("text/plain; charset=UTF-8");
72 resp.getWriter().print(us.getId());
73 } else if (pi.equals(PATH_INFO)) {
74 resp.setContentType("application/json; charset=UTF-8");
75 PrintWriter out = resp.getWriter();
76 String[] uids = req.getParameterValues("id");
77 JSONWriter jw = new JSONWriter(out);
79 for (String i : uids) {
80 User u1 = User.getById(Integer.parseInt(i));
81 if ( !u1.isInGroup(Group.LOCATE_AGENT)) {
84 // date, recheck(?), name
90 jw.value(u1.canAssure());
93 jw.value(u1.getPreferredName().toAbbreviatedString());
97 } else if (pi.equals(PATH_MAIL)) {
98 String id = req.getParameter("from");
99 String rid = req.getParameter("to");
100 String subject = req.getParameter("subject");
101 String body = req.getParameter("body");
102 if (id == null || rid == null || subject == null || body == null) {
103 resp.sendError(500, "Error, parameter missing");
106 User from = User.getById(Integer.parseInt(id));
107 User to = User.getById(Integer.parseInt(rid));
108 if (from == null || to == null) {
109 resp.sendError(500, "Error, user not found");
112 if ( !from.isInGroup(Group.LOCATE_AGENT) || !to.isInGroup(Group.LOCATE_AGENT)) {
113 resp.sendError(501, "Error, user needs to enable access");
117 EmailProvider.getInstance().sendMail(to.getEmail(), "[Find Agent] " + subject, body, null, null, null, null, false);