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