3 # Sapan Bhatia <sapanb@cs.princeton.edu>
5 # Generates a WSDL for geniwrapper
11 import xml.dom.minidom
17 from sfa.trust.auth import Auth
18 from sfa.util.parameter import Parameter,Mixed
25 class SoapError(Exception):
26 def __init__(self, value):
29 return repr(self.value)
36 def fold_complex_type_names(acc, arg):
41 python_is_braindead = acc.doc
42 acc = [python_is_braindead,name]
45 def fold_complex_type(acc, arg):
47 name = name_complex_type(arg)
48 complex_types[arg]=name
52 python_is_braindead = name_complex_type(acc)
53 acc = [python_is_braindead,name]
56 def name_complex_type(arg):
60 types_section = types.getElementsByTagName("xsd:schema")[0]
62 if (isinstance(arg, Mixed)):
63 inner_types = reduce(fold_complex_type, arg)
64 inner_names = reduce(fold_complex_type_names, arg)
65 if (inner_types[-1]=="none"):
66 inner_types=inner_types[:-1]
72 type_name = "Type%d"%num_types
73 complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
74 complex_type.setAttribute("name", type_name)
76 choice = complex_type.appendChild(types.createElement("xsd:choice"))
77 for n,t in zip(inner_names,inner_types):
78 element = choice.appendChild(types.createElement("element"))
79 element.setAttribute("name", n)
80 element.setAttribute("type", "%s"%t)
81 element.setAttribute("minOccurs","%d"%min_args)
82 return "xsdl:%s"%type_name
83 elif (isinstance(arg, Parameter)):
84 return (name_simple_type(arg.type))
85 elif type(arg) == ListType or type(arg) == TupleType:
86 inner_type = name_complex_type(arg[0])
88 type_name = "Type%d"%num_types
89 complex_type = types_section.appendChild(types.createElement("xsd:simpleType"))
90 complex_type.setAttribute("name", type_name)
91 complex_content = complex_type.appendChild(types.createElement("xsd:list"))
92 complex_content.setAttribute("itemType",inner_type)
93 return "xsdl:%s"%type_name
95 elif type(arg) == DictType or arg == DictType or issubclass(arg, dict):
97 type_name = "Type%d"%num_types
98 complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
99 complex_type.setAttribute("name", type_name)
100 complex_content = complex_type.appendChild(types.createElement("xsd:sequence"))
103 inner_type = name_complex_type(arg.fields[k])
104 element=complex_content.appendChild(types.createElement("xsd:element"))
105 element.setAttribute("name",k)
106 element.setAttribute("type",inner_type)
108 return "xsdl:%s"%type_name
111 return (name_simple_type(arg))
113 def name_simple_type(arg_type):
116 if arg_type == DictType:
118 elif arg_type == IntType or arg_type == LongType:
120 elif arg_type == bool:
122 elif arg_type == FloatType:
124 elif arg_type in StringTypes:
128 raise SoapError, "Cannot handle %s objects" % arg_type
131 return (name_complex_type(arg))
133 def add_wsdl_ports_and_bindings (wsdl):
134 for method in apistub.methods:
135 # Skip system. methods
136 if "system." in method:
139 function = apistub.callable(method) # Commented documentation
140 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
141 #print "\n".join(lines)
145 in_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
146 in_el.setAttribute("name", method + "_in")
148 for service_name in function.interfaces:
149 if (services.has_key(service_name)):
150 if (not method in services[service_name]):
151 services[service_name].append(method)
153 services[service_name]=[method]
157 if (function.accepts):
158 (min_args, max_args, defaults) = function.args()
159 for (argname,argtype) in zip(max_args,function.accepts):
160 arg_part = in_el.appendChild(wsdl.createElement("part"))
161 arg_part.setAttribute("name", argname)
162 arg_part.setAttribute("type", param_type(argtype))
165 return_type = function.returns
166 out_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
167 out_el.setAttribute("name", method + "_out")
168 ret_part = out_el.appendChild(wsdl.createElement("part"))
169 ret_part.setAttribute("name", "returnvalue")
170 ret_part.setAttribute("type", param_type(return_type))
172 # Port connecting arguments with return type
174 port_el = wsdl.firstChild.appendChild(wsdl.createElement("portType"))
175 port_el.setAttribute("name", method + "_port")
177 op_el = port_el.appendChild(wsdl.createElement("operation"))
178 op_el.setAttribute("name", method)
179 inp_el=wsdl.createElement("input")
180 inp_el.setAttribute("message","tns:" + method + "_in")
181 inp_el.setAttribute("name",method+"_request")
182 op_el.appendChild(inp_el)
183 out_el = wsdl.createElement("output")
184 out_el.setAttribute("message","tns:" + method + "_out")
185 out_el.setAttribute("name",method+"_response")
186 op_el.appendChild(out_el)
190 bind_el = wsdl.firstChild.appendChild(wsdl.createElement("binding"))
191 bind_el.setAttribute("name", method + "_binding")
192 bind_el.setAttribute("type", "tns:" + method + "_port")
194 soap_bind = bind_el.appendChild(wsdl.createElement("soap:binding"))
195 soap_bind.setAttribute("style", "rpc")
196 soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
199 wsdl_op = bind_el.appendChild(wsdl.createElement("operation"))
200 wsdl_op.setAttribute("name", method)
201 wsdl_op.appendChild(wsdl.createElement("soap:operation")).setAttribute("soapAction",
205 wsdl_input = wsdl_op.appendChild(wsdl.createElement("input"))
206 input_soap_body = wsdl_input.appendChild(wsdl.createElement("soap:body"))
207 input_soap_body.setAttribute("use", "encoded")
208 input_soap_body.setAttribute("namespace", "urn:" + method)
209 input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
212 wsdl_output = wsdl_op.appendChild(wsdl.createElement("output"))
213 output_soap_body = wsdl_output.appendChild(wsdl.createElement("soap:body"))
214 output_soap_body.setAttribute("use", "encoded")
215 output_soap_body.setAttribute("namespace", "urn:" + method)
216 output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
219 def add_wsdl_service(wsdl):
220 for service in services.keys():
221 service_el = wsdl.firstChild.appendChild(wsdl.createElement("service"))
222 service_el.setAttribute("name", service)
224 for method in services[service]:
226 servport_el = service_el.appendChild(wsdl.createElement("port"))
227 servport_el.setAttribute("name", name + "_port")
228 servport_el.setAttribute("binding", "tns:" + name + "_binding")
230 soapaddress = servport_el.appendChild(wsdl.createElement("soap:address"))
231 soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
234 def get_wsdl_definitions():
235 wsdl_text_header = """
237 name="geniwrapper_autogenerated"
238 targetNamespace="%s/2009/07/sfa.wsdl"
239 xmlns="http://schemas.xmlsoap.org/wsdl/"
240 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
241 xmlns:xsdl="%s/2009/07/schema"
242 xmlns:tns="%s/2009/07/sfa.wsdl"
243 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
244 xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
245 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
246 """ % (globals.plc_ns,globals.plc_ns,globals.plc_ns)
248 wsdl = xml.dom.minidom.parseString(wsdl_text_header)
252 def get_wsdl_definitions_and_types():
253 wsdl_text_header = """
255 name="geniwrapper_autogenerated"
256 targetNamespace="%s/2009/07/sfa.wsdl"
257 xmlns="http://schemas.xmlsoap.org/wsdl/"
258 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
259 xmlns:xsdl="%s/2009/07/schema"
260 xmlns:tns="%s/2009/07/sfa.wsdl"
261 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
262 xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
263 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
265 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/2009/07/schema"/>
267 </wsdl:definitions> """ % (globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
268 wsdl = xml.dom.minidom.parseString(wsdl_text_header)
272 types = get_wsdl_definitions_and_types()
274 wsdl = get_wsdl_definitions()
275 add_wsdl_ports_and_bindings(wsdl)
276 wsdl_types = wsdl.importNode(types.getElementsByTagName("types")[0], True)
277 wsdl.firstChild.appendChild(wsdl_types)
278 add_wsdl_service(wsdl)
280 xml.dom.ext.PrettyPrint(wsdl)