1 18 package sync4j.framework.protocol; 19 20 import sync4j.framework.core.*; 21 import sync4j.framework.protocol.v11.BasicRequirements; 22 import sync4j.framework.protocol.ProtocolUtil; 23 import sync4j.framework.logging.Sync4jLogger; 24 25 import java.util.List ; 26 import java.util.ArrayList ; 27 import java.util.logging.Logger ; 28 29 39 public class ClientCompletion extends SyncPackage { 40 41 44 private transient Logger mLog = Sync4jLogger.getLogger(); 45 46 50 private AbstractCommand[] clientCommands = null; 51 52 56 public AbstractCommand[] getClientCommands() { 57 return clientCommands; 58 } 59 60 64 private Map[] mapCommands = null; 65 66 69 public Map[] getMapCommands() { 70 return mapCommands; 71 } 72 73 76 private boolean mapCommandFind = false; 77 78 81 public boolean isMapCommandFind() { 82 return mapCommandFind; 83 } 84 85 88 private String lastMessageId = null; 89 90 96 public ClientCompletion(final SyncHdr syncHeader, final SyncBody syncBody) 97 throws ProtocolException { 98 super(syncHeader, syncBody); 99 checkHeaderRequirements(); 100 checkBodyRequirements(); 101 } 102 103 109 public void checkHeaderRequirements() throws ProtocolException { 110 BasicRequirements.checkDTDVersion(syncHeader.getVerDTD()); 111 BasicRequirements.checkProtocolVersion(syncHeader.getVerProto()); 112 BasicRequirements.checkSessionId(syncHeader.getSessionID()); 113 BasicRequirements.checkMessageId(syncHeader.getMsgID()); 114 BasicRequirements.checkTarget(syncHeader.getTarget()); 115 BasicRequirements.checkSource(syncHeader.getSource()); 116 117 lastMessageId = syncHeader.getMsgID(); 119 } 120 121 127 public void checkBodyRequirements() throws ProtocolException { 128 clientCommands = (AbstractCommand[])syncBody.getCommands().toArray(new AbstractCommand[0]); 129 130 checkStatusCommands(); 132 133 checkMapCommand(); 135 } 136 137 143 private void checkStatusCommands() { 144 } 145 146 150 private void checkMapCommand() throws ProtocolException { 151 List list = ProtocolUtil.filterCommands(clientCommands, Map.class); 152 153 if (list.size() > 0) { 154 mapCommands = (Map[]) list.toArray(new Map[list.size()]); 155 mapCommandFind = true; 156 } else { 157 mapCommands = null; 158 mapCommandFind = false; 159 } 160 } 161 162 174 public SyncML getResponse(String msgId) throws ProtocolException { 175 ArrayList commands = new ArrayList (); 176 177 188 Target target = new Target(syncHeader.getSource().getLocURI(), 189 syncHeader.getSource().getLocName()); 190 Source source = new Source(syncHeader.getTarget().getLocURI(), 191 syncHeader.getTarget().getLocName()); 192 SyncHdr responseHeader = new SyncHdr( 193 getDTDVersion() , 194 getProtocolVersion() , 195 syncHeader.getSessionID() , 196 msgId , 197 target , 198 source , 199 null , 200 false , 201 null , 202 null 203 ); 204 205 209 TargetRef[] targetRefs = new TargetRef[] { new TargetRef(syncHeader.getTarget().getLocURI()) }; 213 SourceRef[] sourceRefs = new SourceRef[] { new SourceRef(syncHeader.getSource().getLocURI()) }; 214 215 Status synchdrStatus = new Status( 216 idGenerator.next(), 217 lastMessageId, 218 "0", 219 "SyncHdr", 220 targetRefs, 221 sourceRefs, 222 null, 223 null, 224 new Data(StatusCode.OK), 225 new Item[0] 226 ); 227 commands.add(synchdrStatus); 228 229 if (mapCommandFind) { 233 235 Status mapStatus = null; 236 for (int i=0; i<mapCommands.length; ++i) { 237 238 targetRefs = new TargetRef[] { new TargetRef(mapCommands[i].getTarget()) }; 239 sourceRefs = new SourceRef[] { new SourceRef(mapCommands[i].getSource()) }; 240 241 mapStatus = new Status( 242 idGenerator.next(), 243 lastMessageId, 244 mapCommands[i].getCmdID().getCmdID(), 245 Map.COMMAND_NAME, 246 targetRefs, 247 sourceRefs, 248 null, 249 null, 250 new Data(StatusCode.OK), 251 new Item[0] 252 ); 253 commands.add(mapStatus); 254 } 255 } 256 257 260 SyncBody responseBody = new SyncBody( 261 (AbstractCommand[]) commands.toArray(new AbstractCommand[0]), 262 isFlag(Flags.FLAG_FINAL_MESSAGE) 263 ); 264 265 try { 266 return new SyncML(responseHeader, responseBody); 267 } catch (RepresentationException e) { 268 throw new ProtocolException("Unexpected error", e); 272 } 273 } 274 } | Popular Tags |