]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/Verify.java
upd: Beautify up the verification form.
[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.HashMap;
6 import java.util.Map;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.dbObjects.Domain;
13 import org.cacert.gigi.dbObjects.EmailAddress;
14 import org.cacert.gigi.dbObjects.Verifyable;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.template.Form;
17
18 public class Verify extends Page {
19
20     private class VerificationForm extends Form {
21
22         private String hash;
23
24         private String type;
25
26         private String id;
27
28         private Verifyable target;
29
30         public VerificationForm(HttpServletRequest hsr) {
31             super(hsr, PATH);
32             hash = hsr.getParameter("hash");
33             type = hsr.getParameter("type");
34             id = hsr.getParameter("id");
35             if ("email".equals(type)) {
36                 target = EmailAddress.getById(Integer.parseInt(id));
37             } else if ("domain".equals("type")) {
38                 target = Domain.getById(Integer.parseInt(id));
39             }
40         }
41
42         @Override
43         public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
44             if ("email".equals(type)) {
45                 try {
46                     target.verify(hash);
47                     out.println("Email verification completed.");
48                 } catch (IllegalArgumentException e) {
49                     out.println(translate(req, "The email address is invalid."));
50                 } catch (GigiApiException e) {
51                     e.format(out, getLanguage(req));
52                 }
53             } else if ("domain".equals(type)) {
54                 try {
55                     target.verify(hash);
56                     out.println("Domain verification completed.");
57                 } catch (IllegalArgumentException e) {
58                     out.println(translate(req, "The domain is invalid."));
59                 } catch (GigiApiException e) {
60                     e.format(out, getLanguage(req));
61                 }
62             }
63             return true;
64         }
65
66         @Override
67         protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
68             vars.put("hash", hash);
69             vars.put("id", id);
70             vars.put("type", type);
71             if (target instanceof EmailAddress) {
72                 vars.put("subject", ((EmailAddress) target).getAddress());
73             } else if (target instanceof Domain) {
74                 vars.put("subject", ((Domain) target).getSuffix());
75             }
76             getDefaultTemplate().output(out, l, vars);
77         }
78     }
79
80     public static final String PATH = "/verify";
81
82     public Verify() {
83         super("Verify email");
84     }
85
86     @Override
87     public boolean needsLogin() {
88         return false;
89     }
90
91     @Override
92     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
93         try {
94             if (Form.getForm(req, VerificationForm.class).submit(resp.getWriter(), req)) {
95             }
96         } catch (GigiApiException e) {
97             e.format(resp.getWriter(), getLanguage(req));
98         }
99     }
100
101     @Override
102     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
103         try {
104             new VerificationForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
105         } catch (IllegalArgumentException e) {
106             resp.getWriter().println(translate(req, "The object to verify is invalid."));
107
108         }
109     }
110
111 }