1 18 19 package sync4j.framework.protocol; 20 21 import java.util.ArrayList ; 22 import java.util.Hashtable ; 23 24 import sync4j.framework.core.*; 25 26 import sync4j.framework.protocol.SyncPackage; 27 import sync4j.framework.protocol.ProtocolUtil; 28 import sync4j.framework.protocol.v11.Errors; 29 import sync4j.framework.protocol.v11.InitializationRequirements; 30 31 import sync4j.framework.core.dm.ddf.DevInfo; 32 import java.util.Collections ; 33 import java.util.Arrays ; 34 import java.util.List ; 35 import java.util.Iterator ; 36 import sync4j.framework.tools.ArrayUtils; 37 38 39 58 61 public class ManagementInitialization 62 extends SyncPackage 63 implements Errors { 64 65 67 71 private AbstractCommand[] clientCommands = null; 72 73 public AbstractCommand[] getClientCommands() { 74 return clientCommands; 75 } 76 77 81 public void addClientCommand(AbstractCommand[] command) { 82 Object [] obj = (AbstractCommand[])ArrayUtils.mergeArrays(clientCommands, command, 83 AbstractCommand.class); 84 clientCommands = (AbstractCommand[])obj; 85 } 86 87 91 private Alert clientAlert = null; 92 93 public Alert getClientAlert() { 94 return clientAlert; 95 } 96 97 98 101 private int authorizedStatusCode = -1; 102 103 public void setAuthorizedStatusCode(int authorizedStatusCode) { 104 this.authorizedStatusCode = authorizedStatusCode; 105 } 106 107 110 private DevInfo devInfo = null; 111 112 117 public DevInfo getDevInfo() { 118 return devInfo; 119 } 120 121 124 AbstractCommand[] managementCommands; 125 126 131 public void setManagementCommands(AbstractCommand[] commands) { 132 managementCommands = commands; 133 } 134 135 public AbstractCommand[] getManagementCommands() { 136 return managementCommands; 137 } 138 139 143 private Chal clientChal = null; 144 145 150 public Chal getClientChal() { 151 return clientChal; 152 } 153 154 158 private Cred serverCredentials; 159 160 165 public void setServerCredentials(Cred serverCredentials) { 166 this.serverCredentials = serverCredentials; 167 } 168 169 172 public Cred getServerCredentials() { 173 return serverCredentials; 174 } 175 176 179 private String serverAuthType = null; 180 181 186 public void setServerAuthType(String authType) { 187 this.serverAuthType = authType; 188 } 189 190 193 private String supportedAuthType = null; 194 195 196 public void setSupportedAuthType(String supportedAuthType) { 197 this.supportedAuthType = supportedAuthType; 198 } 199 200 201 204 private String clientAuthType = null; 205 206 211 public void setClientAuthType(String authType) { 212 this.clientAuthType = authType; 213 } 214 215 216 219 private NextNonce nextNonce = null; 220 221 226 public void setNextNonce(NextNonce nextNonce) { 227 this.nextNonce = nextNonce; 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 ManagementInitialization(final SyncHdr syncHeader, 302 final SyncBody syncBody ) 303 throws ProtocolException { 304 super(syncHeader, syncBody); 305 checkRequirements(); 306 307 } 308 309 311 317 public void checkHeaderRequirements() 318 throws ProtocolException { 319 InitializationRequirements.checkDTDVersion (syncHeader.getVerDTD() ); 320 InitializationRequirements.checkProtocolVersion(syncHeader.getVerProto()); 321 InitializationRequirements.checkSessionId (syncHeader.getSessionID()); 322 InitializationRequirements.checkMessageId (syncHeader.getMsgID() ); 323 InitializationRequirements.checkTarget (syncHeader.getTarget() ); 324 InitializationRequirements.checkSource (syncHeader.getSource() ); 325 } 326 327 333 public void checkBodyRequirements() 334 throws ProtocolException { 335 336 AbstractCommand[] allClientCommands = 337 (AbstractCommand[])syncBody.getCommands().toArray( 338 new AbstractCommand[0]); 339 340 ArrayList alertList = ProtocolUtil.filterCommands(allClientCommands, 344 Alert.class); 345 346 int size = alertList.size(); 347 348 for (int i = 0; i < size; i++) { 351 clientAlert = (Alert)alertList.get(i); 352 353 if (clientAlert.getData() == AlertCode.SESSION_ABORT) { 354 setFlag(FLAG_SESSION_ABORT_REQUIRED); 356 break; 357 } else { 358 359 } 360 } 361 362 367 if (isFinal()) { 373 ArrayList replaceCommands = 374 ProtocolUtil.filterCommands(allClientCommands, Replace.class); 375 376 if (replaceCommands.size() == 0) { 377 throw new ProtocolException(ERRMSG_MISSING_REPLACE); 378 } 379 InitializationRequirements.checkDeviceInfo( (Replace)replaceCommands.get(0)); 380 381 devInfo = ProtocolUtil.devInfoFromReplace( (Replace)replaceCommands.get(0)); 385 } 386 387 clientCommands = allClientCommands; 388 } 389 390 391 392 405 public SyncML getResponse(String msgId) throws ProtocolException { 406 ArrayList responseCommandList = new ArrayList (); 407 408 ArrayList clientCommandList = new ArrayList (Arrays.asList(clientCommands)); 409 410 418 if (syncHeader.isNoResp() == false) { 419 TargetRef[] targetRefs = new TargetRef[] { new TargetRef(syncHeader.getTarget().getLocURI()) }; 423 SourceRef[] sourceRefs = new SourceRef[] { new SourceRef(syncHeader.getSource().getLocURI()) }; 424 425 Chal chal = null; 429 430 if (!isFlag(Flags.FLAG_CHAL_NOT_REQUIRED)) { 431 432 switch (authorizedStatusCode) { 433 case StatusCode.OK: 434 435 if (clientAuthType.equalsIgnoreCase(Constants.AUTH_TYPE_HMAC)) { 440 chal = Chal.getHMACChal(); 441 chal.setNextNonce(nextNonce); 442 } 443 444 break; 445 446 case StatusCode.AUTHENTICATION_ACCEPTED: 447 448 if (clientAuthType.equalsIgnoreCase(Constants.AUTH_TYPE_MD5)) { 452 chal = Chal.getMD5Chal(); 453 chal.setNextNonce(nextNonce); 454 } else if (clientAuthType.equalsIgnoreCase(Constants.AUTH_TYPE_HMAC)) { 455 chal = Chal.getHMACChal(); 456 chal.setNextNonce(nextNonce); 457 } 458 459 break; 460 case StatusCode.INVALID_CREDENTIALS: 461 462 if (clientAuthType != null && supportedAuthType.indexOf(clientAuthType) != -1) { 463 if (clientAuthType.equalsIgnoreCase(Constants.AUTH_TYPE_BASIC)) { 464 chal = Chal.getBasicChal(); 465 } else if (clientAuthType.equalsIgnoreCase(Constants.AUTH_TYPE_CLEAR)) { 466 chal = Chal.getClearChal(); 467 } else if (clientAuthType.equalsIgnoreCase(Constants.AUTH_TYPE_MD5)) { 468 chal = Chal.getMD5Chal(); 469 chal.setNextNonce(nextNonce); 470 } else if (clientAuthType.equalsIgnoreCase(Constants.AUTH_TYPE_HMAC)) { 471 chal = Chal.getHMACChal(); 472 chal.setNextNonce(nextNonce); 473 } 474 475 break; 476 } 477 478 case StatusCode.MISSING_CREDENTIALS: 479 480 if (serverAuthType.equalsIgnoreCase(Constants.AUTH_TYPE_BASIC)) { 481 chal = Chal.getBasicChal(); 482 } else if (serverAuthType.equalsIgnoreCase(Constants.AUTH_TYPE_CLEAR)) { 483 chal = Chal.getClearChal(); 484 } else if (serverAuthType.equalsIgnoreCase(Constants.AUTH_TYPE_MD5)) { 485 chal = Chal.getMD5Chal(); 486 chal.setNextNonce(nextNonce); 487 } else if (serverAuthType.equalsIgnoreCase(Constants.AUTH_TYPE_HMAC)) { 488 chal = Chal.getHMACChal(); 489 chal.setNextNonce(nextNonce); 490 } 491 492 break; 493 494 default: 495 496 } 497 } 498 499 Status statusCommand = new Status( 500 idGenerator.next() , 501 syncHeader.getMsgID() , 502 "0" , 503 "SyncHdr" , 504 targetRefs , 505 sourceRefs , 506 null , 507 chal , 508 new Data(String.valueOf(authorizedStatusCode)), 509 new Item[0] 510 ); 511 512 responseCommandList.add(statusCommand); 513 514 int numClientCommand = clientCommandList.size(); 519 520 AbstractCommand command = null; 521 522 for (int i=0; i<numClientCommand; i++) { 523 524 command = (AbstractCommand)clientCommandList.get(i); 525 526 if (command.isNoResp() || (command instanceof Status)) { 527 continue; 528 } 529 530 targetRefs = null; 536 sourceRefs = null; 537 538 String commandReference = command.getCmdID().getCmdID(); 539 int status = getStatusCodeForCommand(command, StatusCode.OK); 540 541 Item[] items = new Item[0]; 542 543 statusCommand = new Status( 544 idGenerator.next() , 545 syncHeader.getMsgID() , 546 commandReference , 547 command.getName() , 548 targetRefs , 549 sourceRefs , 550 null , 551 null , 552 new Data(status) , 553 items 554 ); 555 556 responseCommandList.add(statusCommand); 557 558 } } 561 562 clientCommandList.removeAll(clientCommandList); 565 566 567 clientCommands = (AbstractCommand[])clientCommandList.toArray(new AbstractCommand[] {}); 569 570 if (!isFlag(FLAG_SESSION_ABORT_REQUIRED)) { 574 if (managementCommands != null) { 575 for (int i = 0; i < managementCommands.length; ++i) { 576 responseCommandList.add(managementCommands[i]); 577 } 578 } 579 } 580 581 582 Target target = new Target(syncHeader.getSource().getLocURI(), 586 syncHeader.getSource().getLocName()); 587 Source source = new Source(syncHeader.getTarget().getLocURI(), 588 syncHeader.getTarget().getLocName()); 589 SyncHdr responseHeader = new SyncHdr ( 590 getDTDVersion() , 591 getProtocolVersion() , 592 syncHeader.getSessionID(), 593 msgId , 594 target , 595 source , 596 null , 597 false , 598 serverCredentials , 599 syncHeader.getMeta() 600 ); 601 602 int size = responseCommandList.size(); 603 AbstractCommand [] aCommands = new AbstractCommand[size]; 604 for (int i=0; i < size; i++) { 605 aCommands[i] = (AbstractCommand)responseCommandList.get(i); 606 607 if (authorizedStatusCode != StatusCode.AUTHENTICATION_ACCEPTED) { 608 if (aCommands[i] instanceof Status) { 609 Status sc = (Status)aCommands[i]; 610 sc.setData(new Data(String.valueOf(authorizedStatusCode))); 611 } 612 } 613 614 } 615 616 SyncBody responseBody = new SyncBody( 617 aCommands, 618 isFlag(Flags.FLAG_FINAL_MESSAGE) 619 ); 620 621 try { 622 return new SyncML(responseHeader, responseBody); 623 } catch (RepresentationException e) { 624 throw new ProtocolException("Unexpected error", e); 628 } 629 } 630 631 632 public void setRequest(SyncML request) { 633 this.syncHeader = request.getSyncHdr(); 634 this.syncBody = request.getSyncBody(); 635 } 636 637 638 639 643 public boolean isSessionAbortRequired() { 644 return isFlag(Flags.FLAG_SESSION_ABORT_REQUIRED); 645 } 646 647 649 650 } | Popular Tags |