def addStatusMessage(self, message_reference, command_reference, command, target, source, status_code, anchor=None): """ Build a status message Status message are used to answer a command (credential, alert) and so contains a reference to the original message """ status = E.Status( E.CmdID(self._getNextCommandId()), E.MsgRef(str(message_reference)), E.CmdRef(str(command_reference)), E.Cmd(command), E.TargetRef(target), E.SourceRef(source), E.Data(resolveSyncmlStatusCode(status_code)), ) if anchor: item = E.Item( E.Data( E.Anchor(anchor, xmlns='syncml:metinf') ) ) status.append(item) self.body_append(status)
def addStatusMessage(self, message_reference, command_reference, command, target, source, status_code, anchor=None): """ Build a status message Status message are used to answer a command (credential, alert) and so contains a reference to the original message """ status = E.Status( E.CmdID(self._getNextCommandId()), E.MsgRef(str(message_reference)), E.CmdRef(str(command_reference)), E.Cmd(command), E.TargetRef(target), E.SourceRef(source), E.Data(resolveSyncmlStatusCode(status_code)), ) if anchor: item = E.Item( E.Data( E.Anchor(anchor, xmlns='syncml:metinf') ) ) status.append(item) self.body_append(status)
def addConfirmationMessage(self, command, sync_code, target_ref=None, source_ref=None, command_ref=None, message_ref=None): """ This is used in order to confirm that an object was correctly synchronized """ xml = E.Status() xml.append(E.CmdID(self._getNextCommandId())) if message_ref: xml.append(E.MsgRef(str(message_ref))) if command_ref: xml.append(E.CmdRef(str(command_ref))) xml.append(E.Cmd(command)) # Add either target ou source if target_ref: xml.append(E.TargetRef(target_ref)) if source_ref: xml.append(E.SourceRef(source_ref)) xml.append(E.Data(resolveSyncmlStatusCode(sync_code))) self.body_append(xml) self.sync_confirmation_counter += 1
def addConfirmationMessage(self, command, sync_code, target_ref=None, source_ref=None, command_ref=None, message_ref=None): """ This is used in order to confirm that an object was correctly synchronized """ xml = E.Status() xml.append(E.CmdID(self._getNextCommandId())) if message_ref: xml.append(E.MsgRef(str(message_ref))) if command_ref: xml.append(E.CmdRef(str(command_ref))) xml.append(E.Cmd(command)) # Add either target ou source if target_ref: xml.append(E.TargetRef(target_ref)) if source_ref: xml.append(E.SourceRef(source_ref)) xml.append(E.Data(resolveSyncmlStatusCode(sync_code))) self.body_append(xml) self.sync_confirmation_counter += 1
def addChallengeMessage(self, message_reference, target, source, command="SyncHdr", authentication_format='b64', authentication_type='syncml:auth-basic', authentication_code='missing_credentials'): """ Create a challenge message (CHAL) which is used to asked credentials to a client """ authentication_code = resolveSyncmlStatusCode(authentication_code) xml = (E.Status(E.CmdID(self._getNextCommandId()), E.MsgRef(str(message_reference)), E.CmdRef('0'), E.Cmd(command), E.TargetRef(target), E.SourceRef(source), E.Chal( E.Meta( E.Format(authentication_format, xmlns='syncml:metinf'), E.Type(authentication_type, xmlns='syncml:metinf') ) ), E.Data(authentication_code) )) self.body_append(xml)
def addChallengeMessage(self, message_reference, target, source, command="SyncHdr", authentication_format='b64', authentication_type='syncml:auth-basic', authentication_code='missing_credentials'): """ Create a challenge message (CHAL) which is used to asked credentials to a client """ authentication_code = resolveSyncmlStatusCode(authentication_code) xml = (E.Status(E.CmdID(self._getNextCommandId()), E.MsgRef(str(message_reference)), E.CmdRef('0'), E.Cmd(command), E.TargetRef(target), E.SourceRef(source), E.Chal( E.Meta( E.Format(authentication_format, xmlns='syncml:metinf'), E.Type(authentication_type, xmlns='syncml:metinf') ) ), E.Data(authentication_code) )) self.body_append(xml)
def _readStatusList(self, syncml_request, domain, syncml_response=None, generate_alert=False): """ Read status (answer to command) and act according to them """ sync_status_counter = 0 path_list = [] for status in syncml_request.status_list: if status["command"] == "SyncHdr": # Check for authentication if domain.getSynchronizationState() != "initializing": raise SynchronizationError( "Authentication header found although it is already done") if status['status_code'] == \ resolveSyncmlStatusCode('missing_credentials'): # Server challenged an authentication syncml_logger.info("\tServer required an authentication") if domain.getAuthenticationFormat() != \ status['authentication_format'] or \ domain.getAuthenticationType() != \ status['authentication_type']: raise ValueError('Authentication definition mismatch between \ client (%s-%s) and server (%s-%s)' % (domain.getAuthenticationFormat(), domain.getAuthenticationType(), status['authentication_format'], status['authentication_type'])) # XXX Not working To Review ! raise NotImplementedError("Adding credentials") syncml_response = domain.generateBaseResponse() syncml_response.addCredentialMessage(domain) return syncml_response elif status['status_code'] == \ resolveSyncmlStatusCode('invalid_credentials'): syncml_logger.error("\tClient authentication refused") raise ValueError("Server rejected client authentication") elif status['status_code'] == \ resolveSyncmlStatusCode('authentication_accepted'): syncml_logger.error("\tClient authentication accepted") else: raise ValueError('Unknown status code %s for authentication' % (status['status_code'])) elif status["command"] == "Alert": # Status about database synchronization # XXX Must check status for asked synchrozation # and must be done for command, not for # For now do nothing = say it is always OK syncml_logger.info("\tChecking database, will generate alert %s" % (generate_alert)) elif status["command"] in ('Add', 'Replace'): sync_status_counter += 1 object_gid = status['source'] or status['target'] if domain.getSyncmlAlertCode() == "refresh_from_client_only": # No signature is created for this kind of sync if status['status_code'] not in (resolveSyncmlStatusCode('success'), resolveSyncmlStatusCode('item_added')): raise ValueError("Impossible to synchronize object %s" % (status['source'] or status['target'])) else: signature = domain.getSignatureFromGid(object_gid) if not signature: # XXX previous call must raise Key/Index-Error instead raise ValueError("Impossible to find signature in %s for gid %s" % (domain.getPath(), object_gid)) if status['status_code'] == resolveSyncmlStatusCode('conflict'): signature.changeToConflict() syncml_logger.error("\tObject in conflict %s" % (status['source'] or status['target'])) elif status['status_code'] == resolveSyncmlStatusCode( 'conflict_resolved_with_merge'): # We will have to apply the update, and we should not care # about conflicts, so we have to force the update signature.noConflict() signature.setForce(True) syncml_logger.error("\tObject merged %s" % (status['source'] or status['target'])) elif status['status_code'] in (resolveSyncmlStatusCode('success'), resolveSyncmlStatusCode('item_added'), resolveSyncmlStatusCode( 'conflict_resolved_with_client_command_winning')): syncml_logger.error("\tObject synchronized %s" % (status['source'] or status['target'],)) if signature.getValidationState() != "no_conflict": signature.noConflict() signature.synchronize() elif status['status_code'] == resolveSyncmlStatusCode('chunk_accepted'): syncml_logger.info("Chunk was accepted for %s" % (object_gid,)) else: raise ValueError("Unknown status code : %r" % (status['status_code'],)) # Index signature now to fill the data column path_list.append(signature.getPath()) elif status['command'] == 'Delete': sync_status_counter += 1 object_gid = status['source'] or status['target'] signature = domain.getSignatureFromGid(object_gid) if status['status_code'] == resolveSyncmlStatusCode('success'): if signature: domain.z_delete_data_from_path(path=signature.getPath()) domain._delObject(signature.getId()) else: raise ValueError("Found no signature to delete for gid %s" %(object_gid,)) else: raise ValueError("Unknown status code : %r" % (status['status_code'],)) syncml_logger.error("\tObject deleted %s" % (status['source'] or status['target'])) else: raise ValueError("Unknown status command : %r" % (status['command'],)) if len(path_list): domain.SQLCatalog_indexSyncMLDocumentList(path_list) return sync_status_counter
def _readStatusList(self, syncml_request, domain, syncml_response=None, generate_alert=False): """ Read status (answer to command) and act according to them """ sync_status_counter = 0 path_list = [] for status in syncml_request.status_list: if status["command"] == "SyncHdr": # Check for authentication if domain.getSynchronizationState() != "initializing": raise SynchronizationError("Authentication header found although it is already done") if status["status_code"] == resolveSyncmlStatusCode("missing_credentials"): # Server challenged an authentication syncml_logger.info("\tServer required an authentication") if ( domain.getAuthenticationFormat() != status["authentication_format"] or domain.getAuthenticationType() != status["authentication_type"] ): raise ValueError( "Authentication definition mismatch between \ client (%s-%s) and server (%s-%s)" % ( domain.getAuthenticationFormat(), domain.getAuthenticationType(), status["authentication_format"], status["authentication_type"], ) ) # XXX Not working To Review ! raise NotImplementedError("Adding credentials") syncml_response = domain.generateBaseResponse() syncml_response.addCredentialMessage(domain) return syncml_response elif status["status_code"] == resolveSyncmlStatusCode("invalid_credentials"): syncml_logger.error("\tClient authentication refused") raise ValueError("Server rejected client authentication") elif status["status_code"] == resolveSyncmlStatusCode("authentication_accepted"): syncml_logger.error("\tClient authentication accepted") else: raise ValueError("Unknown status code %s for authentication" % (status["status_code"])) elif status["command"] == "Alert": # Status about database synchronization # XXX Must check status for asked synchrozation # and must be done for command, not for # For now do nothing = say it is always OK syncml_logger.info("\tChecking database, will generate alert %s" % (generate_alert)) elif status["command"] in ("Add", "Replace"): sync_status_counter += 1 object_gid = status["source"] or status["target"] if domain.getSyncmlAlertCode() == "refresh_from_client_only": # No signature is created for this kind of sync if status["status_code"] not in ( resolveSyncmlStatusCode("success"), resolveSyncmlStatusCode("item_added"), ): raise ValueError("Impossible to synchronize object %s" % (status["source"] or status["target"])) else: signature = domain.getSignatureFromGid(object_gid) if not signature: # XXX previous call must raise Key/Index-Error instead raise ValueError( "Impossible to find signature in %s for gid %s" % (domain.getPath(), object_gid) ) if status["status_code"] == resolveSyncmlStatusCode("conflict"): signature.changeToConflict() syncml_logger.error("\tObject in conflict %s" % (status["source"] or status["target"])) elif status["status_code"] == resolveSyncmlStatusCode("conflict_resolved_with_merge"): # We will have to apply the update, and we should not care # about conflicts, so we have to force the update signature.noConflict() signature.setForce(True) syncml_logger.error("\tObject merged %s" % (status["source"] or status["target"])) elif status["status_code"] in ( resolveSyncmlStatusCode("success"), resolveSyncmlStatusCode("item_added"), resolveSyncmlStatusCode("conflict_resolved_with_client_command_winning"), ): syncml_logger.error("\tObject synchronized %s" % (status["source"] or status["target"],)) if signature.getValidationState() != "no_conflict": signature.noConflict() signature.synchronize() elif status["status_code"] == resolveSyncmlStatusCode("chunk_accepted"): syncml_logger.info("Chunk was accepted for %s" % (object_gid,)) else: raise ValueError("Unknown status code : %r" % (status["status_code"],)) # Index signature now to fill the data column path_list.append(signature.getPath()) elif status["command"] == "Delete": sync_status_counter += 1 object_gid = status["source"] or status["target"] signature = domain.getSignatureFromGid(object_gid) if status["status_code"] == resolveSyncmlStatusCode("success"): if signature: domain.z_delete_data_from_path(path=signature.getPath()) domain._delObject(signature.getId()) else: raise ValueError("Found no signature to delete for gid %s" % (object_gid,)) else: raise ValueError("Unknown status code : %r" % (status["status_code"],)) syncml_logger.error("\tObject deleted %s" % (status["source"] or status["target"])) else: raise ValueError("Unknown status command : %r" % (status["command"],)) if len(path_list): domain.SQLCatalog_indexSyncMLDocumentList(path_list) return sync_status_counter