1 18 package sync4j.framework.protocol; 19 20 import java.util.*; 21 22 import sync4j.framework.core.*; 23 import sync4j.framework.database.Database; 24 import sync4j.framework.protocol.v11.*; 25 import sync4j.framework.tools.MD5; 26 27 35 public class ProtocolUtil { 36 37 45 public static ArrayList filterCommands(AbstractCommand[] commands, Class filterClass) { 46 ArrayList filteredCommands = new ArrayList(); 47 48 for (int i=0; i<commands.length; ++i) { 49 if (filterClass.isInstance(commands[i])) { 50 filteredCommands.add(commands[i]); 51 } 52 } 53 54 return filteredCommands; 55 } 56 57 68 public static ArrayList filterCommands(AbstractCommand[] commands, 69 CmdID cmdId ) { 70 ArrayList filteredCommands = new ArrayList(); 71 72 for (int i=0; i<commands.length; ++i) { 73 if ( (commands[i] instanceof ResponseCommand) 74 && ((((ResponseCommand)commands[i]).getCmdID()).equals(cmdId))) { 75 filteredCommands.add(commands[i]); 76 } 77 } 78 79 return filteredCommands; 80 } 81 82 95 public static ArrayList filterCommands(AbstractCommand[] commands , 96 Class filterClass, 97 CmdID cmdId ) { 98 ArrayList list = filterCommands(commands, cmdId); 104 int size = list.size(); 105 AbstractCommand [] aCommands = new AbstractCommand[size]; 106 for (int i=0; i < size; i++) { 107 aCommands[i] = (AbstractCommand)list.get(i); 108 } 109 return filterCommands( 110 113 aCommands, 114 filterClass 115 ); 116 } 117 118 126 public static List filterCommands(List commands, String [] types) { 127 StringBuffer sb = new StringBuffer (","); 128 129 for (int i = 0; ((types != null) && (i < types.length)); ++i) { 130 sb.append(types[i]).append(','); 131 } 132 133 ArrayList selectedCommands = new ArrayList(); 134 AbstractCommand command = null; 135 Iterator i = commands.iterator(); 136 while (i.hasNext()) { 137 command = (AbstractCommand) i.next(); 138 139 if (sb.indexOf(',' + command.getName() + ',') >= 0) { 140 selectedCommands.add(command); 141 } 142 } 143 144 return selectedCommands; 145 } 146 147 160 public static AbstractCommand filterCommands(AbstractCommand[] commands, 161 Class filterClass , 162 String cmd ) { 163 164 ArrayList all = filterCommands(commands, filterClass); 165 for (int i=0; i<all.size(); ++i) { 166 if (((Status)all.get(i)).getCmd().equals(cmd)) { 167 return (Status)all.get(i); 168 } 169 } 170 return null; 171 } 172 173 183 public static ArrayList inverseFilterCommands(AbstractCommand[] commands, Class filterClass) { 184 ArrayList filteredCommands = new ArrayList(); 185 186 for (int i=0; i<commands.length; ++i) { 187 if (!filterClass.isInstance(commands[i])) { 188 filteredCommands.add(commands[i]); 189 } 190 } 191 192 return filteredCommands; 193 } 194 195 206 public static Alert createAlertCommand(CmdID id , 207 boolean noResponse, 208 Cred credential, 209 Database db ) { 210 Item[] items = new Item[1]; 211 Anchor serverAnchor = db.getServerAnchor(); 212 Meta meta = new Meta(); 213 meta.setAnchor(serverAnchor); 214 items[0] = new Item(db.getTarget(), 215 db.getSource(), 216 meta , 217 null , false ); 220 return new Alert( 221 id , 222 noResponse , 223 credential , 224 db.getMethod(), 225 items 226 ); 227 } 228 229 236 public static Source target2Source(Target target) { 237 if (target == null) { 238 return null; 239 } 240 return new Source(target.getLocURI(), target.getLocName()); 241 } 242 243 250 public static Target source2Target(Source source) { 251 if (source == null) { 252 return null; 253 } 254 return new Target(source.getLocURI(), source.getLocName()); 255 } 256 257 268 public static void extractRefs(Item[] items , 269 TargetRef[] targetRefs, 270 SourceRef[] sourceRefs) { 271 if (items == null) { 272 return; 273 } 274 275 Target t = null; 276 Source s = null; 277 for (int i=0; i<items.length; ++i) { 278 t = items[i].getTarget(); 279 s = items[i].getSource(); 280 281 targetRefs[i] = (t != null) ? new TargetRef(t) : null; 282 sourceRefs[i] = (s != null) ? new SourceRef(s) : null; 283 } 284 } 285 286 299 public static boolean noMoreResponse(SyncML msg) { 300 AbstractCommand[] commands = 301 (AbstractCommand[])msg.getSyncBody().getCommands().toArray( 302 new AbstractCommand[0]); 303 304 for(int i=0; ((commands != null) && (i<commands.length)); ++i) { 305 if (!((commands[i] instanceof Status) || 306 (commands[i] instanceof sync4j.framework.core.Map))) { 307 return false; 308 } 309 } 310 311 return true; 312 } 313 314 321 public static NextNonce generateNextNonce() { 322 return new NextNonce(MD5.getNextNonce()); 323 } 324 325 336 public static int getHeaderStatusCode(SyncML msg) { 337 ArrayList cmdList = msg.getSyncBody().getCommands(); 338 339 cmdList = filterCommands( 340 (AbstractCommand[])cmdList.toArray(new AbstractCommand[cmdList.size()]), 341 Status.class, 342 new CmdID("1") 343 ); 344 345 if (cmdList.size() == 0) { 346 return -1; 347 } 348 349 return ((Status)cmdList.get(0)).getStatusCode(); 350 } 351 352 361 public static Chal getStatusChal(SyncML msg) { 362 ArrayList cmdList = msg.getSyncBody().getCommands(); 363 364 cmdList = filterCommands( 365 (AbstractCommand[])cmdList.toArray(new AbstractCommand[cmdList.size()]), 366 Status.class, 367 new CmdID("1") 368 ); 369 370 if (cmdList.size() == 0) { 371 return null; 372 } 373 374 return ((Status)cmdList.get(0)).getChal(); 375 } 376 377 384 public static DevInf getDevInf(SyncML msg) { 385 ArrayList cmdList = msg.getSyncBody().getCommands(); 386 387 cmdList = filterCommands( 388 (AbstractCommand[])cmdList.toArray(new AbstractCommand[cmdList.size()]), 389 Put.class 390 ); 391 392 if (cmdList.size() <= 0) { 393 return null; 394 } 395 396 ArrayList itemList = ((Put)cmdList.get(0)).getItems(); 397 398 if (itemList == null) { 399 return null; 400 } 401 402 DevInfItem devInfItem = (DevInfItem)itemList.get(0); 403 if (devInfItem == null) { 404 return null; 405 } 406 return devInfItem.getDevInfData().getDevInf(); 407 } 408 409 417 public static Item getLargeObject(List commands) { 418 if (commands.size() == 0) { 419 return null; 420 } 421 422 AbstractCommand command = null; 423 ArrayList items = null; 424 Item item = null; 425 426 command = (AbstractCommand)(commands.get(commands.size() -1)); 427 428 if (!(command instanceof ItemizedCommand)) { 429 return null; 430 } 431 items = ((ItemizedCommand)command).getItems(); 432 item = (Item)items.get(items.size() - 1); 433 if (item.isMoreData()) { 434 return item; 435 } 436 return null; 437 } 438 439 444 public static boolean hasLargeObject(AbstractCommand command) { 445 if (! (command instanceof ItemizedCommand)) { 446 return false; 447 } 448 ArrayList items = ((ItemizedCommand)command).getItems(); 449 Iterator iItems = items.iterator(); 450 Item item = null; 451 while (iItems.hasNext()) { 452 item = (Item)iItems.next(); 453 if (item.isMoreData()) { 454 return true; 455 } 456 } 457 return false; 458 } 459 460 463 public static Item getSyncItem(Sync[] syncs, String itemPath) { 464 String uri = null; 465 int i = 0; 466 for (i=0; i<syncs.length; ++i) { 467 uri = syncs[i].getTarget().getLocURI() + '/'; 468 if (itemPath.startsWith(uri)) { 469 break; 470 } 471 } 472 473 if (i == syncs.length) { 474 return null; 475 } 476 477 String test = null; 478 Iterator j = syncs[i].getCommands().iterator(); 479 while (j.hasNext()) { 480 Item item = (Item)((ItemizedCommand)j.next()).getItems().get(0); 481 482 test = uri 483 + ( (item.getSource() != null) 484 ? item.getSource().getLocURI() 485 : item.getTarget().getLocURI()) 486 ; 487 488 if (test.equals(itemPath)) { 489 return item; 490 } 491 } 492 493 return null; 494 } 495 496 503 public static AbstractCommand[] sortStatusCommand(AbstractCommand[] statusToSort) { 504 StatusComparator comparator = new StatusComparator(); 505 Arrays.sort(statusToSort, comparator); 506 return statusToSort; 507 } 508 509 516 public static AbstractCommand[] sortStatusCommand(List statusToSort) { 517 AbstractCommand[] array = 518 (AbstractCommand[])statusToSort.toArray(new AbstractCommand[statusToSort.size()]); 519 520 StatusComparator comparator = new StatusComparator(); 521 Arrays.sort(array, comparator); 522 return array; 523 } 524 525 539 public static boolean isInitMessage(SyncML msg) { 540 541 Iterator i = msg.getSyncBody().getCommands().iterator(); 542 while(i.hasNext()) { 543 AbstractCommand c = (AbstractCommand)i.next(); 544 545 if ((c instanceof Put) || (c instanceof Get)) { 546 return true; 547 } else if (c instanceof Alert) { 548 if (((Alert)c).getData() != AlertCode.NEXT_MESSAGE) { 552 return true; 553 } 554 } 555 } 556 557 return false; 558 } 559 560 574 public static boolean isSyncMessage(SyncML msg) { 575 Iterator i = msg.getSyncBody().getCommands().iterator(); 576 while(i.hasNext()) { 577 AbstractCommand c = (AbstractCommand)i.next(); 578 579 if ((c instanceof Sync) || 580 (c instanceof Add) || 581 (c instanceof Replace) || 582 (c instanceof Delete) ) { 583 return true; 584 } 585 586 if ((c instanceof Alert) && 587 (((Alert)c).getData() == AlertCode.NEXT_MESSAGE)) { 588 return true; 589 } 590 } 591 592 return false; 593 } 594 595 602 public static boolean isMapMessage(SyncML msg) { 603 Iterator i = msg.getSyncBody().getCommands().iterator(); 604 while(i.hasNext()) { 605 AbstractCommand c = (AbstractCommand)i.next(); 606 607 if (c instanceof sync4j.framework.core.Map) { 608 return true; 609 } 610 } 611 612 return false; 613 } 614 615 621 public static void removeHeaderStatus(List commands) { 622 boolean first = true; 623 ArrayList remove = new ArrayList(); 624 625 Iterator i = commands.iterator(); 626 while (i.hasNext()) { 627 AbstractCommand c = (AbstractCommand)i.next(); 628 if (c instanceof Status) { 629 if (SyncHdr.COMMAND_NAME.equals(((Status)c).getCmd())) { 630 if (first == true) { 631 first = false; 632 } else { 633 remove.add(c); 634 } 635 } 636 } 637 } 638 639 commands.removeAll(remove); 640 } 641 642 649 public static void mergeSyncCommands(List commands) { 650 ArrayList remove = new ArrayList(); 651 652 List syncs = ProtocolUtil.filterCommands(commands, new String [] { Sync.COMMAND_NAME }); 653 654 HashMap h = new HashMap(); 655 656 String uri = null; 657 Sync sync1 = null, sync2 = null; 658 659 Iterator i = syncs.iterator(); 660 while (i.hasNext()) { 661 sync1 = (Sync)i.next(); 662 uri = sync1.getTarget().getLocURI(); 663 sync2 = (Sync)h.get(uri); 664 if (sync2 == null) { 665 h.put(uri, sync1); 666 } else { 667 sync2.getCommands().addAll(sync1.getCommands()); 668 remove.add(sync1); 669 } 670 } 671 672 commands.removeAll(remove); 673 } 674 675 688 public static Long getLOSize(ModificationCommand cmd) { 689 Meta meta = null; 690 Long size = null; 691 692 List items = cmd.getItems(); 696 int l = items.size(); 697 698 if (l>0) { 699 meta = ((Item)items.get(l-1)).getMeta(); 700 701 if (meta != null) { 702 size = meta.getSize(); 703 } 704 705 if (size != null) { 706 return size; 707 } 708 } 709 710 meta = cmd.getMeta(); 714 715 return (meta == null) ? null : meta.getSize(); 716 } 717 718 723 public static void updateCmdId(List commandsList) { 724 updateCmdId(commandsList, 1); 725 } 726 727 735 public static int updateCmdId(List commands, int startId) { 736 Iterator iCommand = commands.iterator(); 737 AbstractCommand command = null; 738 int id = startId; 739 while (iCommand.hasNext()) { 740 command = (AbstractCommand)iCommand.next(); 741 command.setCmdID(new CmdID(id++)); 742 if (command instanceof Sync) { 743 id = updateCmdId( ( (Sync)command).getCommands(), id); 744 } else if (command instanceof Atomic) { 745 id = updateCmdId( ( (Atomic)command).getCommands(), id); 746 } else if (command instanceof Sequence) { 747 id = updateCmdId( ( (Sequence)command).getCommands(), id); 748 } 749 } 750 return id; 751 } 752 753 755 758 private static class StatusComparator implements java.util.Comparator { 759 private StatusComparator() { 760 } 761 762 public int compare(Object o1, Object o2) { 763 764 Object msgRef1 = null; 765 Object msgRef2 = null; 766 767 Object cmdRef1 = null; 768 Object cmdRef2 = null; 769 770 msgRef1 = new Integer (((Status)o1).getMsgRef()); 771 msgRef2 = new Integer (((Status)o2).getMsgRef()); 772 773 cmdRef1 = new Integer ( ( (Status)o1).getCmdRef()); 774 cmdRef2 = new Integer ( ( (Status)o2).getCmdRef()); 775 776 int msgRefCompare = ((Integer )msgRef1).compareTo((Integer )msgRef2); 777 778 if ( msgRefCompare == 0 ) { 782 return ( (Integer )cmdRef1).compareTo((Integer )cmdRef2); 783 } 784 785 return msgRefCompare; 786 } 787 788 } 789 } 790 | Popular Tags |