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.forms;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.apache.log4j.Category;
26  import org.apache.struts.action.ActionError;
27  import org.apache.struts.action.ActionErrors;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionMapping;
30  
31  import com.pow2.util.Util;
32  
33  /***
34   *  Login form class.
35   *  <br>
36   *  Wrap the <code>login</code> and <code>password</code> values
37   *  retrieved from the login form and Validate these fields
38   *  against null or blank strings.
39   *
40   * @author   Luca Fossato
41   * @created  9 maggio 2002
42   */
43  public class LoginForm extends ActionForm
44  {
45    private Category cat = Category.getInstance(this.getClass());
46    private String   login;
47    private String   passwd;
48  
49    /***
50     *  Default constructor.<br>
51     *  Set <code>login</code> and <code>password</code>
52     *  to blank strings.
53     */
54    public LoginForm()
55    {
56      clear();
57    }
58  
59  
60    /***
61     *  Get the login value.
62     *
63     * @return  The login value
64     */
65    public String getLogin()
66    {
67      return login;
68    }
69  
70  
71    /***
72     *  Get the password value.
73     *
74     * @return  The passwd value
75     */
76    public String getPasswd()
77    {
78      return passwd;
79    }
80  
81  
82    /***
83     *  Set the new login value.
84     *
85     * @param  v The new login value.
86     */
87    public void setLogin(String v)
88    {
89      login = v;
90    }
91  
92  
93    /***
94     *  Set the new password value.
95     *
96     * @param  v The new password value
97     */
98    public void setPasswd(String v)
99    {
100     passwd = v;
101   }
102 
103 
104   /***
105    *  Validate <code>login</code> and <code>password</code> fields.<br>
106    *  These fields cannot be null or empty.
107    *  <br>
108    *  If the validation process finds problems, it returns an ActionErrors
109    *  instance containing ActionError's, which are classes that contain
110    *  the error message keys (into the application's MessageResources bundle)
111    *  that should be displayed.
112    *  <br>
113    *  The controller servlet will store this array as a request attribute
114    *  suitable for use by the <html:errors> tag, and will forward control back
115    *  to the input form (identified by the <b>input property</b>
116    *  for this ActionMapping).
117    *
118    * @param  mapping the ActionMapping object.
119    * @param  request the HttpServletRequest object.
120    * @return  a collection of errors.
121    */
122   public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
123   {
124     ActionErrors errors = new ActionErrors();
125 
126     if (Util.isNull(login))
127     {
128       errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.username.invalid"));
129       cat.warn("::validate - username is empty.");
130     }
131 
132     if (Util.isNull(passwd))
133     {
134       errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.passwd.invalid"));
135       cat.warn("::validate - password is empty.");
136     }
137 
138     return (errors.isEmpty()) ? null : errors;
139   }
140 
141 
142   /***
143    *  Clear the form <code>login</code> and <code>passwd</code> attributes
144    */
145   public void clear()
146   {
147     login  = "";
148     passwd = "";
149   }
150 }
151