KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > ActionServlet


1 package jodd.servlet;
2
3 import java.io.IOException;
4 import java.lang.reflect.Method;
5
6 import javax.servlet.RequestDispatcher;
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import jodd.util.StringUtil;
13
14 /**
15  * Replacement and enhancement of <code>HttpServlet</code>, containing additional
16  * functionalities. Must be inherited by user.
17  * <p>
18  *
19  * <code>ActionServlet</code> may be used in two ways: 1) as standard http
20  * serlvet, or 2) as an action servlet in mvc2 framework.
21  * <p>
22  *
23  * <h2>1) <code>ActionServlet</code> as http servlet</h2>
24  *
25  * Here both POST and GET requests calls <code>doRequest</code> method, which
26  * is the entering point of the servlet. For more easier work, this servlet
27  * may use one of the helper methods. Helper methods can be grouped in 3
28  * groups:
29  *
30  * <ul>
31  *
32  * <li>invokers - they execute an action. Action may be specified in request
33  * parameter 'action' or internal, during method invocation. Actions are
34  * (names of the) methods in the same <code>ActionServlet</code>
35  * implementation. Action methods can help in reducing the number of
36  * <code>ActionServlet</code> classes, since one class may be used for
37  * handling more than one request.</li>
38  *
39  * <li>forwarders - perform forwards to specific url. They also can read
40  * forward destination from the request parameter 'forward'. </ul>
41  *
42  * <li>redirectors - perform redirect to specific url. They also can read
43  * redirect destination from the request parameter 'redirect'. </ul>
44  * </ul>
45  *
46  * Default forwarders and redirector names given as request parameter are
47  * either 'forward' or 'redirect' respectively. If more than one parameter
48  * is needed, they have to be named as 'forward-...' or 'redirect-...'.
49  *
50  * <h2>2) <code>ActionServlet</code> as framework action</h2>
51  * <h3>using default handler</h3>
52  *
53  * In this use case, on the client request container will invoke the
54  * controller (<code>ActionController</code>). Controller is configured from
55  * a XML file. Here, controller always invokes <code>doAction()</code> method
56  * (instead of <code>doRequest</code>). This method must return one of the
57  * forwards parameters specified in the XML configuration file, type of
58  * String. Afterm Controller will pass the control to adequate URI, as
59  * specified in the configuration file.
60  * <p>
61  *
62  * It is obvious that forwarders and redirectors should not be used now. On
63  * the other hand, invokers can be freely used. Moreover, they are just a bit
64  * more suited for this kind of usage, since whatever is returned from the
65  * invoker is converted to String so it may be passed back to the controller.
66  *
67  * <h3>using mapped methods</h3>
68  * An action can be directly mapped to an method inside ActionServlet class.
69  * Method should return an string that represent forwarding. All this is
70  * set in the configuration file. In this use case, there is no more need to
71  * use invokers, since invoking is done automatically by the Controller.
72  * <p>
73  *
74  * So, in this case there is no need to overload and use any of the
75  * <code>ActionServlet</code> methods.
76  *
77  * <h3>using action parameters</h3>
78  * Action can use specific parameters as defined in configuration xml file.
79  * This may be handy, for example, when one ActionServlet is mapped to many
80  * requests and when it should act a bit differently for some groups of
81  * request. Anyhow, parameters may be used in different ways. Moreover, each
82  * action can retrieve action path or some of ActionData values of the
83  * current request.
84  */

85 public abstract class ActionServlet extends HttpServlet {
86
87     public static String parameterActionName = "action";
88     public static String parameterForwardName = "forward";
89     public static String parameterRedirectName = "redirect";
90
91
92     // ---------------------------------------------------------------- controler
93

94     ActionController controller = null;
95
96     /**
97      * Returns action controller that created this ActionServlet object.
98      * It should be used for system usage.
99      *
100      * @return action controller instance
101      */

102     public ActionController getController() {
103         return controller;
104     }
105
106     // ---------------------------------------------------------------- doXXX() methods
107

108     public final static int METHOD_UNKNOWN = 0;
109     public final static int METHOD_GET = 1;
110     public final static int METHOD_POST = 2;
111     
112     private int method = METHOD_UNKNOWN;
113     
114     
115     /**
116      * Returns method (POST/GET) which has been used for invoking the action.
117      *
118      * @return method (POST/GET) which has been used for invoking the action
119      */

120     public final int getMethod() {
121         return method;
122     }
123     
124     /**
125      * Default doGet method, calls doRequest(). Should not be overloaded.
126      *
127      * @param request
128      * @param response
129      *
130      * @exception IOException
131      * @exception ServletException
132      * @see #doRequest
133      */

134     protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
135         method = METHOD_GET;
136         doRequest(request, response);
137     }
138
139     /**
140      * Default doGet method, calls doRequest(). Should not be overloaded.
141      *
142      * @param request
143      * @param response
144      *
145      * @exception IOException
146      * @exception ServletException
147      * @see #doRequest
148      */

149     protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
150         method = METHOD_POST;
151         doRequest(request, response);
152     }
153
154     /**
155      * Main Get/Post handler.
156      *
157      * @param request
158      * @param response
159      *
160      * @exception IOException
161      * @exception ServletException
162      * @see #doAction
163      */

164     public void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
165     }
166
167     /**
168      * When <code>ActionController</code> and MVC2 is used,
169      * <code>doRequest()</code> can not be used as servlet entry point. Instead,
170      * controller will invoke this method. The only difference from the
171      * <code>doRequest()</code> is that this method must return a String that
172      * represents a 'mapping' where to forward/redirect, i.e. forwarding and
173      * redirections are done by controler, not by servlet. These mappings are
174      * defined in 'actions.xml' external file.
175      *
176      * @param request
177      * @param response
178      *
179      * @return mapping string
180      * @exception IOException
181      * @exception ServletException
182      * @see #doRequest
183      */

184     public String doAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
185         return null;
186     }
187
188     // ---------------------------------------------------------------- invokers
189

190     /**
191      * Calls a method defined in the request paramenter. Method name is read from
192      * the request. If method name is null (doesn't exist in the request) nothing
193      * happens.
194      *
195      * @param request
196      * @param response
197      *
198      * @return true if action was invoked, otherwise false
199      * @exception IOException
200      * @exception ServletException
201      */

202     public String invokeAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
203         String actionName = request.getParameter(parameterActionName);
204         return invokeAction(request, response, actionName);
205     }
206
207     /**
208      * Invoke a method from this ActionServlet class. Methods name is given as
209      * parameter. If method name is null nothing happens.
210      *
211      * @param request
212      * @param response
213      * @param actionName name of the method to invoke
214      *
215      * @return null if error, otherwise string mapping
216      * @exception IOException
217      * @exception ServletException
218      */

219     public String invokeAction(HttpServletRequest request, HttpServletResponse response, String actionName) throws IOException, ServletException {
220         if (actionName == null) {
221             return null;
222         }
223         try {
224             Method m = this.getClass().getMethod(actionName, new Class[] {HttpServletRequest.class, HttpServletResponse.class});
225             Object result = m.invoke(this, new Object[] {request, response});
226             return StringUtil.toString(result);
227         } catch (Exception e) {
228         }
229         return null;
230     }
231
232
233     /**
234      * Invokes external action that is defined in other ActionServlet class and
235      * not necessary in current one. It simply delegates call to
236      * <code>ActionController.invokeAction()</code>.
237      *
238      * @param request http request
239      * @param response http response
240      * @param actionPath action path as defined in configuration xml file
241      *
242      * @return external action forward string, or ACTION_NOT_FOUND if not founded
243      * @exception IOException
244      * @exception ServletException
245      * @see ActionController#invokeAction(HttpServletRequest, HttpServletResponse, String)
246      */

247     public String invokeExternalAction(HttpServletRequest request, HttpServletResponse response, String actionPath) throws IOException, ServletException {
248         return controller.invokeAction(request, response, actionPath);
249     }
250
251     // ---------------------------------------------------------------- forward
252

253     /**
254      * Performs forward with use of the RequestDispatcher.
255      *
256      * @param request
257      * @param response
258      * @param url URL where to forward
259      *
260      * @exception IOException
261      * @exception ServletException
262      */

263     protected boolean forward(HttpServletRequest request, HttpServletResponse response, String url) throws IOException, ServletException {
264         RequestDispatcher dispatcher = request.getRequestDispatcher(url);
265         if (dispatcher != null) {
266             dispatcher.forward(request, response);
267             return true;
268         }
269         return false;
270     }
271
272     /**
273      * Performs forward with use of the RequestDispatcher.
274      * URL is read from the request. If URL doesn't exist, nothing happens.
275      *
276      * @param request
277      * @param response
278      *
279      * @return true if parameter found, false otherwise
280      * @exception IOException
281      * @exception ServletException
282      */

283     protected boolean forwardParam(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
284         return forwardParam(request, response, null);
285     }
286
287     /**
288      * Performs forward where URL is read from the request. If URL doesn't exist,
289      * nothing happens.
290      *
291      * @param request http request
292      * @param response http response
293      * @param s forward parameters suffix, added to the default parameter name
294      *
295      * @return <code>true</code> if parameter found and forward was sucessful,
296      * <code>false</code> otherwise
297      * @exception IOException
298      * @exception ServletException
299      * @see #forward
300      */

301     protected boolean forwardParam(HttpServletRequest request, HttpServletResponse response, String s) throws IOException, ServletException {
302         String paramName = parameterForwardName;
303         if ((s != null) && (s.equals("") == false)) {
304             paramName += "-" + s;
305         }
306         String url = request.getParameter(paramName);
307         if (url != null) {
308             return forward(request, response, url);
309         }
310         return false;
311     }
312
313     // ---------------------------------------------------------------- redirect
314

315     /**
316      * Performs redirection.
317      *
318      * @param request
319      * @param response
320      * @param url URL where to redirect
321      *
322      * @exception IOException
323      * @exception ServletException
324      */

325     protected void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
326         if (url.startsWith("/") == true) {
327             url = request.getContextPath() + url;
328         }
329         response.sendRedirect(url);
330     }
331
332     /**
333      * Performs redirection. URL is read from the request. If URL doesn't exist,
334      * nothing happens.
335      *
336      * @param request
337      * @param response
338      *
339      * @return true if parameter found, false otherwise
340      * @exception IOException
341      * @exception ServletException
342      */

343     protected boolean redirectParam(HttpServletRequest request, HttpServletResponse response) throws IOException {
344         return redirectParam(request, response, null);
345     }
346
347     /**
348      * Performs redirection where URL is read from the request. If URL doesn't
349      * exist, nothing happens.
350      *
351      * @param request
352      * @param response
353      * @param s redirect parameters suffix, added to the default parameter name
354      *
355      * @return true if parameter found, false otherwise
356      * @exception IOException
357      * @exception ServletException
358      * @see #redirect
359      */

360     protected boolean redirectParam(HttpServletRequest request, HttpServletResponse response, String s) throws IOException {
361         String paramName = parameterRedirectName;
362         if ((s != null) && (s.equals("") == false)) {
363             paramName += "-" + s;
364         }
365         String url = request.getParameter(paramName);
366         if (url != null) {
367             redirect(request, response, url);
368             return true;
369         }
370         return false;
371     }
372
373
374     // ---------------------------------------------------------------- action data
375

376     /**
377      * Returns action parameter value.
378      *
379      * @param request request
380      * @param name parameter name
381      *
382      * @return action parameter value of <code>null</code> if parameter has not been defined.
383      * @see jodd.servlet.ActionData
384      */

385     public String getActionParameter(HttpServletRequest request, String name) {
386         ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA);
387         return actionData.getParameter(name);
388     }
389
390
391     /**
392      * Returns path of mapped action forward.
393      *
394      * @param request request
395      * @param name forward name
396      *
397      * @return forward path
398      * @see jodd.servlet.ActionData
399      */

400     public String getActionForwardPath(HttpServletRequest request, String name) {
401         ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA);
402         return actionData.getForwardPath(name);
403     }
404
405     /**
406      * Returns mapped action method name.
407      *
408      * @param request request
409      *
410      * @return mapped action mathod name
411      * @see jodd.servlet.ActionData
412      */

413     public String getActionMethodName(HttpServletRequest request) {
414         ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA);
415         return actionData.getMethod();
416     }
417
418
419     /**
420      * Returns mapped action type.
421      *
422      * @param request request
423      *
424      * @return mapped action type
425      * @see jodd.servlet.ActionData
426      */

427     public String getActionType(HttpServletRequest request) {
428         ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA);
429         return actionData.getType();
430     }
431     
432     /**
433      * Returns <code>true</code> if specified forward is actually a redirect.
434      *
435      * @param request request
436      * @param name forward name
437      *
438      * @return <code>true</code> if specified forward is actually a redirect, <code>false</code> otherwise
439      * @see jodd.servlet.ActionData
440      */

441     public boolean isActionForwardRedirect(HttpServletRequest request, String name) {
442         ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA);
443         return actionData.isForwardRedirect(name);
444     }
445
446
447     /**
448      * Returns action path of a current request.
449      *
450      * @param request request
451      *
452      * @return action path
453      * @see jodd.servlet.ActionData
454      */

455     public String getActionPath(HttpServletRequest request) {
456         return (String) request.getAttribute(ActionController.ACTION_PATH);
457     }
458
459
460 }
461
Popular Tags