1 package club.wpia.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 club.wpia.gigi.GigiApiException;
13 import club.wpia.gigi.dbObjects.Domain;
14 import club.wpia.gigi.dbObjects.EmailAddress;
15 import club.wpia.gigi.dbObjects.Verifyable;
16 import club.wpia.gigi.localisation.Language;
17 import club.wpia.gigi.output.template.Form;
18 import club.wpia.gigi.output.template.Scope;
19 import club.wpia.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();
56 if ( !target.isVerifyable(hash)) {
57 throw new IllegalArgumentException();
59 } catch (GigiApiException e) {
60 throw new IllegalArgumentException(e);
65 public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
66 HashMap<String, Object> data = new HashMap<>();
67 data.put("subject", subject);
68 if ("email".equals(type)) {
71 } catch (IllegalArgumentException e) {
72 throw new PermamentFormException(new GigiApiException("Given token could not be found to complete the verification process (Email Ping)."));
74 return new SuccessMessageResult(new Scope(emailAddressVerified, data));
75 } else if ("domain".equals(type)) {
78 } catch (IllegalArgumentException e) {
79 throw new PermamentFormException(new GigiApiException("Given token could not be found to complete the verification process (Domain Ping)."));
81 return new SuccessMessageResult(new Scope(domainVerified, data));
83 throw new GigiApiException("Invalid object type.");
88 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
89 vars.put("hash", hash);
91 vars.put("type", type);
93 vars.put("subject", subject);
94 getDefaultTemplate().output(out, l, vars);
98 public static final String PATH = "/verify";
101 super("Verify email");
105 public boolean needsLogin() {
110 public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
111 return Form.getForm(req, VerificationForm.class).submitExceptionProtected(req, resp);
115 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
116 if (Form.printFormErrors(req, resp.getWriter())) {
117 Form.getForm(req, VerificationForm.class).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
122 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
124 new VerificationForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
125 } catch (IllegalArgumentException e) {
126 resp.getWriter().println(translate(req, "The object to verify is invalid."));