KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.NoSuchAlgorithmException JavaDoc;
22
23
24 /**
25  * Represents the Notification message in according with the SyncML DM specification
26  *
27  * @author Stefano Nichele @ Funambol
28  *
29  * @version $Id: TriggerNotificationMessage.java,v 1.1 2005/05/16 17:32:55 nichele Exp $
30  */

31 public class TriggerNotificationMessage {
32
33     // ------------------------------------------------------------ Private data
34
private DigestNotificationMessage digest;
35     private TriggerHeaderNotificationMessage header;
36     private TriggerBodyNotificationMessage body;
37
38
39     // ------------------------------------------------------------ Constructors
40

41
42     public TriggerNotificationMessage() {}
43
44     public TriggerNotificationMessage(DigestNotificationMessage digest,
45                                       TriggerHeaderNotificationMessage header,
46                                       TriggerBodyNotificationMessage body) {
47         this.digest = digest;
48         this.header = header;
49         this.body = body;
50     }
51
52     public TriggerNotificationMessage(DigestNotificationMessage digest,
53                                       TriggerHeaderNotificationMessage header) {
54         this.digest = digest;
55         this.header = header;
56     }
57
58
59     // ---------------------------------------------------------- Public methods
60

61     /**
62      * Gets the digest
63      *
64      * @return the digest
65      */

66     public DigestNotificationMessage getDigest() {
67         return digest;
68     }
69
70     /**
71      * Sets the digest
72      *
73      * @param the digest
74      */

75     public void setDigest(DigestNotificationMessage digest) {
76         this.digest = digest;
77     }
78
79     /**
80      * Gets the header
81      *
82      * @return the header
83      */

84     public TriggerHeaderNotificationMessage getHeader() {
85         return header;
86     }
87
88     /**
89      * Sets the header
90      *
91      * @param the header
92      */

93     public void setHeader(TriggerHeaderNotificationMessage header) {
94         this.header = header;
95     }
96
97     /**
98      * Gets the body
99      *
100      * @return the body
101      */

102     public TriggerBodyNotificationMessage getBody() {
103         return body;
104     }
105
106     /**
107      * Sets the body
108      *
109      * @param the body
110      */

111     public void setBody(TriggerBodyNotificationMessage body) {
112         this.body = body;
113     }
114
115     public byte[] computeTriggerNotificationMessage() throws NotificationException {
116         byte[] toReturn = null;
117
118         byte[] headerMessage = null;
119         byte[] digestMessage = null;
120         byte[] bodyMessage = null;
121
122
123
124         /* Checks values */
125
126         if (header == null) {
127             throw new NotificationException("Header of the notification message could not be null");
128         }
129
130         if (digest == null) {
131             throw new NotificationException("Digest of the notification message could not be null");
132         }
133
134
135         headerMessage = header.buildByteMessageValue();
136
137         if (body != null) {
138            bodyMessage = body.getBodyMessage();
139        }
140
141
142         try {
143             digestMessage = digest.computeDigestMessage(mergeMessage(headerMessage, bodyMessage, null));
144         } catch (NoSuchAlgorithmException JavaDoc ex) {
145             throw new NotificationException("Error during digest computing", ex);
146         }
147
148
149         toReturn = mergeMessage(digestMessage, headerMessage, bodyMessage);
150
151         return toReturn;
152     }
153
154
155     // --------------------------------------------------------- Private methods
156

157     /**
158      * Merge the given byte array into a new byte array
159      * @param msg1 byte[]
160      * @param msg2 byte[]
161      * @param msg3 byte[]
162      * @return byte[]
163      */

164     private byte[] mergeMessage(byte[] msg1, byte[] msg2, byte[] msg3) {
165
166         byte[] toReturnMsg = null;
167
168         int lengthMsg1 = 0;
169         int lengthMsg2 = 0;
170         int lengthMsg3 = 0;
171
172         if (msg1 != null) {
173             lengthMsg1 = msg1.length;
174         } else {
175             msg1 = new byte[0];
176         }
177
178         if (msg2 != null) {
179             lengthMsg2 = msg2.length;
180         } else {
181             msg2 = new byte[0];
182         }
183
184         if (msg3 != null) {
185             lengthMsg3 = msg3.length;
186         } else {
187             msg3 = new byte[0];
188         }
189
190         int toReturnLength = lengthMsg1 + lengthMsg2 + lengthMsg3;
191
192         toReturnMsg = new byte[toReturnLength];
193
194         System.arraycopy(msg1, 0, toReturnMsg, 0, lengthMsg1);
195         System.arraycopy(msg2, 0, toReturnMsg, lengthMsg1, lengthMsg2);
196         System.arraycopy(msg3, 0, toReturnMsg, lengthMsg1 + lengthMsg2, lengthMsg3);
197
198         return toReturnMsg;
199     }
200
201 }
Popular Tags