1 package org.cacert.gigi.pages;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Arrays;
6 import java.util.HashMap;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.dbObjects.Domain;
14 import org.cacert.gigi.dbObjects.EmailAddress;
15 import org.cacert.gigi.dbObjects.Verifyable;
16 import org.cacert.gigi.localisation.Language;
17 import org.cacert.gigi.output.template.Form;
18 import org.cacert.gigi.output.template.SprintfCommand;
20 public class Verify extends Page {
22 private static final SprintfCommand emailAddressVerified = new SprintfCommand("Email address {0} verified", Arrays.asList("${subject}"));
24 private static final SprintfCommand domainVerified = new SprintfCommand("Domain {0} verified", Arrays.asList("${subject}"));
26 private class VerificationForm extends Form {
34 private Verifyable target;
38 public VerificationForm(HttpServletRequest hsr) {
40 hash = hsr.getParameter("hash");
41 type = hsr.getParameter("type");
42 id = hsr.getParameter("id");
43 if ("email".equals(type)) {
44 EmailAddress addr = EmailAddress.getById(Integer.parseInt(id));
45 subject = addr.getAddress();
47 } else if ("domain".equals(type)) {
48 Domain domain = Domain.getById(Integer.parseInt(id));
49 subject = domain.getSuffix();
55 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
56 HashMap<String, Object> data = new HashMap<>();
57 data.put("subject", subject);
58 if ("email".equals(type)) {
61 emailAddressVerified.output(out, getLanguage(req), data);
62 } catch (IllegalArgumentException e) {
63 out.println(translate(req, "The email address is invalid."));
64 } catch (GigiApiException e) {
65 e.format(out, getLanguage(req));
67 } else if ("domain".equals(type)) {
70 domainVerified.output(out, getLanguage(req), data);
71 } catch (IllegalArgumentException e) {
72 out.println(translate(req, "The domain is invalid."));
73 } catch (GigiApiException e) {
74 e.format(out, getLanguage(req));
81 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
82 vars.put("hash", hash);
84 vars.put("type", type);
86 vars.put("subject", subject);
87 getDefaultTemplate().output(out, l, vars);
91 public static final String PATH = "/verify";
94 super("Verify email");
98 public boolean needsLogin() {
103 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
105 if (Form.getForm(req, VerificationForm.class).submit(resp.getWriter(), req)) {
107 } catch (GigiApiException e) {
108 e.format(resp.getWriter(), getLanguage(req));
113 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
115 new VerificationForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
116 } catch (IllegalArgumentException e) {
117 resp.getWriter().println(translate(req, "The object to verify is invalid."));