KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > notification > TriggerHeaderNotificationMessage


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.notification;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24
25
26 /**
27  * Represents the header of the Notification message in according with the SyncML DM specification.
28  *
29  * @author Stefano Nichele @ Funambol
30  *
31  * @version $Id: TriggerHeaderNotificationMessage.java,v 1.1 2005/05/16 17:32:55 nichele Exp $
32  */

33 public class TriggerHeaderNotificationMessage {
34
35     // --------------------------------------------------------------- Constants
36
public static final byte UI_MODE_NOT_SPECIFIED = 0;
37     public static final byte UI_MODE_BACKGROUND = 1;
38     public static final byte UI_MODE_INFORMATIVE = 2;
39     public static final byte UI_MODE_USER_INTERACTION = 3;
40
41     public static final byte INITIATOR_CLIENT = 0;
42     public static final byte INITIATOR_SERVER = 1;
43
44     private static final float MAX_VERSION_VALUE = 102.3f;
45
46     private static final int NUM_BIT_VERSION_FIELD = 10;
47     private static final int NUM_BIT_FUTURE_USE_FIELD = 27;
48     private static final int NUM_BIT_SESSION_ID_FIELD = 16;
49     private static final int NUM_BIT_SERVER_ID_LENGTH_FIELD = 8;
50
51
52
53     // ------------------------------------------------------------ Private data
54
private float version;
55     private int uiMode;
56     private int initiator;
57     private int sessionId;
58     private String JavaDoc serverIdentifier;
59
60     // ------------------------------------------------------------ Constructors
61

62     public TriggerHeaderNotificationMessage() {}
63
64     /**
65      * Creates a new TriggerHeaderNotificationMessage object with the given parameters
66      *
67      * @param version the version of the SyncML DM protocol
68      * @param uiMode the user interaction mode
69      * @param initiator the initiator of the notification process
70      * @param sessionId the session id
71      * @param serverIdentifier the server id
72      */

73     public TriggerHeaderNotificationMessage(final float version,
74                                             final int uiMode,
75                                             final int initiator,
76                                             final int sessionId,
77                                             final String JavaDoc serverIdentifier) {
78         this.version = version;
79         this.uiMode = uiMode;
80         this.initiator = initiator;
81         this.sessionId = sessionId;
82         this.serverIdentifier = serverIdentifier;
83     }
84
85     // ---------------------------------------------------------- Public methods
86
/**
87      * Gets the version
88      *
89      * @return the version
90      */

91     public float getVersion() {
92         return version;
93     }
94
95     /**
96      * Sets the version
97      *
98      * @param the version
99      * @throws NotificationException if the given value is bigger of <code>MAX_VERSION_VALUE</code>
100      */

101     public void setVersion(float version) throws NotificationException {
102
103         if (version > MAX_VERSION_VALUE) {
104             throw new NotificationException("Value specified for version information must be small of " + MAX_VERSION_VALUE);
105         }
106         this.version = version;
107     }
108
109     /**
110      * Gets the uiMode
111      *
112      * @return the uiMode
113      */

114     public int getUiMode() {
115         return uiMode;
116     }
117
118     /**
119      * Sets the uiMode
120      *
121      * @param the uiMode
122      *
123      */

124     public void setUiMode(int uiMode) {
125         this.uiMode = uiMode;
126     }
127
128     /**
129      * Gets the initiator
130      *
131      * @return the initiator
132      */

133     public int getInitiator() {
134         return initiator;
135     }
136
137     /**
138      * Sets the initiator
139      *
140      * @param the initiator
141      *
142      */

143     public void setInitiator(int initiator) {
144         this.initiator = initiator;
145     }
146
147     /**
148      * Gets the sessionId
149      *
150      * @return the sessionId
151      */

152     public int getSessionId() {
153         return sessionId;
154     }
155
156     /**
157      * Sets the sessionId
158      *
159      * @param the sessionId
160      *
161      */

162     public void setSessionId(int sessionId) {
163         this.sessionId = sessionId;
164     }
165
166
167
168     /**
169      * Gets the server Identifier
170      *
171      * @return the server Identifier
172      */

173     public String JavaDoc getServerIdentifier() {
174         return serverIdentifier;
175     }
176
177     /**
178      * Sets the server Identifier
179      *
180      * @param the server Identifier
181      *
182      */

183     public void setServerIdentifier(String JavaDoc serverIdentifier) {
184         this.serverIdentifier = serverIdentifier;
185     }
186
187
188     /**
189      * Builds header message
190      * @throws NotificationException
191      * @return byte[]
192      */

193     public byte[] buildByteMessageValue() throws NotificationException {
194
195         byte[] toReturn = null;
196
197         ByteArrayOutputStream JavaDoc byteOut = new ByteArrayOutputStream JavaDoc();
198         BitOutputStream bout = new BitOutputStream(byteOut);
199
200
201         byte[] bServerId = null;
202         try {
203             // write version field (NUM_BIT_VERSION_FIELD bit)
204
int newVersion = (int) (version * 10);
205             int[] intRepresentation = getIntBinaryRepresentation(newVersion, NUM_BIT_VERSION_FIELD);
206             for (int i = 0; i < NUM_BIT_VERSION_FIELD; i++) {
207                 bout.writeBit(intRepresentation[i]);
208             }
209
210             // write ui mode field (2 bit)
211
switch (uiMode) {
212                 case UI_MODE_NOT_SPECIFIED:
213                     bout.writeBit(0);
214                     bout.writeBit(0);
215                     break;
216                 case UI_MODE_BACKGROUND:
217                     bout.writeBit(0);
218                     bout.writeBit(1);
219                     break;
220                 case UI_MODE_INFORMATIVE:
221                     bout.writeBit(1);
222                     bout.writeBit(0);
223                     break;
224                 case UI_MODE_USER_INTERACTION:
225                     bout.writeBit(1);
226                     bout.writeBit(1);
227                     break;
228                 default:
229                     bout.close();
230                     throw new NotificationException("User interaction mode '" + uiMode +
231                         "' isn't valid");
232             }
233
234             // write initiator field (1 bit)
235
switch (initiator) {
236                 case INITIATOR_CLIENT:
237                     bout.writeBit(0);
238                     break;
239                 case INITIATOR_SERVER:
240                     bout.writeBit(1);
241                     break;
242
243                 default:
244                     bout.close();
245                     throw new NotificationException("Initiator value '" + uiMode + "' isn't valid");
246             }
247
248             // write future use field (NUM_BIT_FUTURE_USE_FIELD bit)
249
// in this version this field isn't used. Then write NUM_BIT_FUTURE_USE_FIELD bit = 0
250
for (int i = 0; i < NUM_BIT_FUTURE_USE_FIELD; i++) {
251                 bout.writeBit(0);
252             }
253
254
255             int[] intRepresentationSessionId = getIntBinaryRepresentation(sessionId,
256                 NUM_BIT_SESSION_ID_FIELD);
257
258             for (int i = 0; i < NUM_BIT_SESSION_ID_FIELD; i++) {
259                 bout.writeBit(intRepresentationSessionId[i]);
260             }
261
262             // write server identifier length
263
int length = serverIdentifier.length();
264             int[] intRepresentationLength = getIntBinaryRepresentation(length,
265                 NUM_BIT_SERVER_ID_LENGTH_FIELD);
266
267             for (int i = 0; i < NUM_BIT_SERVER_ID_LENGTH_FIELD; i++) {
268                 bout.writeBit(intRepresentationLength[i]);
269             }
270
271             // server identifier
272
bServerId = serverIdentifier.getBytes("UTF-8");
273
274             bout.close();
275         } catch (IOException JavaDoc ex) {
276             throw new NotificationException("Error during message building", ex);
277         }
278
279         byte[] tmp = byteOut.toByteArray();
280
281         toReturn = new byte[tmp.length + serverIdentifier.length()];
282
283         System.arraycopy(tmp, 0, toReturn, 0, tmp.length);
284         System.arraycopy(bServerId, 0, toReturn, tmp.length, bServerId.length);
285
286
287         return toReturn;
288     }
289
290
291     // -------------------------------------------------------- Private Methods
292

293     /**
294      * Convert a string as 1010011 into int array with '0' or '1' element
295      * @param binaryRepresentation the binary representation string
296      * @return int[] the int array with '0' or '1' element
297      */

298     private static int[] converteBinaryRepresentation(String JavaDoc binaryRepresentation) {
299         char[] c = binaryRepresentation.toCharArray();
300         int[] intRepresentation = new int[c.length];
301
302         for (int i = 0; i < c.length; i++) {
303             intRepresentation[i] = Integer.parseInt(String.valueOf(c[i]));
304         }
305         return intRepresentation;
306     }
307
308
309     /**
310      * Given a integer, returns a int array with '0' or '1' elements correspondent to
311      * the binary representation of the integer. If the binary representation is smaller
312      * that n bit then the binary representation is padding
313      * @param value the integer value
314      * @param n the length of the binary representation
315      * @return int[] the binary representation
316      */

317     private int[] getIntBinaryRepresentation(int value, int n) {
318         String JavaDoc sBinaryValue = Integer.toBinaryString(value);
319         sBinaryValue = paddingString(sBinaryValue, n, '0', true);
320
321         int[] intRepresentation = converteBinaryRepresentation(sBinaryValue);
322
323         return intRepresentation;
324     }
325
326
327     /**
328      * Pad a string S with a size of N with char C on the left (True) or on the right(False)
329      * @param s the string to paddind
330      * @param n int the size
331      * @param c char the char used to padding
332      * @param paddingLeft indicates if padding on the left (true) or on the right (false)
333      * @return the string after padding
334      */

335     private static String JavaDoc paddingString(String JavaDoc s, int n, char c, boolean paddingLeft) {
336         StringBuffer JavaDoc str = new StringBuffer JavaDoc(s);
337         int strLength = str.length();
338         if (n > 0 && n > strLength) {
339             for (int i = 0; i <= n; i++) {
340                 if (paddingLeft) {
341                     if (i < n - strLength)str.insert(0, c);
342                 } else {
343                     if (i > strLength)str.append(c);
344                 }
345             }
346         }
347         return str.toString();
348     }
349
350 }
Popular Tags