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