KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
21  * This class represents an authentication challenge element. It corresponds
22  * to the <Chal> element in SyncML representation DTD
23  *
24  * @author Stefano Fornari @ Funambol
25  *
26  * @version $Id: Chal.java,v 1.8 2005/03/02 20:57:37 harrie Exp $
27  */

28 public final class Chal
29 implements java.io.Serializable JavaDoc {
30
31     // ------------------------------------------------------------ Private data
32
private Meta meta = null;
33
34     // ------------------------------------------------------------ Constructors
35
/** For serialization purposes */
36     protected Chal() {}
37
38     /**
39      * Constructs a new Chal object.
40      *
41      * @param meta The meta object - NOT NULL
42      *
43      */

44     public Chal (final Meta meta) {
45         this.meta = meta;
46         String JavaDoc type = meta.getType();
47         String JavaDoc format = meta.getFormat();
48         
49         if (type == null) {
50             throw new IllegalArgumentException JavaDoc(
51                                      "The authentication type cannot be null");
52         }
53         if (format == null) {
54             if (type.equalsIgnoreCase(Cred.AUTH_TYPE_BASIC)) {
55                 meta.setFormat(Constants.FORMAT_B64);
56             } else if (type.equalsIgnoreCase(Cred.AUTH_TYPE_MD5)) {
57                 meta.setFormat(Constants.FORMAT_B64);
58             } else {
59                 throw new IllegalArgumentException JavaDoc(
60                                    "The authentication format cannot be null");
61             }
62         }
63     }
64
65     // ---------------------------------------------------------- Public methods
66
/**
67      * Gets the Meta property
68      *
69      * @return meta the Meta property
70      */

71     public Meta getMeta() {
72         return this.meta;
73     }
74     
75     /**
76      * Sets the Meta property
77      *
78      * @param meta the Meta property
79      *
80      */

81     public void setMeta(Meta meta) {
82         this.meta = meta;
83     }
84
85     /**
86      * Returns the nextNonce property or null
87      *
88      * @return the nextNonce property or null
89      */

90     public NextNonce getNextNonce() {
91         return meta.getNextNonce();
92     }
93
94     public void setNextNonce(NextNonce nextNonce) {
95         if (meta == null) {
96             meta = new Meta();
97         }
98         meta.setNextNonce(nextNonce);
99     }
100     /**
101      * Returns the authentication type
102      *
103      * @return authentication type.
104      */

105     public String JavaDoc getType() {
106         return meta.getType();
107     }
108
109     /**
110      * Returns the authentication format
111      *
112      * @return format the authentication format
113      */

114     public String JavaDoc getFormat() {
115         return meta.getFormat();
116     }
117
118     /**
119      * Creates a basic authentication challange.
120      * This will have type = Cred.AUTH_TYPE_BASIC and
121      * format = Constants.FORMAT_B64
122      *
123      * @return the newly created AuthenticationChallange
124      */

125     public static Chal getBasicChal() {
126         Meta m = new Meta();
127         m.setType(Cred.AUTH_TYPE_BASIC);
128         m.setFormat(Constants.FORMAT_B64);
129         m.setNextNonce(null);
130         return new Chal(m);
131     }
132
133     /**
134      * Creates a MD5 authentication challange.
135      * This will have type = Cred.AUTH_TYPE_MD5 and
136      * format = Constants.FORMAT_B64
137      *
138      * @return the newly created AuthenticationChallange
139      */

140     public static Chal getMD5Chal() {
141         Meta m = new Meta();
142         m.setType(Cred.AUTH_TYPE_MD5);
143         m.setFormat(Constants.FORMAT_B64);
144         m.setNextNonce(null);
145         return new Chal(m);
146     }
147 }
148
Popular Tags