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