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();
52 throw new IllegalArgumentException();
57 public boolean submit(HttpServletRequest req) throws GigiApiException {
58 if ("email".equals(type)) {
61 } catch (IllegalArgumentException e) {
62 throw new GigiApiException("The email address is invalid.");
64 } else if ("domain".equals(type)) {
67 } catch (IllegalArgumentException e) {
68 throw new GigiApiException("The domain is invalid.");
75 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
76 vars.put("hash", hash);
78 vars.put("type", type);
80 vars.put("subject", subject);
81 getDefaultTemplate().output(out, l, vars);
85 public static final String PATH = "/verify";
88 super("Verify email");
92 public boolean needsLogin() {
97 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
98 VerificationForm form = Form.getForm(req, VerificationForm.class);
99 if (form.submitProtected(resp.getWriter(), req)) {
100 String type = form.type;
101 HashMap<String, Object> data = new HashMap<>();
102 data.put("subject", form.subject);
103 PrintWriter out = resp.getWriter();
104 if ("email".equals(type)) {
105 emailAddressVerified.output(out, getLanguage(req), data);
106 } else if ("domain".equals(type)) {
107 domainVerified.output(out, getLanguage(req), data);
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."));