]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/domain/DomainOverview.java
upd: use a more strict pattern for handling forms
[gigi.git] / src / org / cacert / gigi / pages / account / domain / DomainOverview.java
1 package org.cacert.gigi.pages.account.domain;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.dbObjects.CertificateOwner;
11 import org.cacert.gigi.dbObjects.Domain;
12 import org.cacert.gigi.dbObjects.User;
13 import org.cacert.gigi.output.template.Form;
14 import org.cacert.gigi.pages.LoginPage;
15 import org.cacert.gigi.pages.Page;
16
17 public class DomainOverview extends Page {
18
19     public static final String PATH = "/account/domains/";
20
21     public DomainOverview() {
22         super("Domains");
23     }
24
25     @Override
26     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
27         CertificateOwner u = LoginPage.getAuthorizationContext(req).getTarget();
28         String pi = req.getPathInfo();
29         if (pi.length() - PATH.length() > 0) {
30             Form.printFormErrors(req, resp.getWriter());
31             int i = Integer.parseInt(pi.substring(PATH.length()));
32             Domain d;
33             try {
34                 d = Domain.getById(i);
35             } catch (IllegalArgumentException e) {
36                 resp.getWriter().println(getLanguage(req).getTranslation("Access denied"));
37                 return;
38             }
39             if (d == null || u.getId() != d.getOwner().getId()) {
40                 resp.getWriter().println(getLanguage(req).getTranslation("Access denied"));
41                 return;
42             }
43             new DomainPinglogForm(req, d).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
44             try {
45                 new PingConfigForm(req, d).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
46             } catch (GigiApiException e) {
47                 e.format(resp.getWriter(), getLanguage(req));
48             }
49             return;
50
51         }
52         try {
53             DomainManagementForm domMan = new DomainManagementForm(req, u, false);
54             HashMap<String, Object> vars = new HashMap<>();
55             vars.put("domainman", domMan);
56             if (u instanceof User) {
57                 DomainAddForm domAdd = new DomainAddForm(req, (User) u);
58                 vars.put("domainadd", domAdd);
59             }
60             getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
61         } catch (GigiApiException e) {
62             e.format(resp.getWriter(), getLanguage(req));
63         }
64     }
65
66     @Override
67     public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
68         String pi = req.getPathInfo();
69         if (pi.length() - PATH.length() > 0) {
70             if (req.getParameter("configId") != null) {
71                 if (Form.getForm(req, DomainPinglogForm.class).submitExceptionProtected(req)) {
72                     resp.sendRedirect(pi);
73                     return true;
74                 }
75
76             } else {
77                 if (Form.getForm(req, PingConfigForm.class).submitExceptionProtected(req)) {
78                     resp.sendRedirect(pi);
79                     return true;
80                 }
81             }
82
83         }
84         return super.beforePost(req, resp);
85     }
86
87     @Override
88     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
89         if (req.getParameter("adddomain") != null) {
90             DomainAddForm f = Form.getForm(req, DomainAddForm.class);
91             if (f.submitProtected(resp.getWriter(), req)) {
92                 resp.sendRedirect(PATH);
93             }
94         } else if (req.getParameter("delete") != null) {
95             DomainManagementForm f = Form.getForm(req, DomainManagementForm.class);
96             if (f.submitProtected(resp.getWriter(), req)) {
97                 resp.sendRedirect(PATH);
98             }
99         }
100         super.doPost(req, resp);
101     }
102 }