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 pow2ACL 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  
21  package com.pow2.struts.actions;
22  
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.struts.action.ActionErrors;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  
32  import com.pow2.struts.action.DispatcherAction;
33  import com.pow2.struts.forms.LoginForm;
34  import com.pow2.user.User;
35  import com.pow2.user.UserManager;
36  import com.pow2.util.Util;
37  
38  
39  /***
40   *  Login Action class.
41   *  <br>
42   *  Try to retrieve an authenticated user from the database,
43   *  then set that user object into the the Session context
44   *  and redirect to the login resource.
45   *  <br>
46   *  The default forward name for a succesfull login is "success";
47   *  you can specify another forward name using:
48   *  <ul>
49   *    <li>
50   *      the <code>parameter</code> attribute of the action element
51   *      of the struts configuration file
52   *    </li>
53   *    <li>
54   *      the <code>fwd</code> request attribute; the <code>fwd</code>
55   *      attribute value specifies the forward name of the ActionForward object to
56   *      retrieve.
57   *      <br>
58   *      Action URL Example: <code>logout.do?fwd=indexPage</code>
59   *    </li>
60   *  </ul>
61   *
62   *  The default failure forward name is specified by
63   *  <code>BaseAction.FWD_FAILURE</code>;
64   *  you can specify a different failure forward name using the
65   *  <code>failure</code> request parameter.
66   *
67   * @author     Luca Fossato
68   * @created    22 aprile 2002
69   */
70  public class LoginAction extends DispatcherAction   //extends BaseAction
71  {
72    /***
73     *  Retrieve the User object from the database using the
74     *  submitted <code>login</code> and <code>passwd</code>
75     *  ActionForm properties.
76     *  <br>
77     *  If login is successful, save the retrieved <code>User</code> object
78     *  reference into the Session, and redirect to the
79     *  specified resource.
80     *  <br>
81     *  Else remove any (previous) user reference from the Session context
82     *  and redirect to the failureForward resource.
83     *  <br>
84     *  <br>Note:</br>
85     *  this action must be used with the
86     *  <code>com.pow2.struts.forms.LoginForm</code> class !!
87     *
88     * @param  mapping   Description of the Parameter
89     * @param  form      Description of the Parameter
90     * @param  request   Description of the Parameter
91     * @param  response  Description of the Parameter
92     * @return           Description of the Return Value
93     */
94    public ActionForward execute(ActionMapping       mapping,
95                                 ActionForm          form,
96                                 HttpServletRequest  request,
97                                 HttpServletResponse response)
98      throws
99        java.io.IOException,
100       javax.servlet.ServletException
101   {
102     ActionErrors  errors      = new ActionErrors();
103     LoginForm     f           = (LoginForm) form;
104     ActionForward fwd         = null;
105     UserManager   userManager = UserManager.instance();
106     User          user        = null;
107 
108     // retrieve or create a new User object and try to
109     // authenticate it versus the user database;
110     try
111     {
112       user = userManager.getUser(request, true);
113     }
114     catch(Exception e)
115     {
116       userManager.deleteUser(request);
117       cat.warn("::perform - cannot retrieve the user object from the database", e);
118       return failureForward(request, mapping, errors, "error.login.failed");
119     }
120 
121     // clean the login form attributes;
122     if (f != null)
123       f.clear();
124 
125     // is the user login successful ?
126     // (if not), the default failure forward name is specified by
127     // BaseAction.FWD_FAILURE;
128     // you can specify a different failure forward name using the
129     // "failure" request parameter.
130     if (!user.isAuthenticated())
131     {
132       String failure = null;
133 
134       return (!Util.isNull(failure = request.getParameter("failure"))) ?
135         getForward(request, mapping, errors, failure,  "error.login.failed") :
136         failureForward(request, mapping, errors, "error.login.failed");
137     }
138 
139     // a) set the retrieved user into the session context
140     // b) get the actionForward object retrieved by the dispatcher (if any)
141     // c) return the appropriate ActionForward object
142     userManager.setUser(request, user);
143     fwd = super.execute(mapping, form, request, response);
144     if (fwd == null)
145       fwd =  mapping.findForward("success");
146 
147     // some info about the returned forward;
148     if (fwd != null)
149       cat.debug("::perform - login is successfull; forwarding to path [" + fwd.getPath() + "]");
150 
151     return fwd;
152   }
153 }