1 18 package sync4j.framework.protocol; 19 20 import java.util.Arrays ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Hashtable ; 25 26 import sync4j.framework.core.*; 27 import sync4j.framework.database.Database; 28 29 import sync4j.framework.protocol.SyncPackage; 30 import sync4j.framework.protocol.ProtocolUtil; 31 import sync4j.framework.protocol.v11.InitializationRequirements; 32 33 import sync4j.framework.security.Sync4jPrincipal; 34 35 55 public class SyncInitialization 56 extends SyncPackage 57 { 58 62 private Get serverCapabilitiesRequest = null; 63 64 67 private Put clientCapabilities = null; 68 69 public DevInf getClientDeviceInfo() throws ProtocolException { 70 if (clientCapabilities != null) { 71 DevInfItem item = (DevInfItem)this.clientCapabilities.getItems().get(0); 72 return item.getDevInfData().getDevInf(); 73 } 74 return null; 75 } 76 77 81 private AbstractCommand[] clientCommands = null; 82 83 public AbstractCommand[] getClientCommands() { 84 return clientCommands; 85 } 86 87 91 private Alert[] clientAlerts = null; 92 93 public Alert[] getClientAlerts() { 94 return clientAlerts; 95 } 96 97 101 private Sync[] clientSyncs = null; 102 103 106 private int clientCapabilitiesStatusCode = -1; 107 108 public int getClientCapabilitiesStatusCode() { 109 return this.clientCapabilitiesStatusCode; 110 } 111 112 public void setClientCapabilitiesStatusCode(int clientCapabilitiesStatusCode) { 113 this.clientCapabilitiesStatusCode = clientCapabilitiesStatusCode; 114 } 115 116 119 private DevInf serverCapabilities = null; 120 121 public void setServerCapabilities(DevInf capabilities) { 122 this.serverCapabilities = capabilities; 123 } 124 125 public DevInf getServerCapabilities() { 126 return this.serverCapabilities; 127 } 128 129 133 private Database[] databases = null; 134 135 public void setDatabases(Database[] databases) { 136 this.databases = databases; 137 } 138 139 public Database[] getDatabases() { 140 return this.databases; 141 } 142 143 146 private boolean clientCapabilitiesRequired = false; 147 148 public void setClientCapabilitiesRequired(boolean clientCapabilitiesRequired) { 149 this.clientCapabilitiesRequired = clientCapabilitiesRequired; 150 } 151 152 public boolean isClientCapabilitiesRequired() { 153 return this.clientCapabilitiesRequired; 154 } 155 156 159 private int authorizedStatusCode = -1; 160 161 public void setAuthorizedStatusCode(int authorizedStatusCode) { 162 this.authorizedStatusCode = authorizedStatusCode; 163 } 164 165 169 private Chal clientChal = null; 170 171 176 public Chal getClientChal() { 177 return clientChal; 178 } 179 180 183 private String clientAuth = null; 184 185 190 public void setClientAuth(String clientAuth) { 191 this.clientAuth = clientAuth; 192 } 193 194 197 private NextNonce nextNonce = null; 198 199 204 public void setNextNonce(NextNonce nextNonce) { 205 this.nextNonce = nextNonce; 206 } 207 208 212 private Cred serverCredentials; 213 214 219 public void setServerCredentials(Cred serverCredentials) { 220 this.serverCredentials = serverCredentials; 221 } 222 223 226 public Cred getServerCredentials() { 227 return serverCredentials; 228 } 229 230 232 235 private Hashtable commandStatus = new Hashtable (); 236 237 243 public void setStatusCodeForCommand(AbstractCommand cmd, int statusCode) { 244 setStatusCodeForCommand(cmd.getCmdID().getCmdID(), statusCode); 245 } 246 247 253 public void setStatusCodeForCommand(String cmdId, int statusCode) { 254 commandStatus.put(cmdId, new Integer (statusCode)); 255 } 256 257 268 public int getStatusCodeForCommand(AbstractCommand cmd, int defaultCode) { 269 String cmdId = cmd.getCmdID().getCmdID(); 270 271 return getStatusCodeForCommand(cmdId, defaultCode); 272 273 } 274 275 285 public int getStatusCodeForCommand(String cmdId, int defaultCode) { 286 Integer statusCode = (Integer )commandStatus.get(cmdId); 287 288 return (statusCode == null) ? defaultCode : statusCode.intValue(); 289 } 290 291 292 294 301 public SyncInitialization(final SyncHdr syncHeader, 302 final SyncBody syncBody ) 303 throws ProtocolException { 304 super(syncHeader, syncBody); 305 checkRequirements(); 306 } 307 308 310 320 public Database[] getDatabasesToBeSynchronized(Sync4jPrincipal principal) { 321 ArrayList dbList = new ArrayList (); 322 323 Database db = null; 324 Item[] items = null; 325 Meta meta = null; 326 for (int i=0; ((clientAlerts != null) && (i < clientAlerts.length)); ++i) { 327 if (!AlertCode.isInitializationCode(clientAlerts[i].getData())) { 331 continue; 332 } 333 334 items = (Item[])clientAlerts[i].getItems().toArray(new Item[0]); 335 for (int j=0; ((items != null) && (j<items.length)); ++j) { 336 meta = items[j].getMeta(); 337 Anchor anchor = meta.getAnchor(); 338 339 if (anchor == null) { 344 continue; 345 } 346 347 db = new Database( 351 items[j].getTarget().getLocURI() , 352 null , 353 ProtocolUtil.source2Target(items[j].getSource()), 354 ProtocolUtil.target2Source(items[j].getTarget()), 355 anchor , 356 principal 357 ); 358 db.setMethod(clientAlerts[i].getData()); 359 db.setAlertCommand(clientAlerts[i]); 360 361 dbList.add(db); 362 } } 365 int dbSize = dbList.size(); 366 Database[] dbArray = new Database[dbSize]; 367 for (int i=0; i<dbSize; i++) { 368 dbArray[i] = (Database)dbList.get(i); 369 } 370 return dbArray; 371 } 372 373 375 381 public void checkHeaderRequirements() 382 throws ProtocolException { 383 InitializationRequirements.checkDTDVersion (syncHeader.getVerDTD() ); 384 InitializationRequirements.checkProtocolVersion(syncHeader.getVerProto()); 385 InitializationRequirements.checkSessionId (syncHeader.getSessionID()); 386 InitializationRequirements.checkMessageId (syncHeader.getMsgID() ); 387 InitializationRequirements.checkTarget (syncHeader.getTarget() ); 388 InitializationRequirements.checkSource (syncHeader.getSource() ); 389 } 390 391 397 public void checkBodyRequirements() 398 throws ProtocolException { 399 ArrayList listAlerts = new ArrayList (); 400 ArrayList mergedClientCommands = new ArrayList (); 401 402 AbstractCommand[] allClientCommands = 403 (AbstractCommand[])syncBody.getCommands().toArray( 404 new AbstractCommand[0]); 405 406 ArrayList alertList = ProtocolUtil.filterCommands(allClientCommands , 410 Alert.class); 411 int size = alertList.size(); 412 Alert[] alerts = new Alert[size]; 413 for (int i=0; i < size; i++) { 414 alerts[i] = (Alert)alertList.get(i); 415 416 InitializationRequirements.checkAlertCommand(alerts[i]); 417 418 String locURI = ((Item)(alerts[i].getItems().get(0))).getTarget().getLocURI(); 423 int sizeLA = listAlerts.size(); 424 boolean isPresent = false; 425 for (int y=0; y < sizeLA; y++) { 426 String locURICached = 427 ((Item)(((Alert)listAlerts.get(y)).getItems().get(0))).getTarget().getLocURI(); 428 429 if (locURICached.equals(locURI)) { 430 isPresent = true; 431 mergedClientCommands.add(alerts[i]); 436 break; 437 } 438 } 439 if (!isPresent) { 440 listAlerts.add(alerts[i]); 441 } 442 } 443 444 clientAlerts = (Alert[])listAlerts.toArray(new Alert[0]); 448 mergedClientCommands.addAll(listAlerts); 449 450 ArrayList clientCapabilitiesList = 451 ProtocolUtil.filterCommands(allClientCommands, Put.class); 452 453 if ((clientCapabilities == null) && (clientCapabilitiesList.size()>0)) { 454 InitializationRequirements.checkCapabilities((Put)clientCapabilitiesList.get(0) , 455 InitializationRequirements.CLIENT_CAPABILITIES); 456 clientCapabilities = (Put)clientCapabilitiesList.get(0); 457 } 458 mergedClientCommands.addAll(clientCapabilitiesList); 459 460 ArrayList capabilitiesRequest = 461 ProtocolUtil.filterCommands(allClientCommands, Get.class); 462 463 if ((capabilitiesRequest != null) && (capabilitiesRequest.size()>0)) { 464 InitializationRequirements.checkCapabilitiesRequest((Get)capabilitiesRequest.get(0)); 465 serverCapabilitiesRequest = (Get)capabilitiesRequest.get(0); 466 } 467 mergedClientCommands.addAll(capabilitiesRequest); 468 469 ArrayList listSync = ProtocolUtil.filterCommands(allClientCommands, Sync.class); 473 clientSyncs = (Sync[])listSync.toArray(new Sync[0]); 474 475 clientCommands = 476 (AbstractCommand[])mergedClientCommands.toArray(new AbstractCommand[0]); 477 478 } 479 480 481 494 public SyncML getResponseMessage(String msgId) throws ProtocolException { 495 SyncHdr responseHeader = getResponseHeader(msgId); 496 AbstractCommand[] commands = 497 (AbstractCommand[]) getResponseCommands(msgId).toArray(new AbstractCommand[0]); 498 SyncBody responseBody = new SyncBody( 499 commands, 500 isFlag(Flags.FLAG_FINAL_MESSAGE) 501 ); 502 503 try { 504 return new SyncML(responseHeader, responseBody); 505 } catch (RepresentationException e) { 506 throw new ProtocolException("Unexpected error", e); 510 } 511 } 512 513 523 public List getResponseCommands(String msgId) 524 throws ProtocolException { 525 ArrayList statusList = new ArrayList (); 526 ArrayList commandList = new ArrayList (); 527 528 536 if (syncHeader.isNoResp() == false) { 537 TargetRef[] targetRefs = new TargetRef[] { new TargetRef(syncHeader.getTarget().getLocURI()) }; 541 SourceRef[] sourceRefs = new SourceRef[] { new SourceRef(syncHeader.getSource().getLocURI()) }; 542 543 Chal chal = null; 547 if (authorizedStatusCode != StatusCode.AUTHENTICATION_ACCEPTED) { 548 if (clientAuth.equalsIgnoreCase(Cred.AUTH_TYPE_BASIC)) { 549 chal = Chal.getBasicChal(); 550 } 551 } 552 553 if (clientAuth.equalsIgnoreCase(Cred.AUTH_TYPE_MD5)) { 557 chal = Chal.getMD5Chal(); 558 chal.setNextNonce(nextNonce); 559 } 560 Status statusCommand = new Status( 561 idGenerator.next() , 562 syncHeader.getMsgID() , 563 "0" , 564 "SyncHdr" , 565 targetRefs , 566 sourceRefs , 567 null , 568 chal , 569 new Data(String.valueOf(authorizedStatusCode)), 570 new Item[0] 571 ); 572 573 statusList.add(statusCommand); 574 575 for (int i=0; ((clientCommands != null) && (i < clientCommands.length)); ++i) { 580 if (clientCommands[i].isNoResp()) { 581 continue; 582 } 583 584 targetRefs = null; 585 sourceRefs = null; 586 if (clientCommands[i] instanceof ItemizedCommand) { 587 Item[] items = (Item[])((ItemizedCommand)clientCommands[i]).getItems().toArray(new Item[0]); 588 589 ArrayList trefs = new ArrayList (); 590 ArrayList srefs = new ArrayList (); 591 Target t; 592 Source s; 593 for (int j=0; j<items.length; ++j) { 594 t = items[j].getTarget(); 595 s = items[j].getSource(); 596 597 if (t != null) { 598 trefs.add(new TargetRef(t)); 599 } 600 if (s != null) { 601 srefs.add(new SourceRef(s)); 602 } 603 } 605 if (trefs.size() > 0) { 606 targetRefs = (TargetRef[])trefs.toArray(new TargetRef[trefs.size()]); 607 } 608 if (srefs.size() > 0) { 609 sourceRefs = (SourceRef[])srefs.toArray(new SourceRef[srefs.size()]); 610 } 611 612 } 613 614 String commandReference = clientCommands[i].getCmdID().getCmdID(); 615 int status = getStatusCodeForCommand(clientCommands[i], StatusCode.OK); 616 617 Item[] items = new Item[0]; 618 619 if (clientCommands[i] instanceof Alert) { 626 for(int j=0; (databases != null) && (j<databases.length) ; ++j) { 627 if((databases[j].getSource().getLocURI()).equals(targetRefs[0].getValue())){ 628 items = new Item[1]; 629 630 Anchor alertAnchor = 631 new Anchor(null, databases[j].getNext()); 632 633 ComplexData data = new ComplexData(); 634 data.setAnchor(alertAnchor); 635 636 items[0] = new Item( 637 null, null, null, data, 641 false ); 643 644 break; 645 } 646 } 647 } 648 649 statusCommand = new Status( 650 idGenerator.next() , 651 syncHeader.getMsgID() , 652 commandReference , 653 clientCommands[i].getName() , 654 targetRefs , 655 sourceRefs , 656 null , 657 null , 658 new Data(status) , 659 items 660 ); 661 662 statusList.add(statusCommand); 663 } 665 if (authorizedStatusCode != StatusCode.AUTHENTICATION_ACCEPTED) { 670 if (clientSyncs != null && clientSyncs.length > 0) { 671 for (int y=0; y<clientSyncs.length; y++) { 672 Sync sync = (Sync)clientSyncs[y]; 673 ArrayList al = sync.getCommands(); 674 675 String cmdRef = clientSyncs[y].getCmdID().getCmdID(); 676 TargetRef[] tRefs = null; 677 if (clientSyncs[y].getTarget() != null) { 678 tRefs = new TargetRef[] { new TargetRef(clientSyncs[y].getTarget().getLocURI()) }; 679 } 680 681 SourceRef[] sRefs = null; 682 if (clientSyncs[y].getSource() != null) { 683 sRefs = new SourceRef[] { new SourceRef(clientSyncs[y].getSource().getLocURI()) }; 684 } 685 686 statusCommand = new Status( 687 idGenerator.next() , 688 syncHeader.getMsgID() , 689 cmdRef , 690 clientSyncs[y].getName() , 691 tRefs , 692 sRefs , 693 null , 694 null , 695 new Data(authorizedStatusCode) , 696 new Item[0] 697 ); 698 699 statusList.add(statusCommand); 700 701 if (al != null) { 702 AbstractCommand[] absCmd = (AbstractCommand[])sync.getCommands().toArray(new AbstractCommand[0]); 703 704 for (int z=0; absCmd != null && z<absCmd.length; z++) { 705 cmdRef = absCmd[z].getCmdID().getCmdID(); 706 707 statusCommand = new Status( 708 idGenerator.next() , 709 syncHeader.getMsgID() , 710 cmdRef , 711 absCmd[z].getName() , 712 tRefs , 713 sRefs , 714 null , 715 null , 716 new Data(authorizedStatusCode), 717 new Item[0] 718 ); 719 720 statusList.add(statusCommand); 721 } 722 } 723 } 724 } 725 } 726 } 728 AbstractCommand[] cmds = (AbstractCommand[])statusList.toArray(new AbstractCommand[0]); 732 ProtocolUtil.sortStatusCommand(cmds); 733 commandList.addAll(Arrays.asList(cmds)); 734 735 if ((serverCapabilitiesRequest != null) && (authorizedStatusCode == StatusCode.AUTHENTICATION_ACCEPTED)) { 739 if (serverCapabilities == null) { 740 throw new ProtocolException("Error in creating a response: server capabilities not set (use setServerCapabilities())"); 741 } 742 743 String commandReference = 744 serverCapabilitiesRequest.getCmdID().getCmdID(); 745 746 Meta meta = serverCapabilitiesRequest.getMeta(); 747 if (meta == null) { 748 meta = new Meta(); 749 meta.setType(Constants.MIMETYPE_SYNCML_DEVICEINFO_XML); 750 } 751 752 ComplexData data = new ComplexData(); 753 data.setDevInf(serverCapabilities); 754 755 Source source = ProtocolUtil.target2Source( 756 ((Item)(serverCapabilitiesRequest.getItems().get(0))).getTarget() 757 ); 758 Item[] capabilities = new Item[] { new Item(null, source, null, data, false) }; 759 760 Results resultsCommand = new Results( 761 idGenerator.next() , 762 syncHeader.getMsgID(), 763 commandReference , 764 meta , 765 null , 766 null , 767 capabilities 768 ); 769 commandList.add(resultsCommand); 770 } 771 772 for (int i=0; (databases != null) && 776 (i<databases.length); ++i ) { 777 778 if (databases[i].isOkStatusCode()) { 779 780 Alert alertCommand = 781 ProtocolUtil.createAlertCommand(idGenerator.next(), 782 false , 783 null , 784 databases[i] ); 785 Item item = 786 (Item)databases[i].getAlertCommand().getItems().get(0); 787 Long maxObjSize = item.getMeta().getMaxObjSize(); 788 ((Item)alertCommand.getItems().get(0)).getMeta().setMaxObjSize(maxObjSize); 789 790 commandList.add(alertCommand); 791 } 792 } 793 794 if (clientCapabilitiesRequired && (clientCapabilities == null)) { 799 Meta meta = new Meta(); 800 meta.setType(Constants.MIMETYPE_SYNCML_DEVICEINFO_XML); 801 802 Target target = new Target( InitializationRequirements.CAPABILITIES_TARGET, 803 InitializationRequirements.CAPABILITIES_TARGET); 804 Item[] items = new Item[1]; 805 806 items[0] = new Item( 807 target, 808 null , 809 null , 810 null , 811 false 812 ); 813 Get getCommand = new Get( 814 idGenerator.next() , 815 false , 816 null , 817 null , 818 meta , 819 items 820 ); 821 commandList.add(getCommand); 822 } 823 824 Iterator i = commandList.iterator(); 825 while (i.hasNext()) { 826 AbstractCommand c = (AbstractCommand)i.next(); 827 if (authorizedStatusCode != StatusCode.AUTHENTICATION_ACCEPTED) { 828 if (c instanceof Status) { 829 ((Status)c).setData(new Data(String.valueOf(authorizedStatusCode))); 830 } 831 } 832 } 833 834 return commandList; 835 } 836 837 847 public SyncHdr getResponseHeader(String msgId) 848 throws ProtocolException { 849 Target target = new Target(syncHeader.getSource().getLocURI(), 853 syncHeader.getSource().getLocName()); 854 Source source = new Source(syncHeader.getTarget().getLocURI(), 855 syncHeader.getTarget().getLocName()); 856 return new SyncHdr ( 857 getDTDVersion() , 858 getProtocolVersion() , 859 syncHeader.getSessionID(), 860 msgId , 861 target , 862 source , 863 null , 864 false , 865 serverCredentials , 866 null 867 ); 868 } 869 } | Popular Tags |