KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > FormFieldValidator


1 package hudson.util;
2
3 import hudson.Util;
4 import hudson.model.Hudson;
5 import org.kohsuke.stapler.StaplerRequest;
6 import org.kohsuke.stapler.StaplerResponse;
7
8 import javax.servlet.ServletException JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11
12 /**
13  * @author Kohsuke Kawaguchi
14  */

15 public abstract class FormFieldValidator {
16     protected final StaplerRequest request;
17     protected final StaplerResponse response;
18     private final boolean isAdminOnly;
19
20     protected FormFieldValidator(StaplerRequest request, StaplerResponse response, boolean adminOnly) {
21         this.request = request;
22         this.response = response;
23         isAdminOnly = adminOnly;
24     }
25
26     /**
27      * Runs the validation code.
28      */

29     public final void process() throws IOException JavaDoc, ServletException JavaDoc {
30         if(isAdminOnly && !Hudson.adminCheck(request,response))
31             return; // failed check
32

33         check();
34     }
35
36     protected abstract void check() throws IOException JavaDoc, ServletException JavaDoc;
37
38     /**
39      * Gets the parameter as a file.
40      */

41     protected final File JavaDoc getFileParameter(String JavaDoc paramName) {
42         return new File JavaDoc(Util.fixNull(request.getParameter(paramName)));
43     }
44
45     /**
46      * Sends out an HTML fragment that indicates a success.
47      */

48     public void ok() throws IOException JavaDoc, ServletException JavaDoc {
49         response.setContentType("text/html");
50         response.getWriter().print("<div/>");
51     }
52
53     /**
54      * Sends out an HTML fragment that indicates an error.
55      */

56     public void error(String JavaDoc message) throws IOException JavaDoc, ServletException JavaDoc {
57         response.setContentType("text/html;charset=UTF-8");
58         response.getWriter().print("<div class=error>"+message+"</div>");
59     }
60 }
61
Popular Tags