1 package org.cacert.gigi.pages;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
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;
18 public class Verify extends Page {
20 private class VerificationForm extends Form {
28 private Verifyable target;
30 public VerificationForm(HttpServletRequest hsr) {
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));
43 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
44 if ("email".equals(type)) {
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));
53 } else if ("domain".equals(type)) {
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));
67 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
68 vars.put("hash", hash);
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());
76 getDefaultTemplate().output(out, l, vars);
80 public static final String PATH = "/verify";
83 super("Verify email");
87 public boolean needsLogin() {
92 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
94 if (Form.getForm(req, VerificationForm.class).submit(resp.getWriter(), req)) {
96 } catch (GigiApiException e) {
97 e.format(resp.getWriter(), getLanguage(req));
102 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
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."));