]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/Verify.java
Merge "Fix error message"
[gigi.git] / src / org / cacert / gigi / pages / Verify.java
1 package org.cacert.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 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;
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         }
56
57         @Override
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)) {
62                 try {
63                     target.verify(hash);
64                 } catch (IllegalArgumentException e) {
65                     throw new PermamentFormException(new GigiApiException("Given token could not be found to complete the verification process (Email Ping)."));
66                 }
67                 return new SuccessMessageResult(new Scope(emailAddressVerified, data));
68             } else if ("domain".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 (Domain Ping)."));
73                 }
74                 return new SuccessMessageResult(new Scope(domainVerified, data));
75             } else {
76                 throw new GigiApiException("Invalid object type.");
77             }
78         }
79
80         @Override
81         protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
82             vars.put("hash", hash);
83             vars.put("id", id);
84             vars.put("type", type);
85
86             vars.put("subject", subject);
87             getDefaultTemplate().output(out, l, vars);
88         }
89     }
90
91     public static final String PATH = "/verify";
92
93     public Verify() {
94         super("Verify email");
95     }
96
97     @Override
98     public boolean needsLogin() {
99         return false;
100     }
101
102     @Override
103     public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
104         return Form.getForm(req, VerificationForm.class).submitExceptionProtected(req, resp);
105     }
106
107     @Override
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>());
111         }
112     }
113
114     @Override
115     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
116         try {
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."));
120         }
121     }
122
123 }