KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > realm > GenericPrincipal


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

17
18
19 package org.apache.catalina.realm;
20
21
22 import java.security.Principal JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.List JavaDoc;
25 import org.apache.catalina.Realm;
26
27
28 /**
29  * Generic implementation of <strong>java.security.Principal</strong> that
30  * is available for use by <code>Realm</code> implementations.
31  *
32  * @author Craig R. McClanahan
33  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
34  */

35
36 public class GenericPrincipal implements Principal JavaDoc {
37
38
39     // ----------------------------------------------------------- Constructors
40

41
42     /**
43      * Construct a new Principal, associated with the specified Realm, for the
44      * specified username and password.
45      *
46      * @param realm The Realm that owns this Principal
47      * @param name The username of the user represented by this Principal
48      * @param password Credentials used to authenticate this user
49      */

50     public GenericPrincipal(Realm realm, String JavaDoc name, String JavaDoc password) {
51
52         this(realm, name, password, null);
53
54     }
55
56
57     /**
58      * Construct a new Principal, associated with the specified Realm, for the
59      * specified username and password, with the specified role names
60      * (as Strings).
61      *
62      * @param realm The Realm that owns this principal
63      * @param name The username of the user represented by this Principal
64      * @param password Credentials used to authenticate this user
65      * @param roles List of roles (must be Strings) possessed by this user
66      */

67     public GenericPrincipal(Realm realm, String JavaDoc name, String JavaDoc password,
68                             List JavaDoc roles) {
69         this(realm, name, password, roles, null);
70     }
71
72     /**
73      * Construct a new Principal, associated with the specified Realm, for the
74      * specified username and password, with the specified role names
75      * (as Strings).
76      *
77      * @param realm The Realm that owns this principal
78      * @param name The username of the user represented by this Principal
79      * @param password Credentials used to authenticate this user
80      * @param roles List of roles (must be Strings) possessed by this user
81      * @param userPrincipal - the principal to be returned from the request
82      * getUserPrincipal call if not null; if null, this will be returned
83      */

84     public GenericPrincipal(Realm realm, String JavaDoc name, String JavaDoc password,
85                             List JavaDoc roles, Principal JavaDoc userPrincipal) {
86
87         super();
88         this.realm = realm;
89         this.name = name;
90         this.password = password;
91         this.userPrincipal = userPrincipal;
92         if (roles != null) {
93             this.roles = new String JavaDoc[roles.size()];
94             this.roles = (String JavaDoc[]) roles.toArray(this.roles);
95             if (this.roles.length > 0)
96                 Arrays.sort(this.roles);
97         }
98     }
99
100
101     // ------------------------------------------------------------- Properties
102

103
104     /**
105      * The username of the user represented by this Principal.
106      */

107     protected String JavaDoc name = null;
108
109     public String JavaDoc getName() {
110         return (this.name);
111     }
112
113
114     /**
115      * The authentication credentials for the user represented by
116      * this Principal.
117      */

118     protected String JavaDoc password = null;
119
120     public String JavaDoc getPassword() {
121         return (this.password);
122     }
123
124
125     /**
126      * The Realm with which this Principal is associated.
127      */

128     protected Realm realm = null;
129
130     public Realm getRealm() {
131         return (this.realm);
132     }
133
134     void setRealm( Realm realm ) {
135         this.realm=realm;
136     }
137
138
139     /**
140      * The set of roles associated with this user.
141      */

142     protected String JavaDoc roles[] = new String JavaDoc[0];
143
144     public String JavaDoc[] getRoles() {
145         return (this.roles);
146     }
147
148
149     /**
150      * The authenticated Principal to be exposed to applications.
151      */

152     protected Principal JavaDoc userPrincipal = null;
153
154     public Principal JavaDoc getUserPrincipal() {
155         if (userPrincipal != null) {
156             return userPrincipal;
157         } else {
158             return this;
159         }
160     }
161
162
163     // --------------------------------------------------------- Public Methods
164

165
166     /**
167      * Does the user represented by this Principal possess the specified role?
168      *
169      * @param role Role to be tested
170      */

171     public boolean hasRole(String JavaDoc role) {
172
173         if("*".equals(role)) // Special 2.4 role meaning everyone
174
return true;
175         if (role == null)
176             return (false);
177         return (Arrays.binarySearch(roles, role) >= 0);
178
179     }
180
181
182     /**
183      * Return a String representation of this object, which exposes only
184      * information that should be public.
185      */

186     public String JavaDoc toString() {
187
188         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("GenericPrincipal[");
189         sb.append(this.name);
190         sb.append("(");
191         for( int i=0;i<roles.length; i++ ) {
192             sb.append( roles[i]).append(",");
193         }
194         sb.append(")]");
195         return (sb.toString());
196
197     }
198
199
200 }
201
Popular Tags