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
|
#!/usr/bin/python
#
# Copyright 2009 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Handler class for implementing a SOAP buffer."""
__author__ = 'api.sgrinberg@gmail.com (Stan Grinberg)'
import re
import sys
from aw_api import ETREE
from aw_api import ETREE_NAME
from aw_api import MIN_ETREE_VERSION
from aw_api import MIN_PY_VERSION
from aw_api import MIN_PYXML_VERSION
from aw_api import PYXML
from aw_api import PYXML_NAME
from aw_api import Utils
from aw_api.Buffer import Buffer
from aw_api.Errors import InvalidInputError
from aw_api.Errors import MalformedBufferError
from aw_api.Errors import MissingPackageError
# Is this running on Google's App Engine?
try:
import google.appengine
from _xmlplus.parsers.expat import ExpatError
except:
from xml.parsers.expat import ExpatError
# Check if PyXML is installed.
try:
# Is this running on Google's App Engine?
try:
import google.appengine
import _xmlplus
except:
import xml as _xmlplus
from xml.dom import minidom
# Test whether xml.version_info exists.
version = _xmlplus.__dict__.get('version_info')
PYXML_LIB = True
except (ImportError, AttributeError):
# An anti-anti-hack/workaround for users of python2.5 (and up) to convince
# Python not to ignore PyXML. We really do want to use _xmlplus here, even
# though it is 'too old'.
if ((list(map(eval, MIN_PY_VERSION.split('.'))) <
list(sys.version_info)[0:3])):
# The change to the environment variables is only active during the life of
# a process.
import os
os.environ['PY_USE_XMLPLUS'] = 'YES'
PYXML_LIB = True
else:
PYXML_LIB = False
else:
if (not version or
list(version) < (list(map(eval, MIN_PYXML_VERSION.split('.'))))):
PYXML_LIB = False
# Check if ElementTree is installed.
try:
import elementtree.ElementTree as etree
etree.VERSION
ETREE_LIB = True
except (ImportError, AttributeError):
ETREE_LIB = False
if not PYXML and not ETREE:
msg = ('PyXML v%s or ElementTree v%s or newer is required.'
% (MIN_PYXML_VERSION, MIN_ETREE_VERSION))
raise MissingPackageError(msg)
class SoapBuffer(Buffer):
"""Implements a SoapBuffer.
Catches and parses outgoing and incoming SOAP XML messages.
"""
def __init__(self, xml_parser=None, pretty_xml=False):
"""Inits SoapBuffer.
Args:
xml_parser: str XML parser to use.
pretty_xml: bool whether to use pretty xml.
"""
self.__buffer = ''
self.__dump = {}
self.__xml_parser = xml_parser
# Pick a default XML parser, if none was set.
if not self.__xml_parser:
for parser, status in [(PYXML, PYXML_LIB), (ETREE, ETREE_LIB)]:
if status:
self.__xml_parser = parser
# Validate current state of the chosen XML parser.
if self.__xml_parser == PYXML and not PYXML_LIB:
msg = 'PyXML v%s or newer is required.' % MIN_PYXML_VERSION
raise MissingPackageError(msg)
elif self.__xml_parser == ETREE and not ETREE_LIB:
msg = 'ElementTree v%s or newer is required.' % MIN_ETREE_VERSION
raise MissingPackageError(msg)
elif self.__xml_parser != PYXML and self.__xml_parser != ETREE:
msg = ('Invalid input for XML parser, expecting one of %s.'
% sorted(set(PYXML + ETREE)))
raise InvalidInputError(msg)
# Set XML parser signature.
if self.__xml_parser == PYXML:
self.__xml_parser_sig = '%s v%s' % (PYXML_NAME, MIN_PYXML_VERSION)
elif self.__xml_parser == ETREE:
self.__xml_parser_sig = '%s v%s' % (ETREE_NAME, MIN_ETREE_VERSION)
self.__pretty_xml = pretty_xml
def write(self, str_in):
"""Append given string to a buffer.
Args:
str_in string to append to a buffer.
"""
self.__buffer += str_in
def flush(self):
pass
def GetBufferAsStr(self):
"""Return buffer as string.
Returns:
str buffer.
"""
return str(self.__buffer)
def IsHandshakeComplete(self):
"""Return state of the handshake.
A successful handshake occurs when a SOAP request is sent and a SOAP
response is recieved.
Returns:
bool True if successful handshake, False otherwise.
"""
if (self.GetHeadersOut() and self.GetSOAPOut() and self.GetHeadersIn() and
self.GetSOAPIn() and not Utils.IsHtml(self.GetSOAPIn())):
return True
return False
def __GetBufferAsDict(self):
"""Parse HTTP headers and SOAP data.
Returns:
dict request's HTTP headers and SOAP data.
"""
tags = (('Outgoing HTTP headers', 'dumpHeadersOut'),
('Outgoing SOAP', 'dumpSOAPOut'),
('Incoming HTTP headers', 'dumpHeadersIn'),
('Incoming SOAP', 'dumpSOAPIn'))
xml_dumps = self.GetBufferAsStr().split('_' * 33)
if len(xml_dumps) > 1:
# The HTTP and SOAP messages were delivered via ZSI.
for xml_part in xml_dumps:
xml_part = xml_part.lstrip('\n').rstrip('\n')
if not xml_part:
continue
xml_part = '\n'.join(xml_part.split('\n')[1:])
in_parts = xml_part.split('\n\n', 1)
if len(in_parts) == 1:
from ZSI.version import Version
xml_out = '<?xml version="1.0" encoding="UTF-8"?>\n%s' % xml_part
if self.__pretty_xml:
xml_out = self.__PrettyPrintXml(xml_out, 1)
self.__dump['dumpHeadersOut'] = (
'%s Outgoing HTTP headers %s\n'
'User-Agent: ZSI %s (http://pywebsvcs.sf.net); %s\n'
'%s' % ('*' * 3, '*' * 46, '.'.join(map(str, Version)),
self.__xml_parser_sig, '*' * 72))
self.__dump['dumpSOAPOut'] = (
'%s Outgoing SOAP %s\n%s\n%s'
% ('*' * 3, '*' * 54, xml_out, '*' * 72))
elif len(in_parts) == 2:
sub_parts = in_parts[0].split('-' * 7)
xml_in = ('<?xml version="1.0" encoding="UTF-8"?>\n%s'
% in_parts[1].lstrip('\n'))
if self.__pretty_xml:
xml_in = self.__PrettyPrintXml(xml_in, 1)
self.__dump['dumpHeadersIn'] = (
'%s Incoming HTTP headers %s\n'
'%s\n%s' % ('*' * 3, '*' * 46,
(' '.join(sub_parts[0].split('\n')) +
sub_parts[1]).replace('\r', ''), '*' * 72))
self.__dump['dumpSOAPIn'] = (
'%s Incoming SOAP %s\n%s\n%s'
% ('*' * 3, '*' * 54, xml_in, '*' * 72))
else:
pass
else:
# The HTTP and SOAP messages were delivered via SOAPpy or httplib.HTTPS.
xml_dumps = self.GetBufferAsStr().split('*' * 72)
for xml_part in xml_dumps:
xml_part = xml_part.lstrip('\n').rstrip('\n')
for name, tag in tags:
if xml_part.find(name) > -1:
# Insert XML parser signature into the SOAP header.
trigger = xml_part[xml_part.lower().find('content-type'):
xml_part.lower().find('content-type')+12]
if trigger and tag == 'dumpHeadersOut':
xml_part = xml_part.replace(
trigger,
'XML-parser: %s\n%s' % (self.__xml_parser_sig, trigger))
if self.__pretty_xml:
doc = []
banner = ''
for line in xml_part.split('\n'):
if line.rfind('SOAP %s' % ('*' * 46)) > -1:
banner = line
continue
doc.append(line)
if banner:
xml_part = '%s\n%s' % (banner,
self.__PrettyPrintXml('\n'.join(doc), 1))
self.__dump[tag] = (xml_part + '\n' + '*' * 72)
break
return self.__dump
def __GetDumpValue(self, dump_type):
"""Return dump value given its type.
Args:
dump_type: str type of the dump.
Returns:
str value of the dump.
"""
dump_value = None
try:
if dump_type in self.__dump:
dump_value = self.__dump[dump_type]
else:
dump_value = self.__GetBufferAsDict()[dump_type]
except KeyError:
dump_value = ''
return dump_value
def GetHeadersOut(self):
"""Return outgoing headers dump.
Returns:
str outgoing headers dump.
"""
return self.__GetDumpValue('dumpHeadersOut')
def GetSOAPOut(self):
"""Return SOAP out dump.
Returns:
str outgoing SOAP dump.
"""
dump_value = self.__GetDumpValue('dumpSOAPOut')
# Mask out login's password, and authToken.
dump_value = dump_value.replace('><', '>\n<')
for mask in ['password', 'authToken', 'ns1:authToken']:
pattern = re.compile('>.*?</%s>' % mask)
dump_value = pattern.sub('>xxxxxx</%s>' % mask, dump_value)
return dump_value
def GetHeadersIn(self):
"""Return incoming headers dump.
Returns:
str incoming headers dump.
"""
return self.__GetDumpValue('dumpHeadersIn')
def GetSOAPIn(self):
"""Return incoming SOAP dump.
Returns:
str incoming SOAP dump.
"""
return self.__GetDumpValue('dumpSOAPIn')
def GetRawSOAPIn(self):
"""Return raw incoming SOAP dump with out banners and not prettified.
Returns:
str raw incoming SOAP dump.
"""
doc = ''.join(self.GetSOAPIn().split('\n')[1:-1])
return self.__PrettyPrintXml(doc, -1)
def __GetXmlOut(self):
"""Remove banners from outgoing SOAP XML and contstruct XML object.
Returns:
Document/Element object generated from string, representing XML message.
"""
# Remove banners.
xml_dump = self.GetSOAPOut().lstrip('\n').rstrip('\n')
xml_parts = xml_dump.split('\n')
# While multiple threads are used, SoapBuffer gets too greedy and tries to
# capture all traffic that goes to sys.stdout. Part that we don't need
# should be redirected back to sys.stdout.
banner = '%s Outgoing SOAP %s' % ('*' * 3, '*' * 54)
begin = 1
for index in range(len(xml_parts)):
if xml_parts[index] == banner:
begin = index + 1
break
non_xml = '\n'.join(xml_parts[:begin-1])
# Send data we don't need back to sys.stdout.
if non_xml:
print non_xml
xml_dump = '\n'.join(xml_parts[begin:len(xml_parts)-1])
try:
if self.__xml_parser == PYXML:
xml_obj = minidom.parseString(xml_dump)
elif self.__xml_parser == ETREE:
xml_obj = etree.fromstring(xml_dump)
except ExpatError, e:
msg = 'Unable to parse SOAP buffer for outgoing messages.' % e
raise MalformedBufferError(msg)
return xml_obj
def __GetXmlIn(self):
"""Remove banners from incoming SOAP XML and construct XML object.
Returns:
Document/Element object generated from string, representing XML message.
"""
# Remove banners.
xml_dump = self.GetSOAPIn().lstrip('\n').rstrip('\n')
xml_parts = xml_dump.split('\n')
xml_dump = '\n'.join(xml_parts[1:len(xml_parts)-1])
try:
if self.__xml_parser == PYXML:
xml_obj = minidom.parseString(xml_dump)
elif self.__xml_parser == ETREE:
xml_obj = etree.fromstring(xml_dump)
except ExpatError, e:
msg = 'Unable to parse SOAP buffer for incoming messages.' % e
raise MalformedBufferError(msg)
return xml_obj
def __RemoveElemAttr(self, elem):
"""Remove element's attribute, if any.
Args:
elem: str XML element with or without an attribute.
Returns:
str XML element without attribute.
"""
pattern = re.compile('({.*?})')
return re.sub(pattern, '', elem)
def __GetXmlNameByName(self, xml_obj, name):
"""Get XML object name from a given tag name.
Args:
xml_obj: Document/Element object.
name: str tag name to look up.
Returns:
str XML object name, or None if tag name is not found in the XML object.
"""
value = None
if self.__xml_parser == PYXML:
for node in xml_obj.getElementsByTagName(name):
if len(node.childNodes) == 1:
value = node.childNodes[0].nodeName
else:
value = node.childNodes[1].nodeName
elif self.__xml_parser == ETREE:
root = xml_obj
etags = etags = name.split('/')
for etag in etags:
for item in root.getchildren():
if item.tag.find(etag) > -1 or etag == '*':
root = item
if etag == etags[-1]:
value = self.__RemoveElemAttr(root.tag)
return value
def __GetXmlValueByName(self, xml_obj, name, get_all=False):
"""Get XML object value from a given tag name.
Args:
xml_obj: Document/Element object.
name: str tag name whose value to look up.
get_all: bool whether to return all values that were found or just one.
Returns:
str XML object value, list if more than one value is found and if
explicitly requested, or None if name is not found in the XML object.
"""
values = []
if self.__xml_parser == PYXML:
for node in xml_obj.getElementsByTagName(name):
values.append(node.childNodes[0].nodeValue)
elif self.__xml_parser == ETREE:
root = xml_obj
etags = name.split('/')
for etag in etags:
for item in root.getchildren():
if item.tag.find(etag) > -1:
root = item
if etag == etags[-1]:
values.append(root.text)
if get_all and values:
return values
elif values:
return values[0]
return None
def IsSoap(self):
"""Whether buffer contains SOAP message(s) or not.
Returns:
bool True if message is a SOAP, False otherwise.
"""
data = self.GetBufferAsStr()
if not data:
return False
try:
if self.__xml_parser == PYXML:
if (self.GetCallResponseTime() is None and
not self.__GetXmlIn().getElementsByTagName('soapenv:Body') and
not self.__GetXmlIn().getElementsByTagName('soap:Body')):
return False
elif self.__xml_parser == ETREE:
if self.__GetXmlNameByName(self.__GetXmlIn(), 'Body') is None:
return False
except Exception:
# Data contains malformed XML message.
return False
return True
def GetServiceName(self):
"""Get name of the API service that was called.
Note: the way service is determined depends on the particular order of
elements in the caller stack (e.g., CampaignService.Get() ->
WebService.__ManageSoap() -> SoapBuffer.GetServiceName().)
Returns:
str name of the API service that was called.
"""
return sys._getframe(3).f_locals['self'].__class__.__name__
def GetCallName(self):
"""Get name of the API method that was called.
Returns:
str name of the API method that was called.
"""
if self.__xml_parser == PYXML:
# Remove "nsX:" if exists.
pattern = re.compile('ns.*?:')
value = pattern.sub('', self.__GetXmlNameByName(self.__GetXmlOut(),
'SOAP-ENV:Body'))
elif self.__xml_parser == ETREE:
value = self.__GetXmlNameByName(self.__GetXmlOut(), 'Body/*')
return value
def GetOperatorName(self):
"""Get name of the operator that was used in the API call.
Returns:
dict a dictionary consisting of the name of the operator mapped to the
number of times that operator was used in the API request.
"""
if self.__xml_parser == PYXML:
values = self.__GetXmlValueByName(self.__GetXmlOut(),
'ns1:operator',
True)
elif self.__xml_parser == ETREE:
values = self.__GetXmlValueByName(self.__GetXmlOut(),
'Body/mutate/operations/operator',
True)
# If no operator found, returns None. Otherwise, the format
# is {'ADD': 1, 'SET': 2}.
operator = {}
if not values:
operator = None
else:
for value in values:
if value in operator:
operator[str(value)] += 1
else:
operator[str(value)] = 1
return operator
def GetCallResponseTime(self):
"""Get value for responseTime header.
Returns:
str responseTime header value.
"""
for name in ['responseTime', 'Header/ResponseHeader/responseTime',
'ns2:responseTime']:
value = self.__GetXmlValueByName(self.__GetXmlIn(), name)
if value:
return value
return None
def GetCallOperations(self):
"""Get value for operations header.
Returns:
str operations header value.
"""
for name in ['operations', 'Header/ResponseHeader/operations',
'ns2:operations']:
value = self.__GetXmlValueByName(self.__GetXmlIn(), name)
if value:
return value
return None
def GetCallUnits(self):
"""Get value for units header.
Returns:
str units header value.
"""
for name in ['units', 'Header/ResponseHeader/units', 'ns2:units']:
value = self.__GetXmlValueByName(self.__GetXmlIn(), name)
if value:
return value
return None
def GetCallRequestId(self):
"""Get value for requestId header.
Returns:
str requestId header value.
"""
for name in ['requestId', 'Header/ResponseHeader/requestId',
'ns2:requestId']:
value = self.__GetXmlValueByName(self.__GetXmlIn(), name)
if value:
return value
return None
def GetFaultAsDict(self):
"""Parse incoming SOAP fault to load all elements into a dictionary.
Returns:
dict dictionary object with all fault elements.
"""
elems = ['soapenv:Fault', 'soap:Fault']
for elem in elems:
xml_obj = self.__GetXmlIn()
if self.__xml_parser == PYXML:
try:
fault_obj = xml_obj.getElementsByTagName(elem)[0]
except:
envelope = xml_obj.childNodes[0]
header = None
body = None
for element in envelope.childNodes:
if element.nodeName.lower().find('header') > -1:
header = element.childNodes[1]
elif element.nodeName.lower().find('body') > -1:
body = element.childNodes[1]
fault_obj = body
elif self.__xml_parser == ETREE:
fault_obj = xml_obj.getchildren()[len(
xml_obj.getchildren())-1].getchildren()[0]
fault = {}
detail = {}
errors = []
nodes = {PYXML: ['childNodes', 'localName', 'childNodes[0].nodeValue'],
ETREE: ['getchildren()', 'tag', 'text']}
# Step through the Document or Element (depending on which XML parser is
# used) and construct a dictionary representation of the fault. See
# different examples of the API fault at
# - data/response_fault_stacktrace.xml
# - data/response_fault.xml
# - data/response_fault_errors.xml
for fitem in eval('fault_obj.%s' % nodes[self.__xml_parser][0]):
if ((self.__xml_parser == PYXML and fitem.hasChildNodes()) or
self.__xml_parser == ETREE):
ftag = self.__RemoveElemAttr(eval('fitem.%s'
% nodes[self.__xml_parser][1]))
if ftag != 'detail':
fault[ftag] = eval('fitem.%s' % nodes[self.__xml_parser][2])
else:
for ditem in eval('fitem.%s' % nodes[self.__xml_parser][0]):
if ((self.__xml_parser == PYXML and ditem.hasChildNodes()) or
self.__xml_parser == ETREE):
dtag = self.__RemoveElemAttr(
eval('ditem.%s' % nodes[self.__xml_parser][1]))
if dtag != 'fault' and dtag != 'ApiExceptionFault':
detail[dtag] = eval('ditem.%s' % nodes[self.__xml_parser][2])
else:
for dfitem in eval('ditem.%s' % nodes[self.__xml_parser][0]):
if ((self.__xml_parser == PYXML
and dfitem.hasChildNodes()) or
self.__xml_parser == ETREE):
dftag = self.__RemoveElemAttr(
eval('dfitem.%s' % nodes[self.__xml_parser][1]))
if dftag != 'errors':
detail[dftag] = eval('dfitem.%s'
% nodes[self.__xml_parser][2])
else:
error = {}
for eitem in eval('dfitem.%s'
% nodes[self.__xml_parser][0]):
if ((self.__xml_parser == PYXML
and eitem.hasChildNodes()) or
self.__xml_parser == ETREE):
etag = self.__RemoveElemAttr(
eval('eitem.%s' % nodes[self.__xml_parser][1]))
# Rename type elements that have a dot in them into
# something more useful,
# - ApiError.Type => type
if etag.find('.') > -1:
etag = etag.split('.')[1].lower()
error[etag] = eval(
'eitem.%s' % nodes[self.__xml_parser][2])
errors.append(error)
detail[dftag] = errors
fault[ftag] = detail
return fault
def GetFaultAsStr(self):
"""Format all fault elements into a printable form.
Returns:
str string with all fault elements.
"""
try:
fault = self.GetFaultAsDict()
if not fault:
return ''
except Exception:
return ''
items = []
for fitem in fault:
if fitem != 'detail':
items.append('%s: %s\n' % (fitem, fault[fitem]))
else:
items.append('\n')
for ditem in fault[fitem]:
if ditem != 'errors':
items.append(('%s: %s\n' % (ditem, fault[fitem][ditem])))
else:
for dfitem in fault[fitem][ditem]:
items.append('\nError:\n')
for eitem in dfitem:
items.append((' %s: %s\n' % (eitem, dfitem[eitem])))
return ''.join(items).strip()
def InjectXml(self, xml_in):
"""Hook into the SoapBuffer to test local SOAP XML.
Prepares dump dict with a given input.
Args:
xml_in: str SOAP XML request, response, or both.
"""
# Prepare string for use with regular expression.
xml_in = xml_in.replace('\n', '%newline%')
try:
pattern = re.compile('(<SOAP-ENV:Envelope.*</SOAP-ENV:Envelope>)')
req = pattern.findall(xml_in)
pattern = re.compile('(<soap.*?:Envelope.*</soap.*?:Envelope>)')
res = pattern.findall(xml_in)
# Do we have a SOAP XML request?
if req:
# Rebuild original formatting of the string and dump it.
req = req[0].replace('%newline%', '\n')
self.__dump['dumpSOAPOut'] = (
'%s Outgoing SOAP %s\n'
'<?xml version="1.0" encoding="UTF-8"?>\n%s\n'
'%s' % ('*' * 3, '*' * 54, req, '*' * 72))
# Do we have a SOAP XML response?
if res:
# Rebuild original formatting of the string and dump it.
res = res[0].replace('%newline%', '\n')
self.__dump['dumpSOAPIn'] = (
'%s Incoming SOAP %s\n'
'<?xml version="1.0" encoding="UTF-8"?>\n%s\n'
'%s' % ('*' * 3, '*' * 54, res.lstrip('\n'), '*' * 72))
except Exception:
msg = 'Invalid input, expecting SOAP XML request, response, or both.'
raise InvalidInputError(msg)
def __Indent(self, elem, level=0):
"""Add whitespace to the tree, to get its pretty-printed version.
See, http://effbot.org/zone/element-lib.htm#prettyprint.
"""
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
self.__Indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
def __PrettyPrintXml(self, doc, level=0):
"""Return a pretty-printed version of the XML document.
Args:
doc: str an XML document.
level: int level of prettiness, defaults to 0. If -1, remove prettiness.
Returns:
str pretty-printed version of the XML document.
"""
# Make sure we have a valid doc to work with.
if Utils.IsHtml(doc):
return doc
if self.__xml_parser == PYXML:
dom = minidom.parseString(doc)
pretty_doc = dom.toprettyxml(indent=' ', encoding='UTF-8')
elif self.__xml_parser == ETREE:
tree = etree.fromstring(doc)
self.__Indent(tree)
pretty_doc = etree.tostring(tree, 'UTF-8')
# Adjust prettiness of data values in XML document.
#
# Before: <operations>
# 0
# </operations>
#
# After: <operations>0</operations>
pattern = re.compile('\n\s+\n')
pretty_doc = pattern.sub('\n', pretty_doc)
groups = re.findall('>(\n\s+(.*?)\n\s+)</', pretty_doc, re.M)
for old, new in groups:
if old and new and (new.find('<') > -1 or new.find('>') > -1):
continue
pretty_doc = pretty_doc.replace(old, new)
if level == -1:
pattern = re.compile('>\s+<')
pretty_doc = pattern.sub('><', pretty_doc)
return pretty_doc.strip('\n')
|