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@309 | 94 | # Detect the method element. |
paulb@309 | 95 | |
paulb@309 | 96 | if Node_parentNode(_node) and Node_localName(Node_parentNode(_node)) == "Body" and \ |
paulb@309 | 97 | Node_namespaceURI(Node_parentNode(_node)) == SOAP_ENVELOPE_NAMESPACE: |
paulb@309 | 98 | |
paulb@309 | 99 | return SOAPMethodElement(_node, self, context_node.ownerDocument) |
paulb@309 | 100 | |
paulb@302 | 101 | # Otherwise, make generic SOAP elements. |
paulb@302 | 102 | |
paulb@302 | 103 | return SOAPElement(_node, self, context_node.ownerDocument) |
paulb@302 | 104 | |
paulb@302 | 105 | else: |
paulb@302 | 106 | return libxml2dom.Implementation.get_node(self, _node, context_node) |
paulb@302 | 107 | |
paulb@302 | 108 | # Convenience functions. |
paulb@302 | 109 | |
paulb@302 | 110 | def createSOAPMessage(self, namespaceURI, localName): |
paulb@302 | 111 | |
paulb@302 | 112 | "Create a new SOAP message document (fragment)." |
paulb@302 | 113 | |
paulb@302 | 114 | return SOAPDocument(Node_createDocument(namespaceURI, localName, None), self).documentElement |
paulb@302 | 115 | |
paulb@302 | 116 | # Node classes. |
paulb@302 | 117 | |
paulb@302 | 118 | class SOAPNode(libxml2dom.Node): |
paulb@302 | 119 | |
paulb@302 | 120 | "Convenience modifications to nodes specific to libxml2dom.soap." |
paulb@302 | 121 | |
paulb@302 | 122 | def xpath(self, expr, variables=None, namespaces=None): |
paulb@302 | 123 | |
paulb@302 | 124 | """ |
paulb@302 | 125 | Evaluate the given 'expr' using the optional 'variables' and |
paulb@302 | 126 | 'namespaces'. If not otherwise specified, the prefixes given in the |
paulb@302 | 127 | module global 'default_ns' will be bound as in that dictionary. |
paulb@302 | 128 | """ |
paulb@302 | 129 | |
paulb@302 | 130 | ns = {} |
paulb@302 | 131 | ns.update(default_ns) |
paulb@302 | 132 | ns.update(namespaces or {}) |
paulb@302 | 133 | return libxml2dom.Node.xpath(self, expr, variables, ns) |
paulb@302 | 134 | |
paulb@302 | 135 | class SOAPDocument(libxml2dom._Document, SOAPNode): |
paulb@302 | 136 | |
paulb@302 | 137 | "A SOAP document fragment." |
paulb@302 | 138 | |
paulb@302 | 139 | def _envelope(self): |
paulb@302 | 140 | return self.xpath("./env:Envelope")[0] |
paulb@302 | 141 | |
paulb@302 | 142 | envelope = property(_envelope) |
paulb@302 | 143 | |
paulb@307 | 144 | # Convenience methods and properties. |
paulb@307 | 145 | |
paulb@307 | 146 | def _fault(self): |
paulb@307 | 147 | return self.envelope.body.fault |
paulb@307 | 148 | |
paulb@309 | 149 | def _method(self): |
paulb@309 | 150 | return self.envelope.body.method |
paulb@309 | 151 | |
paulb@307 | 152 | fault = property(_fault) |
paulb@309 | 153 | method = property(_method) |
paulb@307 | 154 | |
paulb@302 | 155 | class SOAPElement(SOAPNode): |
paulb@302 | 156 | |
paulb@302 | 157 | "A SOAP element." |
paulb@302 | 158 | |
paulb@302 | 159 | pass |
paulb@302 | 160 | |
paulb@302 | 161 | class SOAPEnvelopeElement(SOAPNode): |
paulb@302 | 162 | |
paulb@302 | 163 | "A SOAP envelope element." |
paulb@302 | 164 | |
paulb@302 | 165 | def _body(self): |
paulb@302 | 166 | return self.xpath("./env:Body")[0] |
paulb@302 | 167 | |
paulb@302 | 168 | def _setBody(self, body): |
paulb@302 | 169 | self.appendChild(body) |
paulb@302 | 170 | |
paulb@302 | 171 | def _delBody(self): |
paulb@302 | 172 | self.removeChild(self.body) |
paulb@302 | 173 | |
paulb@302 | 174 | def createBody(self): |
paulb@302 | 175 | return self.ownerDocument.createElementNS(SOAP_ENVELOPE_NAMESPACE, "env:Body") |
paulb@302 | 176 | |
paulb@302 | 177 | body = property(_body, _setBody, _delBody) |
paulb@302 | 178 | |
paulb@302 | 179 | class SOAPHeaderElement(SOAPNode): |
paulb@302 | 180 | |
paulb@302 | 181 | "A SOAP header element." |
paulb@302 | 182 | |
paulb@302 | 183 | pass |
paulb@302 | 184 | |
paulb@302 | 185 | class SOAPBodyElement(SOAPNode): |
paulb@302 | 186 | |
paulb@302 | 187 | "A SOAP body element." |
paulb@302 | 188 | |
paulb@302 | 189 | def _fault(self): |
paulb@307 | 190 | return (self.xpath("./env:Fault") or [None])[0] |
paulb@302 | 191 | |
paulb@302 | 192 | def _method(self): |
paulb@307 | 193 | return (self.xpath("./*[@env:encodingStyle = '%s']" % SOAP_ENCODING_NAMESPACE) or [None])[0] |
paulb@302 | 194 | |
paulb@309 | 195 | # Node construction methods. |
paulb@309 | 196 | |
paulb@309 | 197 | def createFault(self): |
paulb@309 | 198 | return self.ownerDocument.createElementNS(SOAP_ENVELOPE_NAMESPACE, "env:Fault") |
paulb@309 | 199 | |
paulb@309 | 200 | fault = property(_fault) |
paulb@309 | 201 | method = property(_method) |
paulb@309 | 202 | |
paulb@309 | 203 | class SOAPMethodElement(SOAPNode): |
paulb@309 | 204 | |
paulb@309 | 205 | "A SOAP method element." |
paulb@309 | 206 | |
paulb@302 | 207 | def _methodName(self): |
paulb@309 | 208 | return self.localName |
paulb@302 | 209 | |
paulb@302 | 210 | def _resultParameter(self): |
paulb@309 | 211 | return (self.xpath(".//rpc:result") or [None])[0] |
paulb@302 | 212 | |
paulb@302 | 213 | def _resultParameterValue(self): |
paulb@307 | 214 | if self.resultParameter: |
paulb@307 | 215 | name = self.resultParameter.textContent.strip() |
paulb@309 | 216 | result = self.xpath(".//%s" % name, namespaces={self.prefix : self.namespaceURI}) |
paulb@307 | 217 | if result: |
paulb@307 | 218 | return result[0].textContent.strip() |
paulb@307 | 219 | else: |
paulb@307 | 220 | return None |
paulb@307 | 221 | else: |
paulb@307 | 222 | return None |
paulb@302 | 223 | |
paulb@302 | 224 | def _parameters(self): |
paulb@309 | 225 | return self.xpath("*") |
paulb@307 | 226 | |
paulb@309 | 227 | def _parameterValues(self): |
paulb@307 | 228 | values = [] |
paulb@307 | 229 | for parameter in self.parameters: |
paulb@309 | 230 | values.append(self._get_value(parameter)) |
paulb@307 | 231 | return values |
paulb@302 | 232 | |
paulb@309 | 233 | def _setParameterValues(self, parameters): |
paulb@309 | 234 | for node in self.parameters: |
paulb@309 | 235 | self.removeChild(node) |
paulb@307 | 236 | |
paulb@309 | 237 | # Add the parameter values. |
paulb@309 | 238 | |
paulb@309 | 239 | for parameter in parameters: |
paulb@309 | 240 | self._add_value(self, parameter) |
paulb@307 | 241 | |
paulb@307 | 242 | # Internal methods. |
paulb@307 | 243 | |
paulb@309 | 244 | def _add_value(self, value, parameter): |
paulb@309 | 245 | (ns, name), parameter = parameter |
paulb@309 | 246 | container = self.ownerDocument.createElementNS(ns, name) |
paulb@309 | 247 | value.appendChild(container) |
paulb@309 | 248 | if isinstance(parameter, (list, dict)): |
paulb@309 | 249 | if isinstance(parameter, dict): |
paulb@309 | 250 | items = parameter.items() |
paulb@309 | 251 | else: |
paulb@309 | 252 | items = parameter |
paulb@309 | 253 | for item in items: |
paulb@309 | 254 | self._add_value(container, item) |
paulb@309 | 255 | else: |
paulb@309 | 256 | text = self.ownerDocument.createTextNode(unicode(parameter)) |
paulb@309 | 257 | container.appendChild(text) |
paulb@309 | 258 | |
paulb@309 | 259 | def _get_value(self, parameter): |
paulb@307 | 260 | elements = parameter.xpath("*") |
paulb@307 | 261 | if elements: |
paulb@307 | 262 | items = [] |
paulb@307 | 263 | for element in elements: |
paulb@309 | 264 | items.append(self._get_value(element)) |
paulb@309 | 265 | return (parameter.namespaceURI, parameter.name), items |
paulb@307 | 266 | else: |
paulb@309 | 267 | return (parameter.namespaceURI, parameter.name), parameter.textContent.strip() |
paulb@302 | 268 | |
paulb@302 | 269 | methodName = property(_methodName) |
paulb@302 | 270 | resultParameter = property(_resultParameter) |
paulb@302 | 271 | resultParameterValue = property(_resultParameterValue) |
paulb@302 | 272 | parameters = property(_parameters) |
paulb@309 | 273 | parameterValues = property(_parameterValues, _setParameterValues) |
paulb@302 | 274 | |
paulb@302 | 275 | class SOAPFaultElement(SOAPNode): |
paulb@302 | 276 | |
paulb@302 | 277 | "A SOAP fault element." |
paulb@302 | 278 | |
paulb@302 | 279 | def _code(self): |
paulb@307 | 280 | code = self.xpath("./env:Code") |
paulb@307 | 281 | if code: |
paulb@307 | 282 | return code[0].value |
paulb@307 | 283 | else: |
paulb@307 | 284 | return None |
paulb@307 | 285 | |
paulb@307 | 286 | def _subcode(self): |
paulb@307 | 287 | subcode = self.xpath("./env:Code/env:Subcode") |
paulb@307 | 288 | if subcode: |
paulb@307 | 289 | return subcode[0].value |
paulb@307 | 290 | else: |
paulb@307 | 291 | return None |
paulb@302 | 292 | |
paulb@302 | 293 | def _reason(self): |
paulb@307 | 294 | return (self.xpath("./env:Reason") or [None])[0] |
paulb@302 | 295 | |
paulb@302 | 296 | def _detail(self): |
paulb@307 | 297 | return (self.xpath("./env:Detail") or [None])[0] |
paulb@302 | 298 | |
paulb@302 | 299 | def createCode(self): |
paulb@302 | 300 | return self.ownerDocument.createElementNS(SOAP_ENVELOPE_NAMESPACE, "env:Code") |
paulb@302 | 301 | |
paulb@302 | 302 | code = property(_code) |
paulb@307 | 303 | subcode = property(_subcode) |
paulb@302 | 304 | reason = property(_reason) |
paulb@302 | 305 | detail = property(_detail) |
paulb@302 | 306 | |
paulb@302 | 307 | class SOAPSubcodeElement(SOAPNode): |
paulb@302 | 308 | |
paulb@302 | 309 | "A SOAP subcode element." |
paulb@302 | 310 | |
paulb@302 | 311 | def _value(self): |
paulb@307 | 312 | value = self.xpath("./env:Value") |
paulb@307 | 313 | if value: |
paulb@307 | 314 | return value[0].textContent.strip() |
paulb@307 | 315 | else: |
paulb@307 | 316 | return None |
paulb@304 | 317 | |
paulb@304 | 318 | def _setValue(self, value): |
paulb@304 | 319 | nodes = self.xpath("./env:Value") |
paulb@304 | 320 | v = self.createValue() |
paulb@304 | 321 | if nodes: |
paulb@304 | 322 | self.replaceChild(v, nodes[0]) |
paulb@304 | 323 | else: |
paulb@304 | 324 | self.appendChild(v) |
paulb@304 | 325 | v.value = value |
paulb@302 | 326 | |
paulb@302 | 327 | def createValue(self, value=None): |
paulb@302 | 328 | code_value = self.ownerDocument.createElementNS(SOAP_ENVELOPE_NAMESPACE, "env:Value") |
paulb@302 | 329 | if value is not None: |
paulb@302 | 330 | code_value.value = code |
paulb@302 | 331 | return code_value |
paulb@302 | 332 | |
paulb@304 | 333 | value = property(_value, _setValue) |
paulb@302 | 334 | |
paulb@302 | 335 | class SOAPCodeElement(SOAPSubcodeElement): |
paulb@302 | 336 | |
paulb@302 | 337 | "A SOAP code element." |
paulb@302 | 338 | |
paulb@302 | 339 | def _subcode(self): |
paulb@307 | 340 | return (self.xpath("./env:Subcode") or [None])[0] |
paulb@302 | 341 | |
paulb@302 | 342 | def createSubcode(self): |
paulb@302 | 343 | return self.ownerDocument.createElementNS(SOAP_ENVELOPE_NAMESPACE, "env:Subcode") |
paulb@302 | 344 | |
paulb@302 | 345 | subcode = property(_subcode) |
paulb@302 | 346 | |
paulb@302 | 347 | class SOAPValueElement(SOAPNode): |
paulb@302 | 348 | |
paulb@302 | 349 | "A SOAP value element." |
paulb@302 | 350 | |
paulb@302 | 351 | def _value(self): |
paulb@302 | 352 | return self.textContent |
paulb@302 | 353 | |
paulb@302 | 354 | def _setValue(self, value): |
paulb@302 | 355 | for node in self.childNodes: |
paulb@302 | 356 | self.removeChild(node) |
paulb@302 | 357 | text = self.ownerDocument.createTextNode(value) |
paulb@302 | 358 | self.appendChild(text) |
paulb@302 | 359 | |
paulb@302 | 360 | value = property(_value, _setValue) |
paulb@302 | 361 | |
paulb@302 | 362 | class SOAPTextElement(SOAPValueElement): |
paulb@302 | 363 | |
paulb@302 | 364 | "A SOAP text element." |
paulb@302 | 365 | |
paulb@302 | 366 | def _lang(self): |
paulb@302 | 367 | return self.getAttributeNS(libxml2dom.XML_NAMESPACE, "lang") |
paulb@302 | 368 | |
paulb@302 | 369 | def _setLang(self, value): |
paulb@302 | 370 | self.setAttributeNS(libxml2dom.XML_NAMESPACE, "xml:lang", value) |
paulb@302 | 371 | |
paulb@302 | 372 | lang = property(_lang, _setLang) |
paulb@302 | 373 | |
paulb@302 | 374 | # Utility functions. |
paulb@302 | 375 | |
paulb@302 | 376 | createDocument = libxml2dom.createDocument |
paulb@302 | 377 | createDocumentType = libxml2dom.createDocumentType |
paulb@302 | 378 | |
paulb@302 | 379 | def createSOAPMessage(namespaceURI, localName): |
paulb@302 | 380 | return default_impl.createSOAPMessage(namespaceURI, localName) |
paulb@302 | 381 | |
paulb@302 | 382 | def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 383 | return libxml2dom.parse(stream_or_string, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 384 | |
paulb@302 | 385 | def parseFile(filename, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 386 | return libxml2dom.parseFile(filename, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 387 | |
paulb@302 | 388 | def parseString(s, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 389 | return libxml2dom.parseString(s, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 390 | |
paulb@302 | 391 | def parseURI(uri, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 392 | return libxml2dom.parseURI(uri, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 393 | |
paulb@302 | 394 | # Single instance of the implementation. |
paulb@302 | 395 | |
paulb@302 | 396 | default_impl = SOAPImplementation() |
paulb@302 | 397 | |
paulb@302 | 398 | # vim: tabstop=4 expandtab shiftwidth=4 |