Commit a169e349 authored by Sebastien Robin's avatar Sebastien Robin

- replace xpath for loops with xpath expression

- delete an old function : getNextSyncBodyStatus


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@14982 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent beaf9575
...@@ -38,6 +38,7 @@ from Products.PluggableAuthService.interfaces.plugins import\ ...@@ -38,6 +38,7 @@ from Products.PluggableAuthService.interfaces.plugins import\
IAuthenticationPlugin IAuthenticationPlugin
from AccessControl.SecurityManagement import newSecurityManager from AccessControl.SecurityManagement import newSecurityManager
import commands import commands
from DateTime import DateTime
from zLOG import LOG from zLOG import LOG
class PublicationSynchronization(XMLSyncUtils): class PublicationSynchronization(XMLSyncUtils):
...@@ -225,10 +226,9 @@ class PublicationSynchronization(XMLSyncUtils): ...@@ -225,10 +226,9 @@ class PublicationSynchronization(XMLSyncUtils):
""" """
This is the synchronization method for the server This is the synchronization method for the server
""" """
#LOG('PubSync',0,'Starting... id: %s' % str(id)) LOG('PubSync',0,'Starting... publication: %s' % str(publication))
# Read the request from the client # Read the request from the client
xml_client = msg xml_client = msg
publication
if xml_client is None: if xml_client is None:
xml_client = self.readResponse(from_url=publication.getPublicationUrl()) xml_client = self.readResponse(from_url=publication.getPublicationUrl())
#LOG('PubSync',0,'Starting... msg: %s' % str(xml_client)) #LOG('PubSync',0,'Starting... msg: %s' % str(xml_client))
...@@ -249,13 +249,7 @@ class PublicationSynchronization(XMLSyncUtils): ...@@ -249,13 +249,7 @@ class PublicationSynchronization(XMLSyncUtils):
if client_header.nodeName != "SyncHdr": if client_header.nodeName != "SyncHdr":
#LOG('PubSync',0,'This is not a SyncML Header') #LOG('PubSync',0,'This is not a SyncML Header')
raise ValueError, "Sorry, This is not a SyncML Header" raise ValueError, "Sorry, This is not a SyncML Header"
for subnode in client_header.childNodes: subscription_url = self.getSourceURI(client_header)
if subnode.nodeType == subnode.ELEMENT_NODE and \
subnode.nodeName == "Source":
for subnode2 in subnode.childNodes:
if subnode2.nodeType == subnode2.ELEMENT_NODE and \
subnode2.nodeName == "LocURI":
subscription_url = str(subnode2.childNodes[0].data)
# Get the subscriber or create it if not already in the list # Get the subscriber or create it if not already in the list
subscriber = publication.getSubscriber(subscription_url) subscriber = publication.getSubscriber(subscription_url)
if subscriber == None: if subscriber == None:
...@@ -265,8 +259,6 @@ class PublicationSynchronization(XMLSyncUtils): ...@@ -265,8 +259,6 @@ class PublicationSynchronization(XMLSyncUtils):
# first synchronization # first synchronization
result = self.PubSyncInit(publication,xml_client,subscriber=subscriber, result = self.PubSyncInit(publication,xml_client,subscriber=subscriber,
sync_type=self.SLOW_SYNC) sync_type=self.SLOW_SYNC)
elif self.checkAlert(xml_client) and \ elif self.checkAlert(xml_client) and \
alert_code in (self.TWO_WAY,self.SLOW_SYNC): alert_code in (self.TWO_WAY,self.SLOW_SYNC):
result = self.PubSyncInit(publication=publication, result = self.PubSyncInit(publication=publication,
......
...@@ -1016,6 +1016,7 @@ class Subscription(Folder, SyncCode): ...@@ -1016,6 +1016,7 @@ class Subscription(Folder, SyncCode):
signature = self.getSignatureFromGid(gid) signature = self.getSignatureFromGid(gid)
# First look if we do already have the mapping between # First look if we do already have the mapping between
# the id and the gid # the id and the gid
#XXX Slow !!!
object_list = self.getObjectList() object_list = self.getObjectList()
destination = self.getDestination() destination = self.getDestination()
if signature is not None and signature.getObjectId() is not None: if signature is not None and signature.getObjectId() is not None:
...@@ -1040,10 +1041,12 @@ class Subscription(Folder, SyncCode): ...@@ -1040,10 +1041,12 @@ class Subscription(Folder, SyncCode):
""" """
object_list = self.getObjectList() object_list = self.getObjectList()
#XXX very slow with lot of objects #XXX very slow with lot of objects
o = None
for object in object_list: for object in object_list:
if object.getId() == id: if object.getId() == id:
return object o = object
return None break
return o
# def setOneWaySyncFromServer(self,value): # def setOneWaySyncFromServer(self,value):
# """ # """
......
...@@ -31,6 +31,7 @@ from Subscription import Subscription,Signature ...@@ -31,6 +31,7 @@ from Subscription import Subscription,Signature
from XMLSyncUtils import XMLSyncUtils, Parse from XMLSyncUtils import XMLSyncUtils, Parse
import commands import commands
from Conduit.ERP5Conduit import ERP5Conduit from Conduit.ERP5Conduit import ERP5Conduit
from DateTime import DateTime
from zLOG import LOG from zLOG import LOG
class SubscriptionSynchronization(XMLSyncUtils): class SubscriptionSynchronization(XMLSyncUtils):
...@@ -93,12 +94,13 @@ class SubscriptionSynchronization(XMLSyncUtils): ...@@ -93,12 +94,13 @@ class SubscriptionSynchronization(XMLSyncUtils):
xml_client = msg xml_client = msg
if isinstance(xml_client, str) or isinstance(xml_client, unicode): if isinstance(xml_client, str) or isinstance(xml_client, unicode):
xml_client = Parse(xml_client) xml_client = Parse(xml_client)
next_status = self.getNextSyncBodyStatus(xml_client, None) status_list = self.getSyncBodyStatusList(xml_client)
#LOG('readResponse, next status :',0,next_status) if status_list not in (None, []):
if next_status is not None: status_code_syncHdr = status_list[0]['code']
status_code = self.getStatusCode(next_status) if status_code_syncHdr.isdigit():
#LOG('readResponse status code :',0,status_code) status_code_syncHdr = int(status_code_syncHdr)
if status_code == self.AUTH_REQUIRED: #LOG('readResponse status code :',0,status_code_syncHdr)
if status_code_syncHdr == self.AUTH_REQUIRED:
if self.checkChal(xml_client): if self.checkChal(xml_client):
authentication_format, authentication_type = self.getChal(xml_client) authentication_format, authentication_type = self.getChal(xml_client)
#LOG('auth_required :',0, 'format:%s, type:%s' % (authentication_format, authentication_type)) #LOG('auth_required :',0, 'format:%s, type:%s' % (authentication_format, authentication_type))
...@@ -112,7 +114,7 @@ class SubscriptionSynchronization(XMLSyncUtils): ...@@ -112,7 +114,7 @@ class SubscriptionSynchronization(XMLSyncUtils):
#LOG('readResponse', 0, 'Authentication required') #LOG('readResponse', 0, 'Authentication required')
response = self.SubSyncCred(subscription, xml_client) response = self.SubSyncCred(subscription, xml_client)
elif status_code == self.UNAUTHORIZED: elif status_code_syncHdr == self.UNAUTHORIZED:
LOG('readResponse', 0, 'Bad authentication') LOG('readResponse', 0, 'Bad authentication')
return {'has_response':0,'xml':xml_client} return {'has_response':0,'xml':xml_client}
else: else:
...@@ -120,7 +122,6 @@ class SubscriptionSynchronization(XMLSyncUtils): ...@@ -120,7 +122,6 @@ class SubscriptionSynchronization(XMLSyncUtils):
else: else:
response = self.SubSyncModif(subscription, xml_client) response = self.SubSyncModif(subscription, xml_client)
if RESPONSE is not None: if RESPONSE is not None:
RESPONSE.redirect('manageSubscriptions') RESPONSE.redirect('manageSubscriptions')
else: else:
......
...@@ -57,6 +57,7 @@ import os ...@@ -57,6 +57,7 @@ import os
import string import string
import commands import commands
import random import random
from DateTime import DateTime
from zLOG import LOG from zLOG import LOG
...@@ -821,7 +822,7 @@ class SynchronizationTool( SubscriptionSynchronization, ...@@ -821,7 +822,7 @@ class SynchronizationTool( SubscriptionSynchronization,
#LOG('sendResponse, to_url: ',0,to_url) #LOG('sendResponse, to_url: ',0,to_url)
#LOG('sendResponse, from_url: ',0,from_url) #LOG('sendResponse, from_url: ',0,from_url)
#LOG('sendResponse, sync_id: ',0,sync_id) #LOG('sendResponse, sync_id: ',0,sync_id)
#LOG('sendResponse, xml: \n',0,xml) LOG('sendResponse, xml: \n',0,xml)
if isinstance(xml, unicode): if isinstance(xml, unicode):
xml = xml.encode('utf-8') xml = xml.encode('utf-8')
if domain is not None: if domain is not None:
...@@ -891,6 +892,7 @@ class SynchronizationTool( SubscriptionSynchronization, ...@@ -891,6 +892,7 @@ class SynchronizationTool( SubscriptionSynchronization,
opener = urllib2.build_opener(proxy_handler, proxy_auth_handler, opener = urllib2.build_opener(proxy_handler, proxy_auth_handler,
auth_handler, urllib2.HTTPHandler) auth_handler, urllib2.HTTPHandler)
urllib2.install_opener(opener) urllib2.install_opener(opener)
socket.setdefaulttimeout(3660)
to_encode = {} to_encode = {}
head = '<?xml version="1.0" encoding="UTF-8"?>' head = '<?xml version="1.0" encoding="UTF-8"?>'
to_encode['text'] = head + xml to_encode['text'] = head + xml
...@@ -905,8 +907,8 @@ class SynchronizationTool( SubscriptionSynchronization, ...@@ -905,8 +907,8 @@ class SynchronizationTool( SubscriptionSynchronization,
request = urllib2.Request(url=to_url, data=data) request = urllib2.Request(url=to_url, data=data)
#XXX only to synchronize with other server than erp5 (must be improved): #XXX only to synchronize with other server than erp5 (must be improved):
# data=head+xml # data=head+xml
# request = urllib2.Request(to_url, data, headers) # request = urllib2.Request(to_url, data, headers)
try: try:
result = urllib2.urlopen(request).read() result = urllib2.urlopen(request).read()
except socket.error, msg: except socket.error, msg:
...@@ -957,6 +959,7 @@ class SynchronizationTool( SubscriptionSynchronization, ...@@ -957,6 +959,7 @@ class SynchronizationTool( SubscriptionSynchronization,
We will look at the url and we will see if we need to send mail, http We will look at the url and we will see if we need to send mail, http
response, or just copy to a file. response, or just copy to a file.
""" """
LOG('readResponse, text :', 0, text)
# Login as a manager to make sure we can create objects # Login as a manager to make sure we can create objects
uf = self.acl_users uf = self.acl_users
user = uf.getUserById('syncml').__of__(uf) user = uf.getUserById('syncml').__of__(uf)
......
...@@ -453,6 +453,15 @@ class XMLSyncUtilsMixin(SyncCode): ...@@ -453,6 +453,15 @@ class XMLSyncUtilsMixin(SyncCode):
next_anchor = next_anchor.encode('utf-8') next_anchor = next_anchor.encode('utf-8')
return next_anchor return next_anchor
def getSourceURI(self, xml):
"""
return the source URI of the syncml header
"""
subscription_url = xml.xpath('string(//SyncHdr/Source/LocURI)')
if isinstance(subscription_url, unicode):
subscription_url = subscription_url.encode('utf-8')
return subscription_url
def getStatusTarget(self, xml): def getStatusTarget(self, xml):
""" """
Return the value of the alert code inside the xml_stream Return the value of the alert code inside the xml_stream
...@@ -590,35 +599,8 @@ class XMLSyncUtilsMixin(SyncCode): ...@@ -590,35 +599,8 @@ class XMLSyncUtilsMixin(SyncCode):
tmp_dict['source'] = status.xpath('string(./SourceRef)').encode('utf-8') tmp_dict['source'] = status.xpath('string(./SourceRef)').encode('utf-8')
tmp_dict['target'] = status.xpath('string(./TargetRef)').encode('utf-8') tmp_dict['target'] = status.xpath('string(./TargetRef)').encode('utf-8')
status_list.append(tmp_dict) status_list.append(tmp_dict)
return status_list return status_list
def getNextSyncBodyStatus(self, xml_stream, last_status):
"""
It goes throw actions in the Sync section of the SyncML file,
then it returns the next action (could be "add", "replace",
"delete").
"""
first_node = xml_stream.childNodes[0]
client_body = first_node.childNodes[3]
if client_body.nodeName != "SyncBody":
#LOG('getNextSyncBodyStatus',0,"This is not a SyncML Body")
raise ValueError, "Sorry, This is not a SyncML Body"
next_status = None
found = None
for subnode in client_body.childNodes:
if subnode.nodeType == subnode.ELEMENT_NODE and \
subnode.nodeName == "Status":
# if we didn't use this method before
if last_status == None:
next_status = subnode
return next_status
elif subnode == last_status and found is None:
found = 1
elif found is not None:
return subnode
return next_status
def getDataText(self, action): def getDataText(self, action):
""" """
return the section data in text form, it's usefull for the VCardConduit return the section data in text form, it's usefull for the VCardConduit
...@@ -642,24 +624,9 @@ class XMLSyncUtilsMixin(SyncCode): ...@@ -642,24 +624,9 @@ class XMLSyncUtilsMixin(SyncCode):
""" """
Return the node starting with <object....> of the action Return the node starting with <object....> of the action
""" """
for subnode in action.childNodes: comment_list = action.xpath('.//Item/Data[comment()]')
if subnode.nodeType == subnode.ELEMENT_NODE and \ if comment_list != []:
subnode.nodeName == 'Item': return comment_list[0].childNodes[0].data.encode('utf-8')
for subnode2 in subnode.childNodes:
if subnode2.nodeType == subnode2.ELEMENT_NODE and \
subnode2.nodeName == 'Data':
for subnode3 in subnode2.childNodes:
#if subnode3.nodeType == subnode3.ELEMENT_NODE and subnode3.nodeName == 'object':
if subnode3.nodeType == subnode3.COMMENT_NODE:
# No need to remove comment, it is already done by FromXml
#if subnode3.data.find('<!--')>=0:
# data = subnode3.data
# data = data[data.find('<!--')+4:data.rfind('-->')]
xml = subnode3.data
if isinstance(xml, unicode):
xml = xml.encode('utf-8')
return xml
return None return None
def getActionId(self, action, action_name): def getActionId(self, action, action_name):
...@@ -1193,14 +1160,8 @@ class XMLSyncUtils(XMLSyncUtilsMixin): ...@@ -1193,14 +1160,8 @@ class XMLSyncUtils(XMLSyncUtilsMixin):
simulate = 0 # used by applyActionList, should be 0 for client simulate = 0 # used by applyActionList, should be 0 for client
if domain.domain_type == self.PUB: if domain.domain_type == self.PUB:
simulate = 1 simulate = 1
for subnode in xml_header.childNodes: subscription_url = self.getSourceURI(xml_header)
if subnode.nodeType == subnode.ELEMENT_NODE and \ subscriber = domain.getSubscriber(subscription_url)
subnode.nodeName == "Source":
for subnode2 in subnode.childNodes:
if subnode2.nodeType == subnode2.ELEMENT_NODE and \
subnode2.nodeName == 'LocURI':
subscription_url = str(subnode2.childNodes[0].data)
subscriber = domain.getSubscriber(subscription_url)
# We have to check if this message was not already, this can be dangerous # We have to check if this message was not already, this can be dangerous
# to update two times the same object # to update two times the same object
......
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