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.Scope;
19 import org.cacert.gigi.output.template.SprintfCommand;
21 public class Verify extends Page {
23 private static final SprintfCommand emailAddressVerified = new SprintfCommand("Email address {0} verified", Arrays.asList("${subject}"));
25 private static final SprintfCommand domainVerified = new SprintfCommand("Domain {0} verified", Arrays.asList("${subject}"));
27 private class VerificationForm extends Form {
35 private Verifyable target;
39 public VerificationForm(HttpServletRequest hsr) {
41 hash = hsr.getParameter("hash");
42 type = hsr.getParameter("type");
43 id = hsr.getParameter("id");
44 if ("email".equals(type)) {
45 EmailAddress addr = EmailAddress.getById(Integer.parseInt(id));
46 subject = addr.getAddress();
48 } else if ("domain".equals(type)) {
49 Domain domain = Domain.getById(Integer.parseInt(id));
50 subject = domain.getSuffix();
53 throw new IllegalArgumentException();
58 public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
59 HashMap<String, Object> data = new HashMap<>();
60 data.put("subject", subject);
61 if ("email".equals(type)) {
64 } catch (IllegalArgumentException e) {
65 throw new GigiApiException("The email address is invalid.");
67 return new SuccessMessageResult(new Scope(emailAddressVerified, data));
68 } else if ("domain".equals(type)) {
71 } catch (IllegalArgumentException e) {
72 throw new GigiApiException("The domain is invalid.");
74 return new SuccessMessageResult(new Scope(domainVerified, data));
76 throw new GigiApiException("Invalid object type.");
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 boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
104 return Form.getForm(req, VerificationForm.class).submitExceptionProtected(req, resp);
108 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
109 if (Form.printFormErrors(req, resp.getWriter())) {
110 Form.getForm(req, VerificationForm.class).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
115 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
117 new VerificationForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
118 } catch (IllegalArgumentException e) {
119 resp.getWriter().println(translate(req, "The object to verify is invalid."));