]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/template/Form.java
f330fe865a9e36e42ff874528338db347cdb1ea9
[gigi.git] / src / org / cacert / gigi / output / template / Form.java
1 package org.cacert.gigi.output.template;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Map;
6
7 import javax.servlet.ServletRequest;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpSession;
10
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.localisation.Language;
13 import org.cacert.gigi.pages.Page;
14 import org.cacert.gigi.util.RandomToken;
15
16 public abstract class Form implements Outputable {
17
18     public static final String CSRF_FIELD = "csrf";
19
20     private final String csrf;
21
22     private final String action;
23
24     public Form(HttpServletRequest hsr) {
25         this(hsr, null);
26     }
27
28     public Form(HttpServletRequest hsr, String action) {
29         csrf = RandomToken.generateToken(32);
30         this.action = action;
31         HttpSession hs = hsr.getSession();
32         hs.setAttribute("form/" + getClass().getName() + "/" + csrf, this);
33     }
34
35     public abstract boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException;
36
37     protected String getCsrfFieldName() {
38         return CSRF_FIELD;
39     }
40
41     @Override
42     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
43         if (action == null) {
44             out.println("<form method='POST'>");
45         } else {
46             out.println("<form method='POST' action='" + action + "'>");
47         }
48         failed = false;
49         outputContent(out, l, vars);
50         out.print("<input type='hidden' name='" + CSRF_FIELD + "' value='");
51         out.print(getCSRFToken());
52         out.println("'></form>");
53     }
54
55     protected abstract void outputContent(PrintWriter out, Language l, Map<String, Object> vars);
56
57     boolean failed;
58
59     protected void outputError(PrintWriter out, ServletRequest req, String text, Object... contents) {
60         if ( !failed) {
61             failed = true;
62             out.println("<div class='formError'>");
63         }
64         out.print("<div>");
65         if (contents.length == 0) {
66             out.print(Page.translate(req, text));
67         } else {
68             out.print(String.format(Page.translate(req, text), contents));
69         }
70         out.println("</div>");
71     }
72
73     protected void outputErrorPlain(PrintWriter out, String text) {
74         if ( !failed) {
75             failed = true;
76             out.println("<div class='formError'>");
77         }
78         out.print("<div>");
79         out.print(text);
80         out.println("</div>");
81     }
82
83     public boolean isFailed(PrintWriter out) {
84         if (failed) {
85             out.println("</div>");
86         }
87         return failed;
88     }
89
90     protected String getCSRFToken() {
91         return csrf;
92     }
93
94     public static <T extends Form> T getForm(HttpServletRequest req, Class<T> target) throws CSRFException {
95         String csrf = req.getParameter(CSRF_FIELD);
96         if (csrf == null) {
97             throw new CSRFException();
98         }
99         HttpSession hs = req.getSession();
100         if (hs == null) {
101             throw new CSRFException();
102         }
103         Form f = (Form) hs.getAttribute("form/" + target.getName() + "/" + csrf);
104         if (f == null) {
105             throw new CSRFException();
106         }
107         return (T) f;
108     }
109
110     public static class CSRFException extends IOException {
111
112     }
113 }