]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/domain/PingConfigForm.java
Merge branch 'libs/jetty/local'
[gigi.git] / src / org / cacert / gigi / pages / account / domain / PingConfigForm.java
1 package org.cacert.gigi.pages.account.domain;
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.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;
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                 tokenName = parts[0];
91                 tokenValue = parts[1];
92                 ports[portpos] = Integer.parseInt(parts[2]);
93                 if (parts.length == 4) {
94                     sslTypes[portpos] = SSLType.valueOf(parts[3].toUpperCase());
95                 } else {
96                     sslTypes[portpos] = SSLType.DIRECT;
97                 }
98                 portpos++;
99                 break;
100             }
101             }
102         }
103     }
104
105     public void setTarget(Domain target) {
106         this.target = target;
107     }
108
109     @Override
110     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
111         target.clearPings();
112         if (req.getParameter("emailType") != null && req.getParameter("email") != null) {
113             try {
114                 String mail = AUTHORATIVE_EMAILS[Integer.parseInt(req.getParameter("email"))];
115                 target.addPing(DomainPingType.EMAIL, mail);
116             } catch (NumberFormatException e) {
117                 throw new GigiApiException("A email address is required");
118             }
119         }
120         if (req.getParameter("DNSType") != null) {
121             target.addPing(DomainPingType.DNS, tokenName + ":" + tokenValue);
122         }
123         if (req.getParameter("HTTPType") != null) {
124             target.addPing(DomainPingType.HTTP, tokenName + ":" + tokenValue);
125         }
126         if (req.getParameter("SSLType") != null) {
127             List<String> types = Arrays.asList(SSLPinger.TYPES);
128             for (int i = 0; i < MAX_SSL_TESTS; i++) {
129                 String type = req.getParameter("ssl-type-" + i);
130                 String port = req.getParameter("ssl-port-" + i);
131                 if (type == null || port == null || port.equals("")) {
132                     continue;
133                 }
134                 int portInt = Integer.parseInt(port);
135                 if ("direct".equals(type)) {
136                     target.addPing(DomainPingType.SSL, tokenName + ":" + tokenValue + ":" + port);
137                 } else if (types.contains(type)) {
138                     target.addPing(DomainPingType.SSL, tokenName + ":" + tokenValue + ":" + portInt + ":" + type);
139                 }
140
141             }
142         }
143         Gigi.notifyPinger(null);
144         return false;
145     }
146
147     @Override
148     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
149         out.print("<table class=\"wrapper dataTable\"><tbody>");
150         outputEmbeddableContent(out, l, vars);
151         out.print("<tr><td></td><td><input type=\"submit\" value=\"Update\"/></td></tbody></table>");
152     }
153
154     protected void outputEmbeddableContent(PrintWriter out, Language l, Map<String, Object> vars) {
155         vars.put("tokenName", tokenName);
156         vars.put("tokenValue", tokenValue);
157         vars.put("authEmails", new IterableDataset() {
158
159             int i = 0;
160
161             @Override
162             public boolean next(Language l, Map<String, Object> vars) {
163                 if (i >= AUTHORATIVE_EMAILS.length) {
164                     return false;
165                 }
166                 vars.put("i", i);
167                 vars.put("email", AUTHORATIVE_EMAILS[i]);
168                 if (i == selectedMail) {
169                     vars.put("checked", " checked=\"checked\"");
170                 } else {
171                     vars.put("checked", "");
172                 }
173
174                 i++;
175                 return true;
176             }
177         });
178         vars.put("mail", doMail ? " checked=\"checked\"" : "");
179         vars.put("dns", doDNS ? " checked=\"checked\"" : "");
180         vars.put("http", doHTTP ? " checked=\"checked\"" : "");
181         vars.put("ssl", doSSL ? " checked=\"checked\"" : "");
182         vars.put("ssl-services", new IterableDataset() {
183
184             int counter = 0;
185
186             @Override
187             public boolean next(Language l, Map<String, Object> vars) {
188                 if (counter >= MAX_SSL_TESTS) {
189                     return false;
190                 }
191                 vars.put("i", counter);
192                 vars.put("port", ports[counter] == 0 ? "" : Integer.toString(ports[counter]));
193                 final SSLType selectedType = sslTypes[counter];
194                 vars.put("ssl-types", new IterableDataset() {
195
196                     int i = 0;
197
198                     SSLType[] type = SSLType.values();
199
200                     @Override
201                     public boolean next(Language l, Map<String, Object> vars) {
202                         if (i >= type.length) {
203                             return false;
204                         }
205                         vars.put("name", type[i].toString());
206                         if (selectedType == type[i]) {
207                             vars.put("selected", " selected=\"selected\"");
208                         } else {
209                             vars.put("selected", "");
210                         }
211                         i++;
212                         return true;
213                     }
214                 });
215                 counter++;
216                 return true;
217             }
218         });
219         t.output(out, l, vars);
220     }
221 }