1 18 package sync4j.server.notification; 19 20 import java.io.*; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 27 import org.jibx.runtime.BindingDirectory; 28 import org.jibx.runtime.IBindingFactory; 29 import org.jibx.runtime.IMarshallingContext; 30 31 import sync4j.framework.core.*; 32 import sync4j.framework.core.dm.ddf.*; 33 import sync4j.framework.logging.Sync4jLogger; 34 import sync4j.framework.logging.Sync4jLoggerName; 35 import sync4j.framework.notification.BootstrapMessageBuilder; 36 import sync4j.framework.notification.NotificationException; 37 import sync4j.framework.tools.ArrayUtils; 38 import sync4j.framework.tools.Base64; 39 import sync4j.framework.tools.DbgTools; 40 import sync4j.framework.tools.WBXMLTools; 41 42 43 50 public class PlainBootstrapMessageBuilder implements BootstrapMessageBuilder { 51 52 54 56 private Logger log = null; 58 59 public PlainBootstrapMessageBuilder() { 61 log = Sync4jLogger.getLogger(Sync4jLoggerName.SERVER_DM_BOOTSTRAP); 62 } 63 64 66 77 public byte[] buildMessage(String serverUri, String deviceId, 78 String phoneNumber, SyncMLDM syncMLDM) throws NotificationException { 79 80 if (log.isLoggable(Level.INFO)) { 81 log.info("Calling buildMessage in PlainBootstrapMessageBuilder"); 82 } 83 84 SyncML syncMLBootstrapMessage = getSyncMLBootstrapMessage(serverUri, deviceId, syncMLDM); 85 86 byte[] message = null; 87 88 try { 89 90 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 91 92 IBindingFactory f = BindingDirectory.getFactory(SyncML.class); 93 IMarshallingContext c = f.createMarshallingContext(); 94 c.setIndent(0); 95 c.marshalDocument(syncMLBootstrapMessage, "UTF-8", null, bout); 96 97 String inputXml = new String (bout.toByteArray()); 98 inputXml = metInfNamespaceHandler(inputXml); 99 100 if (log.isLoggable(Level.FINE)) { 101 log.fine("PlainBootstrapMessage - xml: " + inputXml); 102 } 103 104 105 message = WBXMLTools.toWBXML(inputXml); 106 107 110 112 } catch (Exception ex) { 113 throw new NotificationException("Error during xml to wbxml transformation", ex); 114 } 115 116 return message; 117 } 118 119 121 130 private SyncML getSyncMLBootstrapMessage(String serverUri, String deviceId, SyncMLDM syncMLDM) throws NotificationException { 131 SyncML syncMLBootstrapMessage = null; 132 133 SyncHdr syncHdrMessage = null; 134 SyncBody syncBodyMessage = null; 135 136 try { 137 138 syncHdrMessage = new SyncHdr( 139 new VerDTD("1.1"), 140 Constants.SYNCML_DM_1_1, 141 new SessionID("0"), 142 "0", new Target(deviceId), 144 new Source(serverUri), 145 null, 146 false, 147 null, 148 null 149 150 ); 151 152 Add addCommand = buildAddCommand(syncMLDM); 153 154 syncBodyMessage = new SyncBody( 155 new AbstractCommand[] {addCommand} 156 , 157 true 158 159 ); 160 161 syncMLBootstrapMessage = new SyncML(syncHdrMessage, syncBodyMessage); 162 163 164 } catch (RepresentationException ex) { 165 throw new NotificationException("Error during creation of the SyncML message", ex); 166 } 167 168 return syncMLBootstrapMessage; 169 } 170 171 172 179 private Add buildAddCommand(SyncMLDM syncMLDM) { 180 Add addCommand = new Add( 181 new CmdID(1), 182 false, 183 null, 184 null, 185 getItemsToAdd(syncMLDM) 186 ); 187 188 return addCommand; 189 } 190 191 196 private Item[] getItemsToAdd(SyncMLDM syncMLDM) { 197 198 Item[] itemDMAcc = getItemsToAddForDMAcc(syncMLDM.getDmAcc()); 200 201 Item[] itemCon = getItemsToAddForConNode(syncMLDM.getCon()); 203 204 return (Item[])(ArrayUtils.mergeArrays(itemDMAcc, itemCon, itemDMAcc.getClass().getComponentType())); 205 } 206 207 208 213 private Item[] getItemsToAddForDMAcc(DMAcc dmAcc) { 214 215 Set keys = dmAcc.getDMAccounts().keySet(); 216 217 int numDMAcc = keys.size(); 218 219 if (numDMAcc == 0) { 220 return new Item[0]; 221 } 222 223 ArrayList listItems = new ArrayList (); 224 225 java.util.Map accounts = dmAcc.getDMAccounts(); 226 Iterator itAccounts = keys.iterator(); 227 228 Meta metaInfoForInteriorNode = new Meta(); 229 metaInfoForInteriorNode.setFormat("node"); 230 231 DMAccount dmAccount = null; 232 String accountName = null; 233 while (itAccounts.hasNext()) { 234 accountName = (String )itAccounts.next(); 235 dmAccount = (DMAccount)accounts.get(accountName); 236 237 240 String localUri = SyncMLDM.SYNCML_DM_BASE_URI + SyncMLDM.SYNCML_DM_DMACC + "/" + accountName; 241 Item itemDMAccount = new Item(new Target(localUri), 242 null, 243 metaInfoForInteriorNode, 244 null, 245 false 246 247 ); 248 249 listItems.add(itemDMAccount); 250 251 252 listItems.add(getItem(localUri + DMAccount.DMACC_ADDR, dmAccount.getAddress(), false, false)); 253 254 255 listItems.add(getItem(localUri + DMAccount.DMACC_ADDR_TYPE, 256 String.valueOf(dmAccount.getAddressType()), false, false)); 257 258 259 listItems.add(getItem(localUri + DMAccount.DMACC_PORT_NBR, 260 String.valueOf(dmAccount.getPortNumber()), false, false)); 261 262 263 String conRef = dmAccount.getConRef(); 264 if (conRef != null && !conRef.equals("")) { 265 listItems.add(getItem(localUri + DMAccount.DMACC_CON_REF, conRef, false, false)); 266 } 267 268 269 270 listItems.add(getItem(localUri + DMAccount.DMACC_SERVER_ID, dmAccount.getServerId(), false, false)); 271 272 273 listItems.add(getItem(localUri + DMAccount.DMACC_SERVER_PASSWORD, 274 dmAccount.getServerPassword(), false, false)); 275 276 277 if (dmAccount.getServerNonce() != null) { 278 String serverNonceB64 = new String (Base64.encode(dmAccount.getServerNonce())); 279 if (log.isLoggable(Level.FINEST)) { 280 log.finest("ServerNonce: " + DbgTools.bytesToHex(dmAccount.getServerNonce())); 281 log.finest("ServerNonceB64: " + serverNonceB64); 282 283 } 284 listItems.add(getItem(localUri + DMAccount.DMACC_SERVER_NONCE, 285 serverNonceB64, false, false)); 286 } 287 288 289 listItems.add(getItem(localUri + DMAccount.DMACC_USERNAME, dmAccount.getUserName(), false, false)); 290 291 292 listItems.add(getItem(localUri + DMAccount.DMACC_CLIENT_PASSWORD, 293 dmAccount.getClientPassword(), false, false)); 294 295 296 if (dmAccount.getClientNonce() != null) { 297 String clientNonceB64 = new String (Base64.encode(dmAccount.getClientNonce())); 298 if (log.isLoggable(Level.FINEST)) { 299 log.finest("ClientNonce: " + DbgTools.bytesToHex(dmAccount.getClientNonce())); 300 log.finest("ClientNonceB64: " + clientNonceB64); 301 } 302 listItems.add(getItem(localUri + DMAccount.DMACC_CLIENT_NONCE, 303 clientNonceB64, false, false)); 304 } 305 306 307 listItems.add(getItem(localUri + DMAccount.DMACC_AUTH_PREF, dmAccount.getAuthPref(), false, false)); 308 309 310 listItems.add(getItem(localUri + DMAccount.DMACC_NAME, dmAccount.getName(), false, false)); 311 } 312 313 return (Item[])listItems.toArray(new Item[0]); 314 } 315 316 317 322 private Item[] getItemsToAddForConNode(ConNode conNode) { 323 if (conNode == null) { 324 return new Item[0]; 325 } 326 Set keys = conNode.getConnections().keySet(); 327 if (keys.size() == 0) { 328 return new Item[0]; 329 } 330 331 ArrayList listItems = new ArrayList (); 332 Iterator itKeys = keys.iterator(); 333 334 Connection connection = null; 335 String connectionName = null; 336 while (itKeys.hasNext()) { 337 connectionName = (String )itKeys.next(); 338 connection = conNode.getConnection(connectionName); 339 listItems.addAll( getItemsToAddForConnection(connection, connectionName)); 340 } 341 return (Item[])listItems.toArray(new Item[0]); 342 } 343 344 345 351 private ArrayList getItemsToAddForConnection(Connection con, String connectionName) { 352 ArrayList listItems = new ArrayList (); 353 354 Meta metaInfoForInteriorNode = new Meta(); 355 metaInfoForInteriorNode.setFormat("node"); 356 357 360 String localUri = SyncMLDM.SYNCML_DM_BASE_URI + SyncMLDM.SYNCML_DM_CON + "/" + connectionName; 361 Item itemConnection = new Item(new Target(localUri), 362 null, 363 metaInfoForInteriorNode, 364 null, 365 false 366 ); 367 368 listItems.add(itemConnection); 369 370 sync4j.framework.core.dm.ddf.Ext conExt = con.getExt(); 371 NAP conNap = con.getNap(); 372 PX conPX = con.getPX(); 373 374 ArrayList itemConExt = getItemsToAddForConExt(conExt, connectionName); 375 ArrayList itemConNap = getItemsToAddForConNap(conNap, connectionName); 376 ArrayList itemConPX = getItemsToAddForConPX(conPX, connectionName); 377 378 listItems.addAll(itemConExt); 379 listItems.addAll(itemConNap); 380 listItems.addAll(itemConPX); 381 382 return listItems; 383 } 384 385 391 private ArrayList getItemsToAddForConExt(sync4j.framework.core.dm.ddf.Ext ext, String connectionName) { 392 ArrayList listItems = new ArrayList (); 393 394 if (ext == null) { 395 return listItems; 396 } 397 398 java.util.Map nodes = ext.getNodes(); 399 int size = nodes.size(); 400 if (size == 0) { 401 return listItems; 402 } 403 404 Meta metaInfoForInteriorNode = new Meta(); 405 metaInfoForInteriorNode.setFormat("node"); 406 407 410 String localUri = SyncMLDM.SYNCML_DM_BASE_URI + SyncMLDM.SYNCML_DM_CON + "/" + connectionName + Connection.CON_EXT; 411 Item itemConExt = new Item(new Target(localUri), 412 null, 413 metaInfoForInteriorNode, 414 new ComplexData(), 415 false 416 ); 417 418 419 listItems.add(itemConExt); 420 421 Iterator itNodes = nodes.keySet().iterator(); 422 423 String name = null; 424 String value = null; 425 while (itNodes.hasNext()) { 426 name = (String )itNodes.next(); 427 value = (String )nodes.get(name); 428 listItems.add(getItem(localUri + "/" + name, 429 value, false, false)); 430 } 431 432 return listItems; 433 434 } 435 436 442 private ArrayList getItemsToAddForConNap(NAP nap, String connectionName) { 443 ArrayList listItems = new ArrayList (); 444 445 if (nap == null) { 446 return listItems; 447 } 448 449 450 Meta metaInfoForInteriorNode = new Meta(); 451 metaInfoForInteriorNode.setFormat("node"); 452 453 456 String localUri = SyncMLDM.SYNCML_DM_BASE_URI + SyncMLDM.SYNCML_DM_CON + "/" + connectionName + Connection.CON_NAP; 457 Item itemConNap = new Item(new Target(localUri), 458 null, 459 metaInfoForInteriorNode, 460 new ComplexData(), 461 false 462 ); 463 464 465 listItems.add(itemConNap); 466 467 468 listItems.add(getItem(localUri + NAP.NAP_BEARER, 469 nap.getBearer(), false, false)); 470 471 472 listItems.add(getItem(localUri + NAP.NAP_ADDR_TYPE, 473 nap.getAddressType(), false, false)); 474 475 476 listItems.add(getItem(localUri + NAP.NAP_ADDR, nap.getAddress(), false, false)); 477 478 479 if (nap.getAuths().size() > 0) { 480 Item itemAuth = new Item(new Target(localUri + NAP.NAP_AUTH), 481 null, 482 metaInfoForInteriorNode, 483 null, 484 false 485 ); 486 487 listItems.add(itemAuth); 488 489 Set keysAuths = nap.getAuths().keySet(); 490 Iterator itKeys = keysAuths.iterator(); 491 Auth conAuth = null; 492 String authName = null; 493 while (itKeys.hasNext()) { 494 authName = (String )itKeys.next(); 495 conAuth = nap.getAuth(authName); 496 listItems.addAll(getItemsToAddForConAuth(conAuth, localUri + NAP.NAP_AUTH + "/" + authName)); 497 } 498 } 499 500 return listItems; 501 } 502 503 509 private ArrayList getItemsToAddForConPX(PX px, String connectionName) { 510 ArrayList listItems = new ArrayList (); 511 512 if (px == null) { 513 return listItems; 514 } 515 516 517 Meta metaInfoForInteriorNode = new Meta(); 518 metaInfoForInteriorNode.setFormat("node"); 519 520 523 String localUri = SyncMLDM.SYNCML_DM_BASE_URI + SyncMLDM.SYNCML_DM_CON + "/" + connectionName + Connection.CON_PX; 524 Item itemConPx = new Item(new Target(localUri), 525 null, 526 metaInfoForInteriorNode, 527 new ComplexData(), 528 false 529 ); 530 531 532 listItems.add(itemConPx); 533 534 535 listItems.add(getItem(localUri + PX.PX_PORT_NBR, 536 px.getPortNbr(), false, false)); 537 538 539 listItems.add(getItem(localUri + PX.PX_ADDR_TYPE, 540 px.getAddressType(), false, false)); 541 542 543 listItems.add(getItem(localUri + PX.PX_ADDR, px.getAddress(), false, false)); 544 545 if (px.getAuths().size() > 0) { 546 Item itemAuth = new Item(new Target(localUri + PX.PX_AUTH), 547 null, 548 metaInfoForInteriorNode, 549 null, 550 false 551 ); 552 553 listItems.add(itemAuth); 554 555 Set keysAuths = px.getAuths().keySet(); 556 Iterator itKeys = keysAuths.iterator(); 557 Auth conAuth = null; 558 String authName = null; 559 while (itKeys.hasNext()) { 560 authName = (String )itKeys.next(); 561 conAuth = px.getAuth(authName); 562 listItems.addAll(getItemsToAddForConAuth(conAuth, localUri + PX.PX_AUTH + "/" + authName)); 563 } 564 } 565 566 return listItems; 567 } 568 569 570 576 private ArrayList getItemsToAddForConAuth(Auth auth, String localUri) { 577 ArrayList listItems = new ArrayList (); 578 579 Meta metaInfoForInteriorNode = new Meta(); 580 metaInfoForInteriorNode.setFormat("node"); 581 582 585 Item itemConnection = new Item(new Target(localUri), 586 null, 587 metaInfoForInteriorNode, 588 null, 589 false 590 ); 591 592 593 listItems.add(itemConnection); 594 595 596 listItems.add(getItem(localUri + Auth.AUTH_ID, 597 auth.getId(), false, false)); 598 599 600 listItems.add(getItem(localUri + Auth.AUTH_SECRET, 601 auth.getSecret(), false, false)); 602 603 604 return listItems; 605 } 606 607 608 617 private Item getItem(String localUri, String data, boolean interiorNode, boolean formatB64) { 618 619 Meta meta = null; 620 621 if (interiorNode) { 622 meta = new Meta(); 623 meta.setFormat("node"); 624 } 625 626 if (formatB64) { 627 meta = new Meta(); 628 meta.setFormat("b64"); 629 } 630 631 Item item = new Item(new Target(localUri), 632 null, 633 meta, 634 new ComplexData(data), 635 false 636 ); 637 638 return item; 639 } 640 641 649 private String metInfNamespaceHandler(String msg) { 650 int s = 0; 651 int e = 0; 652 653 StringBuffer response = new StringBuffer (); 654 while (( e = msg.indexOf("<Meta", s)) >= 0) { 655 656 response = response.append(msg.substring(s, e)); 657 658 int a = msg.indexOf("</Meta>", e); 659 String meta = msg.substring(e, a); 660 661 meta = meta.replaceAll("<Type>" , "<Type xmlns='syncml:metinf'>"); 662 meta = meta.replaceAll("<Format>" , "<Format xmlns='syncml:metinf'>"); 663 meta = meta.replaceAll("<Mark>" , "<Mark xmlns='syncml:metinf'>"); 664 meta = meta.replaceAll("<Size>" , "<Size xmlns='syncml:metinf'>"); 665 meta = meta.replaceAll("<Anchor>" , "<Anchor xmlns='syncml:metinf'>"); 666 meta = meta.replaceAll("<Version>", "<Version xmlns='syncml:metinf'>"); 667 meta = meta.replaceAll("<NextNonce>" , "<NextNonce xmlns='syncml:metinf'>"); 668 meta = meta.replaceAll("<MaxMsgSize>", "<MaxMsgSize xmlns='syncml:metinf'>"); 669 meta = meta.replaceAll("<MaxObjSize>", "<MaxObjSize xmlns='syncml:metinf'>"); 670 meta = meta.replaceAll("<EMI>" , "<EMI xmlns='syncml:metinf'>"); 671 meta = meta.replaceAll("<Mem>" , "<Mem xmlns='syncml:metinf'>"); 672 673 s = a + 7; 674 response.append(meta).append("</Meta>"); 675 } 676 return response.append(msg.substring(s, msg.length())).toString(); 677 } 678 679 685 private void writeBytesToFile(String fileName, byte[] bytes) { 686 try { 687 File file = new File(fileName); 688 FileOutputStream fOut = new FileOutputStream(file); 689 fOut.write(bytes); 690 } catch (FileNotFoundException ex) { 691 } catch (IOException ex) { 692 } 693 694 } 695 } | Popular Tags |