001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.realm;
019:
020: import java.security.Principal;
021: import java.util.Arrays;
022: import java.util.List;
023: import org.apache.catalina.Realm;
024:
025: /**
026: * Generic implementation of <strong>java.security.Principal</strong> that
027: * is available for use by <code>Realm</code> implementations.
028: *
029: * @author Craig R. McClanahan
030: * @version $Revision: 543691 $ $Date: 2007-06-02 03:37:08 +0200 (sam., 02 juin 2007) $
031: */
032:
033: public class GenericPrincipal implements Principal {
034:
035: // ----------------------------------------------------------- Constructors
036:
037: /**
038: * Construct a new Principal, associated with the specified Realm, for the
039: * specified username and password.
040: *
041: * @param realm The Realm that owns this Principal
042: * @param name The username of the user represented by this Principal
043: * @param password Credentials used to authenticate this user
044: */
045: public GenericPrincipal(Realm realm, String name, String password) {
046:
047: this (realm, name, password, null);
048:
049: }
050:
051: /**
052: * Construct a new Principal, associated with the specified Realm, for the
053: * specified username and password, with the specified role names
054: * (as Strings).
055: *
056: * @param realm The Realm that owns this principal
057: * @param name The username of the user represented by this Principal
058: * @param password Credentials used to authenticate this user
059: * @param roles List of roles (must be Strings) possessed by this user
060: */
061: public GenericPrincipal(Realm realm, String name, String password,
062: List<String> roles) {
063: this (realm, name, password, roles, null);
064: }
065:
066: /**
067: * Construct a new Principal, associated with the specified Realm, for the
068: * specified username and password, with the specified role names
069: * (as Strings).
070: *
071: * @param realm The Realm that owns this principal
072: * @param name The username of the user represented by this Principal
073: * @param password Credentials used to authenticate this user
074: * @param roles List of roles (must be Strings) possessed by this user
075: * @param userPrincipal - the principal to be returned from the request
076: * getUserPrincipal call if not null; if null, this will be returned
077: */
078: public GenericPrincipal(Realm realm, String name, String password,
079: List<String> roles, Principal userPrincipal) {
080:
081: super ();
082: this .realm = realm;
083: this .name = name;
084: this .password = password;
085: this .userPrincipal = userPrincipal;
086: if (roles != null) {
087: this .roles = new String[roles.size()];
088: this .roles = (String[]) roles.toArray(this .roles);
089: if (this .roles.length > 0)
090: Arrays.sort(this .roles);
091: }
092: }
093:
094: // ------------------------------------------------------------- Properties
095:
096: /**
097: * The username of the user represented by this Principal.
098: */
099: protected String name = null;
100:
101: public String getName() {
102: return (this .name);
103: }
104:
105: /**
106: * The authentication credentials for the user represented by
107: * this Principal.
108: */
109: protected String password = null;
110:
111: public String getPassword() {
112: return (this .password);
113: }
114:
115: /**
116: * The Realm with which this Principal is associated.
117: */
118: protected Realm realm = null;
119:
120: public Realm getRealm() {
121: return (this .realm);
122: }
123:
124: void setRealm(Realm realm) {
125: this .realm = realm;
126: }
127:
128: /**
129: * The set of roles associated with this user.
130: */
131: protected String roles[] = new String[0];
132:
133: public String[] getRoles() {
134: return (this .roles);
135: }
136:
137: /**
138: * The authenticated Principal to be exposed to applications.
139: */
140: protected Principal userPrincipal = null;
141:
142: public Principal getUserPrincipal() {
143: if (userPrincipal != null) {
144: return userPrincipal;
145: } else {
146: return this ;
147: }
148: }
149:
150: // --------------------------------------------------------- Public Methods
151:
152: /**
153: * Does the user represented by this Principal possess the specified role?
154: *
155: * @param role Role to be tested
156: */
157: public boolean hasRole(String role) {
158:
159: if ("*".equals(role)) // Special 2.4 role meaning everyone
160: return true;
161: if (role == null)
162: return (false);
163: return (Arrays.binarySearch(roles, role) >= 0);
164:
165: }
166:
167: /**
168: * Return a String representation of this object, which exposes only
169: * information that should be public.
170: */
171: public String toString() {
172:
173: StringBuffer sb = new StringBuffer("GenericPrincipal[");
174: sb.append(this .name);
175: sb.append("(");
176: for (int i = 0; i < roles.length; i++) {
177: sb.append(roles[i]).append(",");
178: }
179: sb.append(")]");
180: return (sb.toString());
181:
182: }
183:
184: }
|