5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Ankit Jain <jankit@novell.com>
8 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
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;
36 using System.Xml.Schema;
38 using SMBinding = System.ServiceModel.Channels.Binding;
39 using WSServiceDescription = System.Web.Services.Description.ServiceDescription;
40 using WSBinding = System.Web.Services.Description.Binding;
41 using WSMessage = System.Web.Services.Description.Message;
42 using QName = System.Xml.XmlQualifiedName;
44 namespace System.ServiceModel.Description
47 public class WsdlImporter : MetadataImporter
49 ServiceDescriptionCollection wsdl_documents;
50 XmlSchemaSet xmlschemas;
51 List<XmlElement> policies; /* ?? */
54 KeyedByTypeCollection<IWsdlImportExtension> wsdl_extensions;
57 Collection<ContractDescription> contracts = null;
58 ServiceEndpointCollection endpoint_colln = null;
62 IEnumerable<IPolicyImportExtension> policyImportExtensions,
63 IEnumerable<IWsdlImportExtension> wsdlImportExtensions)
64 : base (policyImportExtensions)
67 throw new ArgumentNullException ("metadata");
69 if (wsdlImportExtensions == null) {
70 wsdl_extensions = new KeyedByTypeCollection<IWsdlImportExtension> ();
72 wsdl_extensions.Add (new StandardBindingImporter ());
73 wsdl_extensions.Add (new TransportBindingElementImporter ());
74 //wsdl_extensions.Add (new MessageEncodingBindingElementImporter ());
75 wsdl_extensions.Add (new XmlSerializerMessageContractImporter ());
76 wsdl_extensions.Add (new DataContractSerializerMessageContractImporter ());
78 wsdl_extensions = new KeyedByTypeCollection<IWsdlImportExtension> (wsdlImportExtensions);
81 this.metadata = metadata;
82 this.wsdl_documents = new ServiceDescriptionCollection ();
83 this.xmlschemas = new XmlSchemaSet ();
84 this.policies = new List<XmlElement> ();
86 foreach (MetadataSection ms in metadata.MetadataSections) {
87 if (ms.Dialect == MetadataSection.ServiceDescriptionDialect &&
88 ms.Metadata.GetType () == typeof (WSServiceDescription))
89 wsdl_documents.Add ((WSServiceDescription) ms.Metadata);
91 if (ms.Dialect == MetadataSection.XmlSchemaDialect &&
92 ms.Metadata.GetType () == typeof (XmlSchema))
93 xmlschemas.Add ((XmlSchema) ms.Metadata);
97 public WsdlImporter (MetadataSet metadata)
98 : this (metadata, null, null)
102 public ServiceDescriptionCollection WsdlDocuments {
103 get { return wsdl_documents; }
106 public KeyedByTypeCollection <IWsdlImportExtension> WsdlImportExtensions {
107 get { return wsdl_extensions; }
110 public XmlSchemaSet XmlSchemas {
111 get { return xmlschemas; }
114 public Collection<SMBinding> ImportAllBindings ()
116 Collection<SMBinding> bindings = new Collection<SMBinding> ();
118 foreach (WSServiceDescription sd in wsdl_documents)
119 foreach (WSBinding binding in sd.Bindings)
120 bindings.Add (ImportBinding (binding));
125 public SMBinding ImportBinding (WSBinding binding)
127 /* Default, CustomBinding.. */
128 CustomBinding smbinding = new CustomBinding ();
130 foreach (IWsdlImportExtension extension in wsdl_extensions)
131 extension.BeforeImport (wsdl_documents, xmlschemas, policies);
133 smbinding.Name = binding.Name;
134 smbinding.Namespace = binding.ServiceDescription.TargetNamespace;
136 //FIXME: Added by MessageEncodingBindingElementConverter.ImportPolicy
137 smbinding.Elements.Add (new TextMessageEncodingBindingElement ());
140 PortType portType = null;
141 foreach (WSServiceDescription sd in wsdl_documents) {
142 portType = sd.PortTypes [binding.Type.Name];
143 if (portType != null)
147 //FIXME: if portType == null
150 // FIXME: ImportContract here..
155 public override Collection<ContractDescription> ImportAllContracts ()
157 if (contracts != null)
160 contracts = new Collection<ContractDescription> ();
162 foreach (WSServiceDescription sd in wsdl_documents) {
163 foreach (PortType pt in sd.PortTypes)
164 contracts.Add (ImportContract (pt));
170 public override ServiceEndpointCollection ImportAllEndpoints ()
172 if (endpoint_colln != null)
173 return endpoint_colln;
175 endpoint_colln = new ServiceEndpointCollection ();
177 foreach (IWsdlImportExtension extension in wsdl_extensions) {
178 extension.BeforeImport (wsdl_documents, xmlschemas, policies);
181 foreach (WSServiceDescription wsd in wsdl_documents)
182 foreach (Service service in wsd.Services)
183 foreach (Port port in service.Ports)
184 endpoint_colln.Add (ImportEndpoint (port));
186 return endpoint_colln;
189 public ContractDescription ImportContract (PortType wsdlPortType)
191 foreach (IWsdlImportExtension extension in wsdl_extensions) {
192 extension.BeforeImport (wsdl_documents, xmlschemas, policies);
195 ContractDescription cd = new ContractDescription (wsdlPortType.Name, wsdlPortType.ServiceDescription.TargetNamespace);
197 foreach (Operation op in wsdlPortType.Operations) {
198 OperationDescription op_descr = new OperationDescription (op.Name, cd);
200 foreach (OperationMessage opmsg in op.Messages) {
201 /* OperationMessageCollection */
202 MessageDescription msg_descr;
203 MessageDirection dir = MessageDirection.Input;
206 if (opmsg.GetType () == typeof (OperationInput))
207 dir = MessageDirection.Input;
208 else if (opmsg.GetType () == typeof (OperationOutput))
209 dir = MessageDirection.Output;
210 /* FIXME: OperationFault--> OperationDescription.Faults ? */
212 if (opmsg.ExtensibleAttributes != null) {
213 for (int i = 0; i < opmsg.ExtensibleAttributes.Length; i++) {
214 if (opmsg.ExtensibleAttributes [i].LocalName == "Action" &&
215 opmsg.ExtensibleAttributes [i].NamespaceURI == "http://www.w3.org/2006/05/addressing/wsdl")
216 /* addressing:Action */
217 action = opmsg.ExtensibleAttributes [i].Value;
218 /* FIXME: other attributes ? */
222 msg_descr = new MessageDescription (action, dir);
223 /* FIXME: Headers ? */
225 op_descr.Messages.Add (msg_descr);
228 cd.Operations.Add (op_descr);
231 WsdlContractConversionContext context = new WsdlContractConversionContext (cd, wsdlPortType);
232 foreach (IWsdlImportExtension extension in wsdl_extensions)
233 extension.ImportContract (this, context);
238 public ServiceEndpoint ImportEndpoint (Port wsdlPort)
240 foreach (IWsdlImportExtension extension in wsdl_extensions) {
241 extension.BeforeImport (wsdl_documents, xmlschemas, policies);
244 //Get the corresponding contract
246 WSBinding wsb = wsdlPort.Service.ServiceDescription.Bindings [wsdlPort.Binding.Name];
249 throw new Exception (String.Format ("Binding named {0} not found.", wsdlPort.Binding.Name));
251 SMBinding binding = ImportBinding (wsb);
253 PortType port_type = null;
254 foreach (WSServiceDescription sd in wsdl_documents) {
255 port_type = sd.PortTypes [wsb.Type.Name];
256 if (port_type != null)
260 if (port_type == null)
262 throw new Exception (String.Format ("PortType named {0} not found.", wsb.Type.Name));
264 ContractDescription contract = ImportContract (port_type);
265 ServiceEndpoint sep = new ServiceEndpoint (contract);
267 sep.Binding = binding;
269 WsdlContractConversionContext contract_context = new WsdlContractConversionContext (contract, port_type);
270 WsdlEndpointConversionContext endpoint_context = new WsdlEndpointConversionContext (
271 contract_context, sep, wsdlPort, wsb);
273 foreach (IWsdlImportExtension extension in wsdl_extensions)
274 extension.ImportEndpoint (this, endpoint_context);
279 public ServiceEndpointCollection ImportEndpoints (
282 throw new NotImplementedException ();
285 public ServiceEndpointCollection ImportEndpoints (
288 throw new NotImplementedException ();
291 public ServiceEndpointCollection ImportEndpoints (
294 throw new NotImplementedException ();