1 package org.cacert.gigi.pages.account;
3 import java.io.PrintWriter;
4 import java.util.Arrays;
8 import javax.servlet.http.HttpServletRequest;
10 import org.cacert.gigi.Gigi;
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.dbObjects.Domain;
13 import org.cacert.gigi.dbObjects.User;
14 import org.cacert.gigi.localisation.Language;
15 import org.cacert.gigi.output.Form;
16 import org.cacert.gigi.output.template.IterableDataset;
17 import org.cacert.gigi.output.template.OutputableArrayIterable;
18 import org.cacert.gigi.output.template.Template;
19 import org.cacert.gigi.pages.Page;
20 import org.cacert.gigi.ping.SSLPinger;
21 import org.cacert.gigi.util.RandomToken;
23 public class DomainAddForm extends Form {
25 private static final Template t = new Template(DomainManagementForm.class.getResource("DomainAddForm.templ"));
29 private String tokenName = RandomToken.generateToken(8);
31 private String tokenValue = RandomToken.generateToken(16);
33 private static final int MAX_SSL_TESTS = 4;
35 public DomainAddForm(HttpServletRequest hsr, User target) {
41 public boolean submit(PrintWriter out, HttpServletRequest req) {
43 String parameter = req.getParameter("newdomain");
44 if (parameter.trim().isEmpty()) {
45 throw new GigiApiException("No domain inserted.");
47 Domain d = new Domain(target, parameter);
49 if (req.getParameter("emailType") != null) {
50 String mail = AUTHORATIVE_EMAILS[Integer.parseInt(req.getParameter("email"))];
51 d.addPing("email", mail);
53 if (req.getParameter("DNSType") != null) {
54 d.addPing("dns", tokenName + ":" + tokenValue);
56 if (req.getParameter("HTTPType") != null) {
57 d.addPing("http", tokenName + ":" + tokenValue);
59 if (req.getParameter("SSLType") != null) {
60 List<String> types = Arrays.asList(SSLPinger.TYPES);
61 for (int i = 0; i < MAX_SSL_TESTS; i++) {
62 String type = req.getParameter("ssl-type-" + i);
63 String port = req.getParameter("ssl-port-" + i);
64 if (type == null || port == null || port.equals("")) {
67 int portInt = Integer.parseInt(port);
68 if ("direct".equals(type)) {
69 d.addPing("ssl", port);
70 } else if (types.contains(type)) {
71 d.addPing("ssl", portInt + ":" + type);
79 } catch (NumberFormatException e) {
80 new GigiApiException("A number could not be parsed").format(out, Page.getLanguage(req));
82 } catch (GigiApiException e) {
83 e.format(out, Page.getLanguage(req));
88 public static final String[] AUTHORATIVE_EMAILS = new String[] {
89 "root", "hostmaster", "postmaster", "admin", "webmaster"
93 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
94 vars.put("tokenName", tokenName);
95 vars.put("tokenValue", tokenValue);
96 vars.put("authEmails", new OutputableArrayIterable(AUTHORATIVE_EMAILS, "email"));
97 vars.put("ssl-services", new IterableDataset() {
102 public boolean next(Language l, Map<String, Object> vars) {
103 if (counter >= MAX_SSL_TESTS) {
106 vars.put("i", counter);
111 t.output(out, l, vars);