View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2Toolkit library.
13   *
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 (C) Power Of Two S.R.L.
16   *  All Rights Reserved.
17   *
18   * Contributor(s):
19   */
20  package com.pow2.struts.action;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  
25  import org.apache.log4j.Category;
26  import org.apache.struts.action.*;
27  
28  import com.pow2.util.Util;
29  
30  
31  /***
32   *  Pow2 Struts base action.
33   *  <br>
34   *  This class adds base utility methods to other Action subclasses.
35   *  <br>
36   *  It also provides the <code>validateSession</code> hook method;
37   *  the default implementation always returns <code>true</code>.
38   *  <br>
39   *  To implement your own session validation, extend this class and
40   *  override the  the <code>validateSession</code> method.
41   *  <br>
42   *  if <code>validateSession</code> returns <code>false</code>, this action
43   *  forwards to the resource specified by the "invalidSession" ActionForward
44   *  object.
45   *
46   * @author     Luca Fossato
47   */
48  public class BaseAction extends Action
49  {
50    /*** ActionForward cancel string */
51    protected final static String FWD_CANCEL = "cancel";
52  
53    /*** ActionForward failure string */
54    protected final static String FWD_FAILURE = "failure";
55  
56    /*** ActionForward failure string */
57    protected final static String FWD_LOGIN = "login";
58  
59      /***  ActionForward invalid session string */
60    protected final static String FWD_INVALID_SESSION = "invalidSession";
61  
62    /***  Resource properties message key for invalidSession forwards */
63    private final static String KEY_INVALID_SESSION = "action.session.invalid";
64  
65    /*** Log4j category */
66    protected Category cat = Category.getInstance(this.getClass());
67  
68  
69    /***
70     *  Get the <code>FWD_LOGIN</code> ActionForward object.
71     *
72     * @param  request   the HttpServletRequest object
73     * @param  mapping   the ActionMapping object
74     * @return           Description of the Return Value
75     */
76    protected ActionForward loginForward(HttpServletRequest request,
77                                         ActionMapping      mapping)
78    {
79      return getForward(request, mapping, null, FWD_LOGIN, null);
80    }
81  
82  
83    /***
84     *  Get the <code>FWD_LOGIN</code> ActionForward object, and store the
85     *  error message identified by the errorKey string into the input
86     *  ActionErrors object.
87     *
88     * @param  request   the HttpServletRequest object
89     * @param  mapping   the ActionMapping object
90     * @param  errors    the ActionErrors object
91     * @param  errorKey  a string key identifying a resource properties error string
92     * @return           Description of the Return Value
93     */
94    protected ActionForward loginForward(HttpServletRequest request,
95                                         ActionMapping mapping,
96                                         ActionErrors errors,
97                                         String errorKey)
98    {
99      return getForward(request, mapping, errors, FWD_LOGIN, errorKey);
100   }
101 
102 
103   /***
104    *  Get the <code>FWD_FAILURE</code> ActionForward object, and store the
105    *  error message identified by the errorKey string into the input
106    *  ActionErrors object.
107    *
108    * @param  request   the HttpServletRequest object
109    * @param  mapping   the ActionMapping object
110    * @param  errors    the ActionErrors object
111    * @param  errorKey  a string key identifying a resource properties error string
112    * @return           Description of the Return Value
113    */
114   protected ActionForward failureForward(HttpServletRequest request,
115                                          ActionMapping      mapping,
116                                          ActionErrors       errors,
117                                          String             errorKey)
118   {
119     cat.debug("::failureForward - using errorKey [" + errorKey + "]");
120     return getForward(request, mapping, errors, FWD_FAILURE, errorKey);
121   }
122 
123 
124   /***
125    *  Get the <code>FWD_INVALID_SESSION</code> ActionForward object, and store
126    *  the error message identified by the <code>KEY_INVALID_SESSION</code> key
127    *  into the input ActionErrors object.
128    *
129    * @param  request  the HttpServletRequest object
130    * @param  mapping  the ActionMapping object
131    * @param  errors   the ActionErrors objects
132    * @return          Description of the Return Value
133    */
134   protected ActionForward invalidSessionForward(HttpServletRequest request,
135                                                 ActionMapping mapping,
136                                                 ActionErrors errors)
137   {
138     return getForward(request, mapping, errors, FWD_INVALID_SESSION, KEY_INVALID_SESSION);
139   }
140 
141 
142   /***
143    *  Get the ActionForward object related to the input <code>forward</code> string,
144    *  and store the error message identified by the errorKey string into
145    *  the input ActionErrors object.
146    *
147    * @param  request   the HttpServletRequest object
148    * @param  mapping   the ActionMapping object
149    * @param  errors    the ActionErrors object
150    * @param  forward   the name of the action forward object to retrieve
151    * @param  errorKey  a string key identifying a resource properties error string
152    * @return           The forward value
153    */
154   protected ActionForward getForward(HttpServletRequest request,
155                                      ActionMapping      mapping,
156                                      ActionErrors       errors,
157                                      String             forward,
158                                      String             errorKey)
159   {
160     if ((errors != null) && !Util.isNull(errorKey))
161     {
162       errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(errorKey));
163       saveErrors(request, errors);
164     }
165 
166     ActionForward fwd = mapping.findForward(forward);
167 
168     if (fwd == null)
169       cat.error("::getForward - ActionForward object [" + forward + "] is null");
170 
171     return fwd;
172   }
173 
174 
175   /***
176    *  Check if the input ActionForward object is a failure forward
177    *
178    * @param forward the ActionForward object to check
179    */
180   protected boolean isFailureForward(ActionForward forward)
181   {
182     return (FWD_FAILURE.equals(forward.getName()));
183   }
184 
185 
186   /***
187    *  Check if the current session is valid.
188    *  <br>
189    *  This is an hook method that returns always true.
190    *  Make your own action class that extends BaseAction
191    *  and override this method.
192    *
193    * @param  HttpServletRequest the input request object
194    * @return true if the current session is valid; false otherwise.
195    *         This implementation returns true
196    */
197   protected boolean validateSession(HttpServletRequest request)
198   {
199     return true;
200   }
201 }