def add_to_schema(cls, schemaDict, nsmap): if not cls.get_datatype(nsmap) in schemaDict: for k, v in cls.soap_members.items(): v.add_to_schema(schemaDict, nsmap) schema_node = create_xml_element( nsmap.get("xs") + "complexType", nsmap) schema_node.set('name',cls.get_datatype()) sequence_node = create_xml_subelement( schema_node, nsmap.get('xs') + 'sequence') for k, v in cls.soap_members.items(): member_node = create_xml_subelement( sequence_node, nsmap.get('xs') + 'element') member_node.set('name', k) member_node.set('minOccurs', '0') member_node.set('type', "%s:%s" % (v.get_namespace_id(), v.get_datatype())) typeElement = create_xml_element( nsmap.get('xs') + 'element', nsmap) typeElement.set('name',cls.get_datatype()) typeElement.set('type', "%s:%s" % (cls.get_namespace_id(),cls.get_datatype())) schemaDict[cls.get_datatype(nsmap)+'Complex'] = schema_node schemaDict[cls.get_datatype(nsmap)] = typeElement
def add_to_schema(self, schema_dict, nsmap): typ = self.get_datatype() self.serializer.add_to_schema(schema_dict, nsmap) if not typ in schema_dict: complexTypeNode = create_xml_element( nsmap.get('xs') + 'complexType', nsmap) complexTypeNode.set('name', self.get_datatype()) sequenceNode = create_xml_subelement( complexTypeNode, nsmap.get('xs') + 'sequence') elementNode = create_xml_subelement( sequenceNode, nsmap.get('xs') + 'element') elementNode.set('minOccurs', '0') elementNode.set('maxOccurs', 'unbounded') elementNode.set('type', "%s:%s" % (self.serializer.get_namespace_id(), self.serializer.get_datatype())) elementNode.set('name', self.item_name) #changed to item_name typeElement = create_xml_element( nsmap.get('xs') + 'element', nsmap) typeElement.set('name', typ) typeElement.set('type', "%s:%s" % (self.namespace_id, self.get_datatype())) schema_dict['%sElement' % (self.get_datatype(nsmap))] = typeElement schema_dict[self.get_datatype(nsmap)] = complexTypeNode
def _add_messages_for_methods(self, root, methods, nsmap): ''' A private method for adding message elements to the wsdl @param the the root element of the wsdl @param the list of methods. ''' messages = [] #make messages for method in methods: methodName = method.name # making in part inMessage = create_xml_element('message', nsmap) inMessage.set('name', method.inMessage.typ) inPart = create_xml_subelement(inMessage, 'part') inPart.set('name', method.inMessage.name) inPart.set('element', 'tns:' + method.inMessage.typ) messages.append(inMessage) # making out part only if necessary if len(method.outMessage.params) > 0: outMessage = create_xml_element('message', nsmap) outMessage.set('name', method.outMessage.typ) outPart = create_xml_subelement(outMessage, 'part') outPart.set('name', method.outMessage.name) outPart.set('element', 'tns:' + method.outMessage.typ) messages.append(outMessage) for message in messages: root.append(message)
def make_soap_envelope(message, tns='', header_elements=None): ''' This method takes the results from a soap method call, and wraps them in the appropriate soap envelope with any specified headers @param the message of the soap envelope, either an element or a list of elements @param any header elements to be included in the soap response @returns the envelope element ''' nsmap = NamespaceLookup(tns) envelope = create_xml_element(nsmap.get('SOAP-ENV') + 'Envelope', nsmap, tns) if header_elements: headerElement = create_xml_subelement(envelope, nsmap.get('SOAP-ENV') + 'Header') for h in header_elements: headerElement.append(h) body = create_xml_subelement(envelope, nsmap.get('SOAP-ENV') + 'Body') if type(message) == list: for m in message: body.append(m) elif message != None: body.append(message) return envelope
def add_to_schema(cls, schema_dict, nsmap): complexTypeNode = create_xml_element('complexType', nsmap) complexTypeNode.set('name', cls.get_datatype()) sequenceNode = create_xml_subelement(complexTypeNode, 'sequence') faultTypeElem = create_xml_subelement(sequenceNode, 'element') faultTypeElem.set('name', 'detail') faultTypeElem.set(nsmap.get('xsi') + 'type', 'xs:string') faultTypeElem = create_xml_subelement(sequenceNode, 'element') faultTypeElem.set('name', 'message') faultTypeElem.set(nsmap.get('xsi') + 'type', 'xs:string') schema_dict[cls.get_datatype()] = complexTypeNode typeElementItem = create_xml_element('element', nsmap) typeElementItem.set('name', 'ExceptionFaultType') typeElementItem.set(nsmap.get('xsi') + 'type', cls.get_datatype(nsmap)) schema_dict['%sElement' % (cls.get_datatype(nsmap))] = typeElementItem
def _generic_to_xml(value, name, cls, nsmap): retval = create_xml_element(name, nsmap) if value: retval.text = value #retval.set( # nsmap.get('xsi') + 'type', # "%s:%s" % (cls.get_namespace_id(), cls.get_datatype())) return retval
def _unicode_to_xml(value, name, cls, nsmap): retval = create_xml_element(name, nsmap) if value == None: return Null.to_xml(value, name, nsmap) if type(value) == unicode: retval.text = value else: retval.text = unicode(value, string_encoding) #FIXME #retval.set( # nsmap.get('xsi') + 'type', # "%s:%s" % (cls.get_namespace_id(), cls.get_datatype())) return retval
def add_to_schema(self, schemaDict, nsmap): complexType = create_xml_element(nsmap.get('xs') + 'complexType', nsmap) complexType.set('name', self.typ) sequence = create_xml_subelement(complexType, nsmap.get('xs') + 'sequence') if self.params: for name, serializer in self.params: e = create_xml_subelement(sequence, nsmap.get('xs') + 'element') e.set('name', name) e.set('type', "%s:%s" % (serializer.get_namespace_id(), serializer.get_datatype())) element = create_xml_element(nsmap.get('xs') + 'element', nsmap) element.set('name', self.typ) element.set('type', '%s:%s' % ('tns', self.typ)) schemaDict[self.typ] = complexType schemaDict[self.typ + 'Element'] = element
def to_xml(self, values, name='retval', nsmap=ns): res = create_xml_element(name, nsmap) typ = self.get_datatype(nsmap) if values == None: values = [] #res.set('type', # "%s:%s" % (self.get_namespace_id(), self.get_datatype())) for value in values: serializer = self.serializer if value == None: serializer = Null res.append( serializer.to_xml(value, self.item_name, nsmap)) #serializer.to_xml(value, serializer.get_datatype(), nsmap)) return res
def to_xml(cls, value, name='retval', nsmap=ns): element = create_xml_element( nsmap.get(cls.get_namespace_id()) + name, nsmap) # Because namespaces are not getting output, explicitly set xmlns as an # attribute. Otherwise .NET will reject the message. if None not in nsmap.nsmap and cls.get_namespace_id() in nsmap.nsmap: xmlns = nsmap.nsmap[cls.get_namespace_id()] element.set('xmlns', xmlns) for k, v in cls.soap_members.items(): subvalue = getattr(value, k, None) subelements = v.to_xml(subvalue, name=k, nsmap=nsmap) if type(subelements) != list: subelements = [subelements] for s in subelements: element.append(s) return element
def make_soap_fault(faultString, faultCode = 'Server', detail = None, header_elements = None): ''' This method populates a soap fault message with the provided fault string and details. @param faultString the short description of the error @param detail the details of the exception, such as a stack trace @param faultCode defaults to 'Server', but can be overridden @param header_elements A list of XML elements to add to the fault header. @returns the element corresponding to the fault message ''' nsmap = NamespaceLookup() envelope = create_xml_element(nsmap.get('SOAP-ENV') + 'Envelope', nsmap) if header_elements: header = create_xml_subelement( envelope, nsmap.get('SOAP-ENV') + 'Header') for element in header_elements: header.append(element) body = create_xml_subelement(envelope, nsmap.get('SOAP-ENV') + 'Body') f = Fault(faultCode, faultString, detail) body.append(Fault.to_xml(f, nsmap.get('SOAP-ENV') + "Fault", nsmap)) return envelope
def to_xml(cls, value, name='retval', nsmap=ns): '''This class method takes the data from the attachment and base64 encodes it as the text of an Element. An attachment can specify a filename and if no data is given, it will read the data from the file ''' if (value.__class__ not in [Attachment, str]): raise Exception("Do not know how to serialize class %s" % type(value)) element = create_xml_element(name, nsmap) if value.__class__ == str: element.text = base64.encodestring(value) elif value.encoded_data is not None: element.text = value.encoded_data elif value.data is not None: # the data has already been loaded, just encode # and return the element element.text = base64.encodestring(value.data) elif value.fileName: # the data hasn't been loaded, but a file has been # specified data_string = cStringIO.StringIO() fileName = value.fileName file = open(fileName, 'rb') base64.encode(file, data_string) file.close() # go back to the begining of the data data_string.seek(0) element.text = str(data_string.read()) else: raise Exception("Neither data nor a filename has been specified") return element
def to_xml(self, *data): if len(self.params): if len(data) != len(self.params): raise Exception("Parameter number mismatch expected [%s] " "got [%s] for response %s"%(len(self.params), len(data), self.name)) nsmap = NamespaceLookup(self.ns) element = create_xml_element(self.name, nsmap, self.ns) for i in range(0, len(self.params)): name, serializer = self.params[i] d = data[i] e = serializer.to_xml(d, name, nsmap) if type(e) in (list, tuple): elist = e for e in elist: element.append(e) elif e == None: pass else: element.append(e) ElementTree.cleanup_namespaces(element) return element
def wsdl(self, url): ''' This method generates and caches the wsdl for this object based on the soap methods designated by the soapmethod or soapdocument descriptors @param url the url that this service can be found at. This must be passed in by the caller because this object has no notion of the server environment in which it runs. @returns the string of the wsdl ''' if not self.__wsdl__ == None: # return the cached __wsdl__ return self.__wsdl__ url = url.replace('.wsdl', '') # otherwise build it serviceName = self.__class__.__name__.split('.')[-1] tns = self.__tns__ methods = self.methods() hasCallbacks = self._hasCallbacks() nsmap = NamespaceLookup(tns, True) if hasCallbacks: nsmap.set('wsa', 'http://schemas.xmlsoap.org/ws/2003/03/addressing') root = create_xml_element( "definitions", nsmap, 'http://schemas.xmlsoap.org/wsdl/') root.set('targetNamespace', tns) root.set('name', serviceName) types = create_xml_subelement(root, "types") self._add_schema(types, methods, nsmap) self._add_messages_for_methods(root, methods, nsmap) # add necessary async headers # WS-Addressing -> RelatesTo ReplyTo MessageID # callback porttype if hasCallbacks: wsaSchemaNode = create_xml_subelement(types, "schema") wsaSchemaNode.set("targetNamespace", tns+'Callback') wsaSchemaNode.set("xmlns", "http://www.w3.org/2001/XMLSchema") wsaSchemaNode.set("elementFormDefault", "qualified") importNode = create_xml_subelement(wsaSchemaNode, "import") importNode.set("namespace", "http://schemas.xmlsoap.org/ws/2003/03/addressing") importNode.set("schemaLocation", "http://schemas.xmlsoap.org/ws/2003/03/addressing/") reltMessage = create_xml_subelement(root, 'message') reltMessage.set('name', 'RelatesToHeader') reltPart = create_xml_subelement(reltMessage, 'part') reltPart.set('name', 'RelatesTo') reltPart.set('element', 'wsa:RelatesTo') replyMessage = create_xml_subelement(root, 'message') replyMessage.set('name', 'ReplyToHeader') replyPart = create_xml_subelement(replyMessage, 'part') replyPart.set('name', 'ReplyTo') replyPart.set('element', 'wsa:ReplyTo') idHeader = create_xml_subelement(root, 'message') idHeader.set('name', 'MessageIDHeader') idPart = create_xml_subelement(idHeader, 'part') idPart.set('name', 'MessageID') idPart.set('element', 'wsa:MessageID') # make portTypes callbackPortType = create_xml_subelement(root, 'portType') callbackPortType.set('name', '%sCallback' % serviceName) cbServiceName = '%sCallback' % serviceName cbService = create_xml_subelement(root, 'service') cbService.set('name', cbServiceName) cbWsdlPort = create_xml_subelement(cbService, 'port') cbWsdlPort.set('name', cbServiceName) cbWsdlPort.set('binding', 'tns:%s' % cbServiceName) cbAddr = create_xml_subelement(cbWsdlPort, nsmap.get('soap') + 'address') cbAddr.set('location', url) serviceName = self.__class__.__name__.split('.')[-1] portType = create_xml_subelement(root, 'portType') portType.set('name', serviceName) for method in methods: if method.isCallback: operation = create_xml_subelement(callbackPortType, 'operation') else: operation = create_xml_subelement(portType, 'operation') operation.set('name', method.name) params = [] for name, param in method.inMessage.params: params.append(name) documentation = create_xml_subelement(operation, 'documentation') documentation.text = method.doc operation.set('parameterOrder', method.inMessage.typ) opInput = create_xml_subelement(operation, 'input') opInput.set('name', method.inMessage.typ) opInput.set('message', 'tns:%s' % method.inMessage.typ) if (len(method.outMessage.params) > 0 and not method.isCallback and not method.isAsync): opOutput = create_xml_subelement(operation, 'output') opOutput.set('name', method.outMessage.typ) opOutput.set('message', 'tns:%s' % method.outMessage.typ) # make partner link plink = create_xml_subelement(root, nsmap.get('plnk') + 'partnerLinkType') plink.set('name', serviceName) role = create_xml_subelement(plink, nsmap.get('plnk') + 'role') role.set('name', serviceName) plinkPortType = create_xml_subelement(role, nsmap.get('plnk') + 'portType') plinkPortType.set('name', 'tns:%s' % serviceName) if hasCallbacks: role = create_xml_subelement(plink, nsmap.get('plnk') + 'role') role.set('name', '%sCallback' % serviceName) plinkPortType = create_xml_subelement(role, nsmap.get('plnk') + 'portType') plinkPortType.set('name', 'tns:%sCallback' % serviceName) self._add_bindings_for_methods(root, serviceName, methods, nsmap) service = create_xml_subelement(root, 'service') service.set('name', serviceName) wsdlPort = create_xml_subelement(service, 'port') wsdlPort.set('name', serviceName) wsdlPort.set('binding', 'tns:%s' % serviceName) addr = create_xml_subelement(wsdlPort, nsmap.get('soap') + 'address') addr.set('location', url) wsdl = ElementTree.tostring(root) wsdl = "<?xml version='1.0' encoding='utf-8' ?>%s" % (wsdl) #cache the wsdl for next time self.__wsdl__ = wsdl return self.__wsdl__
def to_xml(cls, value, name='retval', nsmap=ns): element = create_xml_element(name, nsmap) element.set(cls.get_datatype(nsmap), '1') return element
def to_xml(cls, value, name, nsmap=ns): fault = create_xml_element(name, nsmap) create_xml_subelement(fault, 'faultcode').text = value.faultcode create_xml_subelement(fault, 'faultstring').text = value.faultstring detail = create_xml_subelement(fault, 'detail').text = value.detail return fault
def _add_bindings_for_methods(self, root, serviceName, methods, nsmap): ''' A private method for adding bindings to the wsdld @param the root element of the wsdl @param the name of this service @param the methods to be add to the binding node ''' hasCallbacks = self._hasCallbacks() # make binding binding = create_xml_subelement(root, 'binding') binding.set('name', serviceName) binding.set('type', 'tns:%s'%serviceName) sbinding = create_xml_subelement(binding, nsmap.get('soap') + 'binding') sbinding.set('style', 'document') sbinding.set('transport', 'http://schemas.xmlsoap.org/soap/http') if hasCallbacks: callbackBinding = create_xml_subelement(root, 'binding') callbackBinding.set('name', '%sCallback' % serviceName) callbackBinding.set('type', 'typens:%sCallback' % serviceName) sbinding = create_xml_subelement(callbackBinding, nsmap.get('soap') + 'binding') sbinding.set('transport', 'http://schemas.xmlsoap.org/soap/http') for method in methods: operation = create_xml_element('operation', nsmap) operation.set('name', method.name) soapOperation = create_xml_subelement(operation, nsmap.get('soap') + 'operation') soapOperation.set('soapAction', method.soapAction) soapOperation.set('style', 'document') input = create_xml_subelement(operation, 'input') input.set('name', method.inMessage.typ) soapBody = create_xml_subelement(input, nsmap.get('soap') + 'body') soapBody.set('use', 'literal') if (len(method.outMessage.params) > 0 and not method.isAsync and not method.isCallback): output = create_xml_subelement(operation, 'output') output.set('name', method.outMessage.typ) soapBody = create_xml_subelement(output, nsmap.get('soap') + 'body') soapBody.set('use', 'literal') if method.isCallback: relatesTo = create_xml_subelement(input, nsmap.get('soap') + 'header') relatesTo.set('message', 'tns:RelatesToHeader') relatesTo.set('part', 'RelatesTo') relatesTo.set('use', 'literal') callbackBinding.append(operation) else: if method.isAsync: rtHeader = create_xml_subelement(input, nsmap.get('soap') + 'header') rtHeader.set('message', 'tns:ReplyToHeader') rtHeader.set('part', 'ReplyTo') rtHeader.set('use', 'literal') midHeader = create_xml_subelement(input, nsmap.get('soap') + 'header') midHeader.set('message', 'tns:MessageIDHeader') midHeader.set('part', 'MessageID') midHeader.set('use', 'literal') binding.append(operation)
def to_xml(cls, value, name='retval', nsmap=ns): if type(value) == str: value = ElementTree.fromstring(value) e = create_xml_element(name, nsmap) e.append(value) return e