KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > security > Sync4jPrincipal


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.security;
20
21 import java.security.Principal JavaDoc;
22
23 import sync4j.framework.core.Cred;
24 import sync4j.framework.tools.Base64;
25
26 /**
27  * This class implements the <i>Pricipal</i> interface and represents a Sync4j
28  * principal. In Sync4j a principal is represented by a couple
29  * <i>(username, device id)</i> and is associated to a unique id.
30  *
31  * @author Stefano Fornari @ Funambol.com
32  *
33  * @version $Id: Sync4jPrincipal.java,v 1.18 2005/03/02 20:57:38 harrie Exp $
34  */

35 public class Sync4jPrincipal implements Principal JavaDoc, java.io.Serializable JavaDoc {
36     // --------------------------------------------------------------- Constants
37

38     // ------------------------------------------------------------ Private data
39

40     private String JavaDoc id = null; // principalid
41
private String JavaDoc username = null;
42     private String JavaDoc deviceId = null;
43
44     private String JavaDoc encodedUserPwd = null;
45
46     // ------------------------------------------------------------ Constructors
47
/**
48      * This should not be used. It is here for serialization purposes only.
49      */

50     public Sync4jPrincipal() {};
51
52     /**
53      * Creates a new instance of UsernamePrincipal
54      *
55      * @param id principal id
56      * @param username the username element of the principal
57      * @param deviceId the deviceId element of the principal
58      */

59     public Sync4jPrincipal(final String JavaDoc id ,
60                            final String JavaDoc username,
61                            final String JavaDoc deviceId) {
62         this.id = id ;
63         this.username = username;
64         this.deviceId = deviceId;
65     }
66
67     /**
68      * Creates a new instance of UsernamePrincipal
69      *
70      * @param username the username element of the principal
71      * @param deviceId the deviceId element of the principal
72      *
73      */

74     public Sync4jPrincipal(final String JavaDoc username, final String JavaDoc deviceId) {
75         this(null, username, deviceId);
76     }
77
78
79     /**
80      * Creates a new instance of UsernamePrincipal
81      *
82      * @param id principal id
83      */

84     public Sync4jPrincipal(final String JavaDoc id) {
85         this(id, null, null);
86     }
87
88     /**
89      * Creates a new instance of CredentialPrinciple from the given Credential
90      *
91      * @param userpwd user and password in the form <user>:<password>
92      * @param type the username coding as expressed in the a Credential object
93      * @param deviceId the deviceId element of the principal. Can be NULL
94      *
95      * @throws IllegalArgumentException
96      */

97     public static Sync4jPrincipal fromCredential( String JavaDoc userpwd ,
98                                                   String JavaDoc type ,
99                                                  String JavaDoc deviceId)
100     throws IllegalArgumentException JavaDoc {
101
102         Sync4jPrincipal ret = new Sync4jPrincipal();
103
104         if (Cred.AUTH_SUPPORTED_TYPES.indexOf(type) < 0) {
105             throw new IllegalArgumentException JavaDoc( "Authorization type '"
106                                               + type
107                                               + "' not supported"
108                                               );
109         }
110
111         if (Cred.AUTH_TYPE_BASIC.equals(type)) {
112             String JavaDoc s = new String JavaDoc(Base64.decode(userpwd.getBytes()));
113
114             int p = s.indexOf(':');
115
116             if (p == -1) {
117                 ret.setUsername(s);
118             } else {
119                 ret.setUsername((p>0) ? s.substring(0, p) : "");
120             }
121             ret.setEncodedUserPwd(userpwd);
122         } else {
123             ret.setUsername(userpwd);
124         }
125
126         ret.setId(null);
127         ret.setDeviceId(deviceId);
128
129         return ret;
130     }
131
132     // ---------------------------------------------------------- Public methods
133

134     /**
135      * The principal name that is concatenation of the deviceId, '/' and
136      * username or only the username if the deviceId is not defined.
137      *
138      * @return the principal's name
139      */

140     public String JavaDoc getName() {
141
142         return (deviceId == null)
143              ? username
144              : (deviceId + '/' + username);
145     }
146
147     /**
148      * The username part of the principal name.
149      *
150      * @return the username part of the principal name.
151      */

152     public String JavaDoc getUsername() {
153         return username;
154     }
155
156     /**
157      * Set the username part of the principal name.
158      *
159      * @param username the username principal's username
160      */

161     public void setUsername(String JavaDoc username) {
162         this.username = username;
163     }
164
165     /**
166      * The device id part of the principal name.
167      *
168      * @return the device id part of the principal name.
169      */

170     public String JavaDoc getDeviceId() {
171         return deviceId;
172     }
173
174     /**
175      * Set the device id part of the principal name.
176      *
177      * @param deviceId the device id principal's username
178      */

179     public void setDeviceId(String JavaDoc deviceId) {
180         this.deviceId = deviceId;
181     }
182
183     /**
184      * The principal unique id.
185      *
186      * @return the unique principal id.
187      */

188     public String JavaDoc getId() {
189         return id;
190     }
191
192     /**
193      * Set the principal unique id.
194      *
195      * @param id the principal unique id.
196      */

197     public void setId(String JavaDoc id) {
198         this.id = id;
199     }
200
201     /**
202      * Two Sync4jPrincipals are equals if and only if:
203      * <ul>
204      * <li>o is null or
205      * <li>o is not an instance of Sync4jPrincipal or
206      * <li>the two getName()s do not match
207      * </ul>
208      *
209      * @param o the reference object with which to compare.
210      *
211      * @return true if this object is the same as the obj argument; false otherwise.
212      */

213     public boolean equals(Object JavaDoc o) {
214         if ((o == null) || !(o instanceof Sync4jPrincipal)) {
215             return false;
216         }
217
218         return (getName().equals(((Sync4jPrincipal)o).getName()));
219     }
220
221     public int hashCode() {
222         return getName().hashCode();
223     }
224
225     public String JavaDoc toString() {
226         return getName();
227     }
228
229     /**
230      * @return encodedUserPwd user and password in the form <user>:<password>
231      */

232     public String JavaDoc getEncodedCredentials() {
233         return encodedUserPwd;
234     }
235
236     /**
237      * Set the encodedUserPwd
238      *
239      * @param newEncodedUserPwd the encodedUserPwd.
240      */

241     public void setEncodedUserPwd(String JavaDoc newEncodedUserPwd) {
242         this.encodedUserPwd = newEncodedUserPwd;
243     }
244 }
245
Popular Tags