]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/PingconfigForm.java
51523cbb7090631e22d7fb359977cb8f4be7cde4
[gigi.git] / src / org / cacert / gigi / pages / account / PingconfigForm.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.DomainPingConfiguration;
14 import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.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;
21
22 public class PingconfigForm extends Form {
23
24     public enum SSLType {
25         DIRECT, XMPP, XMPP_SERVER, SMTP, IMAP;
26
27         @Override
28         public String toString() {
29             return super.toString().toLowerCase();
30         }
31     }
32
33     private Domain target;
34
35     private String tokenName = RandomToken.generateToken(8);
36
37     private String tokenValue = RandomToken.generateToken(16);
38
39     private static final int MAX_SSL_TESTS = 4;
40
41     public static final String[] AUTHORATIVE_EMAILS = new String[] {
42             "root", "hostmaster", "postmaster", "admin", "webmaster"
43     };
44
45     private int selectedMail = -1;
46
47     private boolean doMail, doDNS, doHTTP, doSSL;
48
49     private int[] ports = new int[MAX_SSL_TESTS];
50
51     private SSLType[] sslTypes = new SSLType[MAX_SSL_TESTS];
52
53     private final Template t = new Template(PingconfigForm.class.getResource("PingconfigForm.templ"));
54
55     public PingconfigForm(HttpServletRequest hsr, Domain target) throws GigiApiException {
56         super(hsr);
57         this.target = target;
58         if (target == null) {
59             return;
60         }
61         List<DomainPingConfiguration> configs = target.getConfiguredPings();
62         int portpos = 0;
63         for (DomainPingConfiguration dpc : configs) {
64             switch (dpc.getType()) {
65             case EMAIL:
66                 doMail = true;
67                 for (int i = 0; i < AUTHORATIVE_EMAILS.length; i++) {
68                     if (AUTHORATIVE_EMAILS[i].equals(dpc.getInfo())) {
69                         selectedMail = i;
70                     }
71                 }
72                 break;
73             case DNS: {
74                 doDNS = true;
75                 String[] parts = dpc.getInfo().split(":");
76                 tokenName = parts[0];
77                 tokenValue = parts[1];
78                 break;
79             }
80             case HTTP: {
81                 doHTTP = true;
82                 String[] parts = dpc.getInfo().split(":");
83                 tokenName = parts[0];
84                 tokenValue = parts[1];
85                 break;
86             }
87             case SSL: {
88                 doSSL = true;
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());
93                 } else {
94                     sslTypes[portpos] = SSLType.DIRECT;
95                 }
96                 portpos++;
97                 break;
98             }
99             }
100         }
101     }
102
103     public void setTarget(Domain target) {
104         this.target = target;
105     }
106
107     @Override
108     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
109         if (req.getParameter("emailType") != null) {
110             String mail = AUTHORATIVE_EMAILS[Integer.parseInt(req.getParameter("email"))];
111             target.addPing(PingType.EMAIL, mail);
112         }
113         if (req.getParameter("DNSType") != null) {
114             target.addPing(PingType.DNS, tokenName + ":" + tokenValue);
115         }
116         if (req.getParameter("HTTPType") != null) {
117             target.addPing(PingType.HTTP, tokenName + ":" + tokenValue);
118         }
119         if (req.getParameter("SSLType") != null) {
120             List<String> types = Arrays.asList(SSLPinger.TYPES);
121             for (int i = 0; i < MAX_SSL_TESTS; i++) {
122                 String type = req.getParameter("ssl-type-" + i);
123                 String port = req.getParameter("ssl-port-" + i);
124                 if (type == null || port == null || port.equals("")) {
125                     continue;
126                 }
127                 int portInt = Integer.parseInt(port);
128                 if ("direct".equals(type)) {
129                     target.addPing(PingType.SSL, port);
130                 } else if (types.contains(type)) {
131                     target.addPing(PingType.SSL, portInt + ":" + type);
132                 }
133
134             }
135         }
136         Gigi.notifyPinger();
137         return false;
138     }
139
140     @Override
141     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
142         out.print("<table class=\"wrapper dataTable\"><tbody>");
143         outputEmbeddableContent(out, l, vars);
144         out.print("<tr><td></td><td><input type=\"submit\" value=\"Update\"/></td></tbody></table>");
145     }
146
147     protected void outputEmbeddableContent(PrintWriter out, Language l, Map<String, Object> vars) {
148         vars.put("tokenName", tokenName);
149         vars.put("tokenValue", tokenValue);
150         vars.put("authEmails", new IterableDataset() {
151
152             int i = 0;
153
154             @Override
155             public boolean next(Language l, Map<String, Object> vars) {
156                 if (i >= AUTHORATIVE_EMAILS.length) {
157                     return false;
158                 }
159                 vars.put("i", i);
160                 vars.put("email", AUTHORATIVE_EMAILS[i]);
161                 if (i == selectedMail) {
162                     vars.put("checked", " checked=\"checked\"");
163                 } else {
164                     vars.put("checked", "");
165                 }
166
167                 i++;
168                 return true;
169             }
170         });
171         vars.put("mail", doMail ? " checked=\"checked\"" : "");
172         vars.put("dns", doDNS ? " checked=\"checked\"" : "");
173         vars.put("http", doHTTP ? " checked=\"checked\"" : "");
174         vars.put("ssl", doSSL ? " checked=\"checked\"" : "");
175         vars.put("ssl-services", new IterableDataset() {
176
177             int counter = 0;
178
179             @Override
180             public boolean next(Language l, Map<String, Object> vars) {
181                 if (counter >= MAX_SSL_TESTS) {
182                     return false;
183                 }
184                 vars.put("i", counter);
185                 vars.put("port", ports[counter] == 0 ? "" : Integer.toString(ports[counter]));
186                 final SSLType selectedType = sslTypes[counter];
187                 vars.put("ssl-types", new IterableDataset() {
188
189                     int i = 0;
190
191                     SSLType[] type = SSLType.values();
192
193                     @Override
194                     public boolean next(Language l, Map<String, Object> vars) {
195                         if (i >= type.length) {
196                             return false;
197                         }
198                         vars.put("name", type[i].toString());
199                         if (selectedType == type[i]) {
200                             vars.put("selected", " selected=\"selected\"");
201                         } else {
202                             vars.put("selected", "");
203                         }
204                         i++;
205                         return true;
206                     }
207                 });
208                 counter++;
209                 return true;
210             }
211         });
212         t.output(out, l, vars);
213     }
214 }