You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1623 lines
58 KiB

  1. # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
  2. #
  3. # This software is subject to the provisions of the Zope Public License,
  4. # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
  5. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  6. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  7. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  8. # FOR A PARTICULAR PURPOSE.
  9. ident = "$Id$"
  10. import weakref
  11. from cStringIO import StringIO
  12. from Namespaces import OASIS, XMLNS, WSA200408, WSA200403, WSA200303
  13. from Utility import Collection, CollectionNS, DOM, ElementProxy, basejoin
  14. from XMLSchema import XMLSchema, SchemaReader, WSDLToolsAdapter
  15. class WSDLReader:
  16. """A WSDLReader creates WSDL instances from urls and xml data."""
  17. # Custom subclasses of WSDLReader may wish to implement a caching
  18. # strategy or other optimizations. Because application needs vary
  19. # so widely, we don't try to provide any caching by default.
  20. def loadFromStream(self, stream, name=None):
  21. """Return a WSDL instance loaded from a stream object."""
  22. document = DOM.loadDocument(stream)
  23. wsdl = WSDL()
  24. if name:
  25. wsdl.location = name
  26. elif hasattr(stream, 'name'):
  27. wsdl.location = stream.name
  28. wsdl.load(document)
  29. return wsdl
  30. def loadFromURL(self, url):
  31. """Return a WSDL instance loaded from the given url."""
  32. document = DOM.loadFromURL(url)
  33. wsdl = WSDL()
  34. wsdl.location = url
  35. wsdl.load(document)
  36. return wsdl
  37. def loadFromString(self, data):
  38. """Return a WSDL instance loaded from an xml string."""
  39. return self.loadFromStream(StringIO(data))
  40. def loadFromFile(self, filename):
  41. """Return a WSDL instance loaded from the given file."""
  42. file = open(filename, 'rb')
  43. try:
  44. wsdl = self.loadFromStream(file)
  45. finally:
  46. file.close()
  47. return wsdl
  48. class WSDL:
  49. """A WSDL object models a WSDL service description. WSDL objects
  50. may be created manually or loaded from an xml representation
  51. using a WSDLReader instance."""
  52. def __init__(self, targetNamespace=None, strict=1):
  53. self.targetNamespace = targetNamespace or 'urn:this-document.wsdl'
  54. self.documentation = ''
  55. self.location = None
  56. self.document = None
  57. self.name = None
  58. self.services = CollectionNS(self)
  59. self.messages = CollectionNS(self)
  60. self.portTypes = CollectionNS(self)
  61. self.bindings = CollectionNS(self)
  62. self.imports = Collection(self)
  63. self.types = Types(self)
  64. self.extensions = []
  65. self.strict = strict
  66. def __del__(self):
  67. if self.document is not None:
  68. self.document.unlink()
  69. version = '1.1'
  70. def addService(self, name, documentation='', targetNamespace=None):
  71. if self.services.has_key(name):
  72. raise WSDLError(
  73. 'Duplicate service element: %s' % name
  74. )
  75. item = Service(name, documentation)
  76. if targetNamespace:
  77. item.targetNamespace = targetNamespace
  78. self.services[name] = item
  79. return item
  80. def addMessage(self, name, documentation='', targetNamespace=None):
  81. if self.messages.has_key(name):
  82. raise WSDLError(
  83. 'Duplicate message element: %s.' % name
  84. )
  85. item = Message(name, documentation)
  86. if targetNamespace:
  87. item.targetNamespace = targetNamespace
  88. self.messages[name] = item
  89. return item
  90. def addPortType(self, name, documentation='', targetNamespace=None):
  91. if self.portTypes.has_key(name):
  92. raise WSDLError(
  93. 'Duplicate portType element: name'
  94. )
  95. item = PortType(name, documentation)
  96. if targetNamespace:
  97. item.targetNamespace = targetNamespace
  98. self.portTypes[name] = item
  99. return item
  100. def addBinding(self, name, type, documentation='', targetNamespace=None):
  101. if self.bindings.has_key(name):
  102. raise WSDLError(
  103. 'Duplicate binding element: %s' % name
  104. )
  105. item = Binding(name, type, documentation)
  106. if targetNamespace:
  107. item.targetNamespace = targetNamespace
  108. self.bindings[name] = item
  109. return item
  110. def addImport(self, namespace, location):
  111. item = ImportElement(namespace, location)
  112. self.imports[namespace] = item
  113. return item
  114. def toDom(self):
  115. """ Generate a DOM representation of the WSDL instance.
  116. Not dealing with generating XML Schema, thus the targetNamespace
  117. of all XML Schema elements or types used by WSDL message parts
  118. needs to be specified via import information items.
  119. """
  120. namespaceURI = DOM.GetWSDLUri(self.version)
  121. self.document = DOM.createDocument(namespaceURI ,'wsdl:definitions')
  122. # Set up a couple prefixes for easy reading.
  123. child = DOM.getElement(self.document, None)
  124. child.setAttributeNS(None, 'targetNamespace', self.targetNamespace)
  125. child.setAttributeNS(XMLNS.BASE, 'xmlns:wsdl', namespaceURI)
  126. child.setAttributeNS(XMLNS.BASE, 'xmlns:xsd', 'http://www.w3.org/1999/XMLSchema')
  127. child.setAttributeNS(XMLNS.BASE, 'xmlns:soap', 'http://schemas.xmlsoap.org/wsdl/soap/')
  128. child.setAttributeNS(XMLNS.BASE, 'xmlns:tns', self.targetNamespace)
  129. # wsdl:import
  130. for item in self.imports:
  131. item.toDom()
  132. # wsdl:message
  133. for item in self.messages:
  134. item.toDom()
  135. # wsdl:portType
  136. for item in self.portTypes:
  137. item.toDom()
  138. # wsdl:binding
  139. for item in self.bindings:
  140. item.toDom()
  141. # wsdl:service
  142. for item in self.services:
  143. item.toDom()
  144. def load(self, document):
  145. # We save a reference to the DOM document to ensure that elements
  146. # saved as "extensions" will continue to have a meaningful context
  147. # for things like namespace references. The lifetime of the DOM
  148. # document is bound to the lifetime of the WSDL instance.
  149. self.document = document
  150. definitions = DOM.getElement(document, 'definitions', None, None)
  151. if definitions is None:
  152. raise WSDLError(
  153. 'Missing <definitions> element.'
  154. )
  155. self.version = DOM.WSDLUriToVersion(definitions.namespaceURI)
  156. NS_WSDL = DOM.GetWSDLUri(self.version)
  157. self.targetNamespace = DOM.getAttr(definitions, 'targetNamespace',
  158. None, None)
  159. self.name = DOM.getAttr(definitions, 'name', None, None)
  160. self.documentation = GetDocumentation(definitions)
  161. #
  162. # Retrieve all <wsdl:import>'s, append all children of imported
  163. # document to main document. First iteration grab all original
  164. # <wsdl:import>'s from document, second iteration grab all
  165. # "imported" <wsdl:imports> from document, etc break out when
  166. # no more <wsdl:import>'s.
  167. #
  168. imported = []
  169. base_location = self.location
  170. do_it = True
  171. while do_it:
  172. do_it = False
  173. for element in DOM.getElements(definitions, 'import', NS_WSDL):
  174. location = DOM.getAttr(element, 'location')
  175. if base_location is not None:
  176. location = basejoin(base_location, location)
  177. if location not in imported:
  178. do_it = True
  179. self._import(document, element, base_location)
  180. imported.append(location)
  181. else:
  182. definitions.removeChild(element)
  183. base_location = None
  184. #
  185. # No more <wsdl:import>'s, now load up all other
  186. # WSDL information items.
  187. #
  188. for element in DOM.getElements(definitions, None, None):
  189. targetNamespace = DOM.getAttr(element, 'targetNamespace')
  190. localName = element.localName
  191. if not DOM.nsUriMatch(element.namespaceURI, NS_WSDL):
  192. if localName == 'schema':
  193. reader = SchemaReader(base_url=self.location)
  194. schema = reader.loadFromNode(WSDLToolsAdapter(self), element)
  195. schema.setBaseUrl(self.location)
  196. self.types.addSchema(schema)
  197. else:
  198. self.extensions.append(element)
  199. continue
  200. elif localName == 'message':
  201. name = DOM.getAttr(element, 'name')
  202. docs = GetDocumentation(element)
  203. message = self.addMessage(name, docs, targetNamespace)
  204. parts = DOM.getElements(element, 'part', NS_WSDL)
  205. message.load(parts)
  206. continue
  207. elif localName == 'portType':
  208. name = DOM.getAttr(element, 'name')
  209. docs = GetDocumentation(element)
  210. ptype = self.addPortType(name, docs, targetNamespace)
  211. #operations = DOM.getElements(element, 'operation', NS_WSDL)
  212. #ptype.load(operations)
  213. ptype.load(element)
  214. continue
  215. elif localName == 'binding':
  216. name = DOM.getAttr(element, 'name')
  217. type = DOM.getAttr(element, 'type', default=None)
  218. if type is None:
  219. raise WSDLError(
  220. 'Missing type attribute for binding %s.' % name
  221. )
  222. type = ParseQName(type, element)
  223. docs = GetDocumentation(element)
  224. binding = self.addBinding(name, type, docs, targetNamespace)
  225. operations = DOM.getElements(element, 'operation', NS_WSDL)
  226. binding.load(operations)
  227. binding.load_ex(GetExtensions(element))
  228. continue
  229. elif localName == 'service':
  230. name = DOM.getAttr(element, 'name')
  231. docs = GetDocumentation(element)
  232. service = self.addService(name, docs, targetNamespace)
  233. ports = DOM.getElements(element, 'port', NS_WSDL)
  234. service.load(ports)
  235. service.load_ex(GetExtensions(element))
  236. continue
  237. elif localName == 'types':
  238. self.types.documentation = GetDocumentation(element)
  239. base_location = DOM.getAttr(element, 'base-location')
  240. if base_location:
  241. element.removeAttribute('base-location')
  242. base_location = base_location or self.location
  243. reader = SchemaReader(base_url=base_location)
  244. for item in DOM.getElements(element, None, None):
  245. if item.localName == 'schema':
  246. schema = reader.loadFromNode(WSDLToolsAdapter(self), item)
  247. # XXX <types> could have been imported
  248. #schema.setBaseUrl(self.location)
  249. schema.setBaseUrl(base_location)
  250. self.types.addSchema(schema)
  251. else:
  252. self.types.addExtension(item)
  253. # XXX remove the attribute
  254. # element.removeAttribute('base-location')
  255. continue
  256. def _import(self, document, element, base_location=None):
  257. '''Algo take <import> element's children, clone them,
  258. and add them to the main document. Support for relative
  259. locations is a bit complicated. The orig document context
  260. is lost, so we need to store base location in DOM elements
  261. representing <types>, by creating a special temporary
  262. "base-location" attribute, and <import>, by resolving
  263. the relative "location" and storing it as "location".
  264. document -- document we are loading
  265. element -- DOM Element representing <import>
  266. base_location -- location of document from which this
  267. <import> was gleaned.
  268. '''
  269. namespace = DOM.getAttr(element, 'namespace', default=None)
  270. location = DOM.getAttr(element, 'location', default=None)
  271. if namespace is None or location is None:
  272. raise WSDLError(
  273. 'Invalid import element (missing namespace or location).'
  274. )
  275. if base_location:
  276. location = basejoin(base_location, location)
  277. element.setAttributeNS(None, 'location', location)
  278. obimport = self.addImport(namespace, location)
  279. obimport._loaded = 1
  280. importdoc = DOM.loadFromURL(location)
  281. try:
  282. if location.find('#') > -1:
  283. idref = location.split('#')[-1]
  284. imported = DOM.getElementById(importdoc, idref)
  285. else:
  286. imported = importdoc.documentElement
  287. if imported is None:
  288. raise WSDLError(
  289. 'Import target element not found for: %s' % location
  290. )
  291. imported_tns = DOM.findTargetNS(imported)
  292. if imported_tns != namespace:
  293. return
  294. if imported.localName == 'definitions':
  295. imported_nodes = imported.childNodes
  296. else:
  297. imported_nodes = [imported]
  298. parent = element.parentNode
  299. parent.removeChild(element)
  300. for node in imported_nodes:
  301. if node.nodeType != node.ELEMENT_NODE:
  302. continue
  303. child = DOM.importNode(document, node, 1)
  304. parent.appendChild(child)
  305. child.setAttribute('targetNamespace', namespace)
  306. attrsNS = imported._attrsNS
  307. for attrkey in attrsNS.keys():
  308. if attrkey[0] == DOM.NS_XMLNS:
  309. attr = attrsNS[attrkey].cloneNode(1)
  310. child.setAttributeNode(attr)
  311. #XXX Quick Hack, should be in WSDL Namespace.
  312. if child.localName == 'import':
  313. rlocation = child.getAttributeNS(None, 'location')
  314. alocation = basejoin(location, rlocation)
  315. child.setAttribute('location', alocation)
  316. elif child.localName == 'types':
  317. child.setAttribute('base-location', location)
  318. finally:
  319. importdoc.unlink()
  320. return location
  321. class Element:
  322. """A class that provides common functions for WSDL element classes."""
  323. def __init__(self, name=None, documentation=''):
  324. self.name = name
  325. self.documentation = documentation
  326. self.extensions = []
  327. def addExtension(self, item):
  328. item.parent = weakref.ref(self)
  329. self.extensions.append(item)
  330. class ImportElement(Element):
  331. def __init__(self, namespace, location):
  332. self.namespace = namespace
  333. self.location = location
  334. def getWSDL(self):
  335. """Return the WSDL object that contains this Message Part."""
  336. return self.parent().parent()
  337. def toDom(self):
  338. wsdl = self.getWSDL()
  339. ep = ElementProxy(None, DOM.getElement(wsdl.document, None))
  340. epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), 'import')
  341. epc.setAttributeNS(None, 'namespace', self.namespace)
  342. epc.setAttributeNS(None, 'location', self.location)
  343. _loaded = None
  344. class Types(Collection):
  345. default = lambda self,k: k.targetNamespace
  346. def __init__(self, parent):
  347. Collection.__init__(self, parent)
  348. self.documentation = ''
  349. self.extensions = []
  350. def addSchema(self, schema):
  351. name = schema.targetNamespace
  352. self[name] = schema
  353. return schema
  354. def addExtension(self, item):
  355. self.extensions.append(item)
  356. class Message(Element):
  357. def __init__(self, name, documentation=''):
  358. Element.__init__(self, name, documentation)
  359. self.parts = Collection(self)
  360. def addPart(self, name, type=None, element=None):
  361. if self.parts.has_key(name):
  362. raise WSDLError(
  363. 'Duplicate message part element: %s' % name
  364. )
  365. if type is None and element is None:
  366. raise WSDLError(
  367. 'Missing type or element attribute for part: %s' % name
  368. )
  369. item = MessagePart(name)
  370. item.element = element
  371. item.type = type
  372. self.parts[name] = item
  373. return item
  374. def load(self, elements):
  375. for element in elements:
  376. name = DOM.getAttr(element, 'name')
  377. part = MessagePart(name)
  378. self.parts[name] = part
  379. elemref = DOM.getAttr(element, 'element', default=None)
  380. typeref = DOM.getAttr(element, 'type', default=None)
  381. if typeref is None and elemref is None:
  382. raise WSDLError(
  383. 'No type or element attribute for part: %s' % name
  384. )
  385. if typeref is not None:
  386. part.type = ParseTypeRef(typeref, element)
  387. if elemref is not None:
  388. part.element = ParseTypeRef(elemref, element)
  389. def getElementDeclaration(self):
  390. """Return the XMLSchema.ElementDeclaration instance or None"""
  391. element = None
  392. if self.element:
  393. nsuri,name = self.element
  394. wsdl = self.getWSDL()
  395. if wsdl.types.has_key(nsuri) and wsdl.types[nsuri].elements.has_key(name):
  396. element = wsdl.types[nsuri].elements[name]
  397. return element
  398. def getTypeDefinition(self):
  399. """Return the XMLSchema.TypeDefinition instance or None"""
  400. type = None
  401. if self.type:
  402. nsuri,name = self.type
  403. wsdl = self.getWSDL()
  404. if wsdl.types.has_key(nsuri) and wsdl.types[nsuri].types.has_key(name):
  405. type = wsdl.types[nsuri].types[name]
  406. return type
  407. def getWSDL(self):
  408. """Return the WSDL object that contains this Message Part."""
  409. return self.parent().parent()
  410. def toDom(self):
  411. wsdl = self.getWSDL()
  412. ep = ElementProxy(None, DOM.getElement(wsdl.document, None))
  413. epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), 'message')
  414. epc.setAttributeNS(None, 'name', self.name)
  415. for part in self.parts:
  416. part.toDom(epc._getNode())
  417. class MessagePart(Element):
  418. def __init__(self, name):
  419. Element.__init__(self, name, '')
  420. self.element = None
  421. self.type = None
  422. def getWSDL(self):
  423. """Return the WSDL object that contains this Message Part."""
  424. return self.parent().parent().parent().parent()
  425. def getTypeDefinition(self):
  426. wsdl = self.getWSDL()
  427. nsuri,name = self.type
  428. schema = wsdl.types.get(nsuri, {})
  429. return schema.get(name)
  430. def getElementDeclaration(self):
  431. wsdl = self.getWSDL()
  432. nsuri,name = self.element
  433. schema = wsdl.types.get(nsuri, {})
  434. return schema.get(name)
  435. def toDom(self, node):
  436. """node -- node representing message"""
  437. wsdl = self.getWSDL()
  438. ep = ElementProxy(None, node)
  439. epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), 'part')
  440. epc.setAttributeNS(None, 'name', self.name)
  441. if self.element is not None:
  442. ns,name = self.element
  443. prefix = epc.getPrefix(ns)
  444. epc.setAttributeNS(None, 'element', '%s:%s'%(prefix,name))
  445. elif self.type is not None:
  446. ns,name = self.type
  447. prefix = epc.getPrefix(ns)
  448. epc.setAttributeNS(None, 'type', '%s:%s'%(prefix,name))
  449. class PortType(Element):
  450. '''PortType has a anyAttribute, thus must provide for an extensible
  451. mechanism for supporting such attributes. ResourceProperties is
  452. specified in WS-ResourceProperties. wsa:Action is specified in
  453. WS-Address.
  454. Instance Data:
  455. name -- name attribute
  456. resourceProperties -- optional. wsr:ResourceProperties attribute,
  457. value is a QName this is Parsed into a (namespaceURI, name)
  458. that represents a Global Element Declaration.
  459. operations
  460. '''
  461. def __init__(self, name, documentation=''):
  462. Element.__init__(self, name, documentation)
  463. self.operations = Collection(self)
  464. self.resourceProperties = None
  465. def getWSDL(self):
  466. return self.parent().parent()
  467. def getTargetNamespace(self):
  468. return self.targetNamespace or self.getWSDL().targetNamespace
  469. def getResourceProperties(self):
  470. return self.resourceProperties
  471. def addOperation(self, name, documentation='', parameterOrder=None):
  472. item = Operation(name, documentation, parameterOrder)
  473. self.operations[name] = item
  474. return item
  475. def load(self, element):
  476. self.name = DOM.getAttr(element, 'name')
  477. self.documentation = GetDocumentation(element)
  478. self.targetNamespace = DOM.getAttr(element, 'targetNamespace')
  479. if DOM.hasAttr(element, 'ResourceProperties', OASIS.PROPERTIES):
  480. rpref = DOM.getAttr(element, 'ResourceProperties', OASIS.PROPERTIES)
  481. self.resourceProperties = ParseQName(rpref, element)
  482. lookfor = (WSA200408, WSA200403, WSA200303,)
  483. NS_WSDL = DOM.GetWSDLUri(self.getWSDL().version)
  484. elements = DOM.getElements(element, 'operation', NS_WSDL)
  485. for element in elements:
  486. name = DOM.getAttr(element, 'name')
  487. docs = GetDocumentation(element)
  488. param_order = DOM.getAttr(element, 'parameterOrder', default=None)
  489. if param_order is not None:
  490. param_order = param_order.split(' ')
  491. operation = self.addOperation(name, docs, param_order)
  492. item = DOM.getElement(element, 'input', None, None)
  493. if item is not None:
  494. name = DOM.getAttr(item, 'name')
  495. docs = GetDocumentation(item)
  496. msgref = DOM.getAttr(item, 'message')
  497. message = ParseQName(msgref, item)
  498. for WSA in lookfor:
  499. action = DOM.getAttr(item, 'Action', WSA.ADDRESS, None)
  500. if action: break
  501. operation.setInput(message, name, docs, action)
  502. item = DOM.getElement(element, 'output', None, None)
  503. if item is not None:
  504. name = DOM.getAttr(item, 'name')
  505. docs = GetDocumentation(item)
  506. msgref = DOM.getAttr(item, 'message')
  507. message = ParseQName(msgref, item)
  508. for WSA in lookfor:
  509. action = DOM.getAttr(item, 'Action', WSA.ADDRESS, None)
  510. if action: break
  511. operation.setOutput(message, name, docs, action)
  512. for item in DOM.getElements(element, 'fault', None):
  513. name = DOM.getAttr(item, 'name')
  514. docs = GetDocumentation(item)
  515. msgref = DOM.getAttr(item, 'message')
  516. message = ParseQName(msgref, item)
  517. for WSA in lookfor:
  518. action = DOM.getAttr(item, 'Action', WSA.ADDRESS, None)
  519. if action: break
  520. operation.addFault(message, name, docs, action)
  521. def toDom(self):
  522. wsdl = self.getWSDL()
  523. ep = ElementProxy(None, DOM.getElement(wsdl.document, None))
  524. epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), 'portType')
  525. epc.setAttributeNS(None, 'name', self.name)
  526. if self.resourceProperties:
  527. ns,name = self.resourceProperties
  528. prefix = epc.getPrefix(ns)
  529. epc.setAttributeNS(OASIS.PROPERTIES, 'ResourceProperties', '%s:%s'%(prefix,name))
  530. for op in self.operations:
  531. op.toDom(epc._getNode())
  532. class Operation(Element):
  533. def __init__(self, name, documentation='', parameterOrder=None):
  534. Element.__init__(self, name, documentation)
  535. self.parameterOrder = parameterOrder
  536. self.faults = Collection(self)
  537. self.input = None
  538. self.output = None
  539. def getWSDL(self):
  540. """Return the WSDL object that contains this Operation."""
  541. return self.parent().parent().parent().parent()
  542. def getPortType(self):
  543. return self.parent().parent()
  544. def getInputAction(self):
  545. """wsa:Action attribute"""
  546. return GetWSAActionInput(self)
  547. def getInputMessage(self):
  548. if self.input is None:
  549. return None
  550. wsdl = self.getPortType().getWSDL()
  551. return wsdl.messages[self.input.message]
  552. def getOutputAction(self):
  553. """wsa:Action attribute"""
  554. return GetWSAActionOutput(self)
  555. def getOutputMessage(self):
  556. if self.output is None:
  557. return None
  558. wsdl = self.getPortType().getWSDL()
  559. return wsdl.messages[self.output.message]
  560. def getFaultAction(self, name):
  561. """wsa:Action attribute"""
  562. return GetWSAActionFault(self, name)
  563. def getFaultMessage(self, name):
  564. wsdl = self.getPortType().getWSDL()
  565. return wsdl.messages[self.faults[name].message]
  566. def addFault(self, message, name, documentation='', action=None):
  567. if self.faults.has_key(name):
  568. raise WSDLError(
  569. 'Duplicate fault element: %s' % name
  570. )
  571. item = MessageRole('fault', message, name, documentation, action)
  572. self.faults[name] = item
  573. return item
  574. def setInput(self, message, name='', documentation='', action=None):
  575. self.input = MessageRole('input', message, name, documentation, action)
  576. self.input.parent = weakref.ref(self)
  577. return self.input
  578. def setOutput(self, message, name='', documentation='', action=None):
  579. self.output = MessageRole('output', message, name, documentation, action)
  580. self.output.parent = weakref.ref(self)
  581. return self.output
  582. def toDom(self, node):
  583. wsdl = self.getWSDL()
  584. ep = ElementProxy(None, node)
  585. epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), 'operation')
  586. epc.setAttributeNS(None, 'name', self.name)
  587. node = epc._getNode()
  588. if self.input:
  589. self.input.toDom(node)
  590. if self.output:
  591. self.output.toDom(node)
  592. for fault in self.faults:
  593. fault.toDom(node)
  594. class MessageRole(Element):
  595. def __init__(self, type, message, name='', documentation='', action=None):
  596. Element.__init__(self, name, documentation)
  597. self.message = message
  598. self.type = type
  599. self.action = action
  600. def getWSDL(self):
  601. """Return the WSDL object that contains this MessageRole."""
  602. if self.parent().getWSDL() == 'fault':
  603. return self.parent().parent().getWSDL()
  604. return self.parent().getWSDL()
  605. def getMessage(self):
  606. """Return the WSDL object that represents the attribute message
  607. (namespaceURI, name) tuple
  608. """
  609. wsdl = self.getWSDL()
  610. return wsdl.messages[self.message]
  611. def toDom(self, node):
  612. wsdl = self.getWSDL()
  613. ep = ElementProxy(None, node)
  614. epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), self.type)
  615. epc.setAttributeNS(None, 'message', self.message)
  616. if self.action:
  617. epc.setAttributeNS(WSA200408.ADDRESS, 'Action', self.action)
  618. class Binding(Element):
  619. def __init__(self, name, type, documentation=''):
  620. Element.__init__(self, name, documentation)
  621. self.operations = Collection(self)
  622. self.type = type
  623. def getWSDL(self):
  624. """Return the WSDL object that contains this binding."""
  625. return self.parent().parent()
  626. def getPortType(self):
  627. """Return the PortType object associated with this binding."""
  628. return self.getWSDL().portTypes[self.type]
  629. def findBinding(self, kind):
  630. for item in self.extensions:
  631. if isinstance(item, kind):
  632. return item
  633. return None
  634. def findBindings(self, kind):
  635. return [ item for item in self.extensions if isinstance(item, kind) ]
  636. def addOperationBinding(self, name, documentation=''):
  637. item = OperationBinding(name, documentation)
  638. self.operations[name] = item
  639. return item
  640. def load(self, elements):
  641. for element in elements:
  642. name = DOM.getAttr(element, 'name')
  643. docs = GetDocumentation(element)
  644. opbinding = self.addOperationBinding(name, docs)
  645. opbinding.load_ex(GetExtensions(element))
  646. item = DOM.getElement(element, 'input', None, None)
  647. if item is not None:
  648. mbinding = MessageRoleBinding('input')
  649. mbinding.documentation = GetDocumentation(item)
  650. opbinding.input = mbinding
  651. mbinding.load_ex(GetExtensions(item))
  652. item = DOM.getElement(element, 'output', None, None)
  653. if item is not None:
  654. mbinding = MessageRoleBinding('output')
  655. mbinding.documentation = GetDocumentation(item)
  656. opbinding.output = mbinding
  657. mbinding.load_ex(GetExtensions(item))
  658. for item in DOM.getElements(element, 'fault', None):
  659. name = DOM.getAttr(item, 'name')
  660. mbinding = MessageRoleBinding('fault', name)
  661. mbinding.documentation = GetDocumentation(item)
  662. opbinding.faults[name] = mbinding
  663. mbinding.load_ex(GetExtensions(item))
  664. def load_ex(self, elements):
  665. for e in elements:
  666. ns, name = e.namespaceURI, e.localName
  667. if ns in DOM.NS_SOAP_BINDING_ALL and name == 'binding':
  668. transport = DOM.getAttr(e, 'transport', default=None)
  669. style = DOM.getAttr(e, 'style', default='document')
  670. ob = SoapBinding(transport, style)
  671. self.addExtension(ob)
  672. continue
  673. elif ns in DOM.NS_HTTP_BINDING_ALL and name == 'binding':
  674. verb = DOM.getAttr(e, 'verb')
  675. ob = HttpBinding(verb)
  676. self.addExtension(ob)
  677. continue
  678. else:
  679. self.addExtension(e)
  680. def toDom(self):
  681. wsdl = self.getWSDL()
  682. ep = ElementProxy(None, DOM.getElement(wsdl.document, None))
  683. epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), 'binding')
  684. epc.setAttributeNS(None, 'name', self.name)
  685. ns,name = self.type
  686. prefix = epc.getPrefix(ns)
  687. epc.setAttributeNS(None, 'type', '%s:%s' %(prefix,name))
  688. node = epc._getNode()
  689. for ext in self.extensions:
  690. ext.toDom(node)
  691. for op_binding in self.operations:
  692. op_binding.toDom(node)
  693. class OperationBinding(Element):
  694. def __init__(self, name, documentation=''):
  695. Element.__init__(self, name, documentation)
  696. self.input = None
  697. self.output = None
  698. self.faults = Collection(self)
  699. def getWSDL(self):
  700. """Return the WSDL object that contains this binding."""
  701. return self.parent().parent().parent().parent()
  702. def getBinding(self):
  703. """Return the parent Binding object of the operation binding."""
  704. return self.parent().parent()
  705. def getOperation(self):
  706. """Return the abstract Operation associated with this binding."""
  707. return self.getBinding().getPortType().operations[self.name]
  708. def findBinding(self, kind):
  709. for item in self.extensions:
  710. if isinstance(item, kind):
  711. return item
  712. return None
  713. def findBindings(self, kind):
  714. return [ item for item in self.extensions if isinstance(item, kind) ]
  715. def addInputBinding(self, binding):
  716. if self.input is None:
  717. self.input = MessageRoleBinding('input')
  718. self.input.parent = weakref.ref(self)
  719. self.input.addExtension(binding)
  720. return binding
  721. def addOutputBinding(self, binding):
  722. if self.output is None:
  723. self.output = MessageRoleBinding('output')
  724. self.output.parent = weakref.ref(self)
  725. self.output.addExtension(binding)
  726. return binding
  727. def addFaultBinding(self, name, binding):
  728. fault = self.get(name, None)
  729. if fault is None:
  730. fault = MessageRoleBinding('fault', name)
  731. fault.addExtension(binding)
  732. return binding
  733. def load_ex(self, elements):
  734. for e in elements:
  735. ns, name = e.namespaceURI, e.localName
  736. if ns in DOM.NS_SOAP_BINDING_ALL and name == 'operation':
  737. soapaction = DOM.getAttr(e, 'soapAction', default=None)
  738. style = DOM.getAttr(e, 'style', default=None)
  739. ob = SoapOperationBinding(soapaction, style)
  740. self.addExtension(ob)
  741. continue
  742. elif ns in DOM.NS_HTTP_BINDING_ALL and name == 'operation':
  743. location = DOM.getAttr(e, 'location')
  744. ob = HttpOperationBinding(location)
  745. self.addExtension(ob)
  746. continue
  747. else:
  748. self.addExtension(e)
  749. def toDom(self, node):
  750. wsdl = self.getWSDL()
  751. ep = ElementProxy(None, node)
  752. epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), 'operation')
  753. epc.setAttributeNS(None, 'name', self.name)
  754. node = epc._getNode()
  755. for ext in self.extensions:
  756. ext.toDom(node)
  757. if self.input:
  758. self.input.toDom(node)
  759. if self.output:
  760. self.output.toDom(node)
  761. for fault in self.faults:
  762. fault.toDom(node)
  763. class MessageRoleBinding(Element):
  764. def __init__(self, type, name='', documentation=''):
  765. Element.__init__(self, name, documentation)
  766. self.type = type
  767. def getWSDL(self):
  768. """Return the WSDL object that contains this MessageRole."""
  769. if self.type == 'fault':
  770. return self.parent().parent().getWSDL()
  771. return self.parent().getWSDL()
  772. def findBinding(self, kind):
  773. for item in self.extensions:
  774. if isinstance(item, kind):
  775. return item
  776. return None
  777. def findBindings(self, kind):
  778. return [ item for item in self.extensions if isinstance(item, kind) ]
  779. def load_ex(self, elements):
  780. for e in elements:
  781. ns, name = e.namespaceURI, e.localName
  782. if ns in DOM.NS_SOAP_BINDING_ALL and name == 'body':
  783. encstyle = DOM.getAttr(e, 'encodingStyle', default=None)
  784. namespace = DOM.getAttr(e, 'namespace', default=None)
  785. parts = DOM.getAttr(e, 'parts', default=None)
  786. use = DOM.getAttr(e, 'use', default=None)
  787. if use is None:
  788. raise WSDLError(
  789. 'Invalid soap:body binding element.'
  790. )
  791. ob = SoapBodyBinding(use, namespace, encstyle, parts)
  792. self.addExtension(ob)
  793. continue
  794. elif ns in DOM.NS_SOAP_BINDING_ALL and name == 'fault':
  795. encstyle = DOM.getAttr(e, 'encodingStyle', default=None)
  796. namespace = DOM.getAttr(e, 'namespace', default=None)
  797. name = DOM.getAttr(e, 'name', default=None)
  798. use = DOM.getAttr(e, 'use', default=None)
  799. if use is None or name is None:
  800. raise WSDLError(
  801. 'Invalid soap:fault binding element.'
  802. )
  803. ob = SoapFaultBinding(name, use, namespace, encstyle)
  804. self.addExtension(ob)
  805. continue
  806. elif ns in DOM.NS_SOAP_BINDING_ALL and name in (
  807. 'header', 'headerfault'
  808. ):
  809. encstyle = DOM.getAttr(e, 'encodingStyle', default=None)
  810. namespace = DOM.getAttr(e, 'namespace', default=None)
  811. message = DOM.getAttr(e, 'message')
  812. part = DOM.getAttr(e, 'part')
  813. use = DOM.getAttr(e, 'use')
  814. if name == 'header':
  815. _class = SoapHeaderBinding
  816. else:
  817. _class = SoapHeaderFaultBinding
  818. message = ParseQName(message, e)
  819. ob = _class(message, part, use, namespace, encstyle)
  820. self.addExtension(ob)
  821. continue
  822. elif ns in DOM.NS_HTTP_BINDING_ALL and name == 'urlReplacement':
  823. ob = HttpUrlReplacementBinding()
  824. self.addExtension(ob)
  825. continue
  826. elif ns in DOM.NS_HTTP_BINDING_ALL and name == 'urlEncoded':
  827. ob = HttpUrlEncodedBinding()
  828. self.addExtension(ob)
  829. continue
  830. elif ns in DOM.NS_MIME_BINDING_ALL and name == 'multipartRelated':
  831. ob = MimeMultipartRelatedBinding()
  832. self.addExtension(ob)
  833. ob.load_ex(GetExtensions(e))
  834. continue
  835. elif ns in DOM.NS_MIME_BINDING_ALL and name == 'content':
  836. part = DOM.getAttr(e, 'part', default=None)
  837. type = DOM.getAttr(e, 'type', default=None)
  838. ob = MimeContentBinding(part, type)
  839. self.addExtension(ob)
  840. continue
  841. elif ns in DOM.NS_MIME_BINDING_ALL and name == 'mimeXml':
  842. part = DOM.getAttr(e, 'part', default=None)
  843. ob = MimeXmlBinding(part)
  844. self.addExtension(ob)
  845. continue
  846. else:
  847. self.addExtension(e)
  848. def toDom(self, node):
  849. wsdl = self.getWSDL()
  850. ep = ElementProxy(None, node)
  851. epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), self.type)
  852. node = epc._getNode()
  853. for item in self.extensions:
  854. if item: item.toDom(node)
  855. class Service(Element):
  856. def __init__(self, name, documentation=''):
  857. Element.__init__(self, name, documentation)
  858. self.ports = Collection(self)
  859. def getWSDL(self):
  860. return self.parent().parent()
  861. def addPort(self, name, binding, documentation=''):
  862. item = Port(name, binding, documentation)
  863. self.ports[name] = item
  864. return item
  865. def load(self, elements):
  866. for element in elements:
  867. name = DOM.getAttr(element, 'name', default=None)
  868. docs = GetDocumentation(element)
  869. binding = DOM.getAttr(element, 'binding', default=None)
  870. if name is None or binding is None:
  871. raise WSDLError(
  872. 'Invalid port element.'
  873. )
  874. binding = ParseQName(binding, element)
  875. port = self.addPort(name, binding, docs)
  876. port.load_ex(GetExtensions(element))
  877. def load_ex(self, elements):
  878. for e in elements:
  879. self.addExtension(e)
  880. def toDom(self):
  881. wsdl = self.getWSDL()
  882. ep = ElementProxy(None, DOM.getElement(wsdl.document, None))
  883. epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), "service")
  884. epc.setAttributeNS(None, "name", self.name)
  885. node = epc._getNode()
  886. for port in self.ports:
  887. port.toDom(node)
  888. class Port(Element):
  889. def __init__(self, name, binding, documentation=''):
  890. Element.__init__(self, name, documentation)
  891. self.binding = binding
  892. def getWSDL(self):
  893. return self.parent().parent().getWSDL()
  894. def getService(self):
  895. """Return the Service object associated with this port."""
  896. return self.parent().parent()
  897. def getBinding(self):
  898. """Return the Binding object that is referenced by this port."""
  899. wsdl = self.getService().getWSDL()
  900. return wsdl.bindings[self.binding]
  901. def getPortType(self):
  902. """Return the PortType object that is referenced by this port."""
  903. wsdl = self.getService().getWSDL()
  904. binding = wsdl.bindings[self.binding]
  905. return wsdl.portTypes[binding.type]
  906. def getAddressBinding(self):
  907. """A convenience method to obtain the extension element used
  908. as the address binding for the port."""
  909. for item in self.extensions:
  910. if isinstance(item, SoapAddressBinding) or \
  911. isinstance(item, HttpAddressBinding):
  912. return item
  913. raise WSDLError(
  914. 'No address binding found in port.'
  915. )
  916. def load_ex(self, elements):
  917. for e in elements:
  918. ns, name = e.namespaceURI, e.localName
  919. if ns in DOM.NS_SOAP_BINDING_ALL and name == 'address':
  920. location = DOM.getAttr(e, 'location', default=None)
  921. ob = SoapAddressBinding(location)
  922. self.addExtension(ob)
  923. continue
  924. elif ns in DOM.NS_HTTP_BINDING_ALL and name == 'address':
  925. location = DOM.getAttr(e, 'location', default=None)
  926. ob = HttpAddressBinding(location)
  927. self.addExtension(ob)
  928. continue
  929. else:
  930. self.addExtension(e)
  931. def toDom(self, node):
  932. wsdl = self.getWSDL()
  933. ep = ElementProxy(None, node)
  934. epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), "port")
  935. epc.setAttributeNS(None, "name", self.name)
  936. ns,name = self.binding
  937. prefix = epc.getPrefix(ns)
  938. epc.setAttributeNS(None, "binding", "%s:%s" %(prefix,name))
  939. node = epc._getNode()
  940. for ext in self.extensions:
  941. ext.toDom(node)
  942. class SoapBinding:
  943. def __init__(self, transport, style='rpc'):
  944. self.transport = transport
  945. self.style = style
  946. def getWSDL(self):
  947. return self.parent().getWSDL()
  948. def toDom(self, node):
  949. wsdl = self.getWSDL()
  950. ep = ElementProxy(None, node)
  951. epc = ep.createAppendElement(DOM.GetWSDLSoapBindingUri(wsdl.version), 'binding')
  952. if self.transport:
  953. epc.setAttributeNS(None, "transport", self.transport)
  954. if self.style:
  955. epc.setAttributeNS(None, "style", self.style)
  956. class SoapAddressBinding:
  957. def __init__(self, location):
  958. self.location = location
  959. def getWSDL(self):
  960. return self.parent().getWSDL()
  961. def toDom(self, node):
  962. wsdl = self.getWSDL()
  963. ep = ElementProxy(None, node)
  964. epc = ep.createAppendElement(DOM.GetWSDLSoapBindingUri(wsdl.version), 'address')
  965. epc.setAttributeNS(None, "location", self.location)
  966. class SoapOperationBinding:
  967. def __init__(self, soapAction=None, style=None):
  968. self.soapAction = soapAction
  969. self.style = style
  970. def getWSDL(self):
  971. return self.parent().getWSDL()
  972. def toDom(self, node):
  973. wsdl = self.getWSDL()
  974. ep = ElementProxy(None, node)
  975. epc = ep.createAppendElement(DOM.GetWSDLSoapBindingUri(wsdl.version), 'operation')
  976. if self.soapAction:
  977. epc.setAttributeNS(None, 'soapAction', self.soapAction)
  978. if self.style:
  979. epc.setAttributeNS(None, 'style', self.style)
  980. class SoapBodyBinding:
  981. def __init__(self, use, namespace=None, encodingStyle=None, parts=None):
  982. if not use in ('literal', 'encoded'):
  983. raise WSDLError(
  984. 'Invalid use attribute value: %s' % use
  985. )
  986. self.encodingStyle = encodingStyle
  987. self.namespace = namespace
  988. if type(parts) in (type(''), type(u'')):
  989. parts = parts.split()
  990. self.parts = parts
  991. self.use = use
  992. def getWSDL(self):
  993. return self.parent().getWSDL()
  994. def toDom(self, node):
  995. wsdl = self.getWSDL()
  996. ep = ElementProxy(None, node)
  997. epc = ep.createAppendElement(DOM.GetWSDLSoapBindingUri(wsdl.version), 'body')
  998. epc.setAttributeNS(None, "use", self.use)
  999. epc.setAttributeNS(None, "namespace", self.namespace)
  1000. class SoapFaultBinding:
  1001. def __init__(self, name, use, namespace=None, encodingStyle=None):
  1002. if not use in ('literal', 'encoded'):
  1003. raise WSDLError(
  1004. 'Invalid use attribute value: %s' % use
  1005. )
  1006. self.encodingStyle = encodingStyle
  1007. self.namespace = namespace
  1008. self.name = name
  1009. self.use = use
  1010. class SoapHeaderBinding:
  1011. def __init__(self, message, part, use, namespace=None, encodingStyle=None):
  1012. if not use in ('literal', 'encoded'):
  1013. raise WSDLError(
  1014. 'Invalid use attribute value: %s' % use
  1015. )
  1016. self.encodingStyle = encodingStyle
  1017. self.namespace = namespace
  1018. self.message = message
  1019. self.part = part
  1020. self.use = use
  1021. tagname = 'header'
  1022. class SoapHeaderFaultBinding(SoapHeaderBinding):
  1023. tagname = 'headerfault'
  1024. class HttpBinding:
  1025. def __init__(self, verb):
  1026. self.verb = verb
  1027. class HttpAddressBinding:
  1028. def __init__(self, location):
  1029. self.location = location
  1030. class HttpOperationBinding:
  1031. def __init__(self, location):
  1032. self.location = location
  1033. class HttpUrlReplacementBinding:
  1034. pass
  1035. class HttpUrlEncodedBinding:
  1036. pass
  1037. class MimeContentBinding:
  1038. def __init__(self, part=None, type=None):
  1039. self.part = part
  1040. self.type = type
  1041. class MimeXmlBinding:
  1042. def __init__(self, part=None):
  1043. self.part = part
  1044. class MimeMultipartRelatedBinding:
  1045. def __init__(self):
  1046. self.parts = []
  1047. def load_ex(self, elements):
  1048. for e in elements:
  1049. ns, name = e.namespaceURI, e.localName
  1050. if ns in DOM.NS_MIME_BINDING_ALL and name == 'part':
  1051. self.parts.append(MimePartBinding())
  1052. continue
  1053. class MimePartBinding:
  1054. def __init__(self):
  1055. self.items = []
  1056. def load_ex(self, elements):
  1057. for e in elements:
  1058. ns, name = e.namespaceURI, e.localName
  1059. if ns in DOM.NS_MIME_BINDING_ALL and name == 'content':
  1060. part = DOM.getAttr(e, 'part', default=None)
  1061. type = DOM.getAttr(e, 'type', default=None)
  1062. ob = MimeContentBinding(part, type)
  1063. self.items.append(ob)
  1064. continue
  1065. elif ns in DOM.NS_MIME_BINDING_ALL and name == 'mimeXml':
  1066. part = DOM.getAttr(e, 'part', default=None)
  1067. ob = MimeXmlBinding(part)
  1068. self.items.append(ob)
  1069. continue
  1070. elif ns in DOM.NS_SOAP_BINDING_ALL and name == 'body':
  1071. encstyle = DOM.getAttr(e, 'encodingStyle', default=None)
  1072. namespace = DOM.getAttr(e, 'namespace', default=None)
  1073. parts = DOM.getAttr(e, 'parts', default=None)
  1074. use = DOM.getAttr(e, 'use', default=None)
  1075. if use is None:
  1076. raise WSDLError(
  1077. 'Invalid soap:body binding element.'
  1078. )
  1079. ob = SoapBodyBinding(use, namespace, encstyle, parts)
  1080. self.items.append(ob)
  1081. continue
  1082. class WSDLError(Exception):
  1083. pass
  1084. def DeclareNSPrefix(writer, prefix, nsuri):
  1085. if writer.hasNSPrefix(nsuri):
  1086. return
  1087. writer.declareNSPrefix(prefix, nsuri)
  1088. def ParseTypeRef(value, element):
  1089. parts = value.split(':', 1)
  1090. if len(parts) == 1:
  1091. return (DOM.findTargetNS(element), value)
  1092. nsuri = DOM.findNamespaceURI(parts[0], element)
  1093. return (nsuri, parts[1])
  1094. def ParseQName(value, element):
  1095. nameref = value.split(':', 1)
  1096. if len(nameref) == 2:
  1097. nsuri = DOM.findNamespaceURI(nameref[0], element)
  1098. name = nameref[-1]
  1099. else:
  1100. nsuri = DOM.findTargetNS(element)
  1101. name = nameref[-1]
  1102. return nsuri, name
  1103. def GetDocumentation(element):
  1104. docnode = DOM.getElement(element, 'documentation', None, None)
  1105. if docnode is not None:
  1106. return DOM.getElementText(docnode)
  1107. return ''
  1108. def GetExtensions(element):
  1109. return [ item for item in DOM.getElements(element, None, None)
  1110. if item.namespaceURI != DOM.NS_WSDL ]
  1111. def GetWSAActionFault(operation, name):
  1112. """Find wsa:Action attribute, and return value or WSA.FAULT
  1113. for the default.
  1114. """
  1115. attr = operation.faults[name].action
  1116. if attr is not None:
  1117. return attr
  1118. return WSA.FAULT
  1119. def GetWSAActionInput(operation):
  1120. """Find wsa:Action attribute, and return value or the default."""
  1121. attr = operation.input.action
  1122. if attr is not None:
  1123. return attr
  1124. portType = operation.getPortType()
  1125. targetNamespace = portType.getTargetNamespace()
  1126. ptName = portType.name
  1127. msgName = operation.input.name
  1128. if not msgName:
  1129. msgName = operation.name + 'Request'
  1130. if targetNamespace.endswith('/'):
  1131. return '%s%s/%s' %(targetNamespace, ptName, msgName)
  1132. return '%s/%s/%s' %(targetNamespace, ptName, msgName)
  1133. def GetWSAActionOutput(operation):
  1134. """Find wsa:Action attribute, and return value or the default."""
  1135. attr = operation.output.action
  1136. if attr is not None:
  1137. return attr
  1138. targetNamespace = operation.getPortType().getTargetNamespace()
  1139. ptName = operation.getPortType().name
  1140. msgName = operation.output.name
  1141. if not msgName:
  1142. msgName = operation.name + 'Response'
  1143. if targetNamespace.endswith('/'):
  1144. return '%s%s/%s' %(targetNamespace, ptName, msgName)
  1145. return '%s/%s/%s' %(targetNamespace, ptName, msgName)
  1146. def FindExtensions(object, kind, t_type=type(())):
  1147. if isinstance(kind, t_type):
  1148. result = []
  1149. namespaceURI, name = kind
  1150. return [ item for item in object.extensions
  1151. if hasattr(item, 'nodeType') \
  1152. and DOM.nsUriMatch(namespaceURI, item.namespaceURI) \
  1153. and item.name == name ]
  1154. return [ item for item in object.extensions if isinstance(item, kind) ]
  1155. def FindExtension(object, kind, t_type=type(())):
  1156. if isinstance(kind, t_type):
  1157. namespaceURI, name = kind
  1158. for item in object.extensions:
  1159. if hasattr(item, 'nodeType') \
  1160. and DOM.nsUriMatch(namespaceURI, item.namespaceURI) \
  1161. and item.name == name:
  1162. return item
  1163. else:
  1164. for item in object.extensions:
  1165. if isinstance(item, kind):
  1166. return item
  1167. return None
  1168. class SOAPCallInfo:
  1169. """SOAPCallInfo captures the important binding information about a
  1170. SOAP operation, in a structure that is easier to work with than
  1171. raw WSDL structures."""
  1172. def __init__(self, methodName):
  1173. self.methodName = methodName
  1174. self.inheaders = []
  1175. self.outheaders = []
  1176. self.inparams = []
  1177. self.outparams = []
  1178. self.retval = None
  1179. encodingStyle = DOM.NS_SOAP_ENC
  1180. documentation = ''
  1181. soapAction = None
  1182. transport = None
  1183. namespace = None
  1184. location = None
  1185. use = 'encoded'
  1186. style = 'rpc'
  1187. def addInParameter(self, name, type, namespace=None, element_type=0):
  1188. """Add an input parameter description to the call info."""
  1189. parameter = ParameterInfo(name, type, namespace, element_type)
  1190. self.inparams.append(parameter)
  1191. return parameter
  1192. def addOutParameter(self, name, type, namespace=None, element_type=0):
  1193. """Add an output parameter description to the call info."""
  1194. parameter = ParameterInfo(name, type, namespace, element_type)
  1195. self.outparams.append(parameter)
  1196. return parameter
  1197. def setReturnParameter(self, name, type, namespace=None, element_type=0):
  1198. """Set the return parameter description for the call info."""
  1199. parameter = ParameterInfo(name, type, namespace, element_type)
  1200. self.retval = parameter
  1201. return parameter
  1202. def addInHeaderInfo(self, name, type, namespace, element_type=0,
  1203. mustUnderstand=0):
  1204. """Add an input SOAP header description to the call info."""
  1205. headerinfo = HeaderInfo(name, type, namespace, element_type)
  1206. if mustUnderstand:
  1207. headerinfo.mustUnderstand = 1
  1208. self.inheaders.append(headerinfo)
  1209. return headerinfo
  1210. def addOutHeaderInfo(self, name, type, namespace, element_type=0,
  1211. mustUnderstand=0):
  1212. """Add an output SOAP header description to the call info."""
  1213. headerinfo = HeaderInfo(name, type, namespace, element_type)
  1214. if mustUnderstand:
  1215. headerinfo.mustUnderstand = 1
  1216. self.outheaders.append(headerinfo)
  1217. return headerinfo
  1218. def getInParameters(self):
  1219. """Return a sequence of the in parameters of the method."""
  1220. return self.inparams
  1221. def getOutParameters(self):
  1222. """Return a sequence of the out parameters of the method."""
  1223. return self.outparams
  1224. def getReturnParameter(self):
  1225. """Return param info about the return value of the method."""
  1226. return self.retval
  1227. def getInHeaders(self):
  1228. """Return a sequence of the in headers of the method."""
  1229. return self.inheaders
  1230. def getOutHeaders(self):
  1231. """Return a sequence of the out headers of the method."""
  1232. return self.outheaders
  1233. class ParameterInfo:
  1234. """A ParameterInfo object captures parameter binding information."""
  1235. def __init__(self, name, type, namespace=None, element_type=0):
  1236. if element_type:
  1237. self.element_type = 1
  1238. if namespace is not None:
  1239. self.namespace = namespace
  1240. self.name = name
  1241. self.type = type
  1242. element_type = 0
  1243. namespace = None
  1244. default = None
  1245. class HeaderInfo(ParameterInfo):
  1246. """A HeaderInfo object captures SOAP header binding information."""
  1247. def __init__(self, name, type, namespace, element_type=None):
  1248. ParameterInfo.__init__(self, name, type, namespace, element_type)
  1249. mustUnderstand = 0
  1250. actor = None
  1251. def callInfoFromWSDL(port, name):
  1252. """Return a SOAPCallInfo given a WSDL port and operation name."""
  1253. wsdl = port.getService().getWSDL()
  1254. binding = port.getBinding()
  1255. portType = binding.getPortType()
  1256. operation = portType.operations[name]
  1257. opbinding = binding.operations[name]
  1258. messages = wsdl.messages
  1259. callinfo = SOAPCallInfo(name)
  1260. addrbinding = port.getAddressBinding()
  1261. if not isinstance(addrbinding, SoapAddressBinding):
  1262. raise ValueError, 'Unsupported binding type.'
  1263. callinfo.location = addrbinding.location
  1264. soapbinding = binding.findBinding(SoapBinding)
  1265. if soapbinding is None:
  1266. raise ValueError, 'Missing soap:binding element.'
  1267. callinfo.transport = soapbinding.transport
  1268. callinfo.style = soapbinding.style or 'document'
  1269. soap_op_binding = opbinding.findBinding(SoapOperationBinding)
  1270. if soap_op_binding is not None:
  1271. callinfo.soapAction = soap_op_binding.soapAction
  1272. callinfo.style = soap_op_binding.style or callinfo.style
  1273. parameterOrder = operation.parameterOrder
  1274. if operation.input is not None:
  1275. message = messages[operation.input.message]
  1276. msgrole = opbinding.input
  1277. mime = msgrole.findBinding(MimeMultipartRelatedBinding)
  1278. if mime is not None:
  1279. raise ValueError, 'Mime bindings are not supported.'
  1280. else:
  1281. for item in msgrole.findBindings(SoapHeaderBinding):
  1282. part = messages[item.message].parts[item.part]
  1283. header = callinfo.addInHeaderInfo(
  1284. part.name,
  1285. part.element or part.type,
  1286. item.namespace,
  1287. element_type = part.element and 1 or 0
  1288. )
  1289. header.encodingStyle = item.encodingStyle
  1290. body = msgrole.findBinding(SoapBodyBinding)
  1291. if body is None:
  1292. raise ValueError, 'Missing soap:body binding.'
  1293. callinfo.encodingStyle = body.encodingStyle
  1294. callinfo.namespace = body.namespace
  1295. callinfo.use = body.use
  1296. if body.parts is not None:
  1297. parts = []
  1298. for name in body.parts:
  1299. parts.append(message.parts[name])
  1300. else:
  1301. parts = message.parts.values()
  1302. for part in parts:
  1303. callinfo.addInParameter(
  1304. part.name,
  1305. part.element or part.type,
  1306. element_type = part.element and 1 or 0
  1307. )
  1308. if operation.output is not None:
  1309. try:
  1310. message = messages[operation.output.message]
  1311. except KeyError:
  1312. if self.strict:
  1313. raise RuntimeError(
  1314. "Recieved message not defined in the WSDL schema: %s" %
  1315. operation.output.message)
  1316. else:
  1317. message = wsdl.addMessage(operation.output.message)
  1318. print "Warning:", \
  1319. "Recieved message not defined in the WSDL schema.", \
  1320. "Adding it."
  1321. print "Message:", operation.output.message
  1322. msgrole = opbinding.output
  1323. mime = msgrole.findBinding(MimeMultipartRelatedBinding)
  1324. if mime is not None:
  1325. raise ValueError, 'Mime bindings are not supported.'
  1326. else:
  1327. for item in msgrole.findBindings(SoapHeaderBinding):
  1328. part = messages[item.message].parts[item.part]
  1329. header = callinfo.addOutHeaderInfo(
  1330. part.name,
  1331. part.element or part.type,
  1332. item.namespace,
  1333. element_type = part.element and 1 or 0
  1334. )
  1335. header.encodingStyle = item.encodingStyle
  1336. body = msgrole.findBinding(SoapBodyBinding)
  1337. if body is None:
  1338. raise ValueError, 'Missing soap:body binding.'
  1339. callinfo.encodingStyle = body.encodingStyle
  1340. callinfo.namespace = body.namespace
  1341. callinfo.use = body.use
  1342. if body.parts is not None:
  1343. parts = []
  1344. for name in body.parts:
  1345. parts.append(message.parts[name])
  1346. else:
  1347. parts = message.parts.values()
  1348. if parts:
  1349. for part in parts:
  1350. callinfo.addOutParameter(
  1351. part.name,
  1352. part.element or part.type,
  1353. element_type = part.element and 1 or 0
  1354. )
  1355. return callinfo