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 SyncMapping 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 SyncMapping(final SyncHdr syncHeader, final SyncBody syncBody) throws Sync4jException { 97 super(syncHeader, syncBody); 98 checkHeaderRequirements(); 99 checkBodyRequirements(); 100 } 101 102 108 public void checkHeaderRequirements() throws ProtocolException { 109 BasicRequirements.checkDTDVersion(syncHeader.getVerDTD()); 110 BasicRequirements.checkProtocolVersion(syncHeader.getVerProto()); 111 BasicRequirements.checkSessionId(syncHeader.getSessionID()); 112 BasicRequirements.checkMessageId(syncHeader.getMsgID()); 113 BasicRequirements.checkTarget(syncHeader.getTarget()); 114 BasicRequirements.checkSource(syncHeader.getSource()); 115 116 lastMessageId = syncHeader.getMsgID(); 118 } 119 120 126 public void checkBodyRequirements() throws ProtocolException { 127 clientCommands = (AbstractCommand[])syncBody.getCommands().toArray(new AbstractCommand[0]); 128 129 checkStatusCommands(); 131 132 checkMapCommand(); 134 } 135 136 142 private void checkStatusCommands() { 143 } 144 145 149 private void checkMapCommand() throws ProtocolException { 150 List list = ProtocolUtil.filterCommands(clientCommands, Map.class); 151 152 if (list.size() > 0) { 153 mapCommands = (Map[]) list.toArray(new Map[list.size()]); 154 mapCommandFind = true; 155 } else { 156 mapCommands = null; 157 mapCommandFind = false; 158 } 159 } 160 161 174 public SyncML getResponseMessage(String msgId) 175 throws ProtocolException { 176 SyncHdr responseHeader = getResponseHeader(msgId); 177 AbstractCommand[] commands = 178 (AbstractCommand[]) getResponseCommands(msgId).toArray(new AbstractCommand[0]); 179 SyncBody responseBody = new SyncBody( 180 commands, 181 isFlag(Flags.FLAG_FINAL_MESSAGE) 182 ); 183 184 try { 185 return new SyncML(responseHeader, responseBody); 186 } catch (RepresentationException e) { 187 throw new ProtocolException("Unexpected error", e); 191 } 192 } 193 194 204 public List getResponseCommands( String msgId) 205 throws ProtocolException { 206 ArrayList commands = new ArrayList (); 207 208 212 TargetRef[] targetRefs = new TargetRef[] { 216 new TargetRef(syncHeader.getTarget().getLocURI()) }; 217 SourceRef[] sourceRefs = new SourceRef[] { 218 new SourceRef(syncHeader.getSource().getLocURI()) }; 219 Status synchdrStatus = new Status( 220 idGenerator.next(), 221 lastMessageId, 222 "0", 223 "SyncHdr", 224 targetRefs, 225 sourceRefs, 226 null, 227 null, 228 new Data(StatusCode.OK), 229 new Item[0] 230 ); 231 commands.add(synchdrStatus); 232 233 if (mapCommandFind) { 237 239 Status mapStatus = null; 240 for (int i=0; i<mapCommands.length; ++i) { 241 242 targetRefs = new TargetRef[] { new TargetRef(mapCommands[i].getTarget()) }; 243 sourceRefs = new SourceRef[] { new SourceRef(mapCommands[i].getSource()) }; 244 245 mapStatus = new Status( 246 idGenerator.next(), 247 lastMessageId, 248 mapCommands[i].getCmdID().getCmdID(), 249 Map.COMMAND_NAME, 250 targetRefs, 251 sourceRefs, 252 null, 253 null, 254 new Data(StatusCode.OK), 255 new Item[0] 256 ); 257 commands.add(mapStatus); 258 } 259 } 260 261 return commands; 262 } 263 264 274 public SyncHdr getResponseHeader(String msgId) 275 throws ProtocolException { 276 287 Target target = new Target(syncHeader.getSource().getLocURI(), 288 syncHeader.getSource().getLocName()); 289 Source source = new Source(syncHeader.getTarget().getLocURI(), 290 syncHeader.getTarget().getLocName()); 291 SyncHdr responseHeader = new SyncHdr( 292 getDTDVersion() , 293 getProtocolVersion() , 294 syncHeader.getSessionID(), 295 msgId , 296 target , 297 source , 298 null , 299 false , 300 null , 301 null 302 ); 303 304 return responseHeader; 305 306 } 307 } 308 309 | Popular Tags |