KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > notification > WAPBootstrapMessageBuilder


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.server.notification;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import sync4j.framework.core.bootstrap.Characteristic;
28 import sync4j.framework.core.bootstrap.Parm;
29 import sync4j.framework.core.bootstrap.WapProvisioningDoc;
30 import sync4j.framework.core.dm.ddf.Auth;
31 import sync4j.framework.core.dm.ddf.ConNode;
32 import sync4j.framework.core.dm.ddf.Connection;
33 import sync4j.framework.core.dm.ddf.DMAcc;
34 import sync4j.framework.core.dm.ddf.DMAccount;
35 import sync4j.framework.core.dm.ddf.NAP;
36 import sync4j.framework.core.dm.ddf.PX;
37 import sync4j.framework.core.dm.ddf.SyncMLDM;
38 import sync4j.framework.logging.Sync4jLogger;
39 import sync4j.framework.logging.Sync4jLoggerName;
40 import sync4j.framework.notification.BootstrapMessageBuilder;
41 import sync4j.framework.notification.NotificationException;
42 import sync4j.framework.tools.Base64;
43
44 import org.jibx.runtime.JiBXException;
45
46 /**
47  * It's a builder for a bootstrap message for Wap Profile.
48  *
49  * @author Stefano Nichele @ Funambol
50  *
51  * @version $Id: WAPBootstrapMessageBuilder.java,v 1.1 2005/05/16 17:32:57 nichele Exp $
52  */

53 public class WAPBootstrapMessageBuilder implements BootstrapMessageBuilder {
54
55     // --------------------------------------------------------------- Constants
56
private static final String JavaDoc VERSION_WAP_PROVISIONIG_DOC = "1.0";
57     private static final String JavaDoc APPLICATION_ID = "w7";
58     private static final String JavaDoc AAUTH_LEVEL_SERVER = "APPSRV";
59     private static final String JavaDoc AAUTH_LEVEL_CLIENT = "CLIENT";
60
61     private static final String JavaDoc PROPERTY_NAME_ADDR = "ADDR";
62     private static final String JavaDoc PROPERTY_NAME_APPLICATION_ID = "APPID";
63     private static final String JavaDoc PROPERTY_NAME_NAME = "NAME";
64     private static final String JavaDoc PROPERTY_NAME_PROVIDER_ID = "PROVIDER-ID";
65     private static final String JavaDoc PROPERTY_NAME_AAUTHSECRET = "AAUTHSECRET";
66     private static final String JavaDoc PROPERTY_NAME_AAUTHDATA = "AAUTHDATA";
67     private static final String JavaDoc PROPERTY_NAME_AAUTHNAME = "AAUTHNAME";
68     private static final String JavaDoc PROPERTY_NAME_AAUTHLEVEL = "AAUTHLEVEL";
69     private static final String JavaDoc PROPERTY_NAME_PORTNBR = "PORTNBR";
70     private static final String JavaDoc PROPERTY_NAME_NAP_ADDRESS = "NAP-ADDRESS";
71     private static final String JavaDoc PROPERTY_NAME_NAP_BEARER = "BEARER";
72     private static final String JavaDoc PROPERTY_NAME_NAP_ADDRTYPE = "NAP-ADDRTYPE";
73     private static final String JavaDoc PROPERTY_NAME_NAP_ID = "NAPID";
74     private static final String JavaDoc PROPERTY_NAME_NAP_NAME = "NAME";
75
76     private static final String JavaDoc PROPERTY_NAME_AUTHTYPE = "AUTHTYPE";
77     private static final String JavaDoc PROPERTY_NAME_AUTHNAME = "AUTHNAME";
78     private static final String JavaDoc PROPERTY_NAME_AUTHSECRET = "AUTHSECRET";
79
80     private static final String JavaDoc PROPERTY_NAME_PXADDR = "PXADDR";
81     private static final String JavaDoc PROPERTY_NAME_PXADDRTYPE = "PXADDRTYPE";
82
83     private static final String JavaDoc PROPERTY_NAME_PXAUTH_TYPE = "PXAUTH-TYPE";
84     private static final String JavaDoc PROPERTY_NAME_PXAUTH_ID = "PXAUTH-ID";
85     private static final String JavaDoc PROPERTY_NAME_PXAUTH_PW = "PXAUTH-PW";
86
87
88     // -------------------------------------------------------------- Properties
89

90     // ------------------------------------------------------------ Private data
91
private Logger JavaDoc log = null;
92
93     // ------------------------------------------------------------- Constructor
94
/**
95      * Creates a new WapBootstrapMessageBuilder
96      */

97     public WAPBootstrapMessageBuilder() {
98         log = Sync4jLogger.getLogger(Sync4jLoggerName.SERVER_DM_BOOTSTRAP);
99     }
100
101
102     // ---------------------------------------------------------- Public Methods
103

104     /**
105      * Returns the wbxml representation of the bootstrap message for the given <code>SycnMLDM</code> object.
106      *
107      * @param serverUri the server URI
108      * @param deviceId the device id
109      * @param phoneNumber the phone number
110      * @param syncMLDM the SyncMLDM object
111      *
112      * @throws NotificationException if a error occurs during message creation
113      * @return byte[] the wbxml representation of the bootstrap messege
114      */

115     public byte[] buildMessage(String JavaDoc serverUri, String JavaDoc deviceId,
116                                String JavaDoc phoneNumber, SyncMLDM syncMLDM) throws NotificationException {
117
118         if (log.isLoggable(Level.INFO)) {
119             log.info("Calling buildMessage in WapBootstrapMessageBuilder");
120         }
121
122         WapProvisioningDoc wapProvisioningDoc = getWapProvisioningDoc(syncMLDM);
123
124         byte[] message = null;
125
126         try {
127             message = WBXMLTools.toWBXML(wapProvisioningDoc);
128         } catch (Exception JavaDoc e) {
129             throw new NotificationException("Error during the conversion of the bootstrap message in wbxml", e);
130         }
131
132
133
134         return message;
135
136     }
137
138
139     // --------------------------------------------------------- Private Methods
140

141     /**
142       * Returns the <code>WapProvisioningDoc</code> object corresponds to the bootstrap message
143       * for the given <code>SycnMLDM</code> object.
144       * @param serverUri the server uri
145       * @param deviceId the device id
146       * @param syncMLDM the SyncMLDM object
147       *
148       * @return WapProvisioningDoc the <code>WapProvisioningDoc</code> corresponds to the bootstrap message
149       */

150     private WapProvisioningDoc getWapProvisioningDoc(SyncMLDM syncMLDM) {
151
152         WapProvisioningDoc wapProvisioningDoc = new WapProvisioningDoc(VERSION_WAP_PROVISIONIG_DOC);
153
154         DMAcc dmAcc = syncMLDM.getDmAcc();
155
156         Map JavaDoc accounts = dmAcc.getDMAccounts();
157         Set JavaDoc keys = accounts.keySet();
158         Iterator JavaDoc itAccounts = keys.iterator();
159
160         DMAccount dmAccount = null;
161         String JavaDoc accountName = null;
162         Characteristic application = null;
163
164         while (itAccounts.hasNext()) {
165             accountName = (String JavaDoc)itAccounts.next();
166             dmAccount = (DMAccount)accounts.get(accountName);
167
168             // Characteristic list for the dmAccount
169
application = getCharacteristic(dmAccount);
170
171             // adds characteristic to the main document
172
wapProvisioningDoc.addCharacteristic(application);
173         }
174
175         // creates characteristic for Con node
176
ArrayList JavaDoc chConnList = getCharacteristics(syncMLDM.getCon());
177
178         wapProvisioningDoc.addCharacteristicList(chConnList);
179
180         if (log.isLoggable(Level.FINE)) {
181             try {
182                 log.fine("WapBootstrapMessage - xml: " + wapProvisioningDoc.toXml());
183             } catch (JiBXException ex) {
184                 log.fine("Error logging WapBootstrapMessage [" + ex.getMessage() + "]");
185             }
186         }
187
188
189         return wapProvisioningDoc;
190     }
191
192     /**
193      * Returns the first <code>DMAccount</code> contained in the given <code>DMAcc</code>
194      * @param dmAcc
195      * @return DMAccount
196      */

197     private DMAccount getDMAccount(DMAcc dmAcc) {
198         Map JavaDoc dmAccounts = dmAcc.getDMAccounts();
199         Set JavaDoc dmAccountNames = dmAccounts.keySet();
200         Iterator JavaDoc it = dmAccountNames.iterator();
201         DMAccount dmAccount = null;
202         while (it.hasNext()) {
203             dmAccount = (DMAccount)dmAcc.getDMAccount( (String JavaDoc)it.next());
204         }
205         return dmAccount;
206     }
207
208     /**
209      * Returns the <code>Characteristic</code> for the data contained in the given
210      * <code>DMAccount</code>
211      * @param dmAccount DMAccount
212      * @return Characteristic the characteristic for the given DMAccount
213      */

214     private Characteristic getCharacteristic(DMAccount dmAccount) {
215
216         // Application
217
Characteristic application = new Characteristic(Characteristic.TYPE_APPLICATION);
218         Parm applicationId = new Parm(PROPERTY_NAME_APPLICATION_ID, APPLICATION_ID);
219         Parm name = new Parm(PROPERTY_NAME_NAME, dmAccount.getName());
220         Parm providerId = new Parm(PROPERTY_NAME_PROVIDER_ID, dmAccount.getServerId());
221
222         application.add(applicationId);
223         application.add(name);
224         application.add(providerId);
225
226         // Server address and port
227
Characteristic appAddr = new Characteristic(Characteristic.TYPE_APPADDR);
228         Parm addr = new Parm(PROPERTY_NAME_ADDR, dmAccount.getAddress());
229         appAddr.add(addr);
230
231         // Port number
232
Characteristic port = new Characteristic(Characteristic.TYPE_PORT);
233         Parm portNbr = new Parm(PROPERTY_NAME_PORTNBR, String.valueOf(dmAccount.getPortNumber()));
234         port.add(portNbr);
235         appAddr.add(port);
236
237         // Server credential
238
Characteristic appAuthServer = new Characteristic(Characteristic.TYPE_APPAUTH);
239         Parm levelServer = new Parm(PROPERTY_NAME_AAUTHLEVEL, AAUTH_LEVEL_SERVER);
240         Parm serverName = new Parm(PROPERTY_NAME_AAUTHNAME, dmAccount.getServerId());
241         Parm serverPw = new Parm(PROPERTY_NAME_AAUTHSECRET, dmAccount.getServerPassword());
242
243         String JavaDoc serverNonceB64 = new String JavaDoc(Base64.encode(dmAccount.getServerNonce()));
244         Parm serverNonce = new Parm(PROPERTY_NAME_AAUTHDATA, serverNonceB64);
245
246         appAuthServer.add(levelServer);
247         appAuthServer.add(serverName);
248         appAuthServer.add(serverPw);
249         appAuthServer.add(serverNonce);
250
251
252         // Client credential
253
Characteristic appAuthClient = new Characteristic(Characteristic.TYPE_APPAUTH);
254         Parm levelClient = new Parm(PROPERTY_NAME_AAUTHLEVEL, AAUTH_LEVEL_CLIENT);
255         Parm clientName = new Parm(PROPERTY_NAME_AAUTHNAME, dmAccount.getUserName());
256         Parm clientPwd = new Parm(PROPERTY_NAME_AAUTHSECRET, dmAccount.getClientPassword());
257
258         String JavaDoc clientNonceB64 = new String JavaDoc(Base64.encode(dmAccount.getClientNonce()));
259         Parm clientNonce = new Parm(PROPERTY_NAME_AAUTHDATA, clientNonceB64);
260
261         appAuthClient.add(levelClient);
262         appAuthClient.add(clientName);
263         appAuthClient.add(clientPwd);
264         appAuthClient.add(clientNonce);
265
266
267         // Adds the previous characteristic to the 'Application' characteristic
268
application.add(appAddr);
269         application.add(appAuthServer);
270         application.add(appAuthClient);
271
272         return application;
273     }
274
275
276     /**
277      * Returns a ArrayList with the <code>Characteristic</code> for the given <code>ConNode</code>
278      * @param conNode ConNode
279      * @return ArrayList the list of the characteristics for the given DMAccount
280      */

281     private ArrayList JavaDoc getCharacteristics(ConNode conNode) {
282
283         ArrayList JavaDoc listCharacteristics = new ArrayList JavaDoc();
284
285         if (conNode == null) {
286             return listCharacteristics;
287         }
288
289         Set JavaDoc keys = conNode.getConnections().keySet();
290         if (keys.size() == 0) {
291             return listCharacteristics;
292         }
293
294
295         Iterator JavaDoc itKeys = keys.iterator();
296
297         Connection connection = null;
298         String JavaDoc connectionName = null;
299         while (itKeys.hasNext()) {
300             connectionName = (String JavaDoc)itKeys.next();
301             connection = conNode.getConnection(connectionName);
302             listCharacteristics.addAll(getCharacteristic(connection, connectionName));
303         }
304         return listCharacteristics;
305     }
306
307     /**
308      * Returns a ArrayList with the <code>Characteristic</code> for the given <code>Connection</code>
309      * @param con Connection
310      * @param connectionName String
311      * @return ArrayList the list of the characteristics for the given connection
312      */

313     private ArrayList JavaDoc getCharacteristic(Connection con, String JavaDoc connectionName) {
314         ArrayList JavaDoc listCharacteristics = new ArrayList JavaDoc();
315
316         NAP conNap = con.getNap();
317         PX conPX = con.getPX();
318
319         Characteristic chNap = getCharacteristic(conNap, connectionName);
320
321         Characteristic chPX = getCharacteristic(conPX, connectionName);
322
323         if (chNap != null) {
324             listCharacteristics.add(chNap);
325         }
326
327         if (chPX != null) {
328             listCharacteristics.add(chPX);
329         }
330
331
332         return listCharacteristics;
333     }
334
335     /**
336      * Returns the <code>Characteristic</code> for the given <code>NAP</code>
337      * @param conNap NAP
338      * @param connectionName String
339      * @return Characteristic the characteristics for the given nap
340      */

341     private Characteristic getCharacteristic(NAP conNap, String JavaDoc connectionName) {
342
343         if (conNap == null) {
344             return null;
345         }
346
347         Characteristic chNap = new Characteristic(Characteristic.TYPE_NAPDEF);
348         Parm napId = new Parm(PROPERTY_NAME_NAP_ID, connectionName);
349         Parm napName = new Parm(PROPERTY_NAME_NAP_NAME, connectionName);
350         Parm napBearer = new Parm(PROPERTY_NAME_NAP_BEARER, conNap.getBearer());
351         Parm napAddress = new Parm(PROPERTY_NAME_NAP_ADDRESS, conNap.getAddress());
352         Parm napAddressType = new Parm(PROPERTY_NAME_NAP_ADDRTYPE, conNap.getAddressType());
353
354         chNap.add(napId);
355         chNap.add(napBearer);
356         chNap.add(napAddress);
357         chNap.add(napAddressType);
358
359
360         Set JavaDoc keysAuths = conNap.getAuths().keySet();
361         Iterator JavaDoc itKeys = keysAuths.iterator();
362         Auth conAuth = null;
363         String JavaDoc authName = null;
364         while (itKeys.hasNext()) {
365             authName = (String JavaDoc)itKeys.next();
366             conAuth = conNap.getAuth(authName);
367             chNap.add(getCharacteristic(conAuth, authName, 0));
368         }
369
370         return chNap;
371     }
372
373
374     /**
375      * Returns the <code>Characteristic</code> for the given <code>PX</code>
376      * @param conPX PX
377      * @param connectionName String
378      * @return Characteristic the characteristics for the given px
379      */

380     private Characteristic getCharacteristic(PX conPX, String JavaDoc connectionName) {
381
382         if (conPX == null) {
383             return null;
384         }
385
386
387         Characteristic chPXLogical = new Characteristic(Characteristic.TYPE_PXLOGICAL);
388         Characteristic chPXPhisical = new Characteristic(Characteristic.TYPE_PXPHYSICAL);
389
390
391         chPXLogical.add(chPXPhisical);
392
393
394         Parm pxAddress = new Parm(PROPERTY_NAME_PXADDR, conPX.getAddress());
395         Parm pxAddressType = new Parm(PROPERTY_NAME_PXADDRTYPE, conPX.getAddressType());
396
397
398         Characteristic chPXPort = new Characteristic(Characteristic.TYPE_PORT);
399         Parm pxPortNbr = new Parm(PROPERTY_NAME_PORTNBR, conPX.getPortNbr());
400         chPXPort.add(pxPortNbr);
401
402         chPXPhisical.add(pxAddress);
403         chPXPhisical.add(pxAddressType);
404         chPXPhisical.add(chPXPort);
405
406
407         Set JavaDoc keysAuths = conPX.getAuths().keySet();
408         Iterator JavaDoc itKeys = keysAuths.iterator();
409         Auth conAuth = null;
410         String JavaDoc authName = null;
411         while (itKeys.hasNext()) {
412             authName = (String JavaDoc)itKeys.next();
413             conAuth = conPX.getAuth(authName);
414             chPXPhisical.add(getCharacteristic(conAuth, authName, 1));
415         }
416
417         return chPXLogical;
418
419     }
420
421
422     /**
423      * Returns the <code>Characteristic</code> for the given <code>Auth</code>.
424      * <br/>
425      * If the characteristicType is:
426      * <ui>
427      * <li>0 --> the characteristic is created for NAP node
428      * <li>1 --> the characteristic is created for PX node
429      * </ui>
430      * @param auth Auth
431      * @param name String
432      * @param characteristicType int 0 indicates that the auth is relative to the Nap node,
433      * 1 indicates that the auth is relative to the PX node
434      * @return Characteristic the characteristic for the given auth
435      */

436     private Characteristic getCharacteristic(Auth auth, String JavaDoc name, int characteristicType) {
437
438         Characteristic chAuth = null;
439
440         String JavaDoc id = auth.getId();
441         String JavaDoc secret = auth.getSecret();
442
443         switch (characteristicType) {
444             case 0:
445
446                 // NAP
447
chAuth = new Characteristic(Characteristic.TYPE_NAPAUTHINFO);
448
449                 chAuth.add(new Parm(PROPERTY_NAME_AUTHTYPE, name));
450                 chAuth.add(new Parm(PROPERTY_NAME_AUTHNAME, id));
451                 chAuth.add(new Parm(PROPERTY_NAME_AUTHSECRET, secret));
452
453                 break;
454
455             case 1:
456
457                 // PX
458
chAuth = new Characteristic(Characteristic.TYPE_PXAUTHINFO);
459
460                 chAuth.add(new Parm(PROPERTY_NAME_PXAUTH_TYPE, name));
461                 chAuth.add(new Parm(PROPERTY_NAME_PXAUTH_ID, id));
462                 chAuth.add(new Parm(PROPERTY_NAME_PXAUTH_PW, secret));
463
464                 break;
465
466             default:
467
468         }
469
470         return chAuth;
471     }
472
473 }
Popular Tags