paulb@302 | 1 | #!/usr/bin/env python |
paulb@302 | 2 | |
paulb@302 | 3 | """ |
paulb@318 | 4 | SOAP support using libxml2dom. Support for the archaic SOAP namespaces is also |
paulb@318 | 5 | provided. |
paulb@302 | 6 | |
paulb@302 | 7 | See: http://www.w3.org/TR/2007/REC-soap12-part0-20070427/ |
paulb@302 | 8 | |
paulb@302 | 9 | Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@302 | 10 | |
paulb@302 | 11 | This program is free software; you can redistribute it and/or modify it under |
paulb@302 | 12 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@302 | 13 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@302 | 14 | later version. |
paulb@302 | 15 | |
paulb@302 | 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@302 | 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@302 | 18 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@302 | 19 | details. |
paulb@302 | 20 | |
paulb@302 | 21 | You should have received a copy of the GNU Lesser General Public License along |
paulb@302 | 22 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@302 | 23 | |
paulb@302 | 24 | -------- |
paulb@302 | 25 | |
paulb@302 | 26 | The sending and receiving of SOAP messages can be done using traditional HTTP |
paulb@302 | 27 | libraries. |
paulb@302 | 28 | |
paulb@302 | 29 | See tests/soap_test.py for more details. |
paulb@302 | 30 | """ |
paulb@302 | 31 | |
paulb@302 | 32 | import libxml2dom |
paulb@302 | 33 | from libxml2dom.macrolib import * |
paulb@302 | 34 | from libxml2dom.macrolib import \ |
paulb@302 | 35 | createDocument as Node_createDocument |
paulb@302 | 36 | |
paulb@302 | 37 | # SOAP-related namespaces. |
paulb@302 | 38 | |
paulb@302 | 39 | SOAP_ENVELOPE_NAMESPACE = "http://www.w3.org/2003/05/soap-envelope" |
paulb@302 | 40 | SOAP_ENCODING_NAMESPACE = "http://www.w3.org/2003/05/soap-encoding" |
paulb@302 | 41 | SOAP_RPC_NAMESPACE = "http://www.w3.org/2003/05/soap-rpc" |
paulb@302 | 42 | XS_NAMESPACE = "http://www.w3.org/2001/XMLSchema" |
paulb@302 | 43 | XSI_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance" |
paulb@302 | 44 | |
paulb@318 | 45 | # Archaic namespaces. |
paulb@318 | 46 | |
paulb@318 | 47 | OLD_SOAP_ENVELOPE_NAMESPACE = "http://schemas.xmlsoap.org/soap/envelope/" |
paulb@318 | 48 | OLD_SOAP_ENCODING_NAMESPACE = "http://schemas.xmlsoap.org/soap/encoding/" |
paulb@318 | 49 | |
paulb@302 | 50 | # Default namespace bindings for XPath. |
paulb@302 | 51 | |
paulb@302 | 52 | default_ns = { |
paulb@302 | 53 | "env" : SOAP_ENVELOPE_NAMESPACE, |
paulb@302 | 54 | "enc" : SOAP_ENCODING_NAMESPACE, |
paulb@302 | 55 | "rpc" : SOAP_RPC_NAMESPACE, |
paulb@302 | 56 | "xs" : XS_NAMESPACE, |
paulb@318 | 57 | "xsi" : XSI_NAMESPACE, |
paulb@318 | 58 | "SOAP-ENV" : OLD_SOAP_ENVELOPE_NAMESPACE, |
paulb@318 | 59 | "SOAP-ENC" : OLD_SOAP_ENCODING_NAMESPACE |
paulb@302 | 60 | } |
paulb@302 | 61 | |
paulb@302 | 62 | class SOAPImplementation(libxml2dom.Implementation): |
paulb@302 | 63 | |
paulb@302 | 64 | "Contains a SOAP-specific implementation." |
paulb@302 | 65 | |
paulb@302 | 66 | # Wrapping of documents. |
paulb@302 | 67 | |
paulb@302 | 68 | def adoptDocument(self, node): |
paulb@302 | 69 | return SOAPDocument(node, self) |
paulb@302 | 70 | |
paulb@302 | 71 | # Factory functions. |
paulb@302 | 72 | |
paulb@302 | 73 | def get_node(self, _node, context_node): |
paulb@302 | 74 | |
paulb@302 | 75 | """ |
paulb@302 | 76 | Get a libxml2dom node for the given low-level '_node' and libxml2dom |
paulb@302 | 77 | 'context_node'. |
paulb@302 | 78 | """ |
paulb@302 | 79 | |
paulb@302 | 80 | if Node_nodeType(_node) == context_node.ELEMENT_NODE: |
paulb@302 | 81 | |
paulb@302 | 82 | # Make special envelope elements. |
paulb@302 | 83 | |
paulb@318 | 84 | if Node_namespaceURI(_node) in (SOAP_ENVELOPE_NAMESPACE, OLD_SOAP_ENVELOPE_NAMESPACE): |
paulb@302 | 85 | if Node_localName(_node) == "Envelope": |
paulb@302 | 86 | return SOAPEnvelopeElement(_node, self, context_node.ownerDocument) |
paulb@302 | 87 | elif Node_localName(_node) == "Header": |
paulb@302 | 88 | return SOAPHeaderElement(_node, self, context_node.ownerDocument) |
paulb@302 | 89 | elif Node_localName(_node) == "Body": |
paulb@302 | 90 | return SOAPBodyElement(_node, self, context_node.ownerDocument) |
paulb@302 | 91 | elif Node_localName(_node) == "Fault": |
paulb@302 | 92 | return SOAPFaultElement(_node, self, context_node.ownerDocument) |
paulb@302 | 93 | elif Node_localName(_node) == "Code": |
paulb@302 | 94 | return SOAPCodeElement(_node, self, context_node.ownerDocument) |
paulb@302 | 95 | elif Node_localName(_node) == "Subcode": |
paulb@302 | 96 | return SOAPSubcodeElement(_node, self, context_node.ownerDocument) |
paulb@302 | 97 | elif Node_localName(_node) == "Value": |
paulb@302 | 98 | return SOAPValueElement(_node, self, context_node.ownerDocument) |
paulb@302 | 99 | elif Node_localName(_node) == "Text": |
paulb@302 | 100 | return SOAPTextElement(_node, self, context_node.ownerDocument) |
paulb@302 | 101 | |
paulb@309 | 102 | # Detect the method element. |
paulb@309 | 103 | |
paulb@309 | 104 | if Node_parentNode(_node) and Node_localName(Node_parentNode(_node)) == "Body" and \ |
paulb@318 | 105 | Node_namespaceURI(Node_parentNode(_node)) in (SOAP_ENVELOPE_NAMESPACE, OLD_SOAP_ENVELOPE_NAMESPACE): |
paulb@309 | 106 | |
paulb@309 | 107 | return SOAPMethodElement(_node, self, context_node.ownerDocument) |
paulb@309 | 108 | |
paulb@302 | 109 | # Otherwise, make generic SOAP elements. |
paulb@302 | 110 | |
paulb@302 | 111 | return SOAPElement(_node, self, context_node.ownerDocument) |
paulb@302 | 112 | |
paulb@302 | 113 | else: |
paulb@302 | 114 | return libxml2dom.Implementation.get_node(self, _node, context_node) |
paulb@302 | 115 | |
paulb@302 | 116 | # Convenience functions. |
paulb@302 | 117 | |
paulb@302 | 118 | def createSOAPMessage(self, namespaceURI, localName): |
paulb@302 | 119 | |
paulb@302 | 120 | "Create a new SOAP message document (fragment)." |
paulb@302 | 121 | |
paulb@302 | 122 | return SOAPDocument(Node_createDocument(namespaceURI, localName, None), self).documentElement |
paulb@302 | 123 | |
paulb@302 | 124 | # Node classes. |
paulb@302 | 125 | |
paulb@302 | 126 | class SOAPNode(libxml2dom.Node): |
paulb@302 | 127 | |
paulb@302 | 128 | "Convenience modifications to nodes specific to libxml2dom.soap." |
paulb@302 | 129 | |
paulb@302 | 130 | def xpath(self, expr, variables=None, namespaces=None): |
paulb@302 | 131 | |
paulb@302 | 132 | """ |
paulb@302 | 133 | Evaluate the given 'expr' using the optional 'variables' and |
paulb@302 | 134 | 'namespaces'. If not otherwise specified, the prefixes given in the |
paulb@302 | 135 | module global 'default_ns' will be bound as in that dictionary. |
paulb@302 | 136 | """ |
paulb@302 | 137 | |
paulb@302 | 138 | ns = {} |
paulb@302 | 139 | ns.update(default_ns) |
paulb@302 | 140 | ns.update(namespaces or {}) |
paulb@302 | 141 | return libxml2dom.Node.xpath(self, expr, variables, ns) |
paulb@302 | 142 | |
paulb@315 | 143 | # All nodes support convenience methods. |
paulb@315 | 144 | |
paulb@315 | 145 | def convert(self, node): |
paulb@315 | 146 | return node.textContent.strip() |
paulb@315 | 147 | |
paulb@315 | 148 | def _contents(self): |
paulb@318 | 149 | # NOTE: Should check whether this should be a leaf element. |
paulb@315 | 150 | if not self.xpath("*"): |
paulb@315 | 151 | return (self.localName, getattr(self.ownerDocument, "convert", self.convert)(self)) |
paulb@315 | 152 | else: |
paulb@318 | 153 | return (self.localName, SOAPContents(self)) |
paulb@315 | 154 | |
paulb@315 | 155 | def __len__(self): |
paulb@318 | 156 | return 2 |
paulb@315 | 157 | |
paulb@315 | 158 | def __getitem__(self, i): |
paulb@318 | 159 | return self.contents[i] |
paulb@315 | 160 | |
paulb@315 | 161 | def __eq__(self, other): |
paulb@318 | 162 | if hasattr(other, "contents"): |
paulb@315 | 163 | return self.contents == other.contents |
paulb@315 | 164 | else: |
paulb@315 | 165 | return self.contents == other |
paulb@315 | 166 | |
paulb@318 | 167 | # Node construction methods. |
paulb@318 | 168 | |
paulb@318 | 169 | def createSOAPElement(self, localName): |
paulb@318 | 170 | |
paulb@318 | 171 | "Create an element with the appropriate namespace and prefix." |
paulb@318 | 172 | |
paulb@318 | 173 | ref_element = self.ownerDocument.documentElement |
paulb@318 | 174 | prefix = ref_element.prefix |
paulb@318 | 175 | if prefix: |
paulb@318 | 176 | name = prefix + ":" + localName |
paulb@318 | 177 | else: |
paulb@318 | 178 | name = localName |
paulb@318 | 179 | return self.createElementNS(ref_element.namespaceURI, name) |
paulb@318 | 180 | |
paulb@315 | 181 | contents = property(_contents) |
paulb@315 | 182 | |
paulb@318 | 183 | class SOAPContents(object): |
paulb@318 | 184 | |
paulb@318 | 185 | "A wrapper around another node in order to provide sequence-like access." |
paulb@318 | 186 | |
paulb@318 | 187 | def __init__(self, node): |
paulb@318 | 188 | self.node = node |
paulb@318 | 189 | |
paulb@318 | 190 | def __len__(self): |
paulb@318 | 191 | return len(self.node.xpath("*")) |
paulb@318 | 192 | |
paulb@318 | 193 | def __getitem__(self, i): |
paulb@318 | 194 | return self.node.xpath("*")[i] |
paulb@318 | 195 | |
paulb@318 | 196 | def __eq__(self, other): |
paulb@318 | 197 | for i, j in map(None, self, other): |
paulb@318 | 198 | if i != j: |
paulb@318 | 199 | return False |
paulb@318 | 200 | return True |
paulb@318 | 201 | |
paulb@302 | 202 | class SOAPDocument(libxml2dom._Document, SOAPNode): |
paulb@302 | 203 | |
paulb@302 | 204 | "A SOAP document fragment." |
paulb@302 | 205 | |
paulb@302 | 206 | def _envelope(self): |
paulb@318 | 207 | return self.xpath("env:Envelope|SOAP-ENV:Envelope")[0] |
paulb@302 | 208 | |
paulb@302 | 209 | envelope = property(_envelope) |
paulb@302 | 210 | |
paulb@307 | 211 | # Convenience methods and properties. |
paulb@307 | 212 | |
paulb@307 | 213 | def _fault(self): |
paulb@307 | 214 | return self.envelope.body.fault |
paulb@307 | 215 | |
paulb@309 | 216 | def _method(self): |
paulb@309 | 217 | return self.envelope.body.method |
paulb@309 | 218 | |
paulb@307 | 219 | fault = property(_fault) |
paulb@309 | 220 | method = property(_method) |
paulb@307 | 221 | |
paulb@302 | 222 | class SOAPElement(SOAPNode): |
paulb@302 | 223 | |
paulb@302 | 224 | "A SOAP element." |
paulb@302 | 225 | |
paulb@302 | 226 | pass |
paulb@302 | 227 | |
paulb@302 | 228 | class SOAPEnvelopeElement(SOAPNode): |
paulb@302 | 229 | |
paulb@302 | 230 | "A SOAP envelope element." |
paulb@302 | 231 | |
paulb@302 | 232 | def _body(self): |
paulb@318 | 233 | return self.xpath("env:Body|SOAP-ENV:Body")[0] |
paulb@302 | 234 | |
paulb@302 | 235 | def _setBody(self, body): |
paulb@302 | 236 | self.appendChild(body) |
paulb@302 | 237 | |
paulb@302 | 238 | def _delBody(self): |
paulb@302 | 239 | self.removeChild(self.body) |
paulb@302 | 240 | |
paulb@302 | 241 | def createBody(self): |
paulb@318 | 242 | return self.createSOAPElement("Body") |
paulb@302 | 243 | |
paulb@302 | 244 | body = property(_body, _setBody, _delBody) |
paulb@302 | 245 | |
paulb@302 | 246 | class SOAPHeaderElement(SOAPNode): |
paulb@302 | 247 | |
paulb@302 | 248 | "A SOAP header element." |
paulb@302 | 249 | |
paulb@302 | 250 | pass |
paulb@302 | 251 | |
paulb@302 | 252 | class SOAPBodyElement(SOAPNode): |
paulb@302 | 253 | |
paulb@302 | 254 | "A SOAP body element." |
paulb@302 | 255 | |
paulb@302 | 256 | def _fault(self): |
paulb@318 | 257 | return (self.xpath("env:Fault|SOAP-ENV:Fault") or [None])[0] |
paulb@302 | 258 | |
paulb@302 | 259 | def _method(self): |
paulb@318 | 260 | if self.namespaceURI == SOAP_ENVELOPE_NAMESPACE: |
paulb@318 | 261 | return (self.xpath("*[@env:encodingStyle = '%s']" % SOAP_ENCODING_NAMESPACE) or [None])[0] |
paulb@318 | 262 | else: |
paulb@318 | 263 | return (self.xpath("*") or [None])[0] |
paulb@302 | 264 | |
paulb@309 | 265 | # Node construction methods. |
paulb@309 | 266 | |
paulb@309 | 267 | def createFault(self): |
paulb@318 | 268 | return self.createSOAPElement("Fault") |
paulb@309 | 269 | |
paulb@309 | 270 | fault = property(_fault) |
paulb@309 | 271 | method = property(_method) |
paulb@309 | 272 | |
paulb@309 | 273 | class SOAPMethodElement(SOAPNode): |
paulb@309 | 274 | |
paulb@309 | 275 | "A SOAP method element." |
paulb@309 | 276 | |
paulb@302 | 277 | def _methodName(self): |
paulb@309 | 278 | return self.localName |
paulb@302 | 279 | |
paulb@302 | 280 | def _resultParameter(self): |
paulb@309 | 281 | return (self.xpath(".//rpc:result") or [None])[0] |
paulb@302 | 282 | |
paulb@302 | 283 | def _resultParameterValue(self): |
paulb@307 | 284 | if self.resultParameter: |
paulb@307 | 285 | name = self.resultParameter.textContent.strip() |
paulb@315 | 286 | result = self.xpath(".//" + name, namespaces={self.prefix : self.namespaceURI}) |
paulb@307 | 287 | if result: |
paulb@307 | 288 | return result[0].textContent.strip() |
paulb@307 | 289 | else: |
paulb@307 | 290 | return None |
paulb@307 | 291 | else: |
paulb@307 | 292 | return None |
paulb@302 | 293 | |
paulb@314 | 294 | def _parameterValues(self): |
paulb@315 | 295 | return [value.contents for value in self.xpath("*")] |
paulb@302 | 296 | |
paulb@302 | 297 | methodName = property(_methodName) |
paulb@302 | 298 | resultParameter = property(_resultParameter) |
paulb@302 | 299 | resultParameterValue = property(_resultParameterValue) |
paulb@315 | 300 | parameterValues = property(_parameterValues) |
paulb@302 | 301 | |
paulb@302 | 302 | class SOAPFaultElement(SOAPNode): |
paulb@302 | 303 | |
paulb@302 | 304 | "A SOAP fault element." |
paulb@302 | 305 | |
paulb@302 | 306 | def _code(self): |
paulb@318 | 307 | code = self.xpath("env:Code|SOAP-ENV:Code") |
paulb@307 | 308 | if code: |
paulb@307 | 309 | return code[0].value |
paulb@307 | 310 | else: |
paulb@307 | 311 | return None |
paulb@307 | 312 | |
paulb@307 | 313 | def _subcode(self): |
paulb@318 | 314 | subcode = self.xpath("./env:Code/env:Subcode|./SOAP-ENV:Code/SOAP-ENV:Subcode") |
paulb@307 | 315 | if subcode: |
paulb@307 | 316 | return subcode[0].value |
paulb@307 | 317 | else: |
paulb@307 | 318 | return None |
paulb@302 | 319 | |
paulb@302 | 320 | def _reason(self): |
paulb@318 | 321 | return (self.xpath("env:Reason|SOAP-ENV:Reason") or [None])[0] |
paulb@302 | 322 | |
paulb@302 | 323 | def _detail(self): |
paulb@318 | 324 | return (self.xpath("env:Detail|SOAP-ENV:Detail") or [None])[0] |
paulb@302 | 325 | |
paulb@302 | 326 | def createCode(self): |
paulb@318 | 327 | return self.createSOAPElement("Code") |
paulb@302 | 328 | |
paulb@302 | 329 | code = property(_code) |
paulb@307 | 330 | subcode = property(_subcode) |
paulb@302 | 331 | reason = property(_reason) |
paulb@302 | 332 | detail = property(_detail) |
paulb@302 | 333 | |
paulb@302 | 334 | class SOAPSubcodeElement(SOAPNode): |
paulb@302 | 335 | |
paulb@302 | 336 | "A SOAP subcode element." |
paulb@302 | 337 | |
paulb@302 | 338 | def _value(self): |
paulb@318 | 339 | value = self.xpath("env:Value|SOAP-ENV:Value") |
paulb@307 | 340 | if value: |
paulb@307 | 341 | return value[0].textContent.strip() |
paulb@307 | 342 | else: |
paulb@307 | 343 | return None |
paulb@304 | 344 | |
paulb@304 | 345 | def _setValue(self, value): |
paulb@318 | 346 | nodes = self.xpath("env:Value|SOAP-ENV:Value") |
paulb@304 | 347 | v = self.createValue() |
paulb@304 | 348 | if nodes: |
paulb@304 | 349 | self.replaceChild(v, nodes[0]) |
paulb@304 | 350 | else: |
paulb@304 | 351 | self.appendChild(v) |
paulb@304 | 352 | v.value = value |
paulb@302 | 353 | |
paulb@302 | 354 | def createValue(self, value=None): |
paulb@318 | 355 | code_value = self.createSOAPElement("Value") |
paulb@302 | 356 | if value is not None: |
paulb@302 | 357 | code_value.value = code |
paulb@302 | 358 | return code_value |
paulb@302 | 359 | |
paulb@304 | 360 | value = property(_value, _setValue) |
paulb@302 | 361 | |
paulb@302 | 362 | class SOAPCodeElement(SOAPSubcodeElement): |
paulb@302 | 363 | |
paulb@302 | 364 | "A SOAP code element." |
paulb@302 | 365 | |
paulb@302 | 366 | def _subcode(self): |
paulb@318 | 367 | return (self.xpath("env:Subcode|SOAP-ENV:Subcode") or [None])[0] |
paulb@302 | 368 | |
paulb@302 | 369 | def createSubcode(self): |
paulb@318 | 370 | return self.createSOAPElement("Subcode") |
paulb@302 | 371 | |
paulb@302 | 372 | subcode = property(_subcode) |
paulb@302 | 373 | |
paulb@302 | 374 | class SOAPValueElement(SOAPNode): |
paulb@302 | 375 | |
paulb@302 | 376 | "A SOAP value element." |
paulb@302 | 377 | |
paulb@302 | 378 | def _value(self): |
paulb@302 | 379 | return self.textContent |
paulb@302 | 380 | |
paulb@302 | 381 | def _setValue(self, value): |
paulb@302 | 382 | for node in self.childNodes: |
paulb@302 | 383 | self.removeChild(node) |
paulb@302 | 384 | text = self.ownerDocument.createTextNode(value) |
paulb@302 | 385 | self.appendChild(text) |
paulb@302 | 386 | |
paulb@302 | 387 | value = property(_value, _setValue) |
paulb@302 | 388 | |
paulb@302 | 389 | class SOAPTextElement(SOAPValueElement): |
paulb@302 | 390 | |
paulb@302 | 391 | "A SOAP text element." |
paulb@302 | 392 | |
paulb@302 | 393 | def _lang(self): |
paulb@302 | 394 | return self.getAttributeNS(libxml2dom.XML_NAMESPACE, "lang") |
paulb@302 | 395 | |
paulb@302 | 396 | def _setLang(self, value): |
paulb@302 | 397 | self.setAttributeNS(libxml2dom.XML_NAMESPACE, "xml:lang", value) |
paulb@302 | 398 | |
paulb@302 | 399 | lang = property(_lang, _setLang) |
paulb@302 | 400 | |
paulb@302 | 401 | # Utility functions. |
paulb@302 | 402 | |
paulb@302 | 403 | createDocument = libxml2dom.createDocument |
paulb@302 | 404 | createDocumentType = libxml2dom.createDocumentType |
paulb@302 | 405 | |
paulb@302 | 406 | def createSOAPMessage(namespaceURI, localName): |
paulb@302 | 407 | return default_impl.createSOAPMessage(namespaceURI, localName) |
paulb@302 | 408 | |
paulb@302 | 409 | def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 410 | return libxml2dom.parse(stream_or_string, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 411 | |
paulb@302 | 412 | def parseFile(filename, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 413 | return libxml2dom.parseFile(filename, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 414 | |
paulb@302 | 415 | def parseString(s, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 416 | return libxml2dom.parseString(s, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 417 | |
paulb@302 | 418 | def parseURI(uri, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 419 | return libxml2dom.parseURI(uri, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 420 | |
paulb@302 | 421 | # Single instance of the implementation. |
paulb@302 | 422 | |
paulb@302 | 423 | default_impl = SOAPImplementation() |
paulb@302 | 424 | |
paulb@302 | 425 | # vim: tabstop=4 expandtab shiftwidth=4 |