+ boolean failed;
+
+ protected void outputError(PrintWriter out, ServletRequest req, String text, Object... contents) {
+ if ( !failed) {
+ failed = true;
+ out.println("<div class='formError'>");
+ }
+ out.print("<div>");
+ if (contents.length == 0) {
+ out.print(Page.translate(req, text));
+ } else {
+ out.print(String.format(Page.translate(req, text), contents));
+ }
+ out.println("</div>");
+ }
+
+ protected void outputErrorPlain(PrintWriter out, String text) {
+ if ( !failed) {
+ failed = true;
+ out.println("<div class='formError'>");
+ }
+ out.print("<div>");
+ out.print(text);
+ out.println("</div>");
+ }
+
+ public boolean isFailed(PrintWriter out) {
+ if (failed) {
+ out.println("</div>");
+ }
+ return failed;
+ }
+
+ protected String getCSRFToken() {
+ return csrf;
+ }
+
+ public static <T extends Form> T getForm(HttpServletRequest req, Class<T> target) throws CSRFException {
+ String csrf = req.getParameter(CSRF_FIELD);
+ if (csrf == null) {
+ throw new CSRFException();
+ }
+ HttpSession hs = req.getSession();
+ if (hs == null) {
+ throw new CSRFException();
+ }
+ Form f = (Form) hs.getAttribute("form/" + target.getName() + "/" + csrf);
+ if (f == null) {
+ throw new CSRFException();
+ }
+ return (T) f;
+ }
+
+ public static class CSRFException extends IOException {
+
+ }