KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > users > UserForm


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.webapp.admin.users;
18
19
20 import java.net.URLDecoder JavaDoc;
21 import javax.management.MBeanServer JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import org.apache.struts.action.ActionError;
24 import org.apache.struts.action.ActionErrors;
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.webapp.admin.ApplicationServlet;
28 import org.apache.webapp.admin.TomcatTreeBuilder;
29
30 /**
31  * Form bean for the individual user page.
32  *
33  * @author Craig R. McClanahan
34  * @version $Revision: 1.5 $ $Date: 2004/10/18 06:37:55 $
35  * @since 4.1
36  */

37
38 public final class UserForm extends BaseForm {
39
40
41     // ----------------------------------------------------- Instance Variables
42

43    /**
44      * The MBeanServer we will be interacting with.
45      */

46     private MBeanServer JavaDoc mserver = null;
47
48     // ------------------------------------------------------------- Properties
49

50     /**
51      * The full name of the associated user.
52      */

53     private String JavaDoc fullName = null;
54
55     public String JavaDoc getFullName() {
56         return (this.fullName);
57     }
58
59     public void setFullName(String JavaDoc fullName) {
60         this.fullName = fullName;
61     }
62
63
64     /**
65      * The MBean Names of the groups associated with this user.
66      */

67     private String JavaDoc groups[] = new String JavaDoc[0];
68
69     public String JavaDoc[] getGroups() {
70         return (this.groups);
71     }
72
73     public void setGroups(String JavaDoc groups[]) {
74         if (groups == null) {
75             groups = new String JavaDoc[0];
76         }
77         this.groups = groups;
78     }
79
80
81     /**
82      * The password of the associated user.
83      */

84     private String JavaDoc password = null;
85
86     public String JavaDoc getPassword() {
87         return (this.password);
88     }
89
90     public void setPassword(String JavaDoc password) {
91         this.password = password;
92     }
93
94
95     /**
96      * The MBean Names of the roles associated with this user.
97      */

98     private String JavaDoc roles[] = new String JavaDoc[0];
99
100     public String JavaDoc[] getRoles() {
101         return (this.roles);
102     }
103
104     public void setRoles(String JavaDoc roles[]) {
105         if (roles == null) {
106             roles = new String JavaDoc[0];
107         }
108         this.roles = roles;
109     }
110
111
112     /**
113      * The username of the associated user.
114      */

115     private String JavaDoc username = null;
116
117     public String JavaDoc getUsername() {
118         return (this.username);
119     }
120
121     public void setUsername(String JavaDoc username) {
122         this.username = username;
123     }
124
125
126     // --------------------------------------------------------- Public Methods
127

128     /**
129      * Reset all properties to their default values.
130      *
131      * @param mapping The mapping used to select this instance
132      * @param request The servlet request we are processing
133      */

134     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
135
136         super.reset(mapping, request);
137         fullName = null;
138         groups = new String JavaDoc[0];
139         password = null;
140         roles = new String JavaDoc[0];
141         username = null;
142
143     }
144
145
146     /**
147      * Validate the properties that have been set from this HTTP request,
148      * and return an <code>ActionErrors</code> object that encapsulates any
149      * validation errors that have been found. If no errors are found, return
150      * <code>null</code> or an <code>ActionErrors</code> object with no
151      * recorded error messages.
152      *
153      * @param mapping The mapping used to select this instance
154      * @param request The servlet request we are processing
155      */

156     public ActionErrors validate(ActionMapping mapping,
157     HttpServletRequest JavaDoc request) {
158         
159         try {
160             // Look up the components we will be using as needed
161
if (mserver == null) {
162                 mserver = ((ApplicationServlet) getServlet()).getServer();
163             }
164          
165             // Set up beans containing all possible groups and roles
166
String JavaDoc databaseName =
167                 URLDecoder.decode(request.getParameter("databaseName"),TomcatTreeBuilder.URL_ENCODING);
168             request.setAttribute("groupsForm",
169                                  UserUtils.getGroupsForm(mserver,
170                                                          databaseName));
171             request.setAttribute("rolesForm",
172                                  UserUtils.getRolesForm(mserver,
173                                                         databaseName));
174         } catch (Exception JavaDoc e) {
175             // do nothing since the form returns validation error
176
}
177         
178         ActionErrors errors = new ActionErrors();
179
180         String JavaDoc submit = request.getParameter("submit");
181         //if (submit != null) {
182

183             // username is a required field
184
if ((username == null) || (username.length() < 1)) {
185                 errors.add("username",
186                            new ActionError("users.error.username.required"));
187             }
188
189             // uassword is a required field
190
if ((password == null) || (username.length() < 1)) {
191                 errors.add("password",
192                            new ActionError("users.error.password.required"));
193             }
194
195             // Quotes not allowed in username
196
if ((username != null) && (username.indexOf('"') >= 0)) {
197                 errors.add("username",
198                            new ActionError("users.error.quotes"));
199             }
200
201             // Quotes not allowed in password
202
if ((password != null) && (password.indexOf('"') > 0)) {
203                 errors.add("description",
204                            new ActionError("users.error.quotes"));
205             }
206
207             // Quotes not allowed in fullName
208
if ((fullName != null) && (fullName.indexOf('"') > 0)) {
209                 errors.add("fullName",
210                            new ActionError("users.error.quotes"));
211             }
212
213         //}
214

215         return (errors);
216
217     }
218
219
220 }
221
Popular Tags