1 package org.cacert.gigi.api;
3 import java.io.IOException;
4 import java.util.HashMap;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
9 import org.cacert.gigi.dbObjects.Certificate;
10 import org.cacert.gigi.dbObjects.CertificateOwner;
11 import org.cacert.gigi.dbObjects.Group;
12 import org.cacert.gigi.dbObjects.Organisation;
13 import org.cacert.gigi.dbObjects.User;
14 import org.cacert.gigi.email.EmailProvider;
15 import org.cacert.gigi.pages.account.FindAgentAccess;
16 import org.cacert.gigi.util.ServerConstants;
18 public class FindAgent extends APIPoint {
20 public static final String PATH_RESOLVE = "/find-agent/resolve";
22 public static final String PATH_INFO = "/find-agent/info";
24 public static final String PATH_MAIL = "/find-agent/email";
28 public static void register(HashMap<String, APIPoint> api) {
29 APIPoint p = new FindAgent();
30 api.put(PATH_RESOLVE, p);
31 api.put(PATH_INFO, p);
32 api.put(PATH_MAIL, p);
36 public void process(HttpServletRequest req, HttpServletResponse resp, CertificateOwner u) throws IOException {
37 if ( !(u instanceof Organisation)) {
38 resp.sendError(500, "Error, invalid cert");
41 if ( !((Organisation) u).isSelfOrganisation()) {
42 resp.sendError(500, "Error, invalid cert");
45 String pi = req.getPathInfo();
46 if (pi.equals(PATH_RESOLVE)) {
47 String serial = req.getParameter("serial");
49 resp.sendError(500, "Error, requires serial");
52 Certificate c = Certificate.getBySerial(serial);
54 resp.sendError(500, "Error, requires serial");
57 CertificateOwner co = c.getOwner();
58 if ( !(co instanceof User)) {
59 resp.sendError(500, "Error, requires serial");
63 if ( !us.isInGroup(Group.LOCATE_AGENT)) {
65 resp.setContentType("text/plain; charset=UTF-8");
66 resp.getWriter().println("https://" + ServerConstants.getSecureHostNamePort() + FindAgentAccess.PATH);
69 resp.setContentType("text/plain; charset=UTF-8");
70 resp.getWriter().print(us.getId());
71 } else if (pi.equals(PATH_INFO)) {
72 resp.setContentType("text/plain; charset=UTF-8");
73 String[] uids = req.getParameterValues("id");
74 for (String i : uids) {
75 User u1 = User.getById(Integer.parseInt(i));
76 if ( !u1.isInGroup(Group.LOCATE_AGENT)) {
79 // date, recheck(?), name
80 resp.getWriter().println(i + "," + u1.canAssure() + "," + u1.getPreferredName().toAbbreviatedString());
82 } else if (pi.equals(PATH_MAIL)) {
83 String id = req.getParameter("from");
84 String rid = req.getParameter("to");
85 String subject = req.getParameter("subject");
86 String body = req.getParameter("body");
87 if (id == null || rid == null || subject == null || body == null) {
88 resp.sendError(500, "Error, parameter missing");
91 User from = User.getById(Integer.parseInt(id));
92 User to = User.getById(Integer.parseInt(rid));
93 if (from == null || to == null) {
94 resp.sendError(500, "Error, user not found");
97 if ( !from.isInGroup(Group.LOCATE_AGENT) || !to.isInGroup(Group.LOCATE_AGENT)) {
98 resp.sendError(501, "Error, user needs to enable access");
102 EmailProvider.getInstance().sendMail(to.getEmail(), "[Find Agent] " + subject, body, null, null, null, null, false);