KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > core > Authentication


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 package sync4j.framework.core;
19
20 import sync4j.framework.tools.Base64;
21
22 /**
23  * This is a base class for "authentication" classes
24  *
25  * @author Stefano Fornari @ Funambol
26  *
27  * @version $Id: Authentication.java,v 1.8 2005/04/26 14:38:28 luigiafassina Exp $
28  */

29 public final class Authentication {
30
31     // ------------------------------------------------------------ Private data
32
private String JavaDoc data = null;
33     private String JavaDoc username = null;
34     private String JavaDoc password = null;
35     private boolean encode = false;
36     private String JavaDoc deviceId = null ;
37     private String JavaDoc syncMLVerProto = null ;
38     private String JavaDoc principalId = null;
39
40     private Meta meta = null;
41
42     // ------------------------------------------------------------ Constructors
43

44     /** For serialization purposes */
45     protected Authentication() {}
46
47     /**
48      * Creates a new Authentication object with the given data
49      *
50      * @param meta the Meta object with authentication type and format
51      * @param data the data of authentication
52      *
53      */

54     public Authentication(final Meta meta, final String JavaDoc data) {
55         this.meta = meta;
56         createAuthentication(meta.getType(),data);
57     }
58
59     /**
60      * Creates a new Authentication object with the given data
61      *
62      * @param type the authentication type
63      * @param data the data of authentication
64      *
65      */

66     public Authentication(final String JavaDoc type, final String JavaDoc data) {
67         createAuthentication(type, data);
68     }
69
70     /**
71      * Creates a new Authentication object with the given data
72      *
73      * @param type the authentication type
74      * @param data the data of authentication
75      * @param encode true if data is encoded, false otherwise
76      *
77      */

78     public Authentication(final String JavaDoc type,
79                           final String JavaDoc data,
80                           final boolean encode) {
81
82         this.encode = encode;
83         createAuthentication(type, data);
84     }
85
86     /**
87      * Creates a new Authentication object with the given data
88      *
89      * @param type the authentication type
90      * @param username the username
91      * @param password the password
92      *
93      */

94     public Authentication(final String JavaDoc type,
95                           final String JavaDoc username,
96                           final String JavaDoc password) {
97         if (username == null || password == null) {
98             throw new IllegalArgumentException JavaDoc(
99             "The authentication username and password cannot be null");
100         }
101
102         encode = true;
103         createAuthentication(type, username + ":" + password);
104     }
105
106     // ---------------------------------------------------------- Public methods
107

108     public void createAuthentication(String JavaDoc type, String JavaDoc data) {
109
110         if (Cred.AUTH_SUPPORTED_TYPES.indexOf(type) < 0) {
111             type = Cred.AUTH_TYPE_BASIC;
112         }
113
114         if (Cred.AUTH_TYPE_BASIC.equals(type)) {
115             this.setType(Cred.AUTH_TYPE_BASIC);
116             this.setFormat(Constants.FORMAT_B64);
117             this.setData(data);
118         } else if (Cred.AUTH_TYPE_MD5.equals(type)) {
119             this.setType(Cred.AUTH_TYPE_MD5);
120             this.setData(data);
121         }
122     }
123
124     /**
125      * Gets the type property
126      *
127      * @return the type property
128      */

129     public String JavaDoc getType() {
130         return (meta == null) ? null : meta.getType();
131     }
132
133     /**
134      * Sets the type property
135      *
136      * @param type the type property
137      */

138     public void setType(String JavaDoc type) {
139         if (meta == null) {
140             meta = new Meta();
141         }
142         meta.setType(type);
143     }
144
145     /**
146      * Gets the format property
147      *
148      * @return the format property
149      */

150     public String JavaDoc getFormat() {
151         return (meta == null) ? null : meta.getFormat();
152     };
153
154     /**
155      * Sets the format property
156      *
157      * @param format the format property
158      */

159     public void setFormat(String JavaDoc format) {
160         if (meta == null) {
161             meta = new Meta();
162         }
163         meta.setFormat(format);
164     };
165
166     /**
167      * Gets the data property
168      *
169      * @return the data property
170      */

171     public final String JavaDoc getData() {
172         return data;
173     }
174
175     /**
176      * Sets the data property
177      *
178      * @param data the data property
179      *
180      */

181     public void setData(String JavaDoc data) {
182         if (data == null) {
183             throw new IllegalArgumentException JavaDoc("data cannot be null");
184         }
185
186         String JavaDoc type = this.getType();
187
188         if (type.equals(Cred.AUTH_TYPE_BASIC)) {
189             String JavaDoc clearData = null;
190
191             if (encode) {
192                 this.data = new String JavaDoc(Base64.encode(data.getBytes()));
193                 clearData = data;
194             } else {
195                 clearData = new String JavaDoc(Base64.decode(data.getBytes()));
196                 this.data = data;
197             }
198
199             int p = clearData.indexOf(':');
200
201             if (p == -1) {
202                 this.setUsername(clearData);
203                 this.setPassword(null);
204             } else {
205                 this.username = (p>0) ? clearData.substring(0, p) : "";
206                 this.password = (p<data.length()) ? clearData.substring(p+1) : "";
207             }
208         }
209
210         if (type.equals(Cred.AUTH_TYPE_MD5)) {
211             if (meta.getFormat() == null) {
212                 this.setFormat(Constants.FORMAT_B64);
213             }
214             this.username = data;
215             this.data = data;
216         }
217     }
218
219
220     /**
221      * Gets username property
222      *
223      * @return the username property
224      */

225     public String JavaDoc getUsername() {
226         return username;
227     }
228
229     /**
230      * Sets the username property
231      *
232      * @param username the username property
233      */

234     public void setUsername(String JavaDoc username) {
235         this.username = username;
236     }
237
238     /**
239      * Gets password property
240      *
241      * @return the password property
242      */

243     public String JavaDoc getPassword() {
244         return password;
245     }
246
247     /**
248      * Sets the password property
249      *
250      * @param password the password property
251      */

252     public void setPassword(String JavaDoc password) {
253         this.password = password;
254     }
255
256     /**
257      * Gets the nextNonce property
258      *
259      * @return nextNonce the nextNonce property
260      */

261     public NextNonce getNextNonce() {
262         return (meta == null) ? null : meta.getNextNonce();
263     }
264
265     /**
266      * Sets the nextNonce property
267      *
268      * @param nextNonce the nextNonce property
269      *
270      */

271     public void setNextNonce(NextNonce nextNonce) {
272         if (meta == null) {
273             meta = new Meta();
274         }
275         meta.setNextNonce(nextNonce);
276     };
277
278     /**
279      * Gets the meta property
280      *
281      * @return meta the meta property
282      */

283     public Meta getMeta() {
284         return meta;
285     }
286
287     /**
288      * Sets the meta property
289      *
290      * @param meta the meta property
291      *
292      */

293     public void setMeta(Meta meta) {
294         this.meta = meta;
295     };
296
297     /**
298      * Gets the device id
299      *
300      * @return deviceId the device identificator
301      */

302     public String JavaDoc getDeviceId() {
303         return this.deviceId;
304     }
305
306     /**
307      * Sets the device identificator
308      *
309      * @param deviceId the device identificator
310      */

311     public void setDeviceId(String JavaDoc deviceId) {
312         this.deviceId = deviceId;
313     }
314
315     /**
316      * Gets the SyncML Protocol version. It is useful to decide how calculate
317      * the digest with MD5 authentication.
318      *
319      * @return syncMLVerProto the SyncML Protocol version.
320      */

321     public String JavaDoc getSyncMLVerProto() {
322         return this.syncMLVerProto;
323      }
324
325     /**
326      * Sets the SyncML Protocol version. It is useful to decide how calculate
327      * the digest with MD5 authentication.
328      *
329      * @param syncMLVerProto the SyncML Protocol version.
330      *
331      */

332      public void setSyncMLVerProto(String JavaDoc syncMLVerProto) {
333          this.syncMLVerProto = syncMLVerProto;
334      }
335
336     /**
337      * Gets the principal id
338      *
339      * @return principalId the principal identificator
340      */

341     public String JavaDoc getPrincipalId() {
342         return this.principalId;
343     }
344
345     /**
346      * Sets the principal identificator
347      *
348      * @param principalId the principal identificator
349      */

350     public void setPrincipalId(String JavaDoc principalId) {
351         this.principalId = principalId;
352     }
353 }
Popular Tags