1 /***
2 * The contents of this file are subject to the Mozilla Public License Version
3 * 1.1 (the "License"); you may not use this file except in compliance with the
4 * License.
5 *
6 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7 *
8 * Software distributed under the License is distributed on an "AS IS" basis,
9 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
10 * See the License for the specific language governing rights and limitations
11 * under the License.
12 *
13 * The Original Code is pow2Toolkit library.
14 * The Initial Owner of the Original Code is Power Of Two S.R.L.
15 * Portions created by Power Of Two S.R.L. are Copyright
16 * (C) Power Of Two S.R.L. All Rights Reserved.
17 *
18 * Contributor(s):
19 */
20
21 package com.pow2.struts.action;
22
23
24 import java.io.IOException;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29
30 import org.apache.log4j.Category;
31
32 import org.apache.struts.action.*;
33
34 import com.pow2.struts.action.ToolkitActionMapping;
35
36
37 /***
38 * Ask the specified Action instance to handle this request.
39 * <br>
40 * If the input action subclasses <code>BaseAction</code>, execute
41 * its <code>validateSession</code> method.
42 * <br>
43 * If that method returns <code>false</code>, redirect to the resource
44 * specified by the <code>invalidSessionForward</code> method.
45 * <br>
46 * Else execute the action's <code>perform</code> method.
47 *
48 * @author Luca Fossato
49 */
50 public class ActionServlet extends org.apache.struts.action.ActionServlet
51 {
52 /*** Log4j category; */
53 private Category cat = Category.getInstance(this.getClass());
54
55
56 /***
57 * Initialize this servlet.
58 *
59 * @exception ServletException if we cannot configure ourselves correctly
60 */
61 public void init() throws ServletException
62 {
63 super.init();
64 }
65
66
67 /***
68 * Ask the specified Action instance to handle this request.
69 * <br>
70 * If the input action subclasses <code>BaseAction</code>, execute
71 * the provided <code>validateSession</code> method.
72 * <br>
73 * If that method returns <code>false</code>, redirect to the resource
74 * specified by the <code>invalidSessionForward</code> method.
75 * <br>
76 * Else execute the action's <code>perform</code> method.
77 *
78 * @param action The Action to process this request
79 * @param mapping The ActionMapping we are processing
80 * @param formInstance The ActionForm we are processing
81 * @param request The servlet request we are processing
82 * @param response The servlet response we are creating
83 * @return Description of the Return Value
84 * @exception IOException if an input/output error occurs
85 * @exception ServletException if a servlet exception occurs
86 */
87 protected ActionForward processActionPerform(Action action,
88 ActionMapping mapping,
89 ActionForm formInstance,
90 HttpServletRequest request,
91 HttpServletResponse response)
92 throws ServletException
93 {
94
95 if (action instanceof BaseAction)
96 {
97 ActionErrors errors = new ActionErrors();
98 BaseAction baseAction = (BaseAction)action;
99 ToolkitActionMapping tMapping = (ToolkitActionMapping)mapping;
100
101
102
103 if (tMapping.isValidateSession() && !baseAction.validateSession(request))
104 {
105 cat.warn("::processActionPerform - session validation fails; return the invalidSession forward");
106 ActionForward fwd = baseAction.invalidSessionForward(request, mapping, new ActionErrors());
107
108 if (fwd == null)
109 throw new ServletException("cannot retrieve the invalid session forward [" + BaseAction.FWD_INVALID_SESSION + "]");
110
111 return fwd;
112 }
113 }
114
115 ActionForward fwd = null;
116
117 try
118 {
119 fwd = action.execute(mapping, formInstance, request, response);
120 }
121 catch (Exception e)
122 {
123 cat.error("error executing the action " + action.getClass(), e);
124 }
125
126 return fwd;
127 }
128 }