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