1 18 package sync4j.server.notification; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 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 53 public class WAPBootstrapMessageBuilder implements BootstrapMessageBuilder { 54 55 private static final String VERSION_WAP_PROVISIONIG_DOC = "1.0"; 57 private static final String APPLICATION_ID = "w7"; 58 private static final String AAUTH_LEVEL_SERVER = "APPSRV"; 59 private static final String AAUTH_LEVEL_CLIENT = "CLIENT"; 60 61 private static final String PROPERTY_NAME_ADDR = "ADDR"; 62 private static final String PROPERTY_NAME_APPLICATION_ID = "APPID"; 63 private static final String PROPERTY_NAME_NAME = "NAME"; 64 private static final String PROPERTY_NAME_PROVIDER_ID = "PROVIDER-ID"; 65 private static final String PROPERTY_NAME_AAUTHSECRET = "AAUTHSECRET"; 66 private static final String PROPERTY_NAME_AAUTHDATA = "AAUTHDATA"; 67 private static final String PROPERTY_NAME_AAUTHNAME = "AAUTHNAME"; 68 private static final String PROPERTY_NAME_AAUTHLEVEL = "AAUTHLEVEL"; 69 private static final String PROPERTY_NAME_PORTNBR = "PORTNBR"; 70 private static final String PROPERTY_NAME_NAP_ADDRESS = "NAP-ADDRESS"; 71 private static final String PROPERTY_NAME_NAP_BEARER = "BEARER"; 72 private static final String PROPERTY_NAME_NAP_ADDRTYPE = "NAP-ADDRTYPE"; 73 private static final String PROPERTY_NAME_NAP_ID = "NAPID"; 74 private static final String PROPERTY_NAME_NAP_NAME = "NAME"; 75 76 private static final String PROPERTY_NAME_AUTHTYPE = "AUTHTYPE"; 77 private static final String PROPERTY_NAME_AUTHNAME = "AUTHNAME"; 78 private static final String PROPERTY_NAME_AUTHSECRET = "AUTHSECRET"; 79 80 private static final String PROPERTY_NAME_PXADDR = "PXADDR"; 81 private static final String PROPERTY_NAME_PXADDRTYPE = "PXADDRTYPE"; 82 83 private static final String PROPERTY_NAME_PXAUTH_TYPE = "PXAUTH-TYPE"; 84 private static final String PROPERTY_NAME_PXAUTH_ID = "PXAUTH-ID"; 85 private static final String PROPERTY_NAME_PXAUTH_PW = "PXAUTH-PW"; 86 87 88 90 private Logger log = null; 92 93 97 public WAPBootstrapMessageBuilder() { 98 log = Sync4jLogger.getLogger(Sync4jLoggerName.SERVER_DM_BOOTSTRAP); 99 } 100 101 102 104 115 public byte[] buildMessage(String serverUri, String deviceId, 116 String 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 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 141 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 accounts = dmAcc.getDMAccounts(); 157 Set keys = accounts.keySet(); 158 Iterator itAccounts = keys.iterator(); 159 160 DMAccount dmAccount = null; 161 String accountName = null; 162 Characteristic application = null; 163 164 while (itAccounts.hasNext()) { 165 accountName = (String )itAccounts.next(); 166 dmAccount = (DMAccount)accounts.get(accountName); 167 168 application = getCharacteristic(dmAccount); 170 171 wapProvisioningDoc.addCharacteristic(application); 173 } 174 175 ArrayList 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 197 private DMAccount getDMAccount(DMAcc dmAcc) { 198 Map dmAccounts = dmAcc.getDMAccounts(); 199 Set dmAccountNames = dmAccounts.keySet(); 200 Iterator it = dmAccountNames.iterator(); 201 DMAccount dmAccount = null; 202 while (it.hasNext()) { 203 dmAccount = (DMAccount)dmAcc.getDMAccount( (String )it.next()); 204 } 205 return dmAccount; 206 } 207 208 214 private Characteristic getCharacteristic(DMAccount dmAccount) { 215 216 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 Characteristic appAddr = new Characteristic(Characteristic.TYPE_APPADDR); 228 Parm addr = new Parm(PROPERTY_NAME_ADDR, dmAccount.getAddress()); 229 appAddr.add(addr); 230 231 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 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 serverNonceB64 = new String (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 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 clientNonceB64 = new String (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 application.add(appAddr); 269 application.add(appAuthServer); 270 application.add(appAuthClient); 271 272 return application; 273 } 274 275 276 281 private ArrayList getCharacteristics(ConNode conNode) { 282 283 ArrayList listCharacteristics = new ArrayList (); 284 285 if (conNode == null) { 286 return listCharacteristics; 287 } 288 289 Set keys = conNode.getConnections().keySet(); 290 if (keys.size() == 0) { 291 return listCharacteristics; 292 } 293 294 295 Iterator itKeys = keys.iterator(); 296 297 Connection connection = null; 298 String connectionName = null; 299 while (itKeys.hasNext()) { 300 connectionName = (String )itKeys.next(); 301 connection = conNode.getConnection(connectionName); 302 listCharacteristics.addAll(getCharacteristic(connection, connectionName)); 303 } 304 return listCharacteristics; 305 } 306 307 313 private ArrayList getCharacteristic(Connection con, String connectionName) { 314 ArrayList listCharacteristics = new ArrayList (); 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 341 private Characteristic getCharacteristic(NAP conNap, String 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 keysAuths = conNap.getAuths().keySet(); 361 Iterator itKeys = keysAuths.iterator(); 362 Auth conAuth = null; 363 String authName = null; 364 while (itKeys.hasNext()) { 365 authName = (String )itKeys.next(); 366 conAuth = conNap.getAuth(authName); 367 chNap.add(getCharacteristic(conAuth, authName, 0)); 368 } 369 370 return chNap; 371 } 372 373 374 380 private Characteristic getCharacteristic(PX conPX, String 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 keysAuths = conPX.getAuths().keySet(); 408 Iterator itKeys = keysAuths.iterator(); 409 Auth conAuth = null; 410 String authName = null; 411 while (itKeys.hasNext()) { 412 authName = (String )itKeys.next(); 413 conAuth = conPX.getAuth(authName); 414 chPXPhisical.add(getCharacteristic(conAuth, authName, 1)); 415 } 416 417 return chPXLogical; 418 419 } 420 421 422 436 private Characteristic getCharacteristic(Auth auth, String name, int characteristicType) { 437 438 Characteristic chAuth = null; 439 440 String id = auth.getId(); 441 String secret = auth.getSecret(); 442 443 switch (characteristicType) { 444 case 0: 445 446 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 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 |