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