1 18 19 package sync4j.framework.protocol; 20 21 import java.util.List ; 22 import java.util.ArrayList ; 23 24 import sync4j.framework.core.*; 25 import sync4j.framework.database.Database; 26 27 import sync4j.framework.protocol.ProtocolUtil; 28 import sync4j.framework.protocol.v11.ManagementActionsRequirements; 29 import sync4j.framework.protocol.v11.Errors; 30 31 44 public class ManagementActions extends SyncPackage implements Flags, Errors { 45 46 50 private Cred serverCredentials; 51 52 57 public void setServerCredentials(Cred serverCredentials) { 58 this.serverCredentials = serverCredentials; 59 } 60 61 64 public Cred getServerCredentials() { 65 return serverCredentials; 66 } 67 68 69 71 80 public ManagementActions(final SyncHdr syncHeader, final SyncBody syncBody) 81 throws ProtocolException { 82 super(syncHeader, syncBody); 83 checkRequirements(); 84 85 checkSessionAbortRequired(syncBody); 86 } 87 88 90 94 private Alert sessionAbortAlert = null; 95 96 public boolean isSessionAbortRequired() { 97 return !(sessionAbortAlert == null); 98 } 99 100 103 private int authorizedStatusCode = StatusCode.OK; 104 105 public void setAuthorizedStatusCode(int authorizedStatusCode) { 106 this.authorizedStatusCode = authorizedStatusCode; 107 } 108 109 112 private String mimeType = null; 113 114 117 public void setMimeType(String mimeType) { 118 this.mimeType = mimeType; 119 } 120 121 124 private String clientAuthType = null; 125 126 131 public void setClientAuthType(String authType) { 132 this.clientAuthType = authType; 133 } 134 135 136 139 private NextNonce nextNonce = null; 140 141 146 public void setNextNonce(NextNonce nextNonce) { 147 this.nextNonce = nextNonce; 148 } 149 150 153 private AbstractCommand[] managementCommands = null; 154 155 160 public AbstractCommand[] getManagementCommands() { 161 return this.managementCommands; 162 } 163 164 169 public void setManagementCommands(AbstractCommand[] managementCommands) { 170 this.managementCommands = managementCommands; 171 } 172 173 177 private AbstractCommand[] clientCommands = null; 178 179 184 public AbstractCommand[] getClientCommands() { 185 return clientCommands; 186 } 187 188 193 public void setClientCommands(AbstractCommand[] commands) { 194 this.clientCommands = commands; 195 } 196 197 198 200 206 public void checkHeaderRequirements() throws ProtocolException { 207 ManagementActionsRequirements.checkDTDVersion (syncHeader.getVerDTD() ); 208 ManagementActionsRequirements.checkProtocolVersion(syncHeader.getVerProto() ); 209 ManagementActionsRequirements.checkSessionId (syncHeader.getSessionID()); 210 ManagementActionsRequirements.checkMessageId (syncHeader.getMsgID() ); 211 ManagementActionsRequirements.checkTarget (syncHeader.getTarget() ); 212 ManagementActionsRequirements.checkSource (syncHeader.getSource() ); 213 } 214 215 221 public void checkBodyRequirements() throws ProtocolException { 222 clientCommands = (AbstractCommand[])syncBody.getCommands().toArray(new AbstractCommand[0]); 224 225 List results = ProtocolUtil.filterCommands(clientCommands, Results.class); 229 230 if (results.size() > 0) { 231 ManagementActionsRequirements.checkResults(results); 232 } 233 } 234 235 240 public Results[] getResults() { 241 ArrayList ret = 242 ProtocolUtil.filterCommands(clientCommands, Results.class); 243 244 return (Results[])ret.toArray(new Results[ret.size()]); 245 } 246 247 249 264 public SyncML getResponse(String msgId) throws ProtocolException { 265 ArrayList responseCommandList = new ArrayList (); 266 267 if (idGenerator == null) { 268 throw new NullPointerException ("The id generator is null. Please set a value for idGenerator"); 269 } 270 271 if (syncHeader.isNoResp() == false) { 279 280 TargetRef[] targetRefs = new TargetRef[] { new TargetRef(syncHeader.getTarget().getLocURI()) }; 281 SourceRef[] sourceRefs = new SourceRef[] { new SourceRef(syncHeader.getSource().getLocURI()) }; 282 283 284 Chal chal = null; 288 289 if (Constants.AUTH_TYPE_HMAC.equalsIgnoreCase(clientAuthType)) { 290 chal = Chal.getHMACChal(); 294 chal.setNextNonce(nextNonce); 295 } 296 297 Status statusCommand = new Status( 298 idGenerator.next() , 299 syncHeader.getMsgID() , 300 "0" , 301 "SyncHdr" , 302 targetRefs , 303 sourceRefs , 304 null , 305 chal , 306 new Data(authorizedStatusCode) , 307 new Item[0] 308 ); 309 310 responseCommandList.add(statusCommand); 311 } 312 313 if (isFlag(Flags.FLAG_SERVER_ABORT_REQUIRED)) { 314 315 Alert abortAlert = new Alert(idGenerator.next(), 319 false, 320 null, 321 AlertCode.SESSION_ABORT, 322 new Item[0]); 323 324 responseCommandList.add(abortAlert); 325 326 } else { 327 328 int numClientCommand = clientCommands.length; 332 AbstractCommand command = null; 333 TargetRef[] targetRefs = null; 334 SourceRef[] sourceRefs = null; 335 Status statusCommand = null; 336 boolean hasLargeObject = false; 337 Long itemSize = null; 338 for (int i = 0; i < numClientCommand; i++) { 339 340 command = clientCommands[i]; 341 342 if (command.isNoResp() || (command instanceof Status)) { 343 continue; 344 } 345 346 int status = StatusCode.OK; 347 hasLargeObject = ProtocolUtil.hasLargeObject(command); 348 349 if (command instanceof Results) { 359 ArrayList itemsList = ( (Results)command).getItems(); 360 if (itemsList.size() == 1) { 361 itemSize = Util.getItemSize( (Item) (itemsList.get(0))); 362 } 363 364 if (!hasLargeObject) { 365 if (itemSize != null) { 366 Long realSize = Util.getRealItemSize( (Item) (itemsList.get(0)), mimeType); 367 if (itemSize.equals(realSize)) { 368 continue; 370 } else { 371 status = StatusCode.OBJECT_SIZE_MISMATCH; 372 } 373 } else { 374 continue; 375 } 376 } else { 377 if (itemSize == null) { 378 status = StatusCode.INCOMPLETE_COMMAND; 381 } else { 382 status = StatusCode.CHUNKED_ITEM_ACCEPTED; 383 } 384 } 385 } 386 387 targetRefs = null; 393 sourceRefs = null; 394 395 String commandReference = command.getCmdID().getCmdID(); 396 397 Item[] items = new Item[0]; 398 399 statusCommand = new Status( 400 idGenerator.next(), 401 syncHeader.getMsgID(), 402 commandReference, 403 command.getName(), 404 targetRefs, 405 sourceRefs, 406 null 407 408 , 409 null 410 411 , 412 new Data(status), 413 items 414 ); 415 416 responseCommandList.add(statusCommand); 417 418 } 420 Status statusForSessionAbort = null; 421 422 if (sessionAbortAlert != null) { 423 424 TargetRef targerRef = null; 425 SourceRef sourceRef = null; 426 statusForSessionAbort = new Status( 427 idGenerator.next(), 428 syncHeader.getMsgID(), 429 sessionAbortAlert.getCmdID().getCmdID(), 430 "Alert", 431 targerRef, 432 sourceRef, 433 null, null, new Data(StatusCode.OK), 436 new Item[0] 437 ); 438 439 responseCommandList.add(statusForSessionAbort); 440 } else { 441 if (!isFlag(FLAG_MORE_MSG_REQUIRED)) { 442 for (int i = 0; ( (managementCommands != null) && (i < managementCommands.length)); 446 ++i) { 447 responseCommandList.add(managementCommands[i]); 448 } 449 } else { 450 Alert moreMsg = new Alert( 455 idGenerator.next(), false, null, AlertCode.MORE_DATA, null ); 461 responseCommandList.add(moreMsg); 462 unsetFlag(FLAG_FINAL_MESSAGE); 463 } 464 } 465 } 466 467 Target target = new Target(syncHeader.getSource().getLocURI(), 471 syncHeader.getSource().getLocName()); 472 Source source = new Source(syncHeader.getTarget().getLocURI(), 473 syncHeader.getTarget().getLocName()); 474 475 SyncHdr responseHeader = new SyncHdr ( 476 getDTDVersion() , 477 getProtocolVersion() , 478 syncHeader.getSessionID() , 479 msgId , 480 target , 481 source , 482 null , 483 false , 484 serverCredentials , 485 null 486 ); 487 488 AbstractCommand[] commands = null; 489 int numCommand = responseCommandList.size(); 490 if (numCommand == 0) { 491 commands = new AbstractCommand[1]; 492 } else { 493 commands = new AbstractCommand[numCommand]; 494 } 495 for (int i=0; i < numCommand; i++) { 496 commands[i] = (AbstractCommand)responseCommandList.get(i); 497 } 498 499 SyncBody responseBody = new SyncBody( 500 commands, 501 isFlag(FLAG_FINAL_MESSAGE) 502 ); 503 504 try { 505 return new SyncML(responseHeader, responseBody); 506 } catch (RepresentationException e) { 507 throw new ProtocolException("Unexpected error", e); 511 } 512 } 513 514 520 private void checkSessionAbortRequired(SyncBody syncBody) { 521 522 AbstractCommand[] allClientCommands = 523 (AbstractCommand[])syncBody.getCommands().toArray( 524 new AbstractCommand[0]); 525 526 527 ArrayList alertList = ProtocolUtil.filterCommands(allClientCommands, 531 Alert.class); 532 int size = alertList.size(); 533 Alert[] alerts = new Alert[size]; 534 for (int i = 0; i < size; i++) { 535 alerts[i] = (Alert)alertList.get(i); 536 537 if (alerts[i].getData() == AlertCode.SESSION_ABORT) { 538 sessionAbortAlert = alerts[i]; 540 return ; 541 } 542 } 543 sessionAbortAlert = null; 544 } 545 546 } | Popular Tags |