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