Commit 8af948d3 authored by Nicolas Delaby's avatar Nicolas Delaby

Objectify all XML handling

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@25356 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 3cb078f3
...@@ -1029,7 +1029,7 @@ class ERP5Conduit(XMLSyncUtilsMixin): ...@@ -1029,7 +1029,7 @@ class ERP5Conduit(XMLSyncUtilsMixin):
""" """
return an xml string corresponding to the node return an xml string corresponding to the node
""" """
return etree.tostring(node, encoding='utf-8') return etree.tostring(node, encoding='utf-8', pretty_print=True)
def getGidFromObject(self, object): def getGidFromObject(self, object):
""" """
......
...@@ -39,6 +39,8 @@ from AccessControl.SecurityManagement import newSecurityManager ...@@ -39,6 +39,8 @@ from AccessControl.SecurityManagement import newSecurityManager
import commands import commands
from DateTime import DateTime from DateTime import DateTime
from zLOG import LOG, DEBUG, INFO, WARNING from zLOG import LOG, DEBUG, INFO, WARNING
from lxml.etree import Element, SubElement
from lxml import etree
class PublicationSynchronization(XMLSyncUtils): class PublicationSynchronization(XMLSyncUtils):
""" """
...@@ -76,17 +78,15 @@ class PublicationSynchronization(XMLSyncUtils): ...@@ -76,17 +78,15 @@ class PublicationSynchronization(XMLSyncUtils):
subscriber.setSourceURI(self.getTargetURI(xml_client)) subscriber.setSourceURI(self.getTargetURI(xml_client))
subscriber.setTargetURI(self.getSourceURI(xml_client)) subscriber.setTargetURI(self.getSourceURI(xml_client))
xml_list = []
xml = xml_list.append
cmd_id = 1 # specifies a SyncML message-unique command identifier cmd_id = 1 # specifies a SyncML message-unique command identifier
xml('<SyncML>\n') xml = Element('SyncML')
# syncml header # syncml header
xml(self.SyncMLHeader(subscriber.getSessionId(), xml.append(self.SyncMLHeader(subscriber.getSessionId(),
subscriber.getMessageId(), subscriber.getMessageId(),
subscriber.getSubscriptionUrl(), subscriber.getSubscriptionUrl(),
publication.getPublicationUrl())) publication.getPublicationUrl()))
# syncml body # syncml body
xml(' <SyncBody>\n') sync_body = SubElement(xml, 'SyncBody')
#at the begining, the code is initialised at UNAUTHORIZED #at the begining, the code is initialised at UNAUTHORIZED
...@@ -95,7 +95,7 @@ class PublicationSynchronization(XMLSyncUtils): ...@@ -95,7 +95,7 @@ class PublicationSynchronization(XMLSyncUtils):
auth_code = self.AUTH_REQUIRED auth_code = self.AUTH_REQUIRED
LOG("PubSyncInit there's no credential !!!", INFO,'') LOG("PubSyncInit there's no credential !!!", INFO,'')
# Prepare the xml message for the Sync initialization package # Prepare the xml message for the Sync initialization package
xml(self.SyncMLChal(cmd_id, "SyncHdr", sync_body.append(self.SyncMLChal(cmd_id, "SyncHdr",
publication.getPublicationUrl(), subscriber.getSubscriptionUrl(), publication.getPublicationUrl(), subscriber.getSubscriptionUrl(),
publication.getAuthenticationFormat(), publication.getAuthenticationFormat(),
publication.getAuthenticationType(), auth_code)) publication.getAuthenticationType(), auth_code))
...@@ -106,8 +106,8 @@ class PublicationSynchronization(XMLSyncUtils): ...@@ -106,8 +106,8 @@ class PublicationSynchronization(XMLSyncUtils):
auth_code, auth_code,
cmd_id, cmd_id,
next_anchor, next_anchor,
subscription=subscriber).values() subscription=subscriber)
xml(xml_status) sync_body.extend(xml_status)
else: else:
# If slow sync, then resend everything # If slow sync, then resend everything
if alert_code == self.SLOW_SYNC and \ if alert_code == self.SLOW_SYNC and \
...@@ -119,7 +119,7 @@ class PublicationSynchronization(XMLSyncUtils): ...@@ -119,7 +119,7 @@ class PublicationSynchronization(XMLSyncUtils):
# Check if the last time synchronization is the same as the client one # Check if the last time synchronization is the same as the client one
if subscriber.getNextAnchor() != last_anchor: if subscriber.getNextAnchor() != last_anchor:
if last_anchor in (None, ''): if not last_anchor:
LOG('PubSyncInit', INFO, 'anchor null') LOG('PubSyncInit', INFO, 'anchor null')
else: else:
mess = '\nsubscriber.getNextAnchor:\t%s\nsubscriber.getLastAnchor:\t%s\ mess = '\nsubscriber.getNextAnchor:\t%s\nsubscriber.getLastAnchor:\t%s\
...@@ -159,24 +159,30 @@ class PublicationSynchronization(XMLSyncUtils): ...@@ -159,24 +159,30 @@ class PublicationSynchronization(XMLSyncUtils):
# Prepare the xml message for the Sync initialization package # Prepare the xml message for the Sync initialization package
if auth_code == self.AUTH_ACCEPTED: if auth_code == self.AUTH_ACCEPTED:
xml_status, cmd_id = self.SyncMLStatus(xml_client, auth_code, xml_status, cmd_id = self.SyncMLStatus(xml_client, auth_code,
cmd_id, next_anchor, subscription=subscriber).values() cmd_id, next_anchor,
xml(xml_status) subscription=subscriber)
sync_body.extend(xml_status)
# alert message # alert message
xml(self.SyncMLAlert(cmd_id, sync_type, subscriber.getTargetURI(), sync_body.append(self.SyncMLAlert(cmd_id, sync_type,
subscriber.getSourceURI(), subscriber.getLastAnchor(), subscriber.getTargetURI(),
next_anchor)) subscriber.getSourceURI(),
subscriber.getLastAnchor(),
next_anchor))
cmd_id += 1 cmd_id += 1
else: else:
# chal message # chal message
xml(self.SyncMLChal(cmd_id, "SyncHdr", sync_body.append(self.SyncMLChal(cmd_id, "SyncHdr",
publication.getPublicationUrl(), subscriber.getSubscriptionUrl(), publication.getPublicationUrl(),
publication.getAuthenticationFormat(), subscriber.getSubscriptionUrl(),
publication.getAuthenticationType(), auth_code)) publication.getAuthenticationFormat(),
publication.getAuthenticationType(),
auth_code))
cmd_id += 1 cmd_id += 1
xml_status, cmd_id = self.SyncMLStatus(xml_client, xml_status, cmd_id = self.SyncMLStatus(xml_client,
self.AUTH_REQUIRED, cmd_id, next_anchor, self.AUTH_REQUIRED, cmd_id,
subscription=subscriber).values() next_anchor,
xml(xml_status) subscription=subscriber)
sync_body.extend(xml_status)
# We have to set every object as NOT_SYNCHRONIZED # We have to set every object as NOT_SYNCHRONIZED
subscriber.startSynchronization() subscriber.startSynchronization()
...@@ -187,19 +193,17 @@ class PublicationSynchronization(XMLSyncUtils): ...@@ -187,19 +193,17 @@ class PublicationSynchronization(XMLSyncUtils):
has been started from the server (forbiden)" has been started from the server (forbiden)"
# a synchronisation is always starded from a client and can't be from # a synchronisation is always starded from a client and can't be from
# a server ! # a server !
sync_body.append(Element('Final'))
xml(' <Final/>\n') xml_string = etree.tostring(xml, encoding='utf-8', pretty_print=True)
xml(' </SyncBody>\n')
xml('</SyncML>\n')
xml_a = ''.join(xml_list)
if publication.getSyncContentType() == self.CONTENT_TYPE['SYNCML_WBXML']: if publication.getSyncContentType() == self.CONTENT_TYPE['SYNCML_WBXML']:
xml_a = self.xml2wbxml(xml_a) xml_string = self.xml2wbxml(xml_string)
self.sendResponse(from_url=publication.getPublicationUrl(), self.sendResponse(from_url=publication.getPublicationUrl(),
to_url=subscriber.getSubscriptionUrl(), sync_id=publication.getTitle(), to_url=subscriber.getSubscriptionUrl(),
xml=xml_a, domain=publication, sync_id=publication.getTitle(),
content_type=publication.getSyncContentType()) xml=xml_string, domain=publication,
content_type=publication.getSyncContentType())
return {'has_response':1, 'xml':xml_a} return {'has_response':1, 'xml':xml_string}
def PubSyncModif(self, publication, xml_client): def PubSyncModif(self, publication, xml_client):
""" """
......
...@@ -34,6 +34,8 @@ from Conduit.ERP5Conduit import ERP5Conduit ...@@ -34,6 +34,8 @@ from Conduit.ERP5Conduit import ERP5Conduit
from AccessControl import getSecurityManager from AccessControl import getSecurityManager
from DateTime import DateTime from DateTime import DateTime
from zLOG import LOG, DEBUG, INFO from zLOG import LOG, DEBUG, INFO
from lxml.etree import Element, SubElement
from lxml import etree
class SubscriptionSynchronization(XMLSyncUtils): class SubscriptionSynchronization(XMLSyncUtils):
...@@ -51,54 +53,50 @@ class SubscriptionSynchronization(XMLSyncUtils): ...@@ -51,54 +53,50 @@ class SubscriptionSynchronization(XMLSyncUtils):
subscription.setZopeUser(user) subscription.setZopeUser(user)
subscription.setAuthenticated(True) subscription.setAuthenticated(True)
xml_list = [] xml = Element('SyncML')
xml = xml_list.append
xml('<SyncML>\n')
# syncml header # syncml header
xml(self.SyncMLHeader(subscription.incrementSessionId(), xml.append(self.SyncMLHeader(subscription.incrementSessionId(),
subscription.incrementMessageId(), subscription.getPublicationUrl(), subscription.incrementMessageId(), subscription.getPublicationUrl(),
subscription.getSubscriptionUrl(), source_name=subscription.getLogin())) subscription.getSubscriptionUrl(), source_name=subscription.getLogin()))
# syncml body # syncml body
xml(' <SyncBody>\n') sync_body = SubElement(xml, 'SyncBody')
# We have to set every object as NOT_SYNCHRONIZED # We have to set every object as NOT_SYNCHRONIZED
subscription.startSynchronization() subscription.startSynchronization()
# alert message # alert message
xml(self.SyncMLAlert(cmd_id, subscription.getSynchronizationType(), sync_body.append(self.SyncMLAlert(cmd_id, subscription.getSynchronizationType(),
subscription.getTargetURI(), subscription.getTargetURI(),
subscription.getSourceURI(), subscription.getSourceURI(),
subscription.getLastAnchor(), subscription.getLastAnchor(),
subscription.getNextAnchor())) subscription.getNextAnchor()))
cmd_id += 1 cmd_id += 1
syncml_put = self.SyncMLPut(cmd_id, subscription) syncml_put = self.SyncMLPut(cmd_id, subscription)
if syncml_put not in ('', None): if syncml_put is not None:
xml(syncml_put) sync_body.append(syncml_put)
cmd_id += 1 cmd_id += 1
xml(' </SyncBody>\n')
xml('</SyncML>\n')
xml_a = ''.join(xml_list)
xml_string = etree.tostring(xml, encoding='utf-8', xml_declaration=True,
pretty_print=True)
self.sendResponse(from_url=subscription.subscription_url, self.sendResponse(from_url=subscription.subscription_url,
to_url=subscription.publication_url, sync_id=subscription.getTitle(), to_url=subscription.publication_url,
xml=xml_a,domain=subscription, sync_id=subscription.getTitle(),
content_type=subscription.getSyncContentType()) xml=xml_string, domain=subscription,
content_type=subscription.getSyncContentType())
return {'has_response':1,'xml':xml_a} return {'has_response':1, 'xml':xml_string}
def SubSyncCred (self, subscription, msg=None, RESPONSE=None): def SubSyncCred (self, subscription, msg=None, RESPONSE=None):
""" """
This method send crendentials This method send crendentials
""" """
cmd_id = 1 # specifies a SyncML message-unique command identifier cmd_id = 1 # specifies a SyncML message-unique command identifier
xml_list = [] xml = Element('SyncML')
xml = xml_list.append
xml('<SyncML>\n')
# syncml header # syncml header
data = "%s:%s" % (subscription.getLogin(), subscription.getPassword()) data = "%s:%s" % (subscription.getLogin(), subscription.getPassword())
data=subscription.encode(subscription.getAuthenticationFormat(), data) data = subscription.encode(subscription.getAuthenticationFormat(), data)
xml(self.SyncMLHeader( xml.append(self.SyncMLHeader(
subscription.incrementSessionId(), subscription.incrementSessionId(),
subscription.incrementMessageId(), subscription.incrementMessageId(),
subscription.getPublicationUrl(), subscription.getPublicationUrl(),
...@@ -109,31 +107,32 @@ class SubscriptionSynchronization(XMLSyncUtils): ...@@ -109,31 +107,32 @@ class SubscriptionSynchronization(XMLSyncUtils):
authentication_type=subscription.getAuthenticationType())) authentication_type=subscription.getAuthenticationType()))
# syncml body # syncml body
xml(' <SyncBody>\n') sync_body = SubElement(xml, 'SyncBody')
# We have to set every object as NOT_SYNCHRONIZED # We have to set every object as NOT_SYNCHRONIZED
subscription.startSynchronization() subscription.startSynchronization()
# alert message # alert message
xml(self.SyncMLAlert(cmd_id, subscription.getSynchronizationType(), sync_body.append(self.SyncMLAlert(cmd_id, subscription.getSynchronizationType(),
subscription.getTargetURI(), subscription.getTargetURI(),
subscription.getSourceURI(), subscription.getSourceURI(),
subscription.getLastAnchor(), subscription.getLastAnchor(),
subscription.getNextAnchor())) subscription.getNextAnchor()))
cmd_id += 1 cmd_id += 1
xml(self.SyncMLPut(cmd_id, subscription)) syncml_put = self.SyncMLPut(cmd_id, subscription)
if syncml_put is not None:
sync_body.append(syncml_put)
cmd_id += 1 cmd_id += 1
xml(' <Final/>\n') sync_body.append(Element('Final'))
xml(' </SyncBody>\n') xml_string = etree.tostring(xml, encoding='utf-8', xml_declaration=True,
xml('</SyncML>\n') pretty_print=True)
xml_a = ''.join(xml_list)
self.sendResponse(from_url=subscription.subscription_url, self.sendResponse(from_url=subscription.subscription_url,
to_url=subscription.publication_url, sync_id=subscription.getTitle(), to_url=subscription.publication_url,
xml=xml_a, domain=subscription, sync_id=subscription.getTitle(),
content_type=subscription.getSyncContentType()) xml=xml_string, domain=subscription,
content_type=subscription.getSyncContentType())
return {'has_response':1, 'xml':xml_a} return {'has_response':1, 'xml':xml_string}
def SubSyncModif(self, subscription, xml_client): def SubSyncModif(self, subscription, xml_client):
""" """
......
...@@ -41,7 +41,7 @@ class SyncCode(Persistent): ...@@ -41,7 +41,7 @@ class SyncCode(Persistent):
ONE_WAY_FROM_SERVER = 204 ONE_WAY_FROM_SERVER = 204
WAITING_DATA = 214 WAITING_DATA = 214
REFRESH_REQUIRED = 508 REFRESH_REQUIRED = 508
CODE_LIST = ( TWO_WAY, ONE_WAY_FROM_SERVER ) CODE_LIST = ( TWO_WAY, ONE_WAY_FROM_SERVER, )
# SyncML Status Codes # SyncML Status Codes
SUCCESS = 200 SUCCESS = 200
......
...@@ -92,8 +92,8 @@ class SynchronizationTool( SubscriptionSynchronization, ...@@ -92,8 +92,8 @@ class SynchronizationTool( SubscriptionSynchronization,
# On the server, this is use to keep track of the temporary # On the server, this is use to keep track of the temporary
# copies. # copies.
objectsToRemove = [] objectsToRemove = []
security = ClassSecurityInfo() security = ClassSecurityInfo()
# #
...@@ -712,12 +712,12 @@ class SynchronizationTool( SubscriptionSynchronization, ...@@ -712,12 +712,12 @@ class SynchronizationTool( SubscriptionSynchronization,
if solve_conflict: if solve_conflict:
copy_path = conflict.getCopyPath() copy_path = conflict.getCopyPath()
signature.delConflict(conflict) signature.delConflict(conflict)
if signature.getConflictList() == []: if not signature.getConflictList():
if copy_path is not None: if copy_path is not None:
# Delete the copy of the object if the there is one # Delete the copy of the object if the there is one
directory = object.aq_parent directory = object.aq_parent
copy_id = copy_path[-1] copy_id = copy_path[-1]
if hasattr(directory.aq_base, 'hasObject'): if getattr(directory.aq_base, 'hasObject', None) is not None:
# optimize the case of a BTree folder # optimize the case of a BTree folder
if directory.hasObject(id): if directory.hasObject(id):
directory._delObject(copy_id) directory._delObject(copy_id)
...@@ -828,8 +828,6 @@ class SynchronizationTool( SubscriptionSynchronization, ...@@ -828,8 +828,6 @@ class SynchronizationTool( SubscriptionSynchronization,
if content_type == self.CONTENT_TYPE['SYNCML_WBXML']: if content_type == self.CONTENT_TYPE['SYNCML_WBXML']:
xml = self.xml2wbxml(xml) xml = self.xml2wbxml(xml)
#LOG('sendHttpResponse, xml after wbxml: \n', DEBUG, self.hexdump(xml)) #LOG('sendHttpResponse, xml after wbxml: \n', DEBUG, self.hexdump(xml))
if isinstance(xml, unicode):
xml = xml.encode('utf-8')
if domain is not None: if domain is not None:
gpg_key = domain.getGPGKey() gpg_key = domain.getGPGKey()
if gpg_key not in ('',None): if gpg_key not in ('',None):
...@@ -868,7 +866,7 @@ class SynchronizationTool( SubscriptionSynchronization, ...@@ -868,7 +866,7 @@ class SynchronizationTool( SubscriptionSynchronization,
content_type=content_type) content_type=content_type)
elif to_url.find('file://') == 0: elif to_url.find('file://') == 0:
filename = to_url[len('file:/'):] filename = to_url[len('file:/'):]
stream = file(filename,'w') stream = file(filename, 'w')
stream.write(xml) stream.write(xml)
stream.close() stream.close()
# we have to use local files (unit testing for example # we have to use local files (unit testing for example
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment