]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/template/Form.java
Merge changes I29db4164,I90632cf4
[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 /**
17  * A generic HTML-form that handles CSRF-token creation.
18  */
19 public abstract class Form implements Outputable {
20
21     public static final String CSRF_FIELD = "csrf";
22
23     private final String csrf;
24
25     private final String action;
26
27     /**
28      * Creates a new {@link Form}.
29      * 
30      * @param hsr
31      *            the request to register the form against.
32      */
33     public Form(HttpServletRequest hsr) {
34         this(hsr, null);
35     }
36
37     /**
38      * Creates a new {@link Form}.
39      * 
40      * @param hsr
41      *            the request to register the form against.
42      * @param action
43      *            the target path where the form should be submitted.
44      */
45     public Form(HttpServletRequest hsr, String action) {
46         csrf = RandomToken.generateToken(32);
47         this.action = action;
48         HttpSession hs = hsr.getSession();
49         hs.setAttribute("form/" + getClass().getName() + "/" + csrf, this);
50     }
51
52     /**
53      * Update the forms internal state based on submitted data.
54      * 
55      * @param out
56      *            the stream to the user.
57      * @param req
58      *            the request to take the initial data from.
59      * @return true, iff the form succeeded and the user should be redirected.
60      * @throws GigiApiException
61      *             if internal operations went wrong.
62      */
63     public abstract boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException;
64
65     protected String getCsrfFieldName() {
66         return CSRF_FIELD;
67     }
68
69     @Override
70     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
71         if (action == null) {
72             out.println("<form method='POST'>");
73         } else {
74             out.println("<form method='POST' action='" + action + "'>");
75         }
76         failed = false;
77         outputContent(out, l, vars);
78         out.print("<input type='hidden' name='" + CSRF_FIELD + "' value='");
79         out.print(getCSRFToken());
80         out.println("'></form>");
81     }
82
83     /**
84      * Outputs the forms contents.
85      * 
86      * @param out
87      *            Stream to the user.
88      * @param l
89      *            {@link Language} to translate text to.
90      * @param vars
91      *            Variables supplied from the outside.
92      */
93     protected abstract void outputContent(PrintWriter out, Language l, Map<String, Object> vars);
94
95     private boolean failed;
96
97     protected void outputError(PrintWriter out, ServletRequest req, String text, Object... contents) {
98         if ( !failed) {
99             failed = true;
100             out.println("<div class='formError'>");
101         }
102         out.print("<div>");
103         if (contents.length == 0) {
104             out.print(Page.translate(req, text));
105         } else {
106             out.print(String.format(Page.translate(req, text), contents));
107         }
108         out.println("</div>");
109     }
110
111     protected void outputErrorPlain(PrintWriter out, String text) {
112         if ( !failed) {
113             failed = true;
114             out.println("<div class='formError'>");
115         }
116         out.print("<div>");
117         out.print(text);
118         out.println("</div>");
119     }
120
121     public boolean isFailed(PrintWriter out) {
122         if (failed) {
123             out.println("</div>");
124         }
125         return failed;
126     }
127
128     protected String getCSRFToken() {
129         return csrf;
130     }
131
132     /**
133      * Re-fetches a form e.g. when a Post-request is received.
134      * 
135      * @param req
136      *            the request that is directed to the form.
137      * @param target
138      *            the {@link Class} of the expected form.
139      * @return the form where this request is directed to.
140      * @throws CSRFException
141      *             if no CSRF-token is found or the token is wrong.
142      */
143     public static <T extends Form> T getForm(HttpServletRequest req, Class<T> target) throws CSRFException {
144         String csrf = req.getParameter(CSRF_FIELD);
145         if (csrf == null) {
146             throw new CSRFException();
147         }
148         HttpSession hs = req.getSession();
149         if (hs == null) {
150             throw new CSRFException();
151         }
152         Form f = (Form) hs.getAttribute("form/" + target.getName() + "/" + csrf);
153         if (f == null) {
154             throw new CSRFException();
155         }
156         return (T) f;
157     }
158
159     public static class CSRFException extends IOException {
160
161         private static final long serialVersionUID = 59708247477988362L;
162
163     }
164 }