]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/Verify.java
upd: document variables in SprintfCommand more clearly
[gigi.git] / src / club / wpia / gigi / pages / Verify.java
1 package club.wpia.gigi.pages;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
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;
20
21 public class Verify extends Page {
22
23     private static final SprintfCommand emailAddressVerified = new SprintfCommand("Email address {0} verified", Arrays.asList("${subject"));
24
25     private static final SprintfCommand domainVerified = new SprintfCommand("Domain {0} verified", Arrays.asList("${subject"));
26
27     private class VerificationForm extends Form {
28
29         private String hash;
30
31         private String type;
32
33         private String id;
34
35         private Verifyable target;
36
37         String subject;
38
39         public VerificationForm(HttpServletRequest hsr) {
40             super(hsr, PATH);
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();
47                 target = addr;
48             } else if ("domain".equals(type)) {
49                 Domain domain = Domain.getById(Integer.parseInt(id));
50                 subject = domain.getSuffix();
51                 target = domain;
52             } else {
53                 throw new IllegalArgumentException();
54             }
55             try {
56                 if ( !target.isVerifyable(hash)) {
57                     throw new IllegalArgumentException();
58                 }
59             } catch (GigiApiException e) {
60                 throw new IllegalArgumentException(e);
61             }
62         }
63
64         @Override
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)) {
69                 try {
70                     target.verify(hash);
71                 } catch (IllegalArgumentException e) {
72                     throw new PermamentFormException(new GigiApiException("Given token could not be found to complete the verification process (Email Ping)."));
73                 }
74                 return new SuccessMessageResult(new Scope(emailAddressVerified, data));
75             } else if ("domain".equals(type)) {
76                 try {
77                     target.verify(hash);
78                 } catch (IllegalArgumentException e) {
79                     throw new PermamentFormException(new GigiApiException("Given token could not be found to complete the verification process (Domain Ping)."));
80                 }
81                 return new SuccessMessageResult(new Scope(domainVerified, data));
82             } else {
83                 throw new GigiApiException("Invalid object type.");
84             }
85         }
86
87         @Override
88         protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
89             vars.put("hash", hash);
90             vars.put("id", id);
91             vars.put("type", type);
92
93             vars.put("subject", subject);
94             getDefaultTemplate().output(out, l, vars);
95         }
96     }
97
98     public static final String PATH = "/verify";
99
100     public Verify() {
101         super("Verify email");
102     }
103
104     @Override
105     public boolean needsLogin() {
106         return false;
107     }
108
109     @Override
110     public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
111         return Form.getForm(req, VerificationForm.class).submitExceptionProtected(req, resp);
112     }
113
114     @Override
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>());
118         }
119     }
120
121     @Override
122     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
123         try {
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."));
127         }
128     }
129
130 }