File: WsdlImporter.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (585 lines) | stat: -rw-r--r-- 17,835 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//
// WsdlImporter.cs
//
// Authors:
//	Atsushi Enomoto <atsushi@ximian.com>
//	Ankit Jain <jankit@novell.com>	
//	Martin Baulig <martin.baulig@xamarin.com>
//
// Copyright (C) 2005 Novell, Inc.  http://www.novell.com
// Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web.Services.Description;
using System.Xml;
using System.Xml.Schema;

using SMBinding = System.ServiceModel.Channels.Binding;
using WS = System.Web.Services.Description;
using WSServiceDescription = System.Web.Services.Description.ServiceDescription;
using WSBinding = System.Web.Services.Description.Binding;
using WSMessage = System.Web.Services.Description.Message;
using QName = System.Xml.XmlQualifiedName;

namespace System.ServiceModel.Description
{
	[MonoTODO]
	public class WsdlImporter : MetadataImporter
	{
		ServiceDescriptionCollection wsdl_documents;
		XmlSchemaSet xmlschemas;
		List<XmlElement> policies; /* ?? */
		MetadataSet metadata;
		bool beforeImportCalled;

		KeyedByTypeCollection<IWsdlImportExtension> wsdl_extensions;
			
		//Imported
		Collection<ContractDescription> contracts = null;
		ServiceEndpointCollection endpoint_colln = null;

		// Contract by PortType
		Dictionary<PortType, ContractDescription> contractHash = null;
		// ServiceEndpoint by WSBinding
		Dictionary<WSBinding, ServiceEndpoint> bindingHash = null;
		// ServiceEndpoint by Port
		Dictionary<Port, ServiceEndpoint> endpointHash = null;

		public WsdlImporter (
			MetadataSet metadata,
			IEnumerable<IPolicyImportExtension> policyImportExtensions,
			IEnumerable<IWsdlImportExtension> wsdlImportExtensions)
			: base (policyImportExtensions)
		{
			if (metadata == null)
				throw new ArgumentNullException ("metadata");
			
			if (wsdlImportExtensions == null) {
				wsdl_extensions = new KeyedByTypeCollection<IWsdlImportExtension> ();

				wsdl_extensions.Add (new DataContractSerializerMessageContractImporter ());
				wsdl_extensions.Add (new XmlSerializerMessageContractImporter ());
				wsdl_extensions.Add (new MessageEncodingBindingElementImporter ());
				wsdl_extensions.Add (new TransportBindingElementImporter ());
				wsdl_extensions.Add (new StandardBindingImporter ());
			} else {
				wsdl_extensions = new KeyedByTypeCollection<IWsdlImportExtension> (wsdlImportExtensions);
			}

			// It is okay to fill these members immediately when WsdlImporter.ctor() is invoked
			// i.e. after this .ctor(), those metadata docs are not considered anymore.
			this.metadata = metadata;
			this.wsdl_documents = new ServiceDescriptionCollection ();
			this.xmlschemas = new XmlSchemaSet ();
			this.policies = new List<XmlElement> ();
			this.contractHash = new Dictionary<PortType, ContractDescription> ();
			this.bindingHash = new Dictionary<WSBinding, ServiceEndpoint> ();
			this.endpointHash = new Dictionary<Port, ServiceEndpoint> ();

			foreach (MetadataSection ms in metadata.MetadataSections) {
				if (ms.Dialect == MetadataSection.ServiceDescriptionDialect &&
					ms.Metadata.GetType () == typeof (WSServiceDescription))
					wsdl_documents.Add ((WSServiceDescription) ms.Metadata);
				else
				if (ms.Dialect == MetadataSection.XmlSchemaDialect &&
					ms.Metadata.GetType () == typeof (XmlSchema))
					xmlschemas.Add ((XmlSchema) ms.Metadata);
			}
		}

		public WsdlImporter (MetadataSet metadata)
			: this (metadata, null, null)
		{
		}

		public ServiceDescriptionCollection WsdlDocuments {
			get { return wsdl_documents; }
		}

		public KeyedByTypeCollection <IWsdlImportExtension> WsdlImportExtensions {
			get { return wsdl_extensions; }
		}

		public XmlSchemaSet XmlSchemas {
			get { return xmlschemas; }
		}

		public Collection<SMBinding> ImportAllBindings ()
		{
			Collection<SMBinding> bindings = new Collection<SMBinding> ();

			foreach (WSServiceDescription sd in wsdl_documents) {
				foreach (WSBinding binding in sd.Bindings) {
					var endpoint = ImportBinding (binding, false);
					if (endpoint != null)
						bindings.Add (endpoint.Binding);
				}
			}

			return bindings;
		}

		void BeforeImport ()
		{
			if (beforeImportCalled)
				return;

			foreach (IWsdlImportExtension extension in wsdl_extensions)
				extension.BeforeImport (wsdl_documents, xmlschemas, policies);
			
			beforeImportCalled = true;
		}

		public SMBinding ImportBinding (WSBinding wsdlBinding)
		{
			return ImportBinding (wsdlBinding, true).Binding;
		}

		ServiceEndpoint ImportBinding (WSBinding binding, bool throwOnError)
		{
			if (bindingHash.ContainsKey (binding)) {
				var sep = bindingHash [binding];
				if (sep != null)
					return sep;

				if (!throwOnError)
					return null;
				
				throw new InvalidOperationException (String.Format (
					"Failed to import binding {0}, an error has " +
					"already been reported before.", binding.Name));
			}

			try {
				var port_type = GetPortTypeFromBinding (binding);
				var contract = ImportContract (port_type);
				var contract_context = new WsdlContractConversionContext (contract, port_type);
			
				var sep = ImportBinding (binding, contract_context);
				bindingHash.Add (binding, sep);
				return sep;
			} catch (MetadataImportException) {
				bindingHash.Add (binding, null);
				if (throwOnError)
					throw;
				return null;
			} catch (Exception ex) {
				bindingHash.Add (binding, null);
				var error = AddError (
					"Failed to import binding `{0}': {1}", binding.Name, ex.Message);
				if (throwOnError)
					throw new MetadataImportException (error, ex);
				return null;
			}
		}

		ServiceEndpoint ImportBinding (WSBinding binding,
		                               WsdlContractConversionContext contract_context)
		{
			BeforeImport ();

			var sep = new ServiceEndpoint (contract_context.Contract);
			
			var custom = new CustomBinding ();
			custom.Name = binding.Name;
			custom.Namespace = binding.ServiceDescription.TargetNamespace;
			
			sep.Binding = custom;

			try {
				ImportPolicy (binding, sep);
			} catch (Exception ex) {
				// FIXME: Policy import is still experimental.
				AddWarning ("Exception while trying to import policy for " +
				            "binding `{0}': {1}", binding.Name, ex.Message);
			}
			
			var endpoint_context = new WsdlEndpointConversionContext (
				contract_context, sep, null, binding);
			
			foreach (IWsdlImportExtension extension in wsdl_extensions)
				extension.ImportEndpoint (this, endpoint_context);
			
			return sep;
		}

		void ImportPolicy (WSBinding binding, ServiceEndpoint endpoint)
		{
			var context = new Description.CustomPolicyConversionContext (binding, endpoint);
			var assertions = context.GetBindingAssertions ();

			foreach (var ext in binding.Extensions) {
				var xml = ext as XmlElement;
				if (xml == null)
					continue;
				if (!xml.NamespaceURI.Equals (Constants.WspNamespace))
					continue;
				if (xml.LocalName.Equals ("Policy")) {
					context.AddPolicyAssertion (xml);
					continue;
				}
				if (!xml.LocalName.Equals ("PolicyReference"))
					continue;
				var uri = xml.GetAttribute ("URI");

				if (!uri.StartsWith ("#")) {
					// FIXME
					AddWarning (
						"Failed to resolve unknown policy reference `{0}' for " +
						"binding `{1}'.", uri, binding.Name);
					continue;
				}

				foreach (var sext in binding.ServiceDescription.Extensions) {
					var sxml = sext as XmlElement;
					if (sxml == null)
						continue;
					if (!sxml.NamespaceURI.Equals (Constants.WspNamespace))
						continue;
					if (!sxml.LocalName.Equals ("Policy"))
						continue;
					var id = sxml.GetAttribute ("Id", Constants.WsuNamespace);
					if (!uri.Substring (1).Equals (id))
						continue;
					context.AddPolicyAssertion (sxml);
				}
			}

			foreach (IPolicyImportExtension extension in PolicyImportExtensions) {
				try {
					extension.ImportPolicy (this, context);
				} catch (Exception ex) {
					AddWarning (
						"PolicyImportException `{0}' threw an exception while " +
						"trying to import policy references for endpoint `{1}': {2}",
						extension.GetType ().Name, endpoint.Name, ex.Message);
				}
			}
		}

		PortType GetPortTypeFromBinding (WSBinding binding)
		{
			foreach (WSServiceDescription sd in wsdl_documents) {
				var port_type = sd.PortTypes [binding.Type.Name];
				if (port_type != null)
					return port_type;
			}
			
			throw new MetadataImportException (AddError (
				"PortType named {0} not found in namespace {1}.",
				binding.Type.Name, binding.Type.Namespace));
		}
		
		public override Collection<ContractDescription> ImportAllContracts ()
		{
			if (contracts != null)
				return contracts;

			contracts = new Collection<ContractDescription> ();

			foreach (WSServiceDescription sd in wsdl_documents) {
				foreach (PortType pt in sd.PortTypes) {
					var cd = ImportContract (pt, false);
					if (cd != null)
						contracts.Add (cd);
				}
			}

			return contracts;
		}

		public override ServiceEndpointCollection ImportAllEndpoints ()
		{
			if (endpoint_colln != null)
				return endpoint_colln;

			endpoint_colln = new ServiceEndpointCollection ();

			foreach (WSServiceDescription wsd in wsdl_documents) {
				foreach (Service service in wsd.Services) {
					foreach (Port port in service.Ports) {
						var sep = ImportEndpoint (port, false);
						if (sep != null)
							endpoint_colln.Add (sep);
					}
				}
			}

			return endpoint_colln;
		}

		public ContractDescription ImportContract (PortType wsdlPortType)
		{
			return ImportContract (wsdlPortType, true);
		}

		ContractDescription ImportContract (PortType portType, bool throwOnError)
		{
			if (contractHash.ContainsKey (portType)) {
				var cd = contractHash [portType];
				if (cd != null)
					return cd;

				if (!throwOnError)
					return null;

				throw new InvalidOperationException (String.Format (
					"Failed to import contract for port type `{0}', " +
					"an error has already been reported.", portType.Name));
			}

			try {
				var cd = DoImportContract (portType);
				contractHash.Add (portType, cd);
				return cd;
			} catch (MetadataImportException) {
				contractHash.Add (portType, null);
				if (throwOnError)
					throw;
				return null;
			} catch (Exception ex) {
				contractHash.Add (portType, null);
				var error = AddError (
					"Failed to import contract for port type `{0}': {1}",
					portType.Name, ex.Message);
				if (throwOnError)
					throw new MetadataImportException (error, ex);
				return null;
			}
		}

		ContractDescription DoImportContract (PortType wsdlPortType)
		{
			BeforeImport ();

			ContractDescription cd = new ContractDescription (wsdlPortType.Name, wsdlPortType.ServiceDescription.TargetNamespace);

			foreach (Operation op in wsdlPortType.Operations) {
				OperationDescription op_descr = new OperationDescription (op.Name, cd);

				foreach (OperationMessage opmsg in op.Messages) {
					/* OperationMessageCollection */
					MessageDescription msg_descr;
					MessageDirection dir = MessageDirection.Input;
					string action = "";

					if (opmsg.GetType () == typeof (OperationInput))
						dir = MessageDirection.Input;
					else if (opmsg.GetType () == typeof (OperationOutput))
						dir = MessageDirection.Output;
					/* FIXME: OperationFault--> OperationDescription.Faults ? */

					if (opmsg.ExtensibleAttributes != null) {
						for (int i = 0; i < opmsg.ExtensibleAttributes.Length; i++) {
							if (opmsg.ExtensibleAttributes [i].LocalName == "Action" &&
								opmsg.ExtensibleAttributes [i].NamespaceURI == "http://www.w3.org/2006/05/addressing/wsdl")
								/* addressing:Action */
								action = opmsg.ExtensibleAttributes [i].Value;
							/* FIXME: other attributes ? */
						}
					}

					// fill Action from operation binding if required.
					if (action == "") {
						if (dir != MessageDirection.Input)
							action = GetActionFromOperationBinding (wsdlPortType, op.Name);
						else
							action = "*";
					}

					msg_descr = new MessageDescription (action, dir);
					/* FIXME: Headers ? */

					op_descr.Messages.Add (msg_descr);
				}

				cd.Operations.Add (op_descr);
			}

			WsdlContractConversionContext context = new WsdlContractConversionContext (cd, wsdlPortType);
			foreach (IWsdlImportExtension extension in wsdl_extensions)
				extension.ImportContract (this, context);

			return cd;
		}

		string GetActionFromOperationBinding (PortType pt, string opName)
		{
			foreach (WSBinding binding in pt.ServiceDescription.Bindings) {
				foreach (OperationBinding ob in binding.Operations) {
					if (ob.Name != opName)
						continue;
					foreach (var ext in ob.Extensions) {
						var sob = ext as SoapOperationBinding;
						if (sob == null)
							continue;
						return sob.SoapAction;
					}
					return String.Empty;
				}
			}
			return String.Empty;
		}

		public ServiceEndpoint ImportEndpoint (Port wsdlPort)
		{
			return ImportEndpoint (wsdlPort, true);
		}

		ServiceEndpoint ImportEndpoint (Port port, bool throwOnError)
		{
			ServiceEndpoint endpoint;
			if (endpointHash.ContainsKey (port)) {
				endpoint = endpointHash [port];
				if (endpoint != null)
					return endpoint;
				
				if (!throwOnError)
					return null;
				
				throw new InvalidOperationException (String.Format (
					"Failed to import port `{0}', an error has " +
					"already been reported before.", port.Name));
			}

			var binding = port.Service.ServiceDescription.Bindings [port.Binding.Name];
			if (binding == null) {
				endpointHash.Add (port, null);
				var error = AddError (
					"Failed to import port `{0}': cannot find binding `{1}' that " +
					"this port depends on.", port.Name, port.Binding.Name);
				if (throwOnError)
					throw new MetadataImportException (error);
				return null;
			}

			try {
				endpoint = ImportBinding (binding, throwOnError);
			} catch (Exception ex) {
				endpointHash.Add (port, null);
				var error = AddError (
					"Failed to import port `{0}': error while trying to import " +
					"binding `{1}' that this port depends on: {2}",
					port.Name, port.Binding.Name, ex.Message);
				if (throwOnError)
					throw new MetadataImportException (error, ex);
				return null;
			}

			if (endpoint == null) {
				endpointHash.Add (port, null);
				AddError (
					"Failed to import port `{0}': error while trying to import " +
					"binding `{1}' that this port depends on.",
					port.Name, port.Binding.Name);
				return null;
			}

			try {
				ImportEndpoint (port, binding, endpoint, throwOnError);
				endpointHash.Add (port, endpoint);
				return endpoint;
			} catch (MetadataImportException) {
				endpointHash.Add (port, null);
				if (throwOnError)
					throw;
				return null;
			} catch (Exception ex) {
				endpointHash.Add (port, null);
				var error = AddError (
					"Failed to import port `{0}': {1}", port.Name, ex.Message);
				if (throwOnError)
					throw new MetadataImportException (error, ex);
				return null;
			}
		}

		void ImportEndpoint (Port port, WSBinding wsb, ServiceEndpoint sep, bool throwOnError)
		{
			BeforeImport ();

			var port_type = GetPortTypeFromBinding (wsb);

			var contract_context = new WsdlContractConversionContext (sep.Contract, port_type);
			WsdlEndpointConversionContext endpoint_context = new WsdlEndpointConversionContext (
					contract_context, sep, port, wsb);

			foreach (IWsdlImportExtension extension in wsdl_extensions)
				extension.ImportEndpoint (this, endpoint_context);
		}

		void ImportEndpoints (ServiceEndpointCollection coll, WSBinding binding)
		{
			foreach (WSServiceDescription wsd in wsdl_documents) {
				foreach (WS.Service service in wsd.Services) {
					foreach (WS.Port port in service.Ports) {
						if (!binding.Name.Equals (port.Binding.Name))
							continue;
						var sep = ImportEndpoint (port, false);
						if (sep != null)
							coll.Add (sep);
					}
				}
			}
		}

		public ServiceEndpointCollection ImportEndpoints (WSBinding wsdlBinding)
		{
			var coll = new ServiceEndpointCollection ();
			ImportEndpoints (coll, wsdlBinding);
			return coll;
		}

		public ServiceEndpointCollection ImportEndpoints (PortType wsdlPortType)
		{
			var coll = new ServiceEndpointCollection ();

			foreach (WSServiceDescription wsd in wsdl_documents) {
				foreach (WS.Binding binding in wsd.Bindings) {
					if (!binding.Type.Name.Equals (wsdlPortType.Name))
						continue;

					ImportEndpoints (coll, binding);
				}
			}

			return coll;
		}

		public ServiceEndpointCollection ImportEndpoints (Service wsdlService)
		{
			var coll = new ServiceEndpointCollection ();
			
			foreach (Port port in wsdlService.Ports) {
				var sep = ImportEndpoint (port, false);
				if (sep != null)
					coll.Add (sep);
			}

			return coll;
		}
	}
}