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.Domain;
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.User;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.Form;
15 import org.cacert.gigi.output.template.IterableDataset;
16 import org.cacert.gigi.output.template.OutputableArrayIterable;
17 import org.cacert.gigi.output.template.Template;
18 import org.cacert.gigi.pages.Page;
19 import org.cacert.gigi.ping.SSLPinger;
20 import org.cacert.gigi.util.RandomToken;
22 public class DomainAddForm extends Form {
24 private static final Template t = new Template(DomainManagementForm.class.getResource("DomainAddForm.templ"));
28 private String tokenName = RandomToken.generateToken(8);
30 private String tokenValue = RandomToken.generateToken(16);
32 private static final int MAX_SSL_TESTS = 4;
34 public DomainAddForm(HttpServletRequest hsr, User target) {
40 public boolean submit(PrintWriter out, HttpServletRequest req) {
42 String parameter = req.getParameter("newdomain");
43 if (parameter.trim().isEmpty()) {
44 throw new GigiApiException("No domain inserted.");
46 Domain d = new Domain(target, parameter);
48 if (req.getParameter("emailType") != null) {
49 String mail = AUTHORATIVE_EMAILS[Integer.parseInt(req.getParameter("email"))];
50 d.addPing("email", mail);
52 if (req.getParameter("DNSType") != null) {
53 d.addPing("dns", tokenName + ":" + tokenValue);
55 if (req.getParameter("HTTPType") != null) {
56 d.addPing("http", tokenName + ":" + tokenValue);
58 if (req.getParameter("SSLType") != null) {
59 List<String> types = Arrays.asList(SSLPinger.TYPES);
60 for (int i = 0; i < MAX_SSL_TESTS; i++) {
61 String type = req.getParameter("ssl-type-" + i);
62 String port = req.getParameter("ssl-port-" + i);
63 if (type == null || port == null || port.equals("")) {
66 int portInt = Integer.parseInt(port);
67 if ("direct".equals(type)) {
68 d.addPing("ssl", port);
69 } else if (types.contains(type)) {
70 d.addPing("ssl", portInt + ":" + type);
77 } catch (NumberFormatException e) {
78 new GigiApiException("A number could not be parsed").format(out, Page.getLanguage(req));
80 } catch (GigiApiException e) {
81 e.format(out, Page.getLanguage(req));
86 public static final String[] AUTHORATIVE_EMAILS = new String[] {
87 "root", "hostmaster", "postmaster", "admin", "webmaster"
91 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
92 vars.put("tokenName", tokenName);
93 vars.put("tokenValue", tokenValue);
94 vars.put("authEmails", new OutputableArrayIterable(AUTHORATIVE_EMAILS, "email"));
95 vars.put("ssl-services", new IterableDataset() {
100 public boolean next(Language l, Map<String, Object> vars) {
101 if (counter >= MAX_SSL_TESTS) {
104 vars.put("i", counter);
109 t.output(out, l, vars);