WsdlImporter.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // WsdlImporter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. // Ankit Jain <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.ServiceModel;
  33. using System.ServiceModel.Channels;
  34. using System.Web.Services.Description;
  35. using System.Xml;
  36. using System.Xml.Schema;
  37. using SMBinding = System.ServiceModel.Channels.Binding;
  38. using WSServiceDescription = System.Web.Services.Description.ServiceDescription;
  39. using WSBinding = System.Web.Services.Description.Binding;
  40. using WSMessage = System.Web.Services.Description.Message;
  41. using QName = System.Xml.XmlQualifiedName;
  42. namespace System.ServiceModel.Description
  43. {
  44. [MonoTODO]
  45. public class WsdlImporter : MetadataImporter
  46. {
  47. ServiceDescriptionCollection wsdl_documents;
  48. XmlSchemaSet xmlschemas;
  49. List<XmlElement> policies; /* ?? */
  50. MetadataSet metadata;
  51. KeyedByTypeCollection<IWsdlImportExtension> wsdl_extensions;
  52. //Imported
  53. Collection<ContractDescription> contracts = null;
  54. ServiceEndpointCollection endpoint_colln = null;
  55. public WsdlImporter (
  56. MetadataSet metadata,
  57. IEnumerable<IPolicyImportExtension> policyImportExtensions,
  58. IEnumerable<IWsdlImportExtension> wsdlImportExtensions)
  59. : base (policyImportExtensions)
  60. {
  61. if (metadata == null)
  62. throw new ArgumentNullException ("metadata");
  63. if (wsdlImportExtensions == null) {
  64. wsdl_extensions = new KeyedByTypeCollection<IWsdlImportExtension> ();
  65. wsdl_extensions.Add (new StandardBindingImporter ());
  66. wsdl_extensions.Add (new TransportBindingElementImporter ());
  67. //wsdl_extensions.Add (new MessageEncodingBindingElementImporter ());
  68. wsdl_extensions.Add (new XmlSerializerMessageContractImporter ());
  69. wsdl_extensions.Add (new DataContractSerializerMessageContractImporter ());
  70. } else {
  71. wsdl_extensions = new KeyedByTypeCollection<IWsdlImportExtension> (wsdlImportExtensions);
  72. }
  73. this.metadata = metadata;
  74. this.wsdl_documents = new ServiceDescriptionCollection ();
  75. this.xmlschemas = new XmlSchemaSet ();
  76. this.policies = new List<XmlElement> ();
  77. foreach (MetadataSection ms in metadata.MetadataSections) {
  78. if (ms.Dialect == MetadataSection.ServiceDescriptionDialect &&
  79. ms.Metadata.GetType () == typeof (WSServiceDescription))
  80. wsdl_documents.Add ((WSServiceDescription) ms.Metadata);
  81. else
  82. if (ms.Dialect == MetadataSection.XmlSchemaDialect &&
  83. ms.Metadata.GetType () == typeof (XmlSchema))
  84. xmlschemas.Add ((XmlSchema) ms.Metadata);
  85. }
  86. }
  87. public WsdlImporter (MetadataSet metadata)
  88. : this (metadata, null, null)
  89. {
  90. }
  91. public ServiceDescriptionCollection WsdlDocuments {
  92. get { return wsdl_documents; }
  93. }
  94. public KeyedByTypeCollection <IWsdlImportExtension> WsdlImportExtensions {
  95. get { return wsdl_extensions; }
  96. }
  97. public XmlSchemaSet XmlSchemas {
  98. get { return xmlschemas; }
  99. }
  100. public Collection<SMBinding> ImportAllBindings ()
  101. {
  102. Collection<SMBinding> bindings = new Collection<SMBinding> ();
  103. foreach (WSServiceDescription sd in wsdl_documents)
  104. foreach (WSBinding binding in sd.Bindings)
  105. bindings.Add (ImportBinding (binding));
  106. return bindings;
  107. }
  108. public SMBinding ImportBinding (WSBinding binding)
  109. {
  110. /* Default, CustomBinding.. */
  111. CustomBinding smbinding = new CustomBinding ();
  112. foreach (IWsdlImportExtension extension in wsdl_extensions)
  113. extension.BeforeImport (wsdl_documents, xmlschemas, policies);
  114. smbinding.Name = binding.Name;
  115. smbinding.Namespace = binding.ServiceDescription.TargetNamespace;
  116. //FIXME: Added by MessageEncodingBindingElementConverter.ImportPolicy
  117. smbinding.Elements.Add (new TextMessageEncodingBindingElement ());
  118. /*ImportContract
  119. PortType portType = null;
  120. foreach (WSServiceDescription sd in wsdl_documents) {
  121. portType = sd.PortTypes [binding.Type.Name];
  122. if (portType != null)
  123. break;
  124. }
  125. //FIXME: if portType == null
  126. */
  127. // FIXME: ImportContract here..
  128. return smbinding;
  129. }
  130. public override Collection<ContractDescription> ImportAllContracts ()
  131. {
  132. if (contracts != null)
  133. return contracts;
  134. contracts = new Collection<ContractDescription> ();
  135. foreach (WSServiceDescription sd in wsdl_documents) {
  136. foreach (PortType pt in sd.PortTypes)
  137. contracts.Add (ImportContract (pt));
  138. }
  139. return contracts;
  140. }
  141. public override ServiceEndpointCollection ImportAllEndpoints ()
  142. {
  143. if (endpoint_colln != null)
  144. return endpoint_colln;
  145. endpoint_colln = new ServiceEndpointCollection ();
  146. foreach (IWsdlImportExtension extension in wsdl_extensions) {
  147. extension.BeforeImport (wsdl_documents, xmlschemas, policies);
  148. }
  149. foreach (WSServiceDescription wsd in wsdl_documents)
  150. foreach (Service service in wsd.Services)
  151. foreach (Port port in service.Ports)
  152. endpoint_colln.Add (ImportEndpoint (port));
  153. return endpoint_colln;
  154. }
  155. public ContractDescription ImportContract (PortType wsdlPortType)
  156. {
  157. foreach (IWsdlImportExtension extension in wsdl_extensions) {
  158. extension.BeforeImport (wsdl_documents, xmlschemas, policies);
  159. }
  160. ContractDescription cd = new ContractDescription (wsdlPortType.Name, wsdlPortType.ServiceDescription.TargetNamespace);
  161. foreach (Operation op in wsdlPortType.Operations) {
  162. OperationDescription op_descr = new OperationDescription (op.Name, cd);
  163. foreach (OperationMessage opmsg in op.Messages) {
  164. /* OperationMessageCollection */
  165. MessageDescription msg_descr;
  166. MessageDirection dir = MessageDirection.Input;
  167. string action = "";
  168. if (opmsg.GetType () == typeof (OperationInput))
  169. dir = MessageDirection.Input;
  170. else if (opmsg.GetType () == typeof (OperationOutput))
  171. dir = MessageDirection.Output;
  172. /* FIXME: OperationFault--> OperationDescription.Faults ? */
  173. if (opmsg.ExtensibleAttributes != null) {
  174. for (int i = 0; i < opmsg.ExtensibleAttributes.Length; i++) {
  175. if (opmsg.ExtensibleAttributes [i].LocalName == "Action" &&
  176. opmsg.ExtensibleAttributes [i].NamespaceURI == "http://www.w3.org/2006/05/addressing/wsdl")
  177. /* addressing:Action */
  178. action = opmsg.ExtensibleAttributes [i].Value;
  179. /* FIXME: other attributes ? */
  180. }
  181. }
  182. msg_descr = new MessageDescription (action, dir);
  183. /* FIXME: Headers ? */
  184. op_descr.Messages.Add (msg_descr);
  185. }
  186. cd.Operations.Add (op_descr);
  187. }
  188. WsdlContractConversionContext context = new WsdlContractConversionContext (cd, wsdlPortType);
  189. foreach (IWsdlImportExtension extension in wsdl_extensions)
  190. extension.ImportContract (this, context);
  191. return cd;
  192. }
  193. public ServiceEndpoint ImportEndpoint (Port wsdlPort)
  194. {
  195. foreach (IWsdlImportExtension extension in wsdl_extensions) {
  196. extension.BeforeImport (wsdl_documents, xmlschemas, policies);
  197. }
  198. //Get the corresponding contract
  199. //via the PortType
  200. WSBinding wsb = wsdlPort.Service.ServiceDescription.Bindings [wsdlPort.Binding.Name];
  201. if (wsb == null)
  202. //FIXME
  203. throw new Exception (String.Format ("Binding named {0} not found.", wsdlPort.Binding.Name));
  204. SMBinding binding = ImportBinding (wsb);
  205. PortType port_type = null;
  206. foreach (WSServiceDescription sd in wsdl_documents) {
  207. port_type = sd.PortTypes [wsb.Type.Name];
  208. if (port_type != null)
  209. break;
  210. }
  211. if (port_type == null)
  212. //FIXME
  213. throw new Exception (String.Format ("PortType named {0} not found.", wsb.Type.Name));
  214. ContractDescription contract = ImportContract (port_type);
  215. ServiceEndpoint sep = new ServiceEndpoint (contract);
  216. sep.Binding = binding;
  217. WsdlContractConversionContext contract_context = new WsdlContractConversionContext (contract, port_type);
  218. WsdlEndpointConversionContext endpoint_context = new WsdlEndpointConversionContext (
  219. contract_context, sep, wsdlPort, wsb);
  220. foreach (IWsdlImportExtension extension in wsdl_extensions)
  221. extension.ImportEndpoint (this, endpoint_context);
  222. return sep;
  223. }
  224. public ServiceEndpointCollection ImportEndpoints (
  225. WSBinding binding)
  226. {
  227. throw new NotImplementedException ();
  228. }
  229. public ServiceEndpointCollection ImportEndpoints (
  230. PortType portType)
  231. {
  232. throw new NotImplementedException ();
  233. }
  234. public ServiceEndpointCollection ImportEndpoints (
  235. Service service)
  236. {
  237. throw new NotImplementedException ();
  238. }
  239. }
  240. }