1 package org.cacert.gigi.pages.account.domain;
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.DomainPingConfiguration;
14 import org.cacert.gigi.dbObjects.DomainPingType;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.template.Form;
17 import org.cacert.gigi.output.template.IterableDataset;
18 import org.cacert.gigi.output.template.Template;
19 import org.cacert.gigi.ping.SSLPinger;
20 import org.cacert.gigi.util.RandomToken;
22 public class PingConfigForm extends Form {
25 DIRECT, XMPP, XMPP_SERVER, SMTP, IMAP;
28 public String toString() {
29 return super.toString().toLowerCase();
33 private Domain target;
35 private String tokenName = RandomToken.generateToken(8);
37 private String tokenValue = RandomToken.generateToken(16);
39 private static final int MAX_SSL_TESTS = 4;
41 public static final String[] AUTHORATIVE_EMAILS = new String[] {
42 "root", "hostmaster", "postmaster", "admin", "webmaster"
45 private int selectedMail = -1;
47 private boolean doMail, doDNS, doHTTP, doSSL;
49 private int[] ports = new int[MAX_SSL_TESTS];
51 private SSLType[] sslTypes = new SSLType[MAX_SSL_TESTS];
53 private final Template t = new Template(PingConfigForm.class.getResource("PingConfigForm.templ"));
55 public PingConfigForm(HttpServletRequest hsr, Domain target) throws GigiApiException {
61 List<DomainPingConfiguration> configs = target.getConfiguredPings();
63 for (DomainPingConfiguration dpc : configs) {
64 switch (dpc.getType()) {
67 for (int i = 0; i < AUTHORATIVE_EMAILS.length; i++) {
68 if (AUTHORATIVE_EMAILS[i].equals(dpc.getInfo())) {
75 String[] parts = dpc.getInfo().split(":");
77 tokenValue = parts[1];
82 String[] parts = dpc.getInfo().split(":");
84 tokenValue = parts[1];
89 String[] parts = dpc.getInfo().split(":");
90 ports[portpos] = Integer.parseInt(parts[0]);
91 if (parts.length == 2) {
92 sslTypes[portpos] = SSLType.valueOf(parts[1].toUpperCase());
94 sslTypes[portpos] = SSLType.DIRECT;
103 public void setTarget(Domain target) {
104 this.target = target;
108 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
109 if (req.getParameter("emailType") != null && req.getParameter("email") != null) {
111 String mail = AUTHORATIVE_EMAILS[Integer.parseInt(req.getParameter("email"))];
112 target.addPing(DomainPingType.EMAIL, mail);
113 } catch (NumberFormatException e) {
114 throw new GigiApiException("A email address is required");
117 if (req.getParameter("DNSType") != null) {
118 target.addPing(DomainPingType.DNS, tokenName + ":" + tokenValue);
120 if (req.getParameter("HTTPType") != null) {
121 target.addPing(DomainPingType.HTTP, tokenName + ":" + tokenValue);
123 if (req.getParameter("SSLType") != null) {
124 List<String> types = Arrays.asList(SSLPinger.TYPES);
125 for (int i = 0; i < MAX_SSL_TESTS; i++) {
126 String type = req.getParameter("ssl-type-" + i);
127 String port = req.getParameter("ssl-port-" + i);
128 if (type == null || port == null || port.equals("")) {
131 int portInt = Integer.parseInt(port);
132 if ("direct".equals(type)) {
133 target.addPing(DomainPingType.SSL, port);
134 } else if (types.contains(type)) {
135 target.addPing(DomainPingType.SSL, portInt + ":" + type);
140 Gigi.notifyPinger(null);
145 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
146 out.print("<table class=\"wrapper dataTable\"><tbody>");
147 outputEmbeddableContent(out, l, vars);
148 out.print("<tr><td></td><td><input type=\"submit\" value=\"Update\"/></td></tbody></table>");
151 protected void outputEmbeddableContent(PrintWriter out, Language l, Map<String, Object> vars) {
152 vars.put("tokenName", tokenName);
153 vars.put("tokenValue", tokenValue);
154 vars.put("authEmails", new IterableDataset() {
159 public boolean next(Language l, Map<String, Object> vars) {
160 if (i >= AUTHORATIVE_EMAILS.length) {
164 vars.put("email", AUTHORATIVE_EMAILS[i]);
165 if (i == selectedMail) {
166 vars.put("checked", " checked=\"checked\"");
168 vars.put("checked", "");
175 vars.put("mail", doMail ? " checked=\"checked\"" : "");
176 vars.put("dns", doDNS ? " checked=\"checked\"" : "");
177 vars.put("http", doHTTP ? " checked=\"checked\"" : "");
178 vars.put("ssl", doSSL ? " checked=\"checked\"" : "");
179 vars.put("ssl-services", new IterableDataset() {
184 public boolean next(Language l, Map<String, Object> vars) {
185 if (counter >= MAX_SSL_TESTS) {
188 vars.put("i", counter);
189 vars.put("port", ports[counter] == 0 ? "" : Integer.toString(ports[counter]));
190 final SSLType selectedType = sslTypes[counter];
191 vars.put("ssl-types", new IterableDataset() {
195 SSLType[] type = SSLType.values();
198 public boolean next(Language l, Map<String, Object> vars) {
199 if (i >= type.length) {
202 vars.put("name", type[i].toString());
203 if (selectedType == type[i]) {
204 vars.put("selected", " selected=\"selected\"");
206 vars.put("selected", "");
216 t.output(out, l, vars);