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.cluster.session;
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: * The GenericPrincipal does NOT implement serializable and I didn't want to change that implementation
028: * hence I implemented this one instead.
029: * @author Filip Hanik
030: * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:56 $
031: */
032: import org.apache.catalina.realm.GenericPrincipal;
033:
034: public class SerializablePrincipal implements java.io.Serializable {
035:
036: // ----------------------------------------------------------- Constructors
037:
038: public SerializablePrincipal() {
039: super ();
040: }
041:
042: /**
043: * Construct a new Principal, associated with the specified Realm, for the
044: * specified username and password.
045: *
046: * @param realm The Realm that owns this Principal
047: * @param name The username of the user represented by this Principal
048: * @param password Credentials used to authenticate this user
049: */
050: public SerializablePrincipal(Realm realm, String name,
051: String password) {
052:
053: this (realm, name, password, null);
054:
055: }
056:
057: /**
058: * Construct a new Principal, associated with the specified Realm, for the
059: * specified username and password, with the specified role names
060: * (as Strings).
061: *
062: * @param realm The Realm that owns this principal
063: * @param name The username of the user represented by this Principal
064: * @param password Credentials used to authenticate this user
065: * @param roles List of roles (must be Strings) possessed by this user
066: */
067: public SerializablePrincipal(Realm realm, String name,
068: String password, List roles) {
069:
070: super ();
071: this .realm = realm;
072: this .name = name;
073: this .password = password;
074: if (roles != null) {
075: this .roles = new String[roles.size()];
076: this .roles = (String[]) roles.toArray(this .roles);
077: if (this .roles.length > 0)
078: Arrays.sort(this .roles);
079: }
080:
081: }
082:
083: // ------------------------------------------------------------- Properties
084:
085: /**
086: * The username of the user represented by this Principal.
087: */
088: protected String name = null;
089:
090: public String getName() {
091: return (this .name);
092: }
093:
094: /**
095: * The authentication credentials for the user represented by
096: * this Principal.
097: */
098: protected String password = null;
099:
100: public String getPassword() {
101: return (this .password);
102: }
103:
104: /**
105: * The Realm with which this Principal is associated.
106: */
107: protected transient Realm realm = null;
108:
109: public Realm getRealm() {
110: return (this .realm);
111: }
112:
113: public void setRealm(Realm realm) {
114: this .realm = realm;
115: }
116:
117: /**
118: * The set of roles associated with this user.
119: */
120: protected String roles[] = new String[0];
121:
122: public String[] getRoles() {
123: return (this .roles);
124: }
125:
126: // --------------------------------------------------------- Public Methods
127:
128: /**
129: * Return a String representation of this object, which exposes only
130: * information that should be public.
131: */
132: public String toString() {
133:
134: StringBuffer sb = new StringBuffer("SerializablePrincipal[");
135: sb.append(this .name);
136: sb.append("]");
137: return (sb.toString());
138:
139: }
140:
141: public static SerializablePrincipal createPrincipal(
142: GenericPrincipal principal) {
143: if (principal == null)
144: return null;
145: return new SerializablePrincipal(principal.getRealm(),
146: principal.getName(), principal.getPassword(), principal
147: .getRoles() != null ? Arrays.asList(principal
148: .getRoles()) : null);
149: }
150:
151: public GenericPrincipal getPrincipal(Realm realm) {
152: return new GenericPrincipal(realm, name, password,
153: getRoles() != null ? Arrays.asList(getRoles()) : null);
154: }
155:
156: public static GenericPrincipal readPrincipal(
157: java.io.ObjectInputStream in, Realm realm)
158: throws java.io.IOException {
159: String name = in.readUTF();
160: String pwd = in.readUTF();
161: int size = in.readInt();
162: String[] roles = new String[size];
163: for (int i = 0; i < size; i++)
164: roles[i] = in.readUTF();
165: return new GenericPrincipal(realm, name, pwd, Arrays
166: .asList(roles));
167: }
168:
169: public static void writePrincipal(GenericPrincipal p,
170: java.io.ObjectOutputStream out) throws java.io.IOException {
171: out.writeUTF(p.getName());
172: out.writeUTF(p.getPassword());
173: String[] roles = p.getRoles();
174: if (roles == null)
175: roles = new String[0];
176: out.writeInt(roles.length);
177: for (int i = 0; i < roles.length; i++)
178: out.writeUTF(roles[i]);
179: }
180:
181: }
|