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