]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/DomainAddForm.java
Move the "dbObject"s to their own package.
[gigi.git] / src / org / cacert / gigi / pages / account / DomainAddForm.java
1 package org.cacert.gigi.pages.account;
2
3 import java.io.PrintWriter;
4 import java.util.Arrays;
5 import java.util.List;
6 import java.util.Map;
7
8 import javax.servlet.http.HttpServletRequest;
9
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;
22
23 public class DomainAddForm extends Form {
24
25     private static final Template t = new Template(DomainManagementForm.class.getResource("DomainAddForm.templ"));
26
27     private User target;
28
29     private String tokenName = RandomToken.generateToken(8);
30
31     private String tokenValue = RandomToken.generateToken(16);
32
33     private static final int MAX_SSL_TESTS = 4;
34
35     public DomainAddForm(HttpServletRequest hsr, User target) {
36         super(hsr);
37         this.target = target;
38     }
39
40     @Override
41     public boolean submit(PrintWriter out, HttpServletRequest req) {
42         try {
43             String parameter = req.getParameter("newdomain");
44             if (parameter.trim().isEmpty()) {
45                 throw new GigiApiException("No domain inserted.");
46             }
47             Domain d = new Domain(target, parameter);
48             d.insert();
49             if (req.getParameter("emailType") != null) {
50                 String mail = AUTHORATIVE_EMAILS[Integer.parseInt(req.getParameter("email"))];
51                 d.addPing("email", mail);
52             }
53             if (req.getParameter("DNSType") != null) {
54                 d.addPing("dns", tokenName + ":" + tokenValue);
55             }
56             if (req.getParameter("HTTPType") != null) {
57                 d.addPing("http", tokenName + ":" + tokenValue);
58             }
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("")) {
65                         continue;
66                     }
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);
72                     }
73
74                 }
75             }
76             Gigi.notifyPinger();
77
78             return true;
79         } catch (NumberFormatException e) {
80             new GigiApiException("A number could not be parsed").format(out, Page.getLanguage(req));
81             return false;
82         } catch (GigiApiException e) {
83             e.format(out, Page.getLanguage(req));
84             return false;
85         }
86     }
87
88     public static final String[] AUTHORATIVE_EMAILS = new String[] {
89             "root", "hostmaster", "postmaster", "admin", "webmaster"
90     };
91
92     @Override
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() {
98
99             int counter = 0;
100
101             @Override
102             public boolean next(Language l, Map<String, Object> vars) {
103                 if (counter >= MAX_SSL_TESTS) {
104                     return false;
105                 }
106                 vars.put("i", counter);
107                 counter++;
108                 return true;
109             }
110         });
111         t.output(out, l, vars);
112     }
113 }