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):
"""
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):
"""
......
......@@ -39,6 +39,8 @@ from AccessControl.SecurityManagement import newSecurityManager
import commands
from DateTime import DateTime
from zLOG import LOG, DEBUG, INFO, WARNING
from lxml.etree import Element, SubElement
from lxml import etree
class PublicationSynchronization(XMLSyncUtils):
"""
......@@ -76,17 +78,15 @@ class PublicationSynchronization(XMLSyncUtils):
subscriber.setSourceURI(self.getTargetURI(xml_client))
subscriber.setTargetURI(self.getSourceURI(xml_client))
xml_list = []
xml = xml_list.append
cmd_id = 1 # specifies a SyncML message-unique command identifier
xml('<SyncML>\n')
xml = Element('SyncML')
# syncml header
xml(self.SyncMLHeader(subscriber.getSessionId(),
xml.append(self.SyncMLHeader(subscriber.getSessionId(),
subscriber.getMessageId(),
subscriber.getSubscriptionUrl(),
publication.getPublicationUrl()))
# syncml body
xml(' <SyncBody>\n')
sync_body = SubElement(xml, 'SyncBody')
#at the begining, the code is initialised at UNAUTHORIZED
......@@ -95,7 +95,7 @@ class PublicationSynchronization(XMLSyncUtils):
auth_code = self.AUTH_REQUIRED
LOG("PubSyncInit there's no credential !!!", INFO,'')
# 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.getAuthenticationFormat(),
publication.getAuthenticationType(), auth_code))
......@@ -106,8 +106,8 @@ class PublicationSynchronization(XMLSyncUtils):
auth_code,
cmd_id,
next_anchor,
subscription=subscriber).values()
xml(xml_status)
subscription=subscriber)
sync_body.extend(xml_status)
else:
# If slow sync, then resend everything
if alert_code == self.SLOW_SYNC and \
......@@ -119,7 +119,7 @@ class PublicationSynchronization(XMLSyncUtils):
# Check if the last time synchronization is the same as the client one
if subscriber.getNextAnchor() != last_anchor:
if last_anchor in (None, ''):
if not last_anchor:
LOG('PubSyncInit', INFO, 'anchor null')
else:
mess = '\nsubscriber.getNextAnchor:\t%s\nsubscriber.getLastAnchor:\t%s\
......@@ -159,24 +159,30 @@ class PublicationSynchronization(XMLSyncUtils):
# Prepare the xml message for the Sync initialization package
if auth_code == self.AUTH_ACCEPTED:
xml_status, cmd_id = self.SyncMLStatus(xml_client, auth_code,
cmd_id, next_anchor, subscription=subscriber).values()
xml(xml_status)
cmd_id, next_anchor,
subscription=subscriber)
sync_body.extend(xml_status)
# alert message
xml(self.SyncMLAlert(cmd_id, sync_type, subscriber.getTargetURI(),
subscriber.getSourceURI(), subscriber.getLastAnchor(),
sync_body.append(self.SyncMLAlert(cmd_id, sync_type,
subscriber.getTargetURI(),
subscriber.getSourceURI(),
subscriber.getLastAnchor(),
next_anchor))
cmd_id += 1
else:
# chal message
xml(self.SyncMLChal(cmd_id, "SyncHdr",
publication.getPublicationUrl(), subscriber.getSubscriptionUrl(),
sync_body.append(self.SyncMLChal(cmd_id, "SyncHdr",
publication.getPublicationUrl(),
subscriber.getSubscriptionUrl(),
publication.getAuthenticationFormat(),
publication.getAuthenticationType(), auth_code))
publication.getAuthenticationType(),
auth_code))
cmd_id += 1
xml_status, cmd_id = self.SyncMLStatus(xml_client,
self.AUTH_REQUIRED, cmd_id, next_anchor,
subscription=subscriber).values()
xml(xml_status)
self.AUTH_REQUIRED, cmd_id,
next_anchor,
subscription=subscriber)
sync_body.extend(xml_status)
# We have to set every object as NOT_SYNCHRONIZED
subscriber.startSynchronization()
......@@ -187,19 +193,17 @@ class PublicationSynchronization(XMLSyncUtils):
has been started from the server (forbiden)"
# a synchronisation is always starded from a client and can't be from
# a server !
xml(' <Final/>\n')
xml(' </SyncBody>\n')
xml('</SyncML>\n')
xml_a = ''.join(xml_list)
sync_body.append(Element('Final'))
xml_string = etree.tostring(xml, encoding='utf-8', pretty_print=True)
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(),
to_url=subscriber.getSubscriptionUrl(), sync_id=publication.getTitle(),
xml=xml_a, domain=publication,
to_url=subscriber.getSubscriptionUrl(),
sync_id=publication.getTitle(),
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):
"""
......
......@@ -34,6 +34,8 @@ from Conduit.ERP5Conduit import ERP5Conduit
from AccessControl import getSecurityManager
from DateTime import DateTime
from zLOG import LOG, DEBUG, INFO
from lxml.etree import Element, SubElement
from lxml import etree
class SubscriptionSynchronization(XMLSyncUtils):
......@@ -51,54 +53,50 @@ class SubscriptionSynchronization(XMLSyncUtils):
subscription.setZopeUser(user)
subscription.setAuthenticated(True)
xml_list = []
xml = xml_list.append
xml('<SyncML>\n')
xml = Element('SyncML')
# syncml header
xml(self.SyncMLHeader(subscription.incrementSessionId(),
xml.append(self.SyncMLHeader(subscription.incrementSessionId(),
subscription.incrementMessageId(), subscription.getPublicationUrl(),
subscription.getSubscriptionUrl(), source_name=subscription.getLogin()))
# syncml body
xml(' <SyncBody>\n')
sync_body = SubElement(xml, 'SyncBody')
# We have to set every object as NOT_SYNCHRONIZED
subscription.startSynchronization()
# alert message
xml(self.SyncMLAlert(cmd_id, subscription.getSynchronizationType(),
sync_body.append(self.SyncMLAlert(cmd_id, subscription.getSynchronizationType(),
subscription.getTargetURI(),
subscription.getSourceURI(),
subscription.getLastAnchor(),
subscription.getNextAnchor()))
cmd_id += 1
syncml_put = self.SyncMLPut(cmd_id, subscription)
if syncml_put not in ('', None):
xml(syncml_put)
if syncml_put is not None:
sync_body.append(syncml_put)
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,
to_url=subscription.publication_url, sync_id=subscription.getTitle(),
xml=xml_a,domain=subscription,
to_url=subscription.publication_url,
sync_id=subscription.getTitle(),
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):
"""
This method send crendentials
"""
cmd_id = 1 # specifies a SyncML message-unique command identifier
xml_list = []
xml = xml_list.append
xml('<SyncML>\n')
xml = Element('SyncML')
# syncml header
data = "%s:%s" % (subscription.getLogin(), subscription.getPassword())
data=subscription.encode(subscription.getAuthenticationFormat(), data)
xml(self.SyncMLHeader(
data = subscription.encode(subscription.getAuthenticationFormat(), data)
xml.append(self.SyncMLHeader(
subscription.incrementSessionId(),
subscription.incrementMessageId(),
subscription.getPublicationUrl(),
......@@ -109,31 +107,32 @@ class SubscriptionSynchronization(XMLSyncUtils):
authentication_type=subscription.getAuthenticationType()))
# syncml body
xml(' <SyncBody>\n')
sync_body = SubElement(xml, 'SyncBody')
# We have to set every object as NOT_SYNCHRONIZED
subscription.startSynchronization()
# alert message
xml(self.SyncMLAlert(cmd_id, subscription.getSynchronizationType(),
sync_body.append(self.SyncMLAlert(cmd_id, subscription.getSynchronizationType(),
subscription.getTargetURI(),
subscription.getSourceURI(),
subscription.getLastAnchor(),
subscription.getNextAnchor()))
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
xml(' <Final/>\n')
xml(' </SyncBody>\n')
xml('</SyncML>\n')
xml_a = ''.join(xml_list)
sync_body.append(Element('Final'))
xml_string = etree.tostring(xml, encoding='utf-8', xml_declaration=True,
pretty_print=True)
self.sendResponse(from_url=subscription.subscription_url,
to_url=subscription.publication_url, sync_id=subscription.getTitle(),
xml=xml_a, domain=subscription,
to_url=subscription.publication_url,
sync_id=subscription.getTitle(),
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):
"""
......
......@@ -41,7 +41,7 @@ class SyncCode(Persistent):
ONE_WAY_FROM_SERVER = 204
WAITING_DATA = 214
REFRESH_REQUIRED = 508
CODE_LIST = ( TWO_WAY, ONE_WAY_FROM_SERVER )
CODE_LIST = ( TWO_WAY, ONE_WAY_FROM_SERVER, )
# SyncML Status Codes
SUCCESS = 200
......
......@@ -712,12 +712,12 @@ class SynchronizationTool( SubscriptionSynchronization,
if solve_conflict:
copy_path = conflict.getCopyPath()
signature.delConflict(conflict)
if signature.getConflictList() == []:
if not signature.getConflictList():
if copy_path is not None:
# Delete the copy of the object if the there is one
directory = object.aq_parent
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
if directory.hasObject(id):
directory._delObject(copy_id)
......@@ -828,8 +828,6 @@ class SynchronizationTool( SubscriptionSynchronization,
if content_type == self.CONTENT_TYPE['SYNCML_WBXML']:
xml = self.xml2wbxml(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:
gpg_key = domain.getGPGKey()
if gpg_key not in ('',None):
......@@ -868,7 +866,7 @@ class SynchronizationTool( SubscriptionSynchronization,
content_type=content_type)
elif to_url.find('file://') == 0:
filename = to_url[len('file:/'):]
stream = file(filename,'w')
stream = file(filename, 'w')
stream.write(xml)
stream.close()
# 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