-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
soap.py
547 lines (437 loc) · 19 KB
/
soap.py
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
"""
zeep.wsdl.messages.soap
~~~~~~~~~~~~~~~~~~~~~~~
"""
import copy
import typing
from collections import OrderedDict
from lxml import etree
from lxml.builder import ElementMaker
from zeep import exceptions, xsd
from zeep.utils import as_qname
from zeep.wsdl.messages.base import ConcreteMessage, SerializedMessage
from zeep.wsdl.messages.multiref import process_multiref
from zeep.xsd.context import XmlParserContext
from zeep.xsd.valueobjects import CompoundValue
__all__ = ["DocumentMessage", "RpcMessage"]
class SoapMessage(ConcreteMessage):
"""Base class for the SOAP Document and RPC messages
:param wsdl: The main wsdl document
:type wsdl: zeep.wsdl.Document
:param name:
:param operation: The operation to which this message belongs
:type operation: zeep.wsdl.bindings.soap.SoapOperation
:param type: 'input' or 'output'
:type type: str
:param nsmap: The namespace mapping
:type nsmap: dict
"""
if typing.TYPE_CHECKING:
_resolve_info = {} # type: typing.Dict[str, typing.Any]
def __init__(self, wsdl, name, operation, type, nsmap):
super().__init__(wsdl, name, operation)
self.nsmap = nsmap
self.abstract = None # Set during resolve()
self.type = type
self._is_body_wrapped = False
self.body = None
self.header = None
self.envelope = None
def serialize(self, *args, **kwargs):
"""Create a SerializedMessage for this message"""
nsmap = {"soap-env": self.nsmap["soap-env"]}
nsmap.update(self.wsdl.types._prefix_map_custom)
soap = ElementMaker(namespace=self.nsmap["soap-env"], nsmap=nsmap)
# Create the soap:envelope
envelope = soap.Envelope()
# Create the soap:header element
headers_value = kwargs.pop("_soapheaders", None)
header = self._serialize_header(headers_value, nsmap)
if header is not None:
envelope.append(header)
# Create the soap:body element. The _is_body_wrapped attribute signals
# that the self.body element is of type soap:body, so we don't have to
# create it in that case. Otherwise we create a Element soap:body and
# render the content into this.
if self.body:
body_value = self.body(*args, **kwargs)
if self._is_body_wrapped:
self.body.render(envelope, body_value)
else:
body = soap.Body()
envelope.append(body)
self.body.render(body, body_value)
else:
body = soap.Body()
envelope.append(body)
# XXX: This is only used in Soap 1.1 so should be moved to the the
# Soap11Binding._set_http_headers(). But let's keep it like this for
# now.
headers = {
"SOAPAction": '"%s"' % self.operation.soapaction
if self.operation.soapaction
else '""'
}
return SerializedMessage(path=None, headers=headers, content=envelope)
def deserialize(self, envelope):
"""Deserialize the SOAP:Envelope and return a CompoundValue with the
result.
"""
if not self.envelope:
return None
assert self.header
body = envelope.find("soap-env:Body", namespaces=self.nsmap)
body_result = self._deserialize_body(body)
header = envelope.find("soap-env:Header", namespaces=self.nsmap)
headers_result = self._deserialize_headers(header)
kwargs = body_result
kwargs.update(headers_result)
result = self.envelope(**kwargs)
# If the message
if self.header.type._element:
return result
result = result.body
if not hasattr(result, '__len__'): # Return body directly if len is allowed (could indicated valid primitive type).
return result
if result is None or len(result) == 0:
return None
elif len(result) > 1:
return result
# Check if we can remove the wrapping object to make the return value
# easier to use.
result = next(iter(result.__values__.values()))
if isinstance(result, xsd.CompoundValue):
children = result._xsd_type.elements
attributes = result._xsd_type.attributes
if len(children) == 1 and len(attributes) == 0:
item_name, item_element = children[0]
retval = getattr(result, item_name)
return retval
return result
def signature(self, as_output=False):
if not self.envelope:
return None
if as_output:
if isinstance(self.envelope.type, xsd.ComplexType):
try:
if len(self.envelope.type.elements) == 1:
return self.envelope.type.elements[0][1].type.signature(
schema=self.wsdl.types, standalone=False
)
except AttributeError:
return None
return self.envelope.type.signature(
schema=self.wsdl.types, standalone=False
)
if self.body:
parts = [self.body.type.signature(schema=self.wsdl.types, standalone=False)]
else:
parts = []
assert self.header
if self.header.type._element:
parts.append(
"_soapheaders={%s}"
% self.header.type.signature(schema=self.wsdl.types, standalone=False)
)
return ", ".join(part for part in parts if part)
@classmethod
def parse(cls, definitions, xmlelement, operation, type, nsmap):
"""Parse a wsdl:binding/wsdl:operation/wsdl:operation for the SOAP
implementation.
Each wsdl:operation can contain three child nodes:
- input
- output
- fault
Definition for input/output::
<input>
<soap:body parts="nmtokens"? use="literal|encoded"
encodingStyle="uri-list"? namespace="uri"?>
<soap:header message="qname" part="nmtoken" use="literal|encoded"
encodingStyle="uri-list"? namespace="uri"?>*
<soap:headerfault message="qname" part="nmtoken"
use="literal|encoded"
encodingStyle="uri-list"? namespace="uri"?/>*
</soap:header>
</input>
And the definition for fault::
<soap:fault name="nmtoken" use="literal|encoded"
encodingStyle="uri-list"? namespace="uri"?>
"""
name = xmlelement.get("name")
obj = cls(definitions.wsdl, name, operation, nsmap=nsmap, type=type)
body_data = None
header_data = None
# After some profiling it turns out that .find() and .findall() in this
# case are twice as fast as the xpath method
body = xmlelement.find("soap:body", namespaces=operation.binding.nsmap)
if body is not None:
body_data = cls._parse_body(body)
# Parse soap:header (multiple)
elements = xmlelement.findall("soap:header", namespaces=operation.binding.nsmap)
header_data = cls._parse_header(
elements, definitions.target_namespace, operation
)
obj._resolve_info = {"body": body_data, "header": header_data}
return obj
@classmethod
def _parse_body(cls, xmlelement):
"""Parse soap:body and return a dict with data to resolve it.
<soap:body parts="nmtokens"? use="literal|encoded"?
encodingStyle="uri-list"? namespace="uri"?>
"""
return {
"part": xmlelement.get("part"),
"use": xmlelement.get("use", "literal"),
"encodingStyle": xmlelement.get("encodingStyle"),
"namespace": xmlelement.get("namespace"),
}
@classmethod
def _parse_header(cls, xmlelements, tns, operation):
"""Parse the soap:header and optionally included soap:headerfault elements
<soap:header
message="qname"
part="nmtoken"
use="literal|encoded"
encodingStyle="uri-list"?
namespace="uri"?
/>*
The header can optionally contain one ore more soap:headerfault
elements which can contain the same attributes as the soap:header::
<soap:headerfault message="qname" part="nmtoken" use="literal|encoded"
encodingStyle="uri-list"? namespace="uri"?/>*
"""
result = []
for xmlelement in xmlelements:
data = cls._parse_header_element(xmlelement, tns)
# Add optional soap:headerfault elements
data["faults"] = []
fault_elements = xmlelement.findall(
"soap:headerfault", namespaces=operation.binding.nsmap
)
for fault_element in fault_elements:
fault_data = cls._parse_header_element(fault_element, tns)
data["faults"].append(fault_data)
result.append(data)
return result
@classmethod
def _parse_header_element(cls, xmlelement, tns):
attributes = xmlelement.attrib
message_qname = as_qname(attributes["message"], xmlelement.nsmap, tns)
try:
return {
"message": message_qname,
"part": attributes["part"],
"use": attributes["use"],
"encodingStyle": attributes.get("encodingStyle"),
"namespace": attributes.get("namespace"),
}
except KeyError:
raise exceptions.WsdlSyntaxError("Invalid soap:header(fault)")
def resolve(self, definitions, abstract_message):
"""Resolve the data in the self._resolve_info dict (set via parse())
This creates three xsd.Element objects:
- self.header
- self.body
- self.envelope (combination of headers and body)
XXX headerfaults are not implemented yet.
"""
info = self._resolve_info
del self._resolve_info
# If this message has no parts then we have nothing to do. This might
# happen for output messages which don't return anything.
if (
abstract_message is None or not abstract_message.parts
) and self.type != "input":
return
self.abstract = abstract_message
parts = OrderedDict(self.abstract.parts)
self.header = self._resolve_header(info["header"], definitions, parts)
self.body = self._resolve_body(info["body"], definitions, parts)
self.envelope = self._create_envelope_element()
def _create_envelope_element(self):
"""Create combined `envelope` complexType which contains both the
elements from the body and the headers.
"""
all_elements = xsd.Sequence([])
assert self.header
if self.header.type._element:
all_elements.append(
xsd.Element("{%s}header" % self.nsmap["soap-env"], self.header.type)
)
all_elements.append(
xsd.Element(
"{%s}body" % self.nsmap["soap-env"],
self.body.type if self.body else None,
)
)
return xsd.Element(
"{%s}envelope" % self.nsmap["soap-env"], xsd.ComplexType(all_elements)
)
def _serialize_header(self, headers_value, nsmap):
if not headers_value:
return
headers_value = copy.deepcopy(headers_value)
soap = ElementMaker(namespace=self.nsmap["soap-env"], nsmap=nsmap)
header = soap.Header()
if isinstance(headers_value, list):
for header_value in headers_value:
if isinstance(header_value, CompoundValue):
if hasattr(header_value, "_xsd_elm"):
header_value._xsd_elm.render(header, header_value)
else:
header_value._xsd_type.render(header, header_value)
elif isinstance(header_value, etree._Element):
header.append(header_value)
else:
raise ValueError("Invalid value given to _soapheaders")
elif isinstance(headers_value, dict):
if not self.header:
raise ValueError(
"_soapheaders only accepts a dictionary if the wsdl "
"defines the headers."
)
# Only render headers for which we have a value
headers_value = self.header(**headers_value)
for name, elm in self.header.type.elements:
if name in headers_value and headers_value[name] is not None:
elm.render(header, headers_value[name], ["header", name])
else:
raise ValueError("Invalid value given to _soapheaders")
return header
def _deserialize_body(self, xmlelement):
raise NotImplementedError()
def _deserialize_headers(self, xmlelement):
"""Deserialize the values in the SOAP:Header element"""
if not self.header or xmlelement is None:
return {}
context = XmlParserContext(settings=self.wsdl.settings)
result = self.header.parse(xmlelement, self.wsdl.types, context=context)
if result is not None:
return {"header": result}
return {}
def _resolve_header(self, info, definitions, parts):
name = etree.QName(self.nsmap["soap-env"], "Header")
container = xsd.All(consume_other=True)
if not info:
return xsd.Element(name, xsd.ComplexType(container))
for item in info:
message_name = item["message"].text
part_name = item["part"]
message = definitions.get("messages", message_name)
if message == self.abstract and part_name in parts:
del parts[part_name]
part = message.parts[part_name]
if part.element:
element = part.element.clone()
element.attr_name = part_name
else:
element = xsd.Element(part_name, part.type)
container.append(element)
return xsd.Element(name, xsd.ComplexType(container))
def _resolve_body(self, info, definitions, parts):
raise NotImplementedError()
class DocumentMessage(SoapMessage):
"""In the document message there are no additional wrappers, and the
message parts appear directly under the SOAP Body element.
.. inheritance-diagram:: zeep.wsdl.messages.soap.DocumentMessage
:parts: 1
:param wsdl: The main wsdl document
:type wsdl: zeep.wsdl.Document
:param name:
:param operation: The operation to which this message belongs
:type operation: zeep.wsdl.bindings.soap.SoapOperation
:param type: 'input' or 'output'
:type type: str
:param nsmap: The namespace mapping
:type nsmap: dict
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def _deserialize_body(self, xmlelement):
if not self._is_body_wrapped:
# TODO: For now we assume that the body only has one child since
# only one part is specified in the wsdl. This should be handled
# way better
xmlelement = list(xmlelement)[0]
context = XmlParserContext(settings=self.wsdl.settings)
result = self.body.parse(xmlelement, self.wsdl.types, context=context)
return {"body": result}
def _resolve_body(self, info, definitions, parts):
name = etree.QName(self.nsmap["soap-env"], "Body")
if not info or not parts:
return None
# If the part name is omitted then all parts are available under
# the soap:body tag. Otherwise only the part with the given name.
if info["part"]:
part_name = info["part"]
sub_elements = [parts[part_name].element]
else:
sub_elements = []
for part_name, part in parts.items():
if part.element is not None:
element = part.element.clone()
element.attr_name = part_name or element.name
else:
element = xsd.Element(name=part_name, type_=part.type)
sub_elements.append(element)
if len(sub_elements) > 1:
self._is_body_wrapped = True
return xsd.Element(name, xsd.ComplexType(xsd.All(sub_elements)))
else:
self._is_body_wrapped = False
return sub_elements[0]
class RpcMessage(SoapMessage):
"""In RPC messages each part is a parameter or a return value and appears
inside a wrapper element within the body.
The wrapper element is named identically to the operation name and its
namespace is the value of the namespace attribute. Each message part
(parameter) appears under the wrapper, represented by an accessor named
identically to the corresponding parameter of the call. Parts are arranged
in the same order as the parameters of the call.
.. inheritance-diagram:: zeep.wsdl.messages.soap.DocumentMessage
:parts: 1
:param wsdl: The main wsdl document
:type wsdl: zeep.wsdl.Document
:param name:
:param operation: The operation to which this message belongs
:type operation: zeep.wsdl.bindings.soap.SoapOperation
:param type: 'input' or 'output'
:type type: str
:param nsmap: The namespace mapping
:type nsmap: dict
"""
def _resolve_body(self, info, definitions, parts):
"""Return an XSD element for the SOAP:Body.
Each part is a parameter or a return value and appears inside a
wrapper element within the body named identically to the operation
name and its namespace is the value of the namespace attribute.
"""
if not info:
return None
namespace = info["namespace"]
if self.type == "input":
tag_name = etree.QName(namespace, self.operation.name)
else:
tag_name = etree.QName(namespace, self.abstract.name.localname)
# Create the xsd element to create/parse the response. Each part
# is a sub element of the root node (which uses the operation name)
elements = []
for name, msg in parts.items():
if msg.element:
elements.append(msg.element)
else:
elements.append(xsd.Element(name, msg.type))
return xsd.Element(tag_name, xsd.ComplexType(xsd.Sequence(elements)))
def _deserialize_body(self, body_element):
"""The name of the wrapper element is not defined. The WS-I defines
that it should be the operation name with the 'Response' string as
suffix. But lets just do it really stupid for now and use the first
element.
"""
process_multiref(body_element)
response_element = list(body_element)[0]
if self.body:
context = XmlParserContext(self.wsdl.settings)
result = self.body.parse(response_element, self.wsdl.types, context=context)
return {"body": result}
return {"body": None}