]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/Verify.java
fix: correct editable/non-editable DoB display in My Details
[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.SprintfCommand;
19
20 public class Verify extends Page {
21
22     private static final SprintfCommand emailAddressVerified = new SprintfCommand("Email address {0} verified", Arrays.asList("${subject}"));
23
24     private static final SprintfCommand domainVerified = new SprintfCommand("Domain {0} verified", Arrays.asList("${subject}"));
25
26     private class VerificationForm extends Form {
27
28         private String hash;
29
30         private String type;
31
32         private String id;
33
34         private Verifyable target;
35
36         String subject;
37
38         public VerificationForm(HttpServletRequest hsr) {
39             super(hsr, PATH);
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();
46                 target = addr;
47             } else if ("domain".equals(type)) {
48                 Domain domain = Domain.getById(Integer.parseInt(id));
49                 subject = domain.getSuffix();
50                 target = domain;
51             }
52         }
53
54         @Override
55         public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
56             HashMap<String, Object> data = new HashMap<>();
57             data.put("subject", subject);
58             if ("email".equals(type)) {
59                 try {
60                     target.verify(hash);
61                     emailAddressVerified.output(out, getLanguage(req), data);
62                 } catch (IllegalArgumentException e) {
63                     out.println(translate(req, "The email address is invalid."));
64                 } catch (GigiApiException e) {
65                     e.format(out, getLanguage(req));
66                 }
67             } else if ("domain".equals(type)) {
68                 try {
69                     target.verify(hash);
70                     domainVerified.output(out, getLanguage(req), data);
71                 } catch (IllegalArgumentException e) {
72                     out.println(translate(req, "The domain is invalid."));
73                 } catch (GigiApiException e) {
74                     e.format(out, getLanguage(req));
75                 }
76             }
77             return true;
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 void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
104         try {
105             if (Form.getForm(req, VerificationForm.class).submit(resp.getWriter(), req)) {
106             }
107         } catch (GigiApiException e) {
108             e.format(resp.getWriter(), getLanguage(req));
109         }
110     }
111
112     @Override
113     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
114         try {
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."));
118
119         }
120     }
121
122 }