KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > ServletAction


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.servlet;
4
5 import jodd.util.StringUtil;
6
7 import java.io.IOException JavaDoc;
8 import java.lang.reflect.Method JavaDoc;
9
10 import javax.servlet.ServletException JavaDoc;
11 import javax.servlet.http.HttpServlet JavaDoc;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14
15 /**
16  * Replacement and enhancement of <code>HttpServlet</code>, containing additional
17  * functionalities. Must be inherited by user.
18  * <p>
19  *
20  * Here both POST and GET requests calls <code>doRequest</code> method, which
21  * is the entering point of the servlet. For more easier work, this servlet
22  * may use one of the helper methods. Helper methods can be grouped in 3
23  * groups:
24  *
25  * <ul>
26  *
27  * <li>invokers - they execute an action. Action may be specified in request
28  * parameter 'action' or internal, during method invocation. Actions are
29  * (names of the) methods in the same <code>ServletAction</code>
30  * implementation. Action methods can help in reducing the number of
31  * <code>ServletAction</code> classes, since one class may be used for
32  * handling more than one request.</li>
33  *
34  * <li>forwarders - perform forwards to specific URL. They also can read
35  * forward destination from the request parameter 'forward'. </ul>
36  *
37  * <li>redirectors - perform redirect to specific URL. They also can read
38  * redirect destination from the request parameter 'redirect'. </ul>
39  * </ul>
40  *
41  * Default forwarders and redirector names given as request parameter are
42  * either 'forward' or 'redirect' respectively. If more than one parameter
43  * is needed, they have to be named as 'forward-...' or 'redirect-...'.
44  */

45 public abstract class ServletAction extends HttpServlet JavaDoc {
46
47     // ---------------------------------------------------------------- HTTP method
48

49     public final static int HTTP_METHOD_UNKNOWN = 0;
50     public final static int HTTP_METHOD_GET = 1;
51     public final static int HTTP_METHOD_POST = 2;
52
53     private int method = HTTP_METHOD_UNKNOWN;
54
55
56     /**
57      * Returns HTTP method (POST/GET) which invoked the action.
58      */

59     public int getHttpMethod() {
60         return method;
61     }
62
63     /**
64      * Default doGet method, calls {@link #doRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
65      */

66     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
67         method = HTTP_METHOD_GET;
68         doRequest(request, response);
69     }
70
71     /**
72      * Default doPost method, calls {@link #doRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
73      */

74     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
75         method = HTTP_METHOD_POST;
76         doRequest(request, response);
77     }
78
79     /**
80      * Main get/post request handler.
81      */

82     protected void doRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
83     }
84
85     // ---------------------------------------------------------------- UTILTIES: forward
86

87     /**
88      * Performs forward with use of the RequestDispatcher.
89      */

90     public boolean forward(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc url) throws IOException JavaDoc, ServletException JavaDoc {
91         return DispatcherUtil.forward(request, response, url);
92     }
93
94     /**
95      * Performs forward with use of the RequestDispatcher.
96      * URL is read from the request. If URL doesn't exist, nothing happens.
97      *
98      * @return true if parameter found, false otherwise
99      */

100     public boolean forwardParam(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
101         String JavaDoc url = request.getParameter("forward");
102         if (url != null) {
103             return forward(request, response, url);
104         }
105         return false;
106     }
107
108     /**
109      * Performs forward where URL is read from the request. If URL doesn't exist,
110      * nothing happens.
111      *
112      * @return <code>true</code> if parameter found and forward was successful,
113      * <code>false</code> otherwise
114      */

115     public boolean forwardParam(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc s) throws IOException JavaDoc, ServletException JavaDoc {
116         String JavaDoc url = request.getParameter("forward-" + s);
117         if (url != null) {
118             return forward(request, response, url);
119         }
120         return false;
121
122     }
123
124
125     // ---------------------------------------------------------------- UTILTIES: redirect
126

127     /**
128      * Performs redirection.
129      */

130     protected static void redirect(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc url) throws IOException JavaDoc {
131         DispatcherUtil.redirect(request, response, url);
132     }
133
134     /**
135      * Performs redirection. URL is read from the request. If URL doesn't exist,
136      * nothing happens.
137      *
138      * @return true if parameter found, false otherwise
139      */

140     protected static boolean redirectParam(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
141         String JavaDoc url = request.getParameter("redirect");
142         if (url != null) {
143             redirect(request, response, url);
144             return true;
145         }
146         return false;
147
148     }
149
150     /**
151      * Performs redirection where URL is read from the request. If URL doesn't
152      * exist, nothing happens.
153      *
154      * @return true if parameter found, false otherwise
155      */

156     protected static boolean redirectParam(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc s) throws IOException JavaDoc {
157         String JavaDoc url = request.getParameter("redirect-" + s);
158         if (url != null) {
159             redirect(request, response, url);
160             return true;
161         }
162         return false;
163
164     }
165
166
167
168
169     // ---------------------------------------------------------------- UTILTIES: invokers
170

171     /**
172      * Calls a method defined in the request parameter. Method name is read from
173      * the request. If method name is null (doesn't exist in the request) nothing
174      * happens.
175      *
176      * @return true if action was invoked, otherwise false
177      */

178     public String JavaDoc invokeActionParam(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
179         String JavaDoc actionName = request.getParameter("action");
180         return invokeActionParam(this, request, response, actionName);
181     }
182
183     /**
184      * Invoke a method from this ServletAction class. Methods name is given as
185      * parameter. If method name is null nothing happens.
186      *
187      * @return null if error, otherwise string mapping
188      */

189     public String JavaDoc invokeActionParam(Object JavaDoc servlet, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc actionName) throws ServletException JavaDoc {
190         if (actionName == null) {
191             return null;
192         }
193         try {
194             Method JavaDoc m = servlet.getClass().getMethod(actionName, new Class JavaDoc[] {HttpServletRequest JavaDoc.class, HttpServletResponse JavaDoc.class});
195             Object JavaDoc result = m.invoke(servlet, new Object JavaDoc[] {request, response});
196             return StringUtil.toString(result);
197         } catch (Exception JavaDoc ex) {
198             throw new ServletException JavaDoc("Invocation error of action '" + actionName + "': " + ex.getMessage(), ex);
199         }
200     }
201
202
203     // ---------------------------------------------------------------- UTILITIES: include
204

205     /**
206      * Performs include page.
207      */

208     public static boolean include(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc page) throws IOException JavaDoc, ServletException JavaDoc {
209         return DispatcherUtil.include(request, response, page);
210     }
211
212     public static boolean includeParam(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
213         String JavaDoc url = request.getParameter("include");
214         if (url != null) {
215             return include(request, response, url);
216         }
217         return false;
218     }
219
220     public static boolean includeParam(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc includeName) throws IOException JavaDoc, ServletException JavaDoc {
221         String JavaDoc url = request.getParameter("include-" + includeName);
222         if (url != null) {
223             return include(request, response, url);
224         }
225         return false;
226     }
227
228
229 }
230
Popular Tags