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@315 | 135 | # All nodes support convenience methods. |
paulb@315 | 136 | |
paulb@315 | 137 | def convert(self, node): |
paulb@315 | 138 | return node.textContent.strip() |
paulb@315 | 139 | |
paulb@315 | 140 | def _contents(self): |
paulb@315 | 141 | if not self.xpath("*"): |
paulb@315 | 142 | return (self.localName, getattr(self.ownerDocument, "convert", self.convert)(self)) |
paulb@315 | 143 | else: |
paulb@315 | 144 | return self |
paulb@315 | 145 | |
paulb@315 | 146 | def __len__(self): |
paulb@315 | 147 | children = self.xpath("*") |
paulb@315 | 148 | if not children: |
paulb@315 | 149 | return 2 |
paulb@315 | 150 | else: |
paulb@315 | 151 | return len(children) |
paulb@315 | 152 | |
paulb@315 | 153 | def __getitem__(self, i): |
paulb@315 | 154 | children = self.xpath("*") |
paulb@315 | 155 | if not children: |
paulb@315 | 156 | return self.contents[i] |
paulb@315 | 157 | else: |
paulb@315 | 158 | return self.xpath("*")[i] |
paulb@315 | 159 | |
paulb@315 | 160 | def __eq__(self, other): |
paulb@315 | 161 | children = self.xpath("*") |
paulb@315 | 162 | if children: |
paulb@315 | 163 | for i, j in map(None, self, other): |
paulb@315 | 164 | if i != j: |
paulb@315 | 165 | return False |
paulb@315 | 166 | return True |
paulb@315 | 167 | elif hasattr(other, "contents"): |
paulb@315 | 168 | return self.contents == other.contents |
paulb@315 | 169 | else: |
paulb@315 | 170 | return self.contents == other |
paulb@315 | 171 | |
paulb@315 | 172 | contents = property(_contents) |
paulb@315 | 173 | |
paulb@302 | 174 | class SOAPDocument(libxml2dom._Document, SOAPNode): |
paulb@302 | 175 | |
paulb@302 | 176 | "A SOAP document fragment." |
paulb@302 | 177 | |
paulb@302 | 178 | def _envelope(self): |
paulb@302 | 179 | return self.xpath("./env:Envelope")[0] |
paulb@302 | 180 | |
paulb@302 | 181 | envelope = property(_envelope) |
paulb@302 | 182 | |
paulb@307 | 183 | # Convenience methods and properties. |
paulb@307 | 184 | |
paulb@307 | 185 | def _fault(self): |
paulb@307 | 186 | return self.envelope.body.fault |
paulb@307 | 187 | |
paulb@309 | 188 | def _method(self): |
paulb@309 | 189 | return self.envelope.body.method |
paulb@309 | 190 | |
paulb@307 | 191 | fault = property(_fault) |
paulb@309 | 192 | method = property(_method) |
paulb@307 | 193 | |
paulb@302 | 194 | class SOAPElement(SOAPNode): |
paulb@302 | 195 | |
paulb@302 | 196 | "A SOAP element." |
paulb@302 | 197 | |
paulb@302 | 198 | pass |
paulb@302 | 199 | |
paulb@302 | 200 | class SOAPEnvelopeElement(SOAPNode): |
paulb@302 | 201 | |
paulb@302 | 202 | "A SOAP envelope element." |
paulb@302 | 203 | |
paulb@302 | 204 | def _body(self): |
paulb@302 | 205 | return self.xpath("./env:Body")[0] |
paulb@302 | 206 | |
paulb@302 | 207 | def _setBody(self, body): |
paulb@302 | 208 | self.appendChild(body) |
paulb@302 | 209 | |
paulb@302 | 210 | def _delBody(self): |
paulb@302 | 211 | self.removeChild(self.body) |
paulb@302 | 212 | |
paulb@302 | 213 | def createBody(self): |
paulb@302 | 214 | return self.ownerDocument.createElementNS(SOAP_ENVELOPE_NAMESPACE, "env:Body") |
paulb@302 | 215 | |
paulb@302 | 216 | body = property(_body, _setBody, _delBody) |
paulb@302 | 217 | |
paulb@302 | 218 | class SOAPHeaderElement(SOAPNode): |
paulb@302 | 219 | |
paulb@302 | 220 | "A SOAP header element." |
paulb@302 | 221 | |
paulb@302 | 222 | pass |
paulb@302 | 223 | |
paulb@302 | 224 | class SOAPBodyElement(SOAPNode): |
paulb@302 | 225 | |
paulb@302 | 226 | "A SOAP body element." |
paulb@302 | 227 | |
paulb@302 | 228 | def _fault(self): |
paulb@307 | 229 | return (self.xpath("./env:Fault") or [None])[0] |
paulb@302 | 230 | |
paulb@302 | 231 | def _method(self): |
paulb@307 | 232 | return (self.xpath("./*[@env:encodingStyle = '%s']" % SOAP_ENCODING_NAMESPACE) or [None])[0] |
paulb@302 | 233 | |
paulb@309 | 234 | # Node construction methods. |
paulb@309 | 235 | |
paulb@309 | 236 | def createFault(self): |
paulb@309 | 237 | return self.ownerDocument.createElementNS(SOAP_ENVELOPE_NAMESPACE, "env:Fault") |
paulb@309 | 238 | |
paulb@309 | 239 | fault = property(_fault) |
paulb@309 | 240 | method = property(_method) |
paulb@309 | 241 | |
paulb@309 | 242 | class SOAPMethodElement(SOAPNode): |
paulb@309 | 243 | |
paulb@309 | 244 | "A SOAP method element." |
paulb@309 | 245 | |
paulb@302 | 246 | def _methodName(self): |
paulb@309 | 247 | return self.localName |
paulb@302 | 248 | |
paulb@302 | 249 | def _resultParameter(self): |
paulb@309 | 250 | return (self.xpath(".//rpc:result") or [None])[0] |
paulb@302 | 251 | |
paulb@302 | 252 | def _resultParameterValue(self): |
paulb@307 | 253 | if self.resultParameter: |
paulb@307 | 254 | name = self.resultParameter.textContent.strip() |
paulb@315 | 255 | result = self.xpath(".//" + name, namespaces={self.prefix : self.namespaceURI}) |
paulb@307 | 256 | if result: |
paulb@307 | 257 | return result[0].textContent.strip() |
paulb@307 | 258 | else: |
paulb@307 | 259 | return None |
paulb@307 | 260 | else: |
paulb@307 | 261 | return None |
paulb@302 | 262 | |
paulb@314 | 263 | def _parameterValues(self): |
paulb@315 | 264 | return [value.contents for value in self.xpath("*")] |
paulb@302 | 265 | |
paulb@302 | 266 | methodName = property(_methodName) |
paulb@302 | 267 | resultParameter = property(_resultParameter) |
paulb@302 | 268 | resultParameterValue = property(_resultParameterValue) |
paulb@315 | 269 | parameterValues = property(_parameterValues) |
paulb@302 | 270 | |
paulb@302 | 271 | class SOAPFaultElement(SOAPNode): |
paulb@302 | 272 | |
paulb@302 | 273 | "A SOAP fault element." |
paulb@302 | 274 | |
paulb@302 | 275 | def _code(self): |
paulb@307 | 276 | code = self.xpath("./env:Code") |
paulb@307 | 277 | if code: |
paulb@307 | 278 | return code[0].value |
paulb@307 | 279 | else: |
paulb@307 | 280 | return None |
paulb@307 | 281 | |
paulb@307 | 282 | def _subcode(self): |
paulb@307 | 283 | subcode = self.xpath("./env:Code/env:Subcode") |
paulb@307 | 284 | if subcode: |
paulb@307 | 285 | return subcode[0].value |
paulb@307 | 286 | else: |
paulb@307 | 287 | return None |
paulb@302 | 288 | |
paulb@302 | 289 | def _reason(self): |
paulb@307 | 290 | return (self.xpath("./env:Reason") or [None])[0] |
paulb@302 | 291 | |
paulb@302 | 292 | def _detail(self): |
paulb@307 | 293 | return (self.xpath("./env:Detail") or [None])[0] |
paulb@302 | 294 | |
paulb@302 | 295 | def createCode(self): |
paulb@302 | 296 | return self.ownerDocument.createElementNS(SOAP_ENVELOPE_NAMESPACE, "env:Code") |
paulb@302 | 297 | |
paulb@302 | 298 | code = property(_code) |
paulb@307 | 299 | subcode = property(_subcode) |
paulb@302 | 300 | reason = property(_reason) |
paulb@302 | 301 | detail = property(_detail) |
paulb@302 | 302 | |
paulb@302 | 303 | class SOAPSubcodeElement(SOAPNode): |
paulb@302 | 304 | |
paulb@302 | 305 | "A SOAP subcode element." |
paulb@302 | 306 | |
paulb@302 | 307 | def _value(self): |
paulb@307 | 308 | value = self.xpath("./env:Value") |
paulb@307 | 309 | if value: |
paulb@307 | 310 | return value[0].textContent.strip() |
paulb@307 | 311 | else: |
paulb@307 | 312 | return None |
paulb@304 | 313 | |
paulb@304 | 314 | def _setValue(self, value): |
paulb@304 | 315 | nodes = self.xpath("./env:Value") |
paulb@304 | 316 | v = self.createValue() |
paulb@304 | 317 | if nodes: |
paulb@304 | 318 | self.replaceChild(v, nodes[0]) |
paulb@304 | 319 | else: |
paulb@304 | 320 | self.appendChild(v) |
paulb@304 | 321 | v.value = value |
paulb@302 | 322 | |
paulb@302 | 323 | def createValue(self, value=None): |
paulb@302 | 324 | code_value = self.ownerDocument.createElementNS(SOAP_ENVELOPE_NAMESPACE, "env:Value") |
paulb@302 | 325 | if value is not None: |
paulb@302 | 326 | code_value.value = code |
paulb@302 | 327 | return code_value |
paulb@302 | 328 | |
paulb@304 | 329 | value = property(_value, _setValue) |
paulb@302 | 330 | |
paulb@302 | 331 | class SOAPCodeElement(SOAPSubcodeElement): |
paulb@302 | 332 | |
paulb@302 | 333 | "A SOAP code element." |
paulb@302 | 334 | |
paulb@302 | 335 | def _subcode(self): |
paulb@307 | 336 | return (self.xpath("./env:Subcode") or [None])[0] |
paulb@302 | 337 | |
paulb@302 | 338 | def createSubcode(self): |
paulb@302 | 339 | return self.ownerDocument.createElementNS(SOAP_ENVELOPE_NAMESPACE, "env:Subcode") |
paulb@302 | 340 | |
paulb@302 | 341 | subcode = property(_subcode) |
paulb@302 | 342 | |
paulb@302 | 343 | class SOAPValueElement(SOAPNode): |
paulb@302 | 344 | |
paulb@302 | 345 | "A SOAP value element." |
paulb@302 | 346 | |
paulb@302 | 347 | def _value(self): |
paulb@302 | 348 | return self.textContent |
paulb@302 | 349 | |
paulb@302 | 350 | def _setValue(self, value): |
paulb@302 | 351 | for node in self.childNodes: |
paulb@302 | 352 | self.removeChild(node) |
paulb@302 | 353 | text = self.ownerDocument.createTextNode(value) |
paulb@302 | 354 | self.appendChild(text) |
paulb@302 | 355 | |
paulb@302 | 356 | value = property(_value, _setValue) |
paulb@302 | 357 | |
paulb@302 | 358 | class SOAPTextElement(SOAPValueElement): |
paulb@302 | 359 | |
paulb@302 | 360 | "A SOAP text element." |
paulb@302 | 361 | |
paulb@302 | 362 | def _lang(self): |
paulb@302 | 363 | return self.getAttributeNS(libxml2dom.XML_NAMESPACE, "lang") |
paulb@302 | 364 | |
paulb@302 | 365 | def _setLang(self, value): |
paulb@302 | 366 | self.setAttributeNS(libxml2dom.XML_NAMESPACE, "xml:lang", value) |
paulb@302 | 367 | |
paulb@302 | 368 | lang = property(_lang, _setLang) |
paulb@302 | 369 | |
paulb@302 | 370 | # Utility functions. |
paulb@302 | 371 | |
paulb@302 | 372 | createDocument = libxml2dom.createDocument |
paulb@302 | 373 | createDocumentType = libxml2dom.createDocumentType |
paulb@302 | 374 | |
paulb@302 | 375 | def createSOAPMessage(namespaceURI, localName): |
paulb@302 | 376 | return default_impl.createSOAPMessage(namespaceURI, localName) |
paulb@302 | 377 | |
paulb@302 | 378 | def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 379 | return libxml2dom.parse(stream_or_string, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 380 | |
paulb@302 | 381 | def parseFile(filename, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 382 | return libxml2dom.parseFile(filename, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 383 | |
paulb@302 | 384 | def parseString(s, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 385 | return libxml2dom.parseString(s, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 386 | |
paulb@302 | 387 | def parseURI(uri, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@302 | 388 | return libxml2dom.parseURI(uri, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@302 | 389 | |
paulb@302 | 390 | # Single instance of the implementation. |
paulb@302 | 391 | |
paulb@302 | 392 | default_impl = SOAPImplementation() |
paulb@302 | 393 | |
paulb@302 | 394 | # vim: tabstop=4 expandtab shiftwidth=4 |