1 package org.cacert.gigi.pages;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.dbObjects.Domain;
13 import org.cacert.gigi.dbObjects.EmailAddress;
14 import org.cacert.gigi.dbObjects.Verifyable;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.template.Form;
17 import org.cacert.gigi.output.template.SprintfCommand;
19 public class Verify extends Page {
21 private static final SprintfCommand emailAddressVerified = new SprintfCommand("Email address ${subject} verified");
23 private static final SprintfCommand domainVerified = new SprintfCommand("Domain ${subject} verified");
25 private class VerificationForm extends Form {
33 private Verifyable target;
37 public VerificationForm(HttpServletRequest hsr) {
39 hash = hsr.getParameter("hash");
40 type = hsr.getParameter("type");
41 id = hsr.getParameter("id");
42 if ("email".equals(type)) {
43 EmailAddress addr = EmailAddress.getById(Integer.parseInt(id));
44 subject = addr.getAddress();
46 } else if ("domain".equals(type)) {
47 Domain domain = Domain.getById(Integer.parseInt(id));
48 subject = domain.getSuffix();
54 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
55 HashMap<String, Object> data = new HashMap<>();
56 data.put("subject", subject);
57 if ("email".equals(type)) {
60 emailAddressVerified.output(out, getLanguage(req), data);
61 } catch (IllegalArgumentException e) {
62 out.println(translate(req, "The email address is invalid."));
63 } catch (GigiApiException e) {
64 e.format(out, getLanguage(req));
66 } else if ("domain".equals(type)) {
69 domainVerified.output(out, getLanguage(req), data);
70 } catch (IllegalArgumentException e) {
71 out.println(translate(req, "The domain is invalid."));
72 } catch (GigiApiException e) {
73 e.format(out, getLanguage(req));
80 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
81 vars.put("hash", hash);
83 vars.put("type", type);
85 vars.put("subject", subject);
86 getDefaultTemplate().output(out, l, vars);
90 public static final String PATH = "/verify";
93 super("Verify email");
97 public boolean needsLogin() {
102 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
104 if (Form.getForm(req, VerificationForm.class).submit(resp.getWriter(), req)) {
106 } catch (GigiApiException e) {
107 e.format(resp.getWriter(), getLanguage(req));
112 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
114 new VerificationForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
115 } catch (IllegalArgumentException e) {
116 resp.getWriter().println(translate(req, "The object to verify is invalid."));