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\
IAuthenticationPlugin
from AccessControl.SecurityManagement import newSecurityManager
import commands
from DateTime import DateTime
from zLOG import LOG
class PublicationSynchronization(XMLSyncUtils):
......@@ -225,10 +226,9 @@ class PublicationSynchronization(XMLSyncUtils):
"""
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
xml_client = msg
publication
if xml_client is None:
xml_client = self.readResponse(from_url=publication.getPublicationUrl())
#LOG('PubSync',0,'Starting... msg: %s' % str(xml_client))
......@@ -249,13 +249,7 @@ class PublicationSynchronization(XMLSyncUtils):
if client_header.nodeName != "SyncHdr":
#LOG('PubSync',0,'This is not a SyncML Header')
raise ValueError, "Sorry, This is not a SyncML Header"
for subnode in client_header.childNodes:
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)
subscription_url = self.getSourceURI(client_header)
# Get the subscriber or create it if not already in the list
subscriber = publication.getSubscriber(subscription_url)
if subscriber == None:
......@@ -265,8 +259,6 @@ class PublicationSynchronization(XMLSyncUtils):
# first synchronization
result = self.PubSyncInit(publication,xml_client,subscriber=subscriber,
sync_type=self.SLOW_SYNC)
elif self.checkAlert(xml_client) and \
alert_code in (self.TWO_WAY,self.SLOW_SYNC):
result = self.PubSyncInit(publication=publication,
......
......@@ -1016,6 +1016,7 @@ class Subscription(Folder, SyncCode):
signature = self.getSignatureFromGid(gid)
# First look if we do already have the mapping between
# the id and the gid
#XXX Slow !!!
object_list = self.getObjectList()
destination = self.getDestination()
if signature is not None and signature.getObjectId() is not None:
......@@ -1040,10 +1041,12 @@ class Subscription(Folder, SyncCode):
"""
object_list = self.getObjectList()
#XXX very slow with lot of objects
o = None
for object in object_list:
if object.getId() == id:
return object
return None
o = object
break
return o
# def setOneWaySyncFromServer(self,value):
# """
......
......@@ -31,6 +31,7 @@ from Subscription import Subscription,Signature
from XMLSyncUtils import XMLSyncUtils, Parse
import commands
from Conduit.ERP5Conduit import ERP5Conduit
from DateTime import DateTime
from zLOG import LOG
class SubscriptionSynchronization(XMLSyncUtils):
......@@ -93,12 +94,13 @@ class SubscriptionSynchronization(XMLSyncUtils):
xml_client = msg
if isinstance(xml_client, str) or isinstance(xml_client, unicode):
xml_client = Parse(xml_client)
next_status = self.getNextSyncBodyStatus(xml_client, None)
#LOG('readResponse, next status :',0,next_status)
if next_status is not None:
status_code = self.getStatusCode(next_status)
#LOG('readResponse status code :',0,status_code)
if status_code == self.AUTH_REQUIRED:
status_list = self.getSyncBodyStatusList(xml_client)
if status_list not in (None, []):
status_code_syncHdr = status_list[0]['code']
if status_code_syncHdr.isdigit():
status_code_syncHdr = int(status_code_syncHdr)
#LOG('readResponse status code :',0,status_code_syncHdr)
if status_code_syncHdr == self.AUTH_REQUIRED:
if self.checkChal(xml_client):
authentication_format, authentication_type = self.getChal(xml_client)
#LOG('auth_required :',0, 'format:%s, type:%s' % (authentication_format, authentication_type))
......@@ -112,7 +114,7 @@ class SubscriptionSynchronization(XMLSyncUtils):
#LOG('readResponse', 0, 'Authentication required')
response = self.SubSyncCred(subscription, xml_client)
elif status_code == self.UNAUTHORIZED:
elif status_code_syncHdr == self.UNAUTHORIZED:
LOG('readResponse', 0, 'Bad authentication')
return {'has_response':0,'xml':xml_client}
else:
......@@ -120,7 +122,6 @@ class SubscriptionSynchronization(XMLSyncUtils):
else:
response = self.SubSyncModif(subscription, xml_client)
if RESPONSE is not None:
RESPONSE.redirect('manageSubscriptions')
else:
......
......@@ -57,6 +57,7 @@ import os
import string
import commands
import random
from DateTime import DateTime
from zLOG import LOG
......@@ -821,7 +822,7 @@ class SynchronizationTool( SubscriptionSynchronization,
#LOG('sendResponse, to_url: ',0,to_url)
#LOG('sendResponse, from_url: ',0,from_url)
#LOG('sendResponse, sync_id: ',0,sync_id)
#LOG('sendResponse, xml: \n',0,xml)
LOG('sendResponse, xml: \n',0,xml)
if isinstance(xml, unicode):
xml = xml.encode('utf-8')
if domain is not None:
......@@ -891,6 +892,7 @@ class SynchronizationTool( SubscriptionSynchronization,
opener = urllib2.build_opener(proxy_handler, proxy_auth_handler,
auth_handler, urllib2.HTTPHandler)
urllib2.install_opener(opener)
socket.setdefaulttimeout(3660)
to_encode = {}
head = '<?xml version="1.0" encoding="UTF-8"?>'
to_encode['text'] = head + xml
......@@ -957,6 +959,7 @@ class SynchronizationTool( SubscriptionSynchronization,
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 :', 0, text)
# Login as a manager to make sure we can create objects
uf = self.acl_users
user = uf.getUserById('syncml').__of__(uf)
......
......@@ -453,6 +453,15 @@ class XMLSyncUtilsMixin(SyncCode):
next_anchor = next_anchor.encode('utf-8')
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):
"""
Return the value of the alert code inside the xml_stream
......@@ -590,35 +599,8 @@ class XMLSyncUtilsMixin(SyncCode):
tmp_dict['source'] = status.xpath('string(./SourceRef)').encode('utf-8')
tmp_dict['target'] = status.xpath('string(./TargetRef)').encode('utf-8')
status_list.append(tmp_dict)
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):
"""
return the section data in text form, it's usefull for the VCardConduit
......@@ -642,24 +624,9 @@ class XMLSyncUtilsMixin(SyncCode):
"""
Return the node starting with <object....> of the action
"""
for subnode in action.childNodes:
if subnode.nodeType == subnode.ELEMENT_NODE and \
subnode.nodeName == 'Item':
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
comment_list = action.xpath('.//Item/Data[comment()]')
if comment_list != []:
return comment_list[0].childNodes[0].data.encode('utf-8')
return None
def getActionId(self, action, action_name):
......@@ -1193,13 +1160,7 @@ class XMLSyncUtils(XMLSyncUtilsMixin):
simulate = 0 # used by applyActionList, should be 0 for client
if domain.domain_type == self.PUB:
simulate = 1
for subnode in xml_header.childNodes:
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)
subscription_url = self.getSourceURI(xml_header)
subscriber = domain.getSubscriber(subscription_url)
# We have to check if this message was not already, this can be dangerous
......
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