SynchronizationTool.py 118 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
# -*- coding: utf-8 -*-
## Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
#          Sebastien Robin <seb@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

"""
ERP portal_synchronizations tool.
"""

from Products.ERP5Type.Tool.BaseTool import BaseTool
from AccessControl import ClassSecurityInfo, getSecurityManager
from Products.ERP5Type import Permissions
from AccessControl.SecurityManagement import newSecurityManager
from Products.PluggableAuthService.interfaces.plugins import\
     IAuthenticationPlugin
from Products.ERP5Type.Globals import InitializeClass
import urllib
import urllib2
import httplib
import socket
import os
import commands
import random
from DateTime import DateTime
from zLOG import LOG, DEBUG, INFO, WARNING
from urlparse import urlparse

from Products.ERP5SyncML.SyncMLConstant import SYNCML_NAMESPACE, NSMAP,\
     NULL_ANCHOR, ACTIVITY_PRIORITY, MAX_LEN, MAX_OBJECTS,\
     REPLACE_ACTION, ADD_ACTION
from Products.ERP5SyncML.XMLSyncUtils import getConduitByName,\
     getAlertCodeFromXML, checkAlert, getMessageIdFromXml,\
     resolveSyncmlStatusCode, resolveSyncmlAlertCode, getSyncBodyStatusList,\
     xml2wbxml, wbxml2xml, encode, decode, cutXML, checkFinal\
     ,getSubscriptionUrlFromXML, getDataSubNode, getDataText,\
     setRidWithMap

from base64 import b16decode, b16encode

from lxml.builder import ElementMaker
from lxml.etree import Element
from lxml import etree
parser = etree.XMLParser(remove_blank_text=True)
E = ElementMaker(namespace=SYNCML_NAMESPACE, nsmap=NSMAP)


# Logging channel definitions
import logging
# Main logging channel
syncml_logger = logging.getLogger('ERP5SyncML')

# Direct logging to "[instancehome]/log/ERP5SyncML.log", if this directory exists.
# Otherwise, it will end up in root logging facility (ie, event.log).
from App.config import getConfiguration
instancehome = getConfiguration().instancehome
if instancehome is not None:
  log_directory = os.path.join(instancehome, 'log')
  if os.path.isdir(log_directory):
    from Signals import Signals
    from ZConfig.components.logger.loghandler import FileHandler
    log_file_handler = FileHandler(os.path.join(log_directory, 'ERP5SyncML.log'))
    # Default zope log format string borrowed from
    # ZConfig/components/logger/factory.xml, but without the extra "------"
    # line separating entries.
    log_file_handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s %(message)s", "%Y-%m-%dT%H:%M:%S"))
    Signals.registerZopeSignals([log_file_handler])
    syncml_logger.addHandler(log_file_handler)
    syncml_logger.propagate = 0


class TimeoutHTTPConnection(httplib.HTTPConnection):
  """
  Custom Classes to set timeOut on handle sockets
  """
  def connect(self):
    httplib.HTTPConnection.connect(self)
    self.sock.settimeout(3600)

class TimeoutHTTPHandler(urllib2.HTTPHandler):
  def http_open(self, req):
    return self.do_open(TimeoutHTTPConnection, req)

def hexdump(raw=''):
  """
  this function is used to display the raw in a readable format :
  it display raw in hexadecimal format and display too the printable 
  characters (because if not printable characters are printed, it makes 
  terminal display crash)
  """
  buf = ""
  line = ""
  start = 0
  done = False
  while not done:
    end = start + 16
    max = len(str(raw))
    if end > max:
      end = max
      done = True
    chunk = raw[start:end]
    for i in xrange(len(chunk)):
      if i > 0:
        spacing = " "
      else:
        spacing = ""
      buf += "%s%02x" % (spacing, ord(chunk[i]))
    if done:
      for i in xrange(16 - (end % 16)):
        buf += "   "
    buf += "  "
    for c in chunk:
      val = ord(c)
      if val >= 33 and val <= 126:
        buf += c
      else:
        buf += "."
    buf += "\n"
    start += 16
  return buf

class SynchronizationTool(BaseTool):
  """
    This tool implements the synchronization algorithm
  """


  id           = 'portal_synchronizations'
  meta_type    = 'ERP5 Synchronizations'
  portal_type  = 'Synchronization Tool'

  security = ClassSecurityInfo()

  # Do we want to use emails ?
  #email = None
  email = 1
  same_export = 1

  security.declareProtected(Permissions.AccessContentsInformation,
      'getConflictList')
  def getConflictList(self, context=None):
    """
    Retrieve the list of all conflicts
    Here the list is as follow :
    [conflict_1,conflict2,...] where conflict_1 is like:
    ['publication',publication_id,object.getPath(),property_id,
    publisher_value,subscriber_value]
    """
    conflict_list = []
    for publication in self.searchFolder(portal_type='SyncML Publication'):
      for result in publication.searchFolder(
                                            portal_type='SyncML Subscription'):
        subscriber = result.getObject()
        sub_conflict_list = subscriber.getConflictList()
        for conflict in sub_conflict_list:
          if context is None or conflict.getOriginValue() == context:
            conflict_list.append(conflict.__of__(subscriber))
    for result in self.searchFolder(portal_type='SyncML Subscription'):
      subscription = result.getObject()
      sub_conflict_list = subscription.getConflictList()
      #LOG('SynchronizationTool.getConflictList, sub_conflict_list', DEBUG,
          #sub_conflict_list)
      for conflict in sub_conflict_list:
        if context is None or conflict.getOriginValue() == context:
          conflict_list.append(conflict.__of__(subscription))
    return conflict_list

  security.declareProtected(Permissions.AccessContentsInformation,
                            'getDocumentConflictList')
  def getDocumentConflictList(self, context=None):
    """
    Retrieve the list of all conflicts for a given document
    Well, this is the same thing as getConflictList with a path
    """
    return self.getConflictList(context)


  security.declareProtected(Permissions.AccessContentsInformation,
                            'getSynchronizationState')
  def getSynchronizationState(self, context):
    """
    context : the context on which we are looking for state

    This functions have to retrieve the synchronization state,
    it will first look in the conflict list, if nothing is found,
    then we have to check on a publication/subscription.

    This method returns a mapping between subscription and states

    JPS suggestion:
      path -> object, document, context, etc.
      type -> '/titi/toto' or ('','titi', 'toto') or <Base instance 1562567>
      object = self.resolveContext(context) (method to add)

    """
    path = self.resolveContext(context)
    conflict_list = self.getConflictList()
    state_list= []
    #LOG('getSynchronizationState', DEBUG, 'path: %s' % str(path))
    for conflict in conflict_list:
      if conflict.getOrigin() == path:
        #LOG('getSynchronizationState', DEBUG, 'found a conflict: %s' % str(conflict))
        state_list.append([conflict.getSubscriber(), 'conflict'])
    for domain in self.searchFolder():
      destination = domain.getSource()
      #LOG('getSynchronizationState', TRACE, 'destination: %s' % str(destination))
      j_path = '/'.join(path)
      #LOG('getSynchronizationState', TRACE, 'j_path: %s' % str(j_path))
      if destination in j_path:
        o_id = j_path[len(destination)+1:].split('/')[0]
        #LOG('getSynchronizationState', TRACE, 'o_id: %s' % o_id)
        if domain.getPortalType() == 'SyncML Publication':
          subscriber_list = [result.getObject() for result in\
                        domain.searchFolder(portal_type='SyncML Subscription')]
        else:
          subscriber_list = [domain]
        #LOG('getSynchronizationState, subscriber_list:', TRACE, subscriber_list)
        for subscriber in subscriber_list:
          gid = subscriber.getGidFromObject(context)
          signature = subscriber.getSignatureFromGid(gid)
          #XXX check if signature could be not None ...
          if signature is not None:
            state = signature.getValidationState()
            found = False
            # Make sure there is not already a conflict giving the state
            for state_item in state_list:
              if state_item[0] == subscriber:
                found = True
                break
            if not found:
              state_list.append([subscriber, state])
    return state_list

  security.declareProtected(Permissions.ModifyPortalContent,
                            'applyPublisherValue')
  def applyPublisherValue(self, conflict):
    """
      after a conflict resolution, we have decided
      to keep the local version of an object
    """
    object = conflict.getOriginValue()
    subscriber = conflict.getSubscriber()
    # get the signature:
    #LOG('p_sync.applyPublisherValue, subscriber: ', DEBUG, subscriber)
    gid = subscriber.getGidFromObject(object)
    signature = subscriber.getSignatureFromGid(gid)
    signature.delConflict(conflict)
    if not signature.getConflictList():
      signature.resolveConflictWithMerge()

  security.declareProtected(Permissions.ModifyPortalContent,
                            'applyPublisherDocument')
  def applyPublisherDocument(self, conflict):
    """
    apply the publisher value for all conflict of the given document
    """
    subscriber = conflict.getSubscriber()
    for c in self.getConflictList(conflict.getOriginValue()):
      if c.getSubscriber() == subscriber:
        #LOG('applyPublisherDocument, applying on conflict: ', DEBUG, conflict)
        c.applyPublisherValue()

  security.declareProtected(Permissions.AccessContentsInformation,
                            'getPublisherDocumentPath')
  def getPublisherDocumentPath(self, conflict):
    """
    apply the publisher value for all conflict of the given document
    """
    return conflict.getOrigin()

  security.declareProtected(Permissions.AccessContentsInformation,
                            'getPublisherDocument')
  def getPublisherDocument(self, conflict):
    """
    apply the publisher value for all conflict of the given document
    """
    publisher_object_path = self.getPublisherDocumentPath(conflict)
    #LOG('getPublisherDocument publisher_object_path', TRACE, publisher_object_path)
    publisher_object = self.unrestrictedTraverse(publisher_object_path)
    return publisher_object

  security.declareProtected(Permissions.AccessContentsInformation,
                            'getSubscriberDocumentVersion')
  def getSubscriberDocumentVersion(self, conflict, docid):
    """
    Given a 'conflict' and a 'docid' refering to a new version of a
    document, applies the conflicting changes to the document's new
    version. By so, two differents versions of the same document will be
    available.
    Thus, the manager will be able to open both version of the document
    before selecting which one to keep.
    """
    subscriber = conflict.getSubscriber()
    publisher_object = conflict.getOrigineValue()
    publisher_xml = self.getXMLObject(
                       object=publisher_object,
                       xml_mapping=subscriber.getXmlBindingGeneratorMethodId())
    directory = publisher_object.aq_parent
    object_id = docid
    if object_id in directory.objectIds():
      directory._delObject(object_id)
      # Import the conduit and get it
      conduit_name = subscriber.getConduitModuleId()
      conduit = getConduitByName(conduit_name)
      conduit.addNode(xml=publisher_xml, object=directory,
                      object_id=object_id,
                      signature=conflict.getParentValue())
      subscriber_document = directory._getOb(object_id)
      for c in self.getConflictList(conflict.getOriginValue()):
        if c.getSubscriber() == subscriber:
          c.applySubscriberValue(object=subscriber_document)
      return subscriber_document

  def _getCopyId(self, object):
    directory = object.aq_inner.aq_parent
    if directory.getId() != 'portal_repository':
      object_id = object.getId() + '_conflict_copy'
      if object_id in directory.objectIds():
        directory._delObject(object_id)
    else:
      repotool = directory
      docid, rev = repotool.getDocidAndRevisionFromObjectId(object.getId())
      new_rev = repotool.getFreeRevision(docid) + 10 # make sure it's not gonna provoke conflicts
      object_id = repotool._getId(docid, new_rev)
    return object_id

  security.declareProtected(Permissions.AccessContentsInformation,
                            'getSubscriberDocumentPath')
  def getSubscriberDocumentPath(self, conflict):
    """
    apply the publisher value for all conflict of the given document
    """
    subscriber = conflict.getSubscriber()
    publisher_object = conflict.getOriginValue()
    conduit_name = subscriber.getConduitModuleId()
    conduit = getConduitByName(conduit_name)
    publisher_xml = conduit.getXMLFromObjectWithId(publisher_object,
                       xml_mapping=subscriber.getXmlBindingGeneratorMethodId(),
                       context_document=subscriber.getPath())
    directory = publisher_object.aq_inner.aq_parent
    object_id = self._getCopyId(publisher_object)
    # Import the conduit and get it
    conduit.addNode(xml=publisher_xml, object=directory, object_id=object_id,
                    signature=conflict.getParentValue())
    subscriber_document = directory._getOb(object_id)
    subscriber_document._conflict_resolution = 1
    for c in self.getConflictList(conflict.getOriginValue()):
      if c.getSubscriber() == subscriber:
        c.applySubscriberValue(object=subscriber_document)
    copy_path = subscriber_document.getPhysicalPath()
    return copy_path

  security.declareProtected(Permissions.AccessContentsInformation,
                            'getSubscriberDocument')
  def getSubscriberDocument(self, conflict):
    """
    apply the publisher value for all conflict of the given document
    """
    subscriber_object_path = self.getSubscriberDocumentPath(conflict)
    subscriber_object = self.unrestrictedTraverse(subscriber_object_path)
    return subscriber_object

  security.declareProtected(Permissions.ModifyPortalContent,
                            'applySubscriberDocument')
  def applySubscriberDocument(self, conflict):
    """
    apply the subscriber value for all conflict of the given document
    """
    # XXX-AUREL : when we solve one conflict, it solves all conflicts related
    # to the same object ? is it the wanted behaviour ?
    subscriber = conflict.getSubscriber()
    for c in self.getConflictList(conflict.getOriginValue()):
      if c.getSubscriber() == subscriber:
        c.applySubscriberValue()

  security.declareProtected(Permissions.ModifyPortalContent,
                            'applySubscriberValue')
  def applySubscriberValue(self, conflict, object=None):
    """
      after a conflict resolution, we have decided
      to keep the local version of an object
    """
    solve_conflict = 1
    if object is None:
      object = conflict.getOriginValue()
    else:
      # This means an object was given, this is used in order
      # to see change on a copy, so don't solve conflict
      solve_conflict = False
    subscriber = conflict.getSubscriber()
    # get the signature:
    gid = subscriber.getGidFromObject(object)
    signature = subscriber.getSignatureFromGid(gid)
    # Import the conduit and get it
    conduit_name = subscriber.getConduitModuleId()
    conduit = getConduitByName(conduit_name)
    conduit.updateNode(xml=conflict.getDiffChunk(), object=object, force=True, signature=signature)
    if solve_conflict:
      signature.delConflict(conflict)
      if not signature.getConflictList():
        signature.resolveConflictWithMerge()

  #security.declareProtected(Permissions.ModifyPortalContent,
      #'managePublisherValue')
  #def managePublisherValue(self, subscription_url, property_id, object_path,
      #RESPONSE=None):
    #"""
    #Do whatever needed in order to store the local value on
    #the remote server

    #Suggestion (API)
      #add method to view document with applied xupdate
      #of a given subscriber XX
      #(ex. viewSubscriberDocument?path=ddd&subscriber_id=dddd)
      #Version=Version CPS
    #"""
    ## Retrieve the conflict object
    ##LOG('manageLocalValue', DEBUG, '%s %s %s' % (str(subscription_url),
                                          ##str(property_id),
                                          ##str(object_path)))
    #for conflict in self.getConflictList():
      #if conflict.getPropertyId() == property_id:
        #if '/'.join(conflict.getObjectPath()) == object_path:
          #if conflict.getSubscriber().getSubscriptionUrlString() == subscription_url:
            #conflict.applyPublisherValue()
    #if RESPONSE is not None:
      #RESPONSE.redirect('manageConflicts')

  #security.declareProtected(Permissions.ModifyPortalContent, 
      #'manageSubscriberValue')
  #def manageSubscriberValue(self, subscription_url, property_id, object_path, 
      #RESPONSE=None):
    #"""
    #Do whatever needed in order to store the remote value locally
    #and confirmed that the remote box should keep it's value
    #"""
    ##LOG('manageLocalValue', DEBUG, '%s %s %s' % (str(subscription_url),
                                          ##str(property_id),
                                          ##str(object_path)))
    #for conflict in self.getConflictList():
      #if conflict.getPropertyId() == property_id:
        #if '/'.join(conflict.getObjectPath()) == object_path:
          #if conflict.getSubscriber().getSubscriptionUrlString() == subscription_url:
            #conflict.applySubscriberValue()
    #if RESPONSE is not None:
      #RESPONSE.redirect('manageConflicts')

  #security.declareProtected(Permissions.ModifyPortalContent,
      #'manageSubscriberDocument')
  #def manageSubscriberDocument(self, subscription_url, object_path):
    #"""
    #"""
    #for conflict in self.getConflictList():
      #if '/'.join(conflict.getObjectPath()) == object_path:
        #if conflict.getSubscriber().getSubscriptionUrlString() == subscription_url:
          #conflict.applySubscriberDocument()
          #break
    #self.managePublisherDocument(object_path)

  #security.declareProtected(Permissions.ModifyPortalContent, 
      #'managePublisherDocument')
  #def managePublisherDocument(self, object_path):
    #"""
    #"""
    #retry = True
    #while retry:
      #retry = False
      #for conflict in self.getConflictList():
        #if '/'.join(conflict.getObjectPath()) == object_path:
          #conflict.applyPublisherDocument()
          #retry = True
          #break

  def resolveContext(self, context):
    """
    We try to return a path (like ('','erp5','foo') from the context.
    Context can be :
      - a path
      - an object
      - a string representing a path
    """
    if context is None:
      return context
    elif isinstance(context, tuple):
      return context
    elif isinstance(context, tuple):
      return tuple(context.split('/'))
    else:
      return context.getPhysicalPath()

  security.declarePublic('sendResponse')
  def sendResponse(self, to_url=None, from_url=None, sync_id=None, xml=None,
      domain=None, send=1, content_type='application/vnd.syncml+xml'):
    """
    We will look at the url and we will see if we need to send mail, http
    response, or just copy to a file.
    """
##     LOG('sendResponse, domain.getPath(): ', INFO, domain.getPath())
##     LOG('sendResponse, to_url: ', INFO, to_url)
##     LOG('sendResponse, from_url: ', INFO, from_url)
##     LOG('sendResponse, sync_id: ', INFO, sync_id)
##     LOG('sendResponse, xml: \n', INFO, xml)
    if content_type == 'application/vnd.syncml+wbxml':
      xml = xml2wbxml(xml)
      #LOG('sendHttpResponse, xml after wbxml: \n', DEBUG, hexdump(xml))
    if domain is not None:
      gpg_key = domain.getGpgPublicKey()
      if gpg_key:
        filename = str(random.randrange(1,2147483600)) + '.txt'
        decrypted = file('/tmp/%s' % filename,'w')
        decrypted.write(xml)
        decrypted.close()
        (status,output)=commands.getstatusoutput('gzip /tmp/%s' % filename)
        (status,output)=commands.getstatusoutput('gpg --yes --homedir \
            /var/lib/zope/Products/ERP5SyncML/gnupg_keys -r "%s" -se \
            /tmp/%s.gz' % (gpg_key,filename))
        # LOG('sendResponse, gpg output:', DEBUG, output)
        encrypted = file('/tmp/%s.gz.gpg' % filename,'r')
        xml = encrypted.read()
        encrypted.close()
        commands.getstatusoutput('rm -f /tmp/%s.gz' % filename)
        commands.getstatusoutput('rm -f /tmp/%s.gz.gpg' % filename)
    if send:
      if isinstance(to_url, str):
        scheme = urlparse(to_url)[0]
        # XXX-Aurel a mapping between protocol-method should be
        # done instead of treating everything here
        if scheme in ('http', 'https'):
          if domain.getPortalType() == 'SyncML Publication' and not\
                                                 domain.getIsActivityEnabled():
            # not use activity
            # XXX Make sure this is not a problem
            return None
          #use activities to send send an http response
          #LOG('sendResponse, will start sendHttpResponse, xml', INFO, '')
          self.activate(activity='SQLQueue',
                        tag=domain.getId(),
                        priority=ACTIVITY_PRIORITY).sendHttpResponse(
                                              sync_id=sync_id,
                                              to_url=to_url,
                                              xml=xml,
                                              domain_path=domain.getPath(),
                                              content_type=content_type)
        elif scheme in ('file',):
          filename = to_url[len('file:/'):]
          stream = file(filename, 'w')
          stream.write(xml)
          stream.close()
          # we have to use local files (unit testing for example
        elif scheme in ('mailto',):
          # we will send an email
          to_address = to_url[len('mailto:'):]
          from_address = from_url[len('mailto:'):]
          self.sendMail(from_address, to_address, sync_id, xml)
        else:
          LOG("sendResponse", WARNING, "Unknown scheme %s for response %s : %s - %s" %(domain.getPath(),
                                                                                       scheme, to_url, xml))
    return xml

  security.declarePrivate('sendHttpResponse')
  def sendHttpResponse(self, to_url=None, sync_id=None, xml=None,
                       domain_path=None,
                       content_type='application/vnd.syncml+xml'):
    domain = self.unrestrictedTraverse(domain_path)
    #LOG('sendHttpResponse, starting with domain:', INFO, domain)
    if domain is not None:
      if domain.getPortalType() == 'SyncML Publication' and not\
                                                 domain.getIsActivityEnabled():
        return xml
    # Retrieve the proxy from os variables
    proxy_url = ''
    if os.environ.has_key('http_proxy'):
      proxy_url = os.environ['http_proxy']
    #LOG('sendHttpResponse, proxy_url:', DEBUG, proxy_url)
    if proxy_url !='':
      proxy_handler = urllib2.ProxyHandler({"http" :proxy_url})
    else:
      proxy_handler = urllib2.ProxyHandler({})
    pass_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    auth_handler = urllib2.HTTPBasicAuthHandler(pass_mgr)
    proxy_auth_handler = urllib2.ProxyBasicAuthHandler(pass_mgr)
    opener = urllib2.build_opener(proxy_handler, proxy_auth_handler,
        auth_handler, TimeoutHTTPHandler)
    urllib2.install_opener(opener)
    to_encode = {}
    to_encode['text'] = xml
    to_encode['sync_id'] = sync_id
    headers = {'User-Agent':'ERP5SyncML', 'Content-Type':content_type}

    #XXX bad hack for synchronization with erp5
    # because at this time, when we call the readResponse method, we must
    # encode the data with urlencode if we want the readResponse method to
    # receive the data's in parameters.
    # All this should be improved to not use urlencode in all cases.
    # to do this, perhaps use SOAP :
    #  - http://en.wikipedia.org/wiki/SOAP
    #  - http://www.contentmanagementsoftware.info/zope/SOAPSupport
    #  - http://svn.zope.org/soap/trunk/

    if domain.getIsSynchronizedWithErp5Portal():
      #LOG('Synchronization with another ERP5 instance ...', INFO, 'to_encode = %s' %(to_encode))
      if to_url.find('readResponse')<0:
        to_url = to_url + '/portal_synchronizations/readResponse'
      encoded = urllib.urlencode(to_encode)
      data = encoded
      request = urllib2.Request(url=to_url, data=data)
    else:
      #XXX only to synchronize with other server than erp5 (must be improved):
      # XXX-AUREL : head is an undefined variable here !!!
      data = head+xml
      request = urllib2.Request(to_url, data, headers)

    try:
      url_file = urllib2.urlopen(request)
##       LOG("sendHttpResponse, sent to url %s :" % to_url, INFO, data)
##       LOG('sendHttpResponse, url_file', INFO, (url_file))
      result = url_file.read()
    except socket.error, msg:
      self.activate(activity='SQLQueue',
                    tag=domain.getId(),
                    priority=ACTIVITY_PRIORITY).sendHttpResponse(
                                                  to_url=to_url,
                                                  sync_id=sync_id,
                                                  xml=xml,
                                                  domain_path=domain.getPath(),
                                                  content_type=content_type)
      LOG('sendHttpResponse, socket ERROR:', INFO, msg)
      LOG('sendHttpResponse, url, data', INFO, (to_url, data))
      return
    except urllib2.URLError, msg:
      LOG("sendHttpResponse, can't open url %s :" % to_url, INFO, msg)
      LOG('sendHttpResponse, to_url, data', INFO, (to_url, data))
      return

    if domain is not None:
      if domain.getPortalType() == 'SyncML Subscription' and not\
                                                 domain.getIsActivityEnabled():
        #if we don't use activity :
        if result:
          self.readResponse(sync_id=sync_id, text=result)
    return result

  security.declareProtected(Permissions.ManagePortal, 'sync')
  def sync(self):
    """
    This will try to synchronize every subscription
    XXX Should not be used
    """
    for subscription in self.getSubscriptionList():
      #user_id = subscription.getZopeUser()
      #uf = self.getPortalObject().acl_users
      #user = uf.getUserById(user_id).__of__(uf)
      #newSecurityManager(None, user)
      subscription.activate(activity='SQLQueue',
                            tag=subscription.getId(),
                            priority=ACTIVITY_PRIORITY
                                ).SubSync(subscription.getPath())

  security.declarePublic('readResponse')
  def readResponse(self, text='', sync_id=None, to_url=None, from_url=None):
    """
    We will look at the url and we will see if we need to send mail, http
    response, or just copy to a file.
    """
##     LOG('readResponse, text:', INFO, text)
##     LOG('readResponse, hexdump(text) :', INFO, hexdump(text))
    syncml_logger.info('readResponse to_url %s' %(to_url,))
    syncml_logger.info('readResponse from_url %s' %(from_url,))
    syncml_logger.info('readResponse sync_id %s' %(sync_id,))
    status_code = None
    if text:
      # XXX We will look everywhere for a publication/subsription with
      # the id sync_id, this is not so good, but there is no way yet
      # to know if we will call a publication or subscription XXX
##       LOG('readResponse, sync_id :', INFO, sync_id)
      gpg_key = ''
      # we are still anonymous at this time, use unrestrictedSearchResults
      # to fetch the Subcribers
      catalog_tool = self.getPortalObject().portal_catalog
      publication_list = catalog_tool.unrestrictedSearchResults(
                                              parent_uid=self.getUid(),
                                              portal_type='SyncML Publication',
                                              source_reference=sync_id,
                                              validation_state='validated')
      if publication_list:
        publication = publication_list[0].getObject()
        gpg_key = publication.getGpgPublicKey()
        domain = publication
      if not gpg_key:
        # Retrieve Subscription and login with intial user
        # Who start the process
        # sync_id parameter can be source_reference or destination_reference
        subscription_list = catalog_tool.unrestrictedSearchResults(
                                               parent_uid=self.getUid(),
                                               portal_type='SyncML Subscription',
                                               validation_state='validated')
        for subscription in subscription_list:
          subscription = subscription.getObject()
          if sync_id in (subscription.getSourceReference(),
                                       subscription.getDestinationReference()):
            gpg_key = subscription.getGpgPublicKey()
            domain = subscription
            user_id = domain.getProperty('zope_user')
            if user_id:
              ##LOG('readResponse, user :', DEBUG, user)
              user_folder = self.getPortalObject().acl_users
              user = user_folder.getUserById(user_id).__of__(user_folder)
              newSecurityManager(None, user)
      else:
        # decrypt the message if needed
        filename = str(random.randrange(1, 2147483600)) + '.txt'
        encrypted = file('/tmp/%s.gz.gpg' % filename,'w')
        encrypted.write(text)
        encrypted.close()
        (status, output) = commands.getstatusoutput('gpg --homedir \
            /var/lib/zope/Products/ERP5SyncML/gnupg_keys -r "%s"  --decrypt \
            /tmp/%s.gz.gpg > /tmp/%s.gz' % (gpg_key, filename, filename))
        # LOG('readResponse, gpg output:', INFO, output)
        (status,output)=commands.getstatusoutput('gunzip /tmp/%s.gz' % filename)
        decrypted = file('/tmp/%s' % filename,'r')
        text = decrypted.read()
        #LOG('readResponse, text:', INFO, text)
        decrypted.close()
        commands.getstatusoutput('rm -f /tmp/%s' % filename)
        commands.getstatusoutput('rm -f /tmp/%s.gz.gpg' % filename)
      # Get the target and then find the corresponding publication or
      # Subscription
      # LOG('type(text) : ', INFO, type(text))
      if domain is None:
        LOG('Impossible to find a publication or subscription for %s' %(sync_id,),
            WARNING, "Check that your pub/sub are validated and their source URI")
        return " "

      if domain.getContentType() == 'application/vnd.syncml+wbxml':
        text = wbxml2xml(text)
      syncml_logger.info('readResponse, text after wbxml : %s\n' %(text,))
      xml = etree.XML(text, parser=parser)
      url = '%s' % xml.xpath('string(/syncml:SyncML/syncml:SyncHdr/'\
                             'syncml:Target/syncml:LocURI)',
                             namespaces=xml.nsmap)
      # XXX-AUREL : the following part assumes that pub and sub have different
      # source_reference now since url can be the same
      # What is the gain in this ? If it has to remains like this, developper
      # must be informed not to waste time !!!
      for publication in self.searchFolder(portal_type='SyncML Publication',
                                           source_reference=sync_id,
                                           validation_state='validated'):
        if publication.getUrlString() == url:
          if publication.getIsActivityEnabled():
            #use activities to send SyncML data.
            publication.activate(activity='SQLQueue',
                                 tag=publication.getId(),
                                 priority=ACTIVITY_PRIORITY).PubSync(
                                                         publication.getPath(),
                                                         text)
            return ' '
          else:
            result = self.PubSync(publication.getPath(), xml)
            # Then encrypt the message
            xml = result['xml']
            if publication.getContentType() == 'application/vnd.syncml+wbxml':
              xml = xml2wbxml(xml)
            return xml
      for subscription in self.searchFolder(portal_type='SyncML Subscription',
                                            source_reference=sync_id,
                                            validation_state='validated'):
        if subscription.getSubscriptionUrlString() == url:
          subscription_path = subscription.getPath()
          self.activate(activity='SQLQueue',
                        tag=subscription.getId(),
                        priority=ACTIVITY_PRIORITY).SubSync(subscription_path,
                                                            text)
          return ' '
    # we use from only if we have a file
    elif isinstance(from_url, str):
      if from_url.find('file://') == 0:
        try:
          filename = from_url[len('file:/'):]
          stream = file(filename, 'r')
          xml = stream.read()
          # LOG('readResponse', DEBUG, 'file... msg: %s' % str(stream.read()))
        except IOError:
          LOG('readResponse, cannot read file: ', INFO, filename)
          xml = None
        if xml is not None and len(xml) == 0:
          xml = None
        return xml

  security.declareProtected(Permissions.ModifyPortalContent, 'PubSync')
  def PubSync(self, publication_path, msg=None, RESPONSE=None, subscriber=None):
    """
      This is the synchronization method for the server
    """
    LOG('PubSync', DEBUG, 'Starting... publication: %s' % (publication_path))
    # Read the request from the client
    publication = self.unrestrictedTraverse(publication_path)
    xml_client = msg
    if xml_client is None:
      xml_client = self.readResponse(from_url=publication.getUrlString(),
                                     sync_id=publication.getSourceReference())
    LOG('PubSync', DEBUG, 'Starting... msg: %s' % str(xml_client))
    result = None
    if xml_client is not None:
      if isinstance(xml_client, (str, unicode)):
        xml_client = etree.XML(xml_client, parser=parser)
      #FIXME to apply a DTD or schema
      if xml_client.xpath('local-name()') != "SyncML":
        LOG('PubSync', INFO, 'This is not a SyncML Message')
        raise ValueError, "Sorry, This is not a SyncML Message"
      alert_code = getAlertCodeFromXML(xml_client)
      category_tool = self.getPortalObject().portal_categories
      if alert_code:
        alert_code = [category.getId() for category in\
                      category_tool.syncml_alert_code.objectValues() if\
                      category.getReference() == alert_code][0]
      # Get informations from the header
      client_header = xml_client[0]
      #FIXME to apply a DTD or schema
      if client_header.xpath('local-name()') != "SyncHdr":
        LOG('PubSync', INFO, 'This is not a SyncML Header')
        raise ValueError, "Sorry, This is not a SyncML Header"
      subscription_url = getSubscriptionUrlFromXML(client_header)
      # Get the subscriber or create it if not already in the list
      subscriber = publication.getSubscriber(subscription_url)
      if subscriber is None:
        subscriber = publication.createUnrestrictedSubscriber(
                                  subscription_url_string=subscription_url,
                                  xml_binding_generator_method_id=\
                                  publication.getXmlBindingGeneratorMethodId(),
                                  conduit_module_id=\
                                              publication.getConduitModuleId(),
                                  list_method_id=publication.getListMethodId(),
                                  gid_generator_method_id=publication.getGidGeneratorMethodId(),
                                  source=publication.getSource(),
                                  syncml_alert_code=alert_code)
        # first synchronization
        result = self.PubSyncInit(publication=publication,
                                  xml_client=xml_client,
                                  subscriber=subscriber,
                                  sync_type=alert_code)
      elif checkAlert(xml_client) and alert_code in ('two_way', 'slow_sync',
                                                     'one_way_from_server',
                                                     'one_way_from_client',):
        subscriber.setXmlBindingGeneratorMethodId(
                                  publication.getXmlBindingGeneratorMethodId())
        subscriber.setConduitModuleId(publication.getConduitModuleId())
        result = self.PubSyncInit(publication=publication,
                                  xml_client=xml_client,
                                  subscriber=subscriber,
                                  sync_type=alert_code)
      elif not alert_code:
        #we log the user authenticated to do the synchronization with him
        if xml_client.xpath('string(/syncml:SyncML/syncml:SyncBody/'\
                            'syncml:Map)', namespaces=xml_client.nsmap):
          setRidWithMap(xml_client, subscriber)
        if subscriber.getAuthenticationState() == 'logged_in':
          uf = self.getPortalObject().acl_users
          authenticated_user = subscriber.getProperty('authenticated_user')
          user = uf.getUserById(authenticated_user).__of__(uf)
          newSecurityManager(None, user)
        result = self.PubSyncModif(publication, xml_client)
      elif alert_code in category_tool.syncml_alert_code.objectIds():
        raise NotImplementedError('ALert code not handled yet: %r' % alert_code)
      else:
        # Must return (405) Command not allowed
        raise NotImplementedError
    elif subscriber is not None:
      # This looks like we are starting a synchronization after
      # a conflict resolution by the user
      result = self.PubSyncInit(publication=publication,
                                xml_client=None,
                                subscriber=subscriber,
                                sync_type='two_way')
    return result

  security.declareProtected(Permissions.ModifyPortalContent, 'PubSyncInit')
  def PubSyncInit(self, publication=None, xml_client=None, subscriber=None,
      sync_type=None):
    """
      Read the client xml message
      Send the first XML message from the server
    """
    LOG('PubSyncInit', INFO,
        'Starting... publication: %s' % (publication.getPath()))
    #the session id is set at the same value of those of the client
    session_id = int(xml_client.xpath(
                      'string(/syncml:SyncML/syncml:SyncHdr/syncml:SessionID)',
                      namespaces=xml_client.nsmap))
    subscriber.setSessionId(session_id)
    #same for the message id
    message_id = getMessageIdFromXml(xml_client)
    subscriber.setMessageId(message_id)
    #at the begining of the synchronization the subscriber is not authenticated
    if subscriber.getAuthenticationState() == 'logged_in':
      subscriber.logout()
    #the last_message_id is 1 because the message that 
    #we are about to send is the message 1
    subscriber.initLastMessageId(1)

    alert = None
    # Get informations from the body
    if xml_client is not None: # We have received a message
      last_anchor = '%s' % xml_client.xpath('string(.//syncml:Alert/'\
                                            'syncml:Item/syncml:Meta/'\
                                            'syncml:Anchor/syncml:Last)',
                                            namespaces=xml_client.nsmap)
      next_anchor = '%s' % xml_client.xpath('string(.//syncml:Alert/'\
                                            'syncml:Item/syncml:Meta/'\
                                            'syncml:Anchor/syncml:Next)',
                                            namespaces=xml_client.nsmap)
      alert = checkAlert(xml_client)
      alert_code = getAlertCodeFromXML(xml_client)
      cred_node_list = xml_client.xpath('/syncml:SyncML/syncml:SyncHdr/'\
                                        'syncml:Cred',
                                        namespaces=xml_client.nsmap)

      #the source and the target of the subscriber are reversed compared 
      # to those of the publication :
      target_uri = '%s' % xml_client.xpath('string(//syncml:SyncBody/'\
                                           'syncml:Alert/syncml:Item/'\
                                           'syncml:Target/syncml:LocURI)',
                                           namespaces=xml_client.nsmap)
      subscriber.setSourceReference(target_uri)
      source_uri = '%s' % xml_client.xpath('string(//syncml:SyncBody/'\
                                           'syncml:Alert/syncml:Item/'\
                                           'syncml:Source/syncml:LocURI)',
                                           namespaces=xml_client.nsmap)
      subscriber.setDestinationReference(source_uri)

      cmd_id = 1 # specifies a SyncML message-unique command identifier
      #create element 'SyncML' with a default namespace
      xml = E.SyncML()
      # syncml header
      xml.append(self.SyncMLHeader(subscriber.getSessionId(),
        subscriber.getMessageId(),
        subscriber.getSubscriptionUrlString(),
        publication.getUrlString()))
      # syncml body
      sync_body = E.SyncBody()
      xml.append(sync_body)

      # at the begining, the code is initialised at UNAUTHORIZED
      auth_code = 'invalid_credentials'
      if not len(cred_node_list):
        auth_code = 'missing_credentials'
        LOG("PubSyncInit : there's no credential in the SyncML Message!!!", INFO,'return status 407 - %s' %(auth_code))
        # Prepare the xml message for the Sync initialization package
        sync_body.append(self.SyncMLChal(cmd_id, "SyncHdr",
                                         publication.getUrlString(),
                                         subscriber.getSubscriptionUrlString(),
                                         publication.getAuthenticationFormat(),
                                         publication.getAuthenticationType(),
                                         auth_code))
        cmd_id += 1
        # chal message
        xml_status, cmd_id = self.SyncMLStatus(
                                      xml_client,
                                      auth_code,
                                      cmd_id,
                                      next_anchor,
                                      subscription=subscriber)
        sync_body.extend(xml_status)
      else:
        # If slow sync, then resend everything
        if alert_code == resolveSyncmlAlertCode(self, 'slow_sync') and \
                                        subscriber.getNextAnchor() is not None:
          LOG('Warning !!!, reseting client synchronization for subscriber:', WARNING,
              subscriber.getPath())
          subscriber.resetAllSignatures()
          subscriber.resetAnchorList()

        # Check if the last time synchronization is the same as the client one
        if subscriber.getNextAnchor() != last_anchor:
          if not last_anchor:
            LOG('PubSyncInit', INFO, 'anchor null')
          else:
            message = '\nsubscriber.getNextAnchor:\t%s\nsubscriber.getLastAnchor:\t%s\
                  \nlast_anchor:\t\t\t%s\nnext_anchor:\t\t\t%s' % \
                  (subscriber.getNextAnchor(),
                    subscriber.getLastAnchor(),
                    last_anchor,
                    next_anchor)
            LOG('PubSyncInit Anchors', INFO, message)
        else:
          subscriber.setNextAnchor(next_anchor)
        cred_node = cred_node_list[0]
        meta_node = cred_node.xpath('syncml:Meta',
                                                 namespaces=cred_node.nsmap)[0]
        authentication_format = '%s' % meta_node.xpath('string(./*'\
                                                  '[local-name() = "Format"])',
                                                  namespaces=meta_node.nsmap)
        authentication_type = '%s' % meta_node.xpath('string(./*'\
                                                    '[local-name() = "Type"])',
                                                  namespaces=meta_node.nsmap)
        data = '%s' % cred_node.xpath('string(syncml:Data)',
                                      namespaces=cred_node.nsmap)

        if authentication_type == publication.getAuthenticationType():
          authentication_format = publication.getAuthenticationFormat()
          decoded = decode(authentication_format, data)
          if decoded and ':' in decoded:
            login, password = decoded.split(':')
            user_folder = self.getPortalObject().acl_users
            for plugin_name, plugin in user_folder._getOb('plugins')\
                                           .listPlugins(IAuthenticationPlugin):
              #LOG('PubSyncInit Authentication', INFO,
                  #'%r %s:%s' % (plugin, login, password))
              if plugin.authenticateCredentials(
                        {'login':login, 'password':password}) is not None:
                subscriber.login()
                LOG("PubSyncInit Authentication Accepted", INFO, '')
                auth_code = 'authentication_accepted'
                #here we must log in with the user authenticated :
                user = user_folder.getUserById(login).__of__(user_folder)
                newSecurityManager(None, user)
                subscriber._edit(authenticated_user=login)
                break
              else:
                # in all others cases, the auth_code is set to UNAUTHORIZED
                auth_code = 'invalid_credentials'
                LOG('PubSyncInit Authentication Failed !! with', INFO,
                    'login:%r' % (login,))

        # Prepare the xml message for the Sync initialization package
        if auth_code == 'authentication_accepted':
          xml_status, cmd_id = self.SyncMLStatus(xml_client, auth_code,
                                                 cmd_id, next_anchor,
                                                 subscription=subscriber)
          sync_body.extend(xml_status)
          # alert message
          sync_body.append(self.SyncMLAlert(cmd_id, sync_type,
                                            subscriber.getDestinationReference(),
                                            subscriber.getSourceReference(),
                                            subscriber.getLastAnchor(),
                                            next_anchor))
          cmd_id += 1

          subscriber.initialiseSynchronization()
        else:
          # chal message
          sync_body.append(self.SyncMLChal(cmd_id, "SyncHdr",
                                         publication.getUrlString(),
                                         subscriber.getSubscriptionUrlString(),
                                         publication.getAuthenticationFormat(),
                                         publication.getAuthenticationType(),
                                         auth_code))
          cmd_id += 1
          xml_status, cmd_id = self.SyncMLStatus(xml_client,
                                                 'missing_credentials', cmd_id,
                                                 next_anchor,
                                                 subscription=subscriber)
          sync_body.extend(xml_status)

    else:
      # We have started the sync from the server (may be for a conflict 
      # resolution)
      raise ValueError, "the syncml message is None. Maybe a synchronization \
          has been started from the server (forbiden)"
      # a synchronization is always starded from a client and can't be from
      # a server !
    sync_body.append(E.Final())
    xml_string = etree.tostring(xml, encoding='utf-8', pretty_print=True)
    if publication.getContentType() == 'application/vnd.syncml+wbxml':
      xml_string = xml2wbxml(xml_string)
    self.sendResponse(from_url=publication.getUrlString(),
                      to_url=subscriber.getSubscriptionUrlString(),
                      sync_id=subscriber.getDestinationReference(),
                      xml=xml_string, domain=publication,
                      content_type=publication.getContentType())

    return {'has_response': True, 'xml': xml_string}

  security.declareProtected(Permissions.ModifyPortalContent, 'PubSyncModif')
  def PubSyncModif(self, publication, xml_client):
    """
    The modidification message for the publication
    """
    return self.SyncModif(publication, xml_client)


  security.declareProtected(Permissions.ModifyPortalContent, 'SubSyncInit')
  def SubSyncInit(self, subscription):
    """
      Send the first XML message from the client
    """
    LOG('SubSyncInit',0,'starting....')
    cmd_id = 1 # specifies a SyncML message-unique command identifier
    subscription.createNewAnchor()
    subscription.initLastMessageId()

    # save the actual user to use it in all the session:
1113 1114 1115 1116 1117 1118 1119
    user_id = getSecurityManager().getUser().getId()
    user_folder = self.getPortalObject().acl_users
    user = user_folder.getUserById(user_id)
    if user is None:
      raise ValueError, "Current logged user %s cannot be found in user folder, \
                 synchronization cannot work with this kind of user"
    subscription._edit(zope_user=user_id)
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
    if subscription.getAuthenticationState() != 'logged_in':
      subscription.login()

    #create element 'SyncML'
    xml = E.SyncML()
    # syncml header
    xml.append(self.SyncMLHeader(subscription.incrementSessionId(),
                                 subscription.incrementMessageId(),
                                 subscription.getUrlString(),
                                 subscription.getSubscriptionUrlString(),
                                 source_name=subscription.getUserId()))

    # syncml body
    sync_body = E.SyncBody()
    xml.append(sync_body)

    subscription.initialiseSynchronization()

    # alert message
    sync_body.append(self.SyncMLAlert(cmd_id,
                                      subscription.getSyncmlAlertCode(),
                                      subscription.getDestinationReference(),
                                      subscription.getSourceReference(),
                                      subscription.getLastAnchor(),
                                      subscription.getNextAnchor()))
    cmd_id += 1
    syncml_put = self.SyncMLPut(cmd_id, subscription)
    if syncml_put is not None:
      sync_body.append(syncml_put)
      cmd_id += 1

    xml_string = etree.tostring(xml, encoding='utf-8', xml_declaration=True,
                                pretty_print=True)
    self.sendResponse(from_url=subscription.getSubscriptionUrlString(),
                      to_url=subscription.getUrlString(),
                      sync_id=subscription.getDestinationReference(),
                      xml=xml_string, domain=subscription,
                      content_type=subscription.getContentType())

    return {'has_response': True, 'xml': xml_string}

  security.declareProtected(Permissions.ModifyPortalContent, 'SubSync')
  def SubSync(self, subscription_path, msg=None, RESPONSE=None):
    """
      This is the synchronization method for the client
    """
    response = None #check if subsync replies to this messages
    subscription = self.unrestrictedTraverse(subscription_path)
    if msg is None and (subscription.getSubscriptionUrlString()).find('file') >= 0:
      msg = self.readResponse(sync_id=subscription.getDestinationReference(),
                              from_url=subscription.getSubscriptionUrlString())
    if msg is None:
      response = self.SubSyncInit(subscription)
    else:
      xml_client = msg
      if isinstance(xml_client, (str, unicode)):
        xml_client = etree.XML(xml_client, parser=parser)
      status_list = getSyncBodyStatusList(xml_client)
      if status_list:
        status_code_syncHdr = status_list[0]['code']
        LOG('SubSync status code : ', DEBUG, status_code_syncHdr)
        if status_code_syncHdr ==\
                      resolveSyncmlStatusCode(self, 'missing_credentials'):
          if xml_client.xpath('string(/syncml:SyncML/syncml:SyncBody/'\
                              'syncml:Status/syncml:Chal)',
                              namespaces=xml_client.nsmap):
            authentication_format = '%s' % xml_client.xpath(
                                      'string(//*[local-name() = "Format"])',
                                      namespaces=xml_client.nsmap)
            authentication_type = '%s' % xml_client.xpath(
                                      'string(//*[local-name() = "Type"])',
                                      namespaces=xml_client.nsmap)
            LOG('SubSync auth_required :', INFO,
                'format:%s, type:%s' % (authentication_format,
                                        authentication_type))
            subscription.setAuthenticationFormat(authentication_format)
            subscription.setAuthenticationType(authentication_type)
          else:
            raise ValueError, "Sorry, the server chalenge for an \
                authentication, but the authentication format is not find"

          LOG('SubSync', INFO, 'Authentication required')
          response = self.SubSyncCred(subscription, xml_client)
        elif status_code_syncHdr ==\
                        resolveSyncmlStatusCode(self, 'invalid_credentials'):
          LOG('SubSync', INFO, 'Bad authentication')
          return {'has_response': False, 'xml': xml_client}
        else:
          response = self.SubSyncModif(subscription, xml_client)
      else:
        response = self.SubSyncModif(subscription, xml_client)

    return response

  security.declareProtected(Permissions.ModifyPortalContent, 'SubSyncModif')
  def SubSyncModif(self, subscription, xml_client):
    """
      Send the client modification, this happens after the Synchronization
      initialization
    """
    return self.SyncModif(subscription, xml_client)

  security.declareProtected(Permissions.ModifyPortalContent, 'SyncModif')
  def SyncModif(self, domain, remote_xml):
    """
    Modification Message, this is used after the first
    message in order to send modifications.
    Send the server modification, this happens after the Synchronization
    initialization
    """
    has_response = False #check if syncmodif replies to this messages
    cmd_id = 1 # specifies a SyncML message-unique command identifier
    #LOG('SyncModif', DEBUG, 'Starting... domain: %s' % domain.getId())
    # Get informations from the header
    xml_header = remote_xml[0]
    #FIXME to apply a DTD or schema
    if xml_header.xpath('local-name()') != "SyncHdr":
      LOG('SyncModif', INFO, 'This is not a SyncML Header')
      raise ValueError, "Sorry, This is not a SyncML Header"

    subscriber = domain # If we are the client, this is fine
    simulate = False # used by applyActionList, should be False for client
    if domain.getPortalType() == 'SyncML Publication':
      subscription_url = getSubscriptionUrlFromXML(xml_header)
      subscriber = domain.getSubscriber(subscription_url)

    # We have to check if this message was not already, this can be dangerous
    # to update two times the same object
    message_id = getMessageIdFromXml(remote_xml)
    correct_message = subscriber.checkCorrectRemoteMessageId(message_id)
    if not correct_message: # We need to send again the message
      LOG('SyncModif, no correct message:', INFO, "sending again...")
      LOG('SyncModif message_id:', INFO, repr(message_id))
      last_xml = subscriber.getLastSentMessage()
      LOG("SyncModif last_xml :", INFO, last_xml)
      remote_xml = etree.tostring(remote_xml, encoding='utf-8',
                                  xml_declaration=True,
                                  pretty_print=True)
      LOG("SyncModif remote_xml :", INFO, remote_xml)
      if last_xml:
        has_response = True
        if domain.getPortalType() == 'SyncML Publication': # We always reply
          self.sendResponse(from_url=domain.getUrlString(),
                            to_url=subscriber.getSubscriptionUrlString(),
                            sync_id=subscriber.getDestinationReference(),
                            xml=last_xml,
                            domain=domain,
                            content_type=domain.getContentType())
        elif domain.getPortalType() == 'SyncML Subscription':
          self.sendResponse(from_url=domain.getSubscriptionUrlString(),
                            to_url=domain.getUrlString(),
                            sync_id=domain.getDestinationReference(),
                            xml=last_xml,
                            domain=domain,
                            content_type=domain.getContentType())
        else:
          raise ValueError('domain type not known %s' % domain.getPath())
      return {'has_response': has_response, 'xml': last_xml}
    subscriber.setLastSentMessage('')

    # First apply the list of status codes
    destination_waiting_more_data, has_status_list = self.applyStatusList(
                                                         subscriber=subscriber,
                                                         remote_xml=remote_xml)

    alert_code = getAlertCodeFromXML(remote_xml)
    # Import the conduit and get it
    conduit = getConduitByName(subscriber.getConduitModuleId())
    # Then apply the list of actions
    xml_confirmation_list, has_next_action, cmd_id = self.applyActionList(
                                                         cmd_id=cmd_id,
                                                         domain=domain,
                                                         subscriber=subscriber,
                                                         remote_xml=remote_xml,
                                                         conduit=conduit,
                                                         simulate=simulate)

    xml = E.SyncML()

    # syncml header
    if domain.getPortalType() == 'SyncML Publication':
      xml.append(self.SyncMLHeader(
                  subscriber.getSessionId(),
                  subscriber.incrementMessageId(),
                  subscriber.getSubscriptionUrlString(),
                  domain.getUrlString()))
    elif domain.getPortalType() == 'SyncML Subscription':
      xml.append(self.SyncMLHeader(
                  domain.getSessionId(), domain.incrementMessageId(),
                  domain.getUrlString(),
                  domain.getSubscriptionUrlString()))
    else:
      raise ValueError('domain not handled %r' % domain.getPath())
    # syncml body
    sync_body = E.SyncBody()
    xml.append(sync_body)

    xml_status, cmd_id = self.SyncMLStatus(
                                    remote_xml,
                                    'success',
                                    cmd_id,
                                    subscriber.getNextAnchor(),
                                    subscription=subscriber)
    sync_body.extend(xml_status)

    destination_url = ''
    # alert message if we want more data
    if destination_waiting_more_data:
      sync_body.append(self.SyncMLAlert(
                        cmd_id,
                        'partial_content',
                        subscriber.getTargetURI(),
                        subscriber.getSourceReference(),
                        subscriber.getLastAnchor(),
                        subscriber.getNextAnchor()))
    # Now we should send confirmations
    cmd_id_before_getsyncmldata = cmd_id
    cmd_id = cmd_id+1
    # XXX Not sure that reading getSyncMLData before sending
    # confimations is a good idea.
    # It means that synchronisations are crossed executed
    # It must be clarified and fixed.
    if domain.getIsActivityEnabled():
      #use activities to get SyncML data.
      remote_xml = etree.tostring(remote_xml, encoding='utf-8',
                                  xml_declaration=True, pretty_print=False)
      xml_tree = etree.tostring(xml, encoding='utf-8', xml_declaration=True,
                                pretty_print=False)
      xml_confirmation_list = [etree.tostring(xml, encoding='utf-8',\
                                              xml_declaration=True,\
                                              pretty_print=False) for xml in \
                                              xml_confirmation_list]
      domain.activate(activity='SQLQueue',
                      tag=domain.getId(),
                      priority=ACTIVITY_PRIORITY).activateSyncModif(
                      domain_relative_url=domain.getRelativeUrl(),
                      remote_xml=remote_xml,
                      xml_tree=xml_tree,
                      subscriber_relative_url=subscriber.getRelativeUrl(),
                      cmd_id=cmd_id,
                      xml_confirmation_list=xml_confirmation_list,
                      syncml_data_list=[],
                      cmd_id_before_getsyncmldata=cmd_id_before_getsyncmldata,
                      has_status_list=has_status_list,
                      has_response=has_response )
      return {'has_response': True, 'xml': ''}
    else:
      result = self.getSyncMLData(domain=domain,
                             remote_xml=remote_xml,
                             subscriber=subscriber,
                             cmd_id=cmd_id,
                             xml_confirmation_list=xml_confirmation_list,
                             conduit=conduit,
                             maximum_object_list_len=None)
      syncml_data_list = result['syncml_data_list']
      xml_confirmation_list = result['xml_confirmation_list']
      cmd_id = result['cmd_id']
      return self.sendSyncModif(syncml_data_list, cmd_id_before_getsyncmldata,
                                subscriber, domain, xml_confirmation_list,
                                remote_xml, xml, has_status_list,
                                has_response)

  security.declarePrivate('applyStatusList')
  def applyStatusList(self, subscriber=None, remote_xml=None):
    """
    This read a list of status list (ie syncml confirmations).
    This method have to change status codes on signatures
    """
    status_list = getSyncBodyStatusList(remote_xml)
    has_status_list = False
    destination_waiting_more_data = False
    for status in status_list:
      if not status['code']:
        continue
      status_cmd = status['cmd']
      object_gid = status['source']
      if not object_gid:
        object_gid = status['target']
      status_code = status['code']
      signature = subscriber.getSignatureFromGid(object_gid)
      #if signature is None:
        ##the client give his id but not the gid
        #signature = subscriber.getSignatureFromRid(object_gid)
      #if signature is not None:
        #LOG('signature.getId()', 0, signature.getId())
        #LOG('signature.getReference()', 0, signature.getReference())
        #LOG('signature.getValidationState()', 0, signature.getValidationState())
        #LOG('status_cmd', 0, status_cmd)
        #LOG('status_code', 0, status_code)
      if status_cmd in ('Add', 'Replace',):
        has_status_list = True
        if status_code == resolveSyncmlStatusCode(self, 'partial_content'):
          destination_waiting_more_data = True
          signature.changeToPartial()
        elif status_code == resolveSyncmlStatusCode(self, 'conflict'):
          signature.changeToConflict()
        elif status_code == resolveSyncmlStatusCode(self,
                                               '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.drift()
          signature.setForce(True)
        elif status_code in (resolveSyncmlStatusCode(self, 'success'),
                             resolveSyncmlStatusCode(self, 'item_added'),
                             resolveSyncmlStatusCode(self,
                             'conflict_resolved_with_client_command_winning')):#\
                          #and signature.getValidationState() != 'synchronized':
          signature.synchronize()
      elif status_cmd == 'Delete':
        has_status_list = True
        if status_code == resolveSyncmlStatusCode(self, 'success'):
          if signature is not None:
            subscriber._delObject(signature.getId())
    return destination_waiting_more_data, has_status_list

  security.declarePrivate('SyncMLChal')
  def SyncMLChal(self, cmd_id, cmd, target_ref, source_ref, auth_format,
      auth_type, auth_code):
    """
    This is used in order to ask crendentials
    """
    auth_code = resolveSyncmlStatusCode(self, auth_code)
    xml = (E.Status(
             E.CmdID('%s' % cmd_id),
             E.MsgRef('1'),
             E.CmdRef('0'),
             E.Cmd(cmd),
             E.TargetRef(target_ref),
             E.SourceRef(source_ref),
             E.Chal(
               E.Meta(
                 E.Format(auth_format, xmlns='syncml:metinf'),
                 E.Type(auth_type, xmlns='syncml:metinf')
                 )
               ),
            E.Data('%s' % auth_code)
            ))
    return xml

  security.declarePrivate('SyncMLAlert')
  def SyncMLAlert(self, cmd_id, sync_code, target, source, last_anchor,
                  next_anchor):
    """
      Since the Alert section is always almost the same, this is the
      way to set one quickly.
    """
    if isinstance(last_anchor, DateTime):
      last_anchor = last_anchor.strftime('%Y%m%dT%H%M%SZ')
    elif not last_anchor:
      last_anchor = NULL_ANCHOR
    if isinstance(next_anchor, DateTime):
      next_anchor = next_anchor.strftime('%Y%m%dT%H%M%SZ')
    elif not next_anchor:
      next_anchor = NULL_ANCHOR
    sync_code = resolveSyncmlAlertCode(self, sync_code)
    xml = (E.Alert(
            E.CmdID('%s' % cmd_id),
            E.Data(sync_code),
            E.Item(
              E.Target(
                E.LocURI(target)
                ),
              E.Source(
                E.LocURI(source)
                ),
              E.Meta(
                E.Anchor(
                  E.Last(last_anchor),
                  E.Next(next_anchor)
                  )
                )
              )
            ))
    return xml

  security.declarePrivate('SubSyncCred')
  def SubSyncCred(self, subscription, msg=None, RESPONSE=None):
    """
      This method send crendentials
    """
    cmd_id = 1 # specifies a SyncML message-unique command identifier
    #create element 'SyncML' with a default namespace
    xml = E.SyncML()
    # syncml header
    data = "%s:%s" % (subscription.getUserId(), subscription.getPassword())
    data = encode(subscription.getAuthenticationFormat(), data)
    xml.append(self.SyncMLHeader(
      subscription.incrementSessionId(),
      subscription.incrementMessageId(),
      subscription.getUrlString(),
      subscription.getSubscriptionUrlString(),
      source_name=subscription.getUserId(),
      dataCred=data,
      authentication_format=subscription.getAuthenticationFormat(),
      authentication_type=subscription.getAuthenticationType()))

    # syncml body
    sync_body = E.SyncBody()
    xml.append(sync_body)

    # alert message
    sync_body.append(self.SyncMLAlert(cmd_id,
                                      subscription.getSyncmlAlertCode(),
                                      subscription.getDestinationReference(),
                                      subscription.getSourceReference(),
                                      subscription.getLastAnchor(),
                                      subscription.getNextAnchor()))
    cmd_id += 1
    syncml_put = self.SyncMLPut(cmd_id, subscription)
    if syncml_put is not None:
      sync_body.append(syncml_put)
    sync_body.append(E.Final())
    xml_string = etree.tostring(xml, encoding='utf-8', xml_declaration=True,
                                pretty_print=True)
    self.sendResponse(from_url=subscription.getSubscriptionUrlString(),
                      to_url=subscription.getUrlString(),
                      sync_id=subscription.getDestinationReference(),
                      xml=xml_string, domain=subscription,
                      content_type=subscription.getContentType())

    return {'has_response': True, 'xml': xml_string}

  security.declarePrivate('SyncMLHeader')
  def SyncMLHeader(self, session_id, msg_id, target, source, target_name=None,
      source_name=None, dataCred=None, authentication_format='b64',
      authentication_type='syncml:auth-basic'):
    """
      Since the Header is always almost the same, this is the
      way to set one quickly.
    """
    xml = (E.SyncHdr(
            E.VerDTD('1.2'),
            E.VerProto('SyncML/1.2'),
            E.SessionID('%s' % session_id),
            E.MsgID('%s' % msg_id),
          ))
    target_node = E.Target(E.LocURI(target))
    if target_name:
      target_node.append(E.LocName(target_name.decode('utf-8')))
    xml.append(target_node)
    source_node = E.Source(E.LocURI(source))
    if source_name:
      source_node.append(E.LocName(source_name.decode('utf-8')))
    xml.append(source_node)
    if dataCred:
      xml.append(E.Cred(
                  E.Meta(E.Format(authentication_format, xmlns='syncml:metinf'),
                  E.Type(authentication_type, xmlns='syncml:metinf'),),
                  E.Data(dataCred)
                  ))
    return xml

  security.declarePrivate('SyncMLStatus')
  def SyncMLStatus(self, remote_xml, data_code, cmd_id, next_anchor,
                   subscription=None):
    """
    return a status bloc with all status corresponding to the syncml
    commands in remote_xml
    """
    #list of element in the SyncBody bloc
    sub_syncbody_element_list = remote_xml.xpath(
                                            '/syncml:SyncML/syncml:SyncBody/*',
                                            namespaces=remote_xml.nsmap)
    message_id = getMessageIdFromXml(remote_xml)
    status_list = []
    target_uri = '%s' % remote_xml.xpath('string(/syncml:SyncML/'\
                                         'syncml:SyncHdr/syncml:Target/'\
                                         'syncml:LocURI)',
                                         namespaces=remote_xml.nsmap)
    source_uri = '%s' % remote_xml.xpath('string(/syncml:SyncML/'\
                                         'syncml:SyncHdr/syncml:Source/'\
                                         'syncml:LocURI)',
                                         namespaces=remote_xml.nsmap)
    if isinstance(next_anchor, DateTime):
      next_anchor = next_anchor.strftime('%Y%m%dT%H%M%SZ')
    elif not next_anchor:
      next_anchor = NULL_ANCHOR
    if data_code != 'missing_credentials':
      xml = (E.Status(
               E.CmdID('%s' % cmd_id),
               E.MsgRef('%s' % message_id),
               E.CmdRef('0'),
               E.Cmd('SyncHdr'),
               E.TargetRef(target_uri),
               E.SourceRef(source_uri),
               E.Data(resolveSyncmlStatusCode(self, data_code)),
               ))
      cmd_id += 1
      status_list.append(xml)
    for sub_syncbody_element in sub_syncbody_element_list:
      if sub_syncbody_element.xpath('local-name()') not in\
                                                    ('Status', 'Final', 'Get'):
        nsmap = sub_syncbody_element.nsmap
        xml = (E.Status(
                 E.CmdID('%s' % cmd_id),
                 E.MsgRef('%s' % message_id),
                 E.CmdRef('%s' %\
                 sub_syncbody_element.xpath('string(.//syncml:CmdID)',
                                            namespaces=nsmap)),
                 E.Cmd('%s' % sub_syncbody_element.xpath('local-name()'))
                 ))
        cmd_id += 1
        #target_ref = sub_syncbody_element.xpath(
                                      #'string(.//syncml:Target/syncml:LocURI)',
                                      #namespaces=nsmap)
        #if target_ref:
          #xml.append(E.TargetRef('%s' % target_ref))
        #source_ref = sub_syncbody_element.xpath(
                                      #'string(.//syncml:Source/syncml:LocURI)',
                                      #namespaces=nsmap)
        #if source_ref:
          #xml.append(E.SourceRef('%s' % source_ref))
        target_ref = sub_syncbody_element.xpath(
                                      'string(.//syncml:Target/syncml:LocURI)',
                                      namespaces=nsmap)
        if target_ref:
          xml.append(E.SourceRef('%s' % target_ref))
        source_ref = sub_syncbody_element.xpath(
                                      'string(.//syncml:Source/syncml:LocURI)',
                                      namespaces=nsmap)
        if source_ref:
          xml.append(E.TargetRef('%s' % source_ref))
        if sub_syncbody_element.xpath('local-name()') == 'Add':
          xml.append(E.Data(resolveSyncmlStatusCode(self, 'item_added')))
        elif sub_syncbody_element.xpath('local-name()') == 'Alert' and \
            sub_syncbody_element.xpath('string(.//syncml:Data)',
                                       namespaces=nsmap) == \
            resolveSyncmlAlertCode(self, 'slow_sync'):
          xml.append(E.Data(resolveSyncmlStatusCode(self, 'refresh_required')))
        elif sub_syncbody_element.xpath('local-name()') == 'Alert':
          xml.append(E.Item(E.Data(E.Anchor(E.Next(next_anchor)))))
        else:
          xml.append(E.Data(resolveSyncmlStatusCode(self, 'success')))
        status_list.append(xml)
      #FIXME to do a test for Get
      if sub_syncbody_element.xpath('local-name()') == 'Get'\
          and subscription is not None:
        cmd_ref = '%s' % sub_syncbody_element.xpath('string(.//syncml:CmdID)',
                                                    namespaces=nsmap)
        syncml_result = self.SyncMLPut(
                                  cmd_id,
                                  subscription,
                                  markup='Results',
                                  cmd_ref=cmd_ref,
                                  message_id=message_id)
        if syncml_result is not None:
          status_list.append(syncml_result)
        cmd_id += 1

    return status_list, cmd_id

  security.declarePrivate('SyncMLPut')
  def SyncMLPut(self, cmd_id, subscription, markup='Put', cmd_ref=None,
      message_id=None):
    """
    this is used to inform the server of the CTType version supported
    but if the server use it to respond to a Get request, it's a <Result> markup
    instead of <Put>
    """
    conduit_name = subscription.getConduitModuleId()
    conduit = getConduitByName(conduit_name)
    xml = None
    #if the conduit support the SyncMLPut :
    if getattr(conduit, 'getCapabilitiesCTTypeList', None) is not None and \
       getattr(conduit, 'getCapabilitiesVerCTList', None) is not None and \
       getattr(conduit, 'getPreferedCapabilitieVerCT', None) is not None:
      xml = Element('{%s}%s' % (SYNCML_NAMESPACE, markup))
      xml.append(E.CmdID('%s' % cmd_id))
      if message_id:
        xml.append(E.MsgRef('%s' % message_id))
      if cmd_ref:
        xml.append(E.CmdRef('%s' % cmd_ref))
      xml.extend((E.Meta(E.Type('application/vnd.syncml-devinf+xml')),
                 E.Item(E.Source(E.LocURI('./devinf11')),
                 E.Data(E.DevInf(
                   E.VerDTD('1.1'),
                   E.Man('Nexedi'),
                   E.Mod('ERP5SyncML'),
                   E.OEM('Open Source'),
                   E.SwV('0.1'),
                   E.DevID(subscription.getSubscriptionUrlString()),
                   E.DevTyp('workstation'),
                   E.UTC(),
                   E.DataStore(E.SourceRef(subscription.getSourceReference()))
                   )
                 )
               )))
      data_store = xml.find('{%(ns)s}Item/{%(ns)s}Data/{%(ns)s}DevInf/{%(ns)s}DataStore' % {'ns': SYNCML_NAMESPACE})
      tx_element_list = []
      rx_element_list = []
      for type in conduit.getCapabilitiesCTTypeList():
        if type != 'text/xml':
          for x_version in conduit.getCapabilitiesVerCTList(type):
            rx_element_list.append(E.Rx(E.CTType(type), E.VerCT(x_version)))
            tx_element_list.append(E.Tx(E.CTType(type), E.VerCT(x_version)))
      rx_pref = Element('{%s}Rx-Pref' % SYNCML_NAMESPACE)
      rx_pref.extend((E.CTType(conduit.getPreferedCapabilitieCTType()),
                      E.VerCT(conduit.getPreferedCapabilitieVerCT())))
      data_store.append(rx_pref)
      data_store.extend(rx_element_list)
      tx_pref = Element('{%s}Tx-Pref' % SYNCML_NAMESPACE)
      tx_pref.extend((E.CTType(conduit.getPreferedCapabilitieCTType()),
                      E.VerCT(conduit.getPreferedCapabilitieVerCT())))
      data_store.append(tx_pref)
      data_store.extend(tx_element_list)
      data_store.append(E.SyncCap(
                          E.SyncType('2'),
                          E.SyncType('1'),
                          E.SyncType('4'),
                          E.SyncType('6')
                          ))
    return xml

  security.declarePrivate('getSyncMLData')
  def getSyncMLData(self, domain=None, remote_xml=None, cmd_id=0,
                    subscriber=None, xml_confirmation_list=None, conduit=None,
                    maximum_object_list_len=None, **kw):
    """
    This generate the syncml data message. This returns a string
    with all modification made locally (ie replace, add ,delete...)

    if object is not None, this usually means we want to set the
    actual xupdate on the signature.
    """
    # LOG('getSyncMLData starting...', INFO, "%s - %s" %(domain.getTitle(), domain.getPath()))
    if isinstance(conduit, str):
      conduit = getConduitByName(conduit)
    if xml_confirmation_list is None:
      xml_confirmation_list = []
    local_gid_list = []
    syncml_data_list = kw.get('syncml_data_list', [])
    result = {'finished': True}
    if isinstance(remote_xml, (str, unicode)):
      remote_xml = etree.XML(remote_xml, parser=parser)
    if domain.isOneWayFromServer() or subscriber.isOneWayFromClient():
      # Do not fill in object_path_list, client send nothing to server
      subscriber._edit(remaining_object_path_list=[])
    elif subscriber.getProperty('remaining_object_path_list') is None:
      object_list = domain.getObjectList()
      object_path_list = [x.getPath() for x in object_list]
      LOG("getSyncMLData", 300, "object path list got from %s is %s" %(domain, object_path_list))
      subscriber._edit(remaining_object_path_list=object_path_list)
      if conduit.getContentType() == 'text/vcard':
        # XXX This part should be handled by Conduit itself,
        # not by SynchronizationTool
        #here the method getGidFromObject don't return the good gid because
        #the conduit use the catalog to determine it and object are not yet
        #cataloged so if there is many times the same gid, we must count it
        gid_not_encoded_list = []
        for object in object_list:
          #LOG('getSyncMLData :', DEBUG, 'object:%s,  objectTitle:%s, local_gid_list:%s' % (object, object.getTitle(), local_gid_list))
          gid = b16decode(domain.getGidFromObject(object))
          if gid in gid_not_encoded_list:
            number = len([item for item in gid_not_encoded_list if item.startswith(gid)])
            if number:
              gid = '%s__%s' %  (gid, str(number+1))
          gid_not_encoded_list.append(gid)
          local_gid_list.append(b16encode(gid))
          #LOG('getSyncMLData :', DEBUG,'gid_not_encoded_list:%s, local_gid_list:%s, gid:%s' % (gid_not_encoded_list, local_gid_list, gid))
      else:
        local_gid_list = [domain.getGidFromObject(x) for x in object_list]
      # Objects to remove
      #LOG('getSyncMLData remove object to remove ...', DEBUG, '')
      for object_gid in subscriber.getGidList():
        if object_gid not in local_gid_list:
          # This is an object to remove
          signature = subscriber.getSignatureFromGid(object_gid)
          if signature.getValidationState() != 'partial':
            # If partial, then we have a signature but no local object
            rid = signature.getRid()
            syncml_data_list.append(self.deleteXMLObject(object_gid=object_gid,
                                                         rid=rid,
                                                         cmd_id=cmd_id))
            cmd_id += 1
            # Delete Signature if object does not exist anymore
            subscriber._delObject(object_gid)

    local_gid_list = []
    loop = 0
    for object_path in subscriber.getProperty('remaining_object_path_list'):
      if maximum_object_list_len is not None and\
                                               loop >= maximum_object_list_len:
        result['finished'] = False
        break
      #LOG('getSyncMLData object_path', INFO, object_path)
      object = self.getPortalObject().unrestrictedTraverse(object_path)
      gid = domain.getGidFromObject(object)
      if not gid:
        continue
      local_gid_list.append(gid)
      force = False
      #if ''.join(syncml_data_list).count('\n') < MAX_LINES and not \
          #object.getId().startswith('.'):
      if len(''.join(syncml_data_list)) < MAX_LEN:
        # If not we have to cut
        #LOG('getSyncMLData', INFO, 'object_path:%r' % (object_path,))
        #LOG('getSyncMLData', INFO, 'gid:%r' % (gid,))
        #LOG('getSyncMLData', INFO, 'xml_mapping:%r' %\
                                       #domain.getXmlBindingGeneratorMethodId())
        #LOG('getSyncMLData', INFO, 'code:%r' % getAlertCodeFromXML(remote_xml))
        #LOG('getSyncMLData', INFO, 'gid_list:%r' % local_gid_list)
        #LOG('getSyncMLData', INFO, 'subscriber.getGidList:%r' %\
                                                       #subscriber.getGidList())
        #LOG('getSyncMLData', INFO, 'hasSignature:%r' %\
                                           #subscriber.hasSignature(gid))
        #LOG('getSyncMLData', INFO, 'alert_code == slowsync:%r' %\
           #(getAlertCodeFromXML(remote_xml) == resolveSyncmlAlertCode(self,
                                                                 #'slow_sync')))
        signature = subscriber.getSignatureFromGid(gid)
        ## Here we first check if the object was modified or not by looking at dates
        #status = self.SENT
        more_data = False
        # For the case it was never synchronized, we have to send everything
        if signature is None or (not signature.hasData() and\
            signature.getValidationState() != 'partial') or\
            getAlertCodeFromXML(remote_xml) ==\
                                     resolveSyncmlAlertCode(self, 'slow_sync'):
          #LOG('getSyncMLData', DEBUG, 'Current object.getPath: %s' % object.getPath())
          xml_string = conduit.getXMLFromObjectWithId(object,
                                                      xml_mapping=\
                                       domain.getXmlBindingGeneratorMethodId(),
                                       context_document=subscriber.getPath())
          if signature is None:
            LOG("SynchronizationTool", 300, "creating a signature for gid = %s, path %s" %(gid, object.getPath()))
            signature = subscriber.newContent(portal_type='SyncML Signature',
                                              id=gid,
                                              reference=object.getPath(),
                                              temporary_data=xml_string)
          #if xml_string.count('\n') > MAX_LINES:
            #more_data = True
            #xml_string, rest_string = cutXML(xml_string)
            #signature.setPartialData(rest_string)
            #signature.setPartialAction(ADD_ACTION)
            #signature.changeToPartial()
          if xml_string and len(xml_string) > MAX_LEN:
            more_data = True
            xml_string, rest_string = cutXML(xml_string)
            signature.setPartialData(rest_string)
            signature.setPartialAction(ADD_ACTION)
            signature.changeToPartial()
          else:
            signature.setTemporaryData(xml_string)

          if xml_string:
            syncml_data_list.append(self.addXMLObject(cmd_id=cmd_id,
                                          object=object,
                                          gid=gid,
                                          xml_string=xml_string,
                                          more_data=more_data,
                                          media_type=conduit.getContentType()))
          cmd_id += 1
        elif signature.getValidationState() in ('not_synchronized',
                                                'synchronized',
                                               'conflict_resolved_with_merge'):
          # We don't have synchronized this object yet but it has a signature
          xml_object = conduit.getXMLFromObjectWithId(object,
                           xml_mapping=domain.getXmlBindingGeneratorMethodId(),
                           context_document=subscriber.getPath())
          #LOG('getSyncMLData', DEBUG, 'checkMD5: %s' % str(signature.checkMD5(xml_object)))
          #LOG('getSyncMLData', DEBUG, 'getStatus: %s' % str(signature.getStatus()))
          if signature.getValidationState() == 'conflict_resolved_with_merge':
            xml_confirmation_list.append(self.SyncMLConfirmation(
                                      cmd_id=cmd_id,
                                      source_ref=signature.getId(),
                                      sync_code='conflict_resolved_with_merge',
                                      cmd='Replace'))
          set_synchronized = True
          # LOG('signature.checkMD5(xml_object)', INFO, '%r' % signature.checkMD5(xml_object))
          if not signature.checkMD5(xml_object):
            #LOG("signature data is %r\n\nxml_object is %r\n\n\nXXXXXXXXXXXXX" %(signature.getData(), xml_object), 300, "")
            set_synchronized = False
            if conduit.getContentType() != 'text/xml':
              # If there is no xml, we re-send all the objects
              xml_string = xml_object
            else:
              # This object has changed on this side, we have to generate some xmldiff
              xml_object_with_gid = conduit.replaceIdFromXML(xml_object, 'gid',
                                                             gid)
              previous_xml_with_gid = conduit.replaceIdFromXML(
                                                           signature.getData(),
                                                           'gid', gid)
              xml_string = conduit.generateDiff(xml_object_with_gid,
                                                previous_xml_with_gid)
              #LOG('XMLSyncUtils diff:%s' % object.getPath(), INFO, xml_string)
              #if xml_string.count('\n') > MAX_LINES:
                ## This make comment fails, so we need to replace
                #more_data = True
                #xml_string, rest_string = cutXML(xml_string)
                #signature.setPartialData(rest_string)
                ##status = self.PARTIAL
                #signature.setPartialAction(REPLACE_ACTION)
                #signature.changeToPartial()
              if len(xml_string) > MAX_LEN:
                # This make comment fails, so we need to replace
                more_data = True
                xml_string, rest_string = cutXML(xml_string)
                signature.setPartialData(rest_string)
                #status = self.PARTIAL
                signature.setPartialAction(REPLACE_ACTION)
                if signature.getValidationState() != 'partial':
                  signature.changeToPartial()
            syncml_data_list.append(self.replaceXMLObject(
                                        cmd_id=cmd_id, object=object,
                                        gid=gid,
                                        xml_string=xml_string,
                                        more_data=more_data,
                                        media_type=conduit.getContentType()))
            cmd_id += 1
            signature.setTemporaryData(xml_object)
          # XXX getSyncMLData must not edit objects this is a read only
          # methods. The code below is probabbly disabled since
          # applyDiff is implemented on Signature
          # Now we can apply the xupdate from the subscriber
          subscriber_xupdate = signature.getSubscriberXupdate()
          #LOG('getSyncMLData subscriber_xupdate', INFO, subscriber_xupdate)
          if subscriber_xupdate is not None:
            # The modification in the xml from signature is compared and
            # updated with xml_xupdate from subscriber
            previous_xml_with_gid = conduit.replaceIdFromXML(
                                                           signature.getData(),
                                                           'gid', gid,
                                                           as_string=False)
            conduit.updateNode(xml=subscriber_xupdate, object=object,
                         previous_xml=previous_xml_with_gid,
                         force=(domain.getPortalType()=='SyncML Subscription'),
                         simulate=False,
                         signature=signature,
                         domain=domain)
            xml_object = conduit.getXMLFromObjectWithId(object,
                                                        xml_mapping=\
                                       domain.getXmlBindingGeneratorMethodId(),
                                       context_document=subscriber.getPath())
            signature.setTemporaryData(xml_object)
          if set_synchronized and\
                              signature.getValidationState() != 'synchronized':
            # We should not have this case when we are in CONFLICT_MERGE
            signature.synchronize()
        elif signature.getValidationState() ==\
                               'conflict_resolved_with_client_command_winning':
          # We have decided to apply the update
          # XXX previous_xml will be geXML instead of getTempXML because
          # some modification was already made and the update
          # may not apply correctly
          xml_update = signature.getPartialData()
          previous_xml_with_gid = conduit.replaceIdFromXML(signature.getData(),
                                                           'gid', gid,
                                                           as_string=False)
          conduit.updateNode(xml=xml_update, object=object,
                             previous_xml=previous_xml_with_gid, force=True,
                             gid=gid,
                             signature=signature,
                             domain=domain)
          xml_confirmation_list.append(self.SyncMLConfirmation(
                                  cmd_id=cmd_id,
                                  target_ref=gid,
                                  sync_code='conflict_resolved_with_client_command_winning',
                                  cmd='Replace'))
          signature.synchronize()
        elif signature.getValidationState() == 'partial':
          # Receive the chunk of partial xml
          if conduit.getContentType() != 'text/xml':
            xml_string = conduit.getXMLFromObjectWithId(object,
                                        xml_mapping=domain.getXmlBindingGeneratorMethodId(),
                                        context_document=subscriber.getPath())
          else:
            # Wrapp it into CDATA
            #xml_string = signature.getPartialData('')
            #if xml_string.count('\n') > MAX_LINES:
              #more_data = True
              #xml_string = signature.getFirstChunkPdata(MAX_LINES)
              #signature.changeToPartial()
            xml_string = signature.getPartialData('')
            if len(xml_string) > MAX_LEN:
              more_data = True
              xml_string = signature.getFirstPdataChunk(MAX_LEN)
              if signature.getValidationState() != 'partial':
                signature.changeToPartial()
            xml_string = etree.CDATA(xml_string.decode('utf-8'))
          #if signature.getValidationState() != 'partial':
            #signature.sent()
          if signature.getPartialAction() == REPLACE_ACTION:
            #rid = signature.getRid()
            # In first, we try with rid if there is one
            syncml_data_list.append(self.replaceXMLObject(
                                       cmd_id=cmd_id,
                                       object=object,
                                       gid=gid,
                                       #rid=rid,
                                       xml_string=xml_string,
                                       more_data=more_data,
                                       media_type=subscriber.getContentType()))
          elif signature.getPartialAction() == ADD_ACTION:
            #in fisrt, we try with rid if there is one
            syncml_data_list.append(self.addXMLObject(
                                        cmd_id=cmd_id,
                                        object=object,
                                        gid=gid,
                                        xml_string=xml_string,
                                        more_data=more_data,
                                        media_type=subscriber.getContentType()))
        if not more_data:
          subscriber.removeRemainingObjectPath(object_path)
      else:
        result['finished'] = True
        break
      loop += 1
    result['syncml_data_list'] = syncml_data_list
    result['xml_confirmation_list'] = xml_confirmation_list
    result['cmd_id'] = cmd_id
    return result

  security.declarePrivate('applyActionList')
  def applyActionList(self, domain=None, subscriber=None, cmd_id=0,
                      remote_xml=None, conduit=None, simulate=False):
    """
    This just look to a list of action to do, then id applies
    each action one by one, thanks to a conduit
    """
    xml_confirmation_list = []
    has_next_action = False
    gid_from_xml_list = []
    destination = domain.getSourceValue()
    # LOG('applyActionList args', INFO, 'domain : %s\n subscriber : %s\n cmd_id: %s'\
    #     % (domain.getPath(), subscriber.getPath(), cmd_id))
    #LOG('XMLSyncUtils applyActionList', DEBUG, self.getSyncActionList(remote_xml))
    for action in remote_xml.xpath('//syncml:Add|//syncml:Delete|'\
                              '//syncml:Replace', namespaces=remote_xml.nsmap):
      conflict_list = []
      status_code = 'success'
      # Thirst we have to check the kind of action it is

      # The rid is the Temporary GUID (SYNCML Protocol). the rid sent by the
      # client unlike gid. The rid is in MapItem for each Action Map it's the LocURI in
      # the action.
      gid = '%s' % action.xpath('string(.//syncml:Item/syncml:Source/'\
                                      'syncml:LocURI)',
                                      namespaces=action.nsmap)
      if not gid:
        gid = '%s' % action.xpath('string(.//syncml:Item/syncml:Target/'\
                                        'syncml:LocURI)',
                                        namespaces=action.nsmap)
      #The action delete hasn't need a gid and retrieve the gid of conduit for
      #object.
      if action.xpath('local-name()') != 'Delete':
        data_action = getDataSubNode(action)
        if conduit.getContentType() != 'text/xml':
          #data in unicode
          data_action = getDataText(action)
        if getattr(conduit, 'getGidFromXML', None) is not None:
          temp_gid = conduit.getGidFromXML(data_action, gid_from_xml_list)
          if temp_gid:
            gid_from_xml_list.append(temp_gid)
            gid = b16encode(temp_gid)
      #the rid unlike gid, it's the rid or gid (if rid == gid) will use for
      #retrieve object and send response to client
      signature = subscriber.getSignatureFromGid(gid)
      object = subscriber.getObjectFromGid(gid)
      LOG("gid is %s, found existing object = %s" %(gid, object), 300, "")
      object_id = domain.generateNewIdWithGenerator(object=destination, gid=gid)
      if signature is None:
        LOG("SynchronizationTool", 300, "creating a signature for gid = %s without ref" %(gid))
        signature = subscriber.newContent(portal_type='SyncML Signature',
                                          id=gid,
                                          content_type=conduit.getContentType()
                                         )
        if object is not None:
          LOG("SynchronizationTool", 300, "\tsetting ref %s" %(object.getPath()))
          signature.setReference(object.getPath())
      elif signature.getValidationState() == 'synchronized':
        signature.drift()
      force = signature.isForce()
      data_node = action.find('.//{%(ns)s}Item/{%(ns)s}Data'\
                                                    % {'ns': SYNCML_NAMESPACE})
      if data_node is not None:
        if len(data_node):
          data = etree.tostring(data_node[0])
        else:
          data = data_node.text or ''
      else:
        data = ''
      #LOG('applyActionList gid', 0, gid)
      #LOG('applyActionList data', 0, data)
      if not action.xpath('.//syncml:Item/syncml:MoreData',
                          namespaces=action.nsmap):
        # This is the last chunk of a partial xml
        # or this is just an entire data chunk
        data_subnode = None
        if signature.hasPartialData():
          # rebuild the entire data
          signature.appendPartialData(data)
          # fetch data as string
          data_subnode = signature.getPartialData()
          # clear partial data cache on Signature
          signature.setPartialData(None)
          #LOG('applyActionList', DEBUG, 'data_subnode: %s' % data_subnode)
          if conduit.getContentType() == 'text/xml':
            data_subnode = etree.XML(data_subnode, parser=parser)
        else:
          if conduit.getContentType() != 'text/xml':
            data_subnode = getDataText(action)
          else:
            data_subnode = getDataSubNode(action)
        if action.xpath('local-name()') == 'Add':
          # Then store the xml of this new subobject
          reset = False
          if object is None:
            add_data = conduit.addNode(xml=data_subnode,
                                       object=destination,
                                       object_id=object_id,
                                       signature=signature,
                                       domain=domain)
            conflict_list.extend(add_data['conflict_list'])
            # Retrieve directly the object from addNode
            object = add_data['object']
            if object is not None:
              signature.setReference(object.getPath())
          else:
            reset = True
            # Object was retrieved but need to be updated without recreated
            # usefull when an object is only deleted by workflow.
            if data_subnode is not None:
              actual_xml = conduit.getXMLFromObjectWithGid(object, gid,
                             xml_mapping=\
                             domain.getXmlBindingGeneratorMethodId(force=True),
                             context_document=subscriber.getPath())
              # use gid to compare because their ids can be different
              data_subnode = conduit.replaceIdFromXML(data_subnode, 'gid', gid)
              # produce xupdate
              data_subnode = conduit.generateDiff(data_subnode, actual_xml)
            conflict_list.extend(conduit.updateNode(
                                        xml=data_subnode,
                                        object=object,
                                        previous_xml=actual_xml,
                                        force=force,
                                        simulate=simulate,
                                        reset=reset,
                                        signature=signature,
                                        domain=domain))
            xml_object = conduit.getXMLFromObjectWithId(object,
                                       xml_mapping=\
                                       domain.getXmlBindingGeneratorMethodId(),
                                       context_document=subscriber.getPath())
            signature.setTemporaryData(xml_object)
          if object is not None:
            #LOG('applyActionList', DEBUG, 'addNode, found the object')
            if reset:
              #After a reset we want copy the LAST XML view on Signature.
              #this implementation is not sufficient, need to be improved.
              if not isinstance(xml_object, str):
                xml_object = etree.tostring(xml_object, encoding='utf-8',
                                            pretty_print=True)
            else: 
              xml_object = conduit.getXMLFromObjectWithId(object,
                                       xml_mapping=\
                                       domain.getXmlBindingGeneratorMethodId(),
                                       context_document=subscriber.getPath())
            #if signature.getValidationState() != 'synchronized':
            signature.synchronize()
            signature.setReference(object.getPath())
            signature.setData(xml_object)
            xml_confirmation_list.append(self.SyncMLConfirmation(
                                                        cmd_id=cmd_id,
                                                        cmd='Add',
                                                        sync_code='item_added',
                                                        remote_xml=action))
            cmd_id +=1
        elif action.xpath('local-name()') == 'Replace':
          #LOG('applyActionList', INFO, 'object: %s will be updated...' % str(object))
          if object is not None:
            #LOG('applyActionList', DEBUG, 'object: %s will be updated...' % object.id)
            signature = subscriber.getSignatureFromGid(gid)
            #LOG('applyActionList', DEBUG, 'previous signature: %s' % str(signature))
            previous_xml = signature.getData()
            if previous_xml:
              # can be None
              previous_xml = conduit.replaceIdFromXML(previous_xml, 'gid', gid)
            conflict_list += conduit.updateNode(xml=data_subnode,
                                                object=object,
                                                previous_xml=previous_xml,
                                                force=force,
                                                signature=signature,
                                                simulate=simulate,
                                                domain=domain)
            if previous_xml:
              # here compute patched data with given diff
              xml_object = conduit.applyDiff(previous_xml, data_subnode)
              xml_object = conduit.replaceIdFromXML(xml_object, 'id',
                                                    object.getId(),
                                                    as_string=True)
2209
            elif conduit.getContentType() == 'text/xml':
2210 2211 2212
              # no previous, this is the first synchronization
              # store xml view from object as it has been provided.
              xml_object = etree.tostring(data_subnode)
2213 2214
            else:
              xml_object = data_subnode
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
            signature.setTemporaryData(xml_object)
            if conflict_list:
              status_code = 'conflict'
              signature.changeToConflict()
              data_subnode_string = etree.tostring(data_subnode,
                                                   encoding='utf-8')
              signature.setPartialData(data_subnode_string)
            else: #if not simulate:
              signature.synchronize()
            xml_confirmation_list.append(self.SyncMLConfirmation(
                                                         cmd_id=cmd_id,
                                                         cmd='Replace',
                                                         sync_code=status_code,
                                                         remote_xml=action))
            cmd_id += 1
            if simulate:
              # This means we are on the publisher side and we want to store
              # the xupdate from the subscriber and we also want to generate
              # the current xupdate from the last synchronization
              if not isinstance(data_subnode, str):
                data_subnode = etree.tostring(data_subnode, encoding='utf-8')
              #LOG('applyActionList, subscriber_xupdate:', TRACE, data_subnode_string)
              signature.setSubscriberXupdate(data_subnode)

        elif action.xpath('local-name()') == 'Delete':
          #LOG("applyactionlist delete", INFO, "")
          object_id = signature.getId()
          #LOG('applyActionList Delete on : ', DEBUG, (signature.getId(), subscriber.getObjectFromGid(object_id)))
          if conduit.getContentType() != 'text/xml':
            data_subnode = getDataText(action)
          else:
            data_subnode = getDataSubNode(action)
          #LOG('applyActionList, object gid to delete :', INFO, subscriber.getObjectFromGid(object_id))
          document = subscriber.getObjectFromGid(object_id)
          if document is not None:
          #if the object exist:
            conduit.deleteNode(xml=data_subnode, object=destination,
                               object_id=document.getId(),
2253 2254
                               signature=signature,
                               domain=domain)
2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
            subscriber._delObject(gid)
          xml_confirmation_list.append(self.SyncMLConfirmation(
                                                         cmd_id=cmd_id,
                                                         cmd='Delete',
                                                         sync_code=status_code,
                                                         remote_xml=action))
      else: # We want to retrieve more data
        if signature.getValidationState() != 'partial':
          signature.changeToPartial()
        signature.appendPartialData(data)
        #LOG('applyActionList', INFO, 'waiting more data for :%s' % signature.getId())
        #LOG('applyActionList', INFO, 'waiting more data for :%s' % object.getPath())
        #LOG('applyActionList', INFO, data)
        xml_confirmation_list.append(self.SyncMLConfirmation(
                                             cmd_id=cmd_id,
                                             cmd="%s" % action.xpath('name()'),
                                             sync_code=status_code,
                                             remote_xml=action))
      if conflict_list and signature is not None and\
                                  signature.getValidationState() != 'conflict':
        # We had a conflict
        signature.changeToConflict()

    return xml_confirmation_list, has_next_action, cmd_id

  security.declarePrivate('addXMLObject')
  def addXMLObject(self, **kw):
    """
      Add an object with the SyncML protocol
    """
    LOG("addXMLObject", INFO, "xml = %s" %(kw.get('xml_string', '')))
    return self._createAddOrReplaceNode('Add', **kw)

  security.declarePrivate('addXMLObject')
  def replaceXMLObject(self, **kw):
    """
      Replace an object with the SyncML protocol
    """
    LOG("replaceXMLObject", INFO, "xml = %s" %(kw.get('xml_string', '')))
    return self._createAddOrReplaceNode('Replace', **kw)

  def _createAddOrReplaceNode(self, id_tag, cmd_id=0, object=None,
                              xml_string=None, more_data=False, gid=None,
                              rid=None, media_type=None):
    """Mixin for addXMLObject() and replaceXMLObject()
    """
    data_node = E.Data()
    if media_type == 'text/xml':
      if isinstance(xml_string, str):
        data_node.append(etree.XML(xml_string, parser=parser))
      elif isinstance(xml_string, etree.CDATA):
        #xml_string could be Data element if partial XML
        data_node.text = xml_string
      else:
        data_node.append(xml_string)
    else:
      if isinstance(xml_string, etree.CDATA):
        data_node.text = xml_string
      else:
        cdata = etree.CDATA(xml_string.decode('utf-8'))
        data_node.text = cdata
    main_tag = Element('{%s}%s' % (SYNCML_NAMESPACE, id_tag))
    main_tag.append(E.CmdID('%s' % cmd_id))
    main_tag.append(E.Meta(E.Type(media_type)))
    main_tag.append(E.Item(E.Source(E.LocURI(gid)), data_node))
    if more_data:
      item_node = main_tag.find('{%s}Item' % SYNCML_NAMESPACE)
      item_node.append(E.MoreData())
    return etree.tostring(main_tag, encoding='utf-8', pretty_print=True)

  security.declarePrivate('deleteXMLObject')
  def deleteXMLObject(self, cmd_id=0, object_gid=None, rid=None):
    """
      Delete an object with the SyncML protocol
    """
    LOG("deleteXMLObject", INFO, "object_gid = %s" %(object_gid))
    if rid:
      elem_to_append = E.Target(E.LocURI('%s' % rid))
    else:
      elem_to_append = E.Source(E.LocURI('%s' % object_gid))
    xml = (E.Delete(
             E.CmdID('%s' % cmd_id),
             E.Item(
               elem_to_append
               )
             ))
    return etree.tostring(xml, encoding='utf-8', pretty_print=True)

  security.declarePrivate('SyncMLConfirmation')
  def SyncMLConfirmation(self, cmd_id=None, target_ref=None, cmd=None,
      sync_code=None, msg_ref=None, cmd_ref=None, source_ref=None,
      remote_xml=None):
    """
    This is used in order to confirm that an object was correctly
    synchronized
    """
    if remote_xml is not None:
      msg_id = '%s' % remote_xml.xpath('string(/syncml:SyncML/'\
                                        'syncml:SyncHdr/syncml:MsgID)',
                                        namespaces=remote_xml.nsmap)
      cmd_ref = '%s' % remote_xml.xpath('string(.//syncml:CmdID)',
                                        namespaces=remote_xml.nsmap)
      target_ref = '%s' % remote_xml.xpath('string(.//syncml:Target/'\
                                           'syncml:LocURI)',
                                           namespaces=remote_xml.nsmap)
      source_ref = '%s' % remote_xml.xpath('string(.//syncml:Source/'\
                                           'syncml:LocURI)',
                                           namespaces=remote_xml.nsmap)
    xml = E.Status()
    if cmd_id:
      xml.append(E.CmdID('%s' % cmd_id))
    if msg_ref:
      xml.append(E.MsgRef(msg_id))
    if cmd_ref:
      xml.append(E.CmdRef(cmd_ref))
    if cmd:
      xml.append(E.Cmd(cmd))
    if target_ref:
      xml.append(E.TargetRef(target_ref))
    if source_ref:
      xml.append(E.SourceRef(source_ref))
    if sync_code:
      xml.append(E.Data(resolveSyncmlStatusCode(self, sync_code)))
    return xml

  security.declarePrivate('sendSyncModif')
  def sendSyncModif(self, syncml_data_list, cmd_id_before_getsyncmldata,
                    subscriber, domain, xml_confirmation_list, remote_xml,
                    xml_tree, has_status_list, has_response):
    sync_body = xml_tree.find('SyncBody')
    if sync_body is None:
      sync_body = xml_tree.xpath('syncml:SyncBody',
                                 namespaces=xml_tree.nsmap)[0]
    if syncml_data_list:
      sync_node = E.Sync(E.CmdID('%s' % cmd_id_before_getsyncmldata))
      sync_body.append(sync_node)
      target_uri = subscriber.getDestinationReference()
      if target_uri:
        sync_node.append(E.Target(E.LocURI(target_uri)))
      source_uri = subscriber.getSourceReference()
      if source_uri:
        sync_node.append(E.Source(E.LocURI(source_uri)))
      for syncml_data in syncml_data_list:
        sync_node.append(etree.XML(syncml_data, parser=parser))
    for xml_confirmation in xml_confirmation_list:
      if isinstance(xml_confirmation, str):
        xml_confirmation = etree.XML(xml_confirmation, parser=parser)
      sync_body.append(xml_confirmation)

    sync_finished = False
    if domain.getPortalType() == 'SyncML Publication': # We always reply
      # When the publication receive the response Final and the modification 
      # data is finished so the publication send the tag "Final"
      if not remote_xml.xpath('string(/syncml:SyncML/syncml:SyncBody/'\
                              'syncml:Sync)', namespaces=remote_xml.nsmap)\
        and not xml_confirmation_list and not syncml_data_list\
        and checkFinal(remote_xml):
        sync_body.append(E.Final())
        sync_finished = True
      xml_string = etree.tostring(xml_tree, encoding='utf-8',
                                  pretty_print=True)
      subscriber.setLastSentMessage(xml_string)
      self.sendResponse(from_url=domain.getUrlString(),
                        to_url=subscriber.getSubscriptionUrlString(),
                        sync_id=subscriber.getDestinationReference(),
                        xml=xml_string,
                        domain=domain,
                        content_type=domain.getContentType())
      if sync_finished:
        LOG('this is the end of the synchronization session from PUB !!!',
            INFO, domain.getTitle())
        if subscriber.getAuthenticationState() == 'logged_in':
          subscriber.logout()
        if domain.getAuthenticationState() == 'logged_in':
          domain.logout()
        subscriber._edit(authenticated_user=None,
                         remaining_object_path_list=None)
      has_response = True
    elif domain.getPortalType() == 'SyncML Subscription':
      # the modification data is finished on the subscription so the tag
      # "Final" sent to the publication
      if not checkAlert(remote_xml) and not xml_confirmation_list\
                                                      and not syncml_data_list:
        sync_body.append(E.Final())
        sync_finished = True
      xml_string = etree.tostring(xml_tree, encoding='utf-8', pretty_print=True)
      if not sync_finished or not checkFinal(remote_xml):
        subscriber.setLastSentMessage(xml_string)
        self.sendResponse(
                  from_url=domain.getSubscriptionUrlString(),
                  to_url=domain.getUrlString(),
                  sync_id=domain.getDestinationReference(),
                  xml=xml_string, domain=domain,
                  content_type=domain.getContentType())
        has_response = True
      #When the receive the final element and the sub finished synchronization
      else:
        if domain.isOneWayFromServer() or subscriber.isOneWayFromClient():
          self.deleteRemainObjectList(domain, subscriber)
        has_response = False
        LOG('this is the end of the synchronization session from SUB !!!',
            INFO, domain.getTitle())
        if domain.getAuthenticationState() == 'logged_in':
          domain.logout()
        domain._edit(zope_user=None)
    return {'has_response': has_response, 'xml': xml_string}

  security.declareProtected(Permissions.ModifyPortalContent,
                            'deleteRemainObjectList')
  def deleteRemainObjectList(self, domain, subscriber):
    """
    This method allow deletion on not synchronized Objects at the end of Synchronization session.
    Usefull only after reseting in One Way Sync
    """
    object_list = domain.getObjectList()
    gid_list = [domain.getGidFromObject(x) for x in object_list]
    domain_path = domain.getPath()
    subscriber_path = subscriber.getPath()
    while len(gid_list):
      sliced_gid_list = [gid_list.pop() for i in gid_list[:MAX_OBJECTS]]
      #Split List Processing in activities
      self.activate(activity='SQLQueue',
                    tag=domain.getId(),
                    priority=ACTIVITY_PRIORITY)\
                                   .activateDeleteRemainObjectList(domain_path,
                                                               subscriber_path,
                                                               sliced_gid_list)

  security.declareProtected(Permissions.ModifyPortalContent,
                            'activateDeleteRemainObjectList')
  def activateDeleteRemainObjectList(self, domain_path, subscriber_path, gid_list):
    """
    Execute Deletion in Activities
    """
    domain = self.unrestrictedTraverse(domain_path)
    subscriber = self.unrestrictedTraverse(subscriber_path)
    folder = domain.getSourceValue()
    conduit_name = subscriber.getConduitModuleId()
    conduit = getConduitByName(conduit_name)
    for gid in gid_list:
      if subscriber.getSignatureFromGid(gid) is None:
        object_id = b16decode(gid)
        conduit.deleteObject(object=folder, object_id=object_id)

  security.declareProtected(Permissions.ModifyPortalContent,
                            'activateSyncModif')
  def activateSyncModif(self, **kw):
    domain = self.unrestrictedTraverse(kw['domain_relative_url'])
    subscriber = self.unrestrictedTraverse(kw['subscriber_relative_url'])
    conduit = subscriber.getConduitModuleId()
    result = self.getSyncMLData(domain=domain, subscriber=subscriber,
                                conduit=conduit,
                                maximum_object_list_len=MAX_OBJECTS, **kw)
    syncml_data_list = result['syncml_data_list']
    cmd_id = result['cmd_id']
    kw['syncml_data_list'] = syncml_data_list
    kw['cmd_id'] = cmd_id
    finished = result['finished']
    if not finished:
      domain.activate(activity='SQLQueue',
                      tag=domain.getId(),
                      priority=self.PRIORITY).activateSyncModif(**kw)
    else:
      cmd_id = result['cmd_id']
      cmd_id_before_getsyncmldata = kw['cmd_id_before_getsyncmldata']
      remote_xml = etree.XML(kw['remote_xml'], parser=parser)
      xml_tree = etree.XML(kw['xml_tree'], parser=parser)
      xml_confirmation_list = kw['xml_confirmation_list']
      has_status_list = kw['has_status_list']
      has_response = kw['has_response']
      return self.sendSyncModif(
                        syncml_data_list,
                        cmd_id_before_getsyncmldata,
                        subscriber,
                        domain,
                        xml_confirmation_list,
                        remote_xml,
                        xml_tree,
                        has_status_list,
                        has_response)

  security.declareProtected(Permissions.ModifyPortalContent, 'addNode')
  def addNode(self, conduit='ERP5Conduit', **kw):
    """
    """
    # Import the conduit and get it
    conduit_object = getConduitByName(conduit)
    return conduit_object.addNode(**kw)

InitializeClass( SynchronizationTool )