File: BaseMessagesFormatter.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, 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 (533 lines) | stat: -rw-r--r-- 18,605 bytes parent folder | download | duplicates (8)
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
//
// DefaultMessageOperationFormatter.cs
//
// Author:
//	Atsushi Enomoto <atsushi@ximian.com>
//	Eyal Alaluf <eyala@mainsoft.com>
//
// Copyright (C) 2005-2010 Novell, Inc.  http://www.novell.com
// Copyright (C) 2008 Mainsoft Co. http://www.mainsoft.com
// Copyright 2011 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.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace System.ServiceModel.Dispatcher
{
	// This type is introduced for moonlight compatibility.
	internal class OperationFormatter
		: IDispatchMessageFormatter, IClientMessageFormatter
	{
		BaseMessagesFormatter impl;
		string operation_name;

		public OperationFormatter (OperationDescription od, bool isRpc, bool isEncoded)
		{
			Validate (od, isRpc, isEncoded);

			impl = BaseMessagesFormatter.Create (od);

			operation_name = od.Name;
		}

		public string OperationName {
			get { return operation_name; }
		}

		internal static bool IsValidReturnValue (MessagePartDescription part)
		{
			return part != null && part.Type != typeof (void);
		}

		internal static void Validate (OperationDescription od, bool isRpc, bool isEncoded)
		{
			bool hasVoid = false;
			foreach (var md in od.Messages) {
				if (md.IsTypedMessage || md.IsUntypedMessage) {
					if (isRpc && !isEncoded)
						throw new InvalidOperationException ("Message with action {0} is either strongly-typed or untyped, but defined as RPC and encoded.");
					if (isRpc && hasVoid)
						throw new InvalidOperationException (String.Format ("This operation '{0}' is defined as RPC and contains a message with void, which is not allowed.", od.Name));
				} else {
					hasVoid |= md.IsVoid;
				}
			}
		}

		public object DeserializeReply (Message message, object [] parameters)
		{
			return impl.DeserializeReply (message, parameters);
		}

		public Message SerializeRequest (MessageVersion messageVersion, object [] parameters)
		{
			return impl.SerializeRequest (messageVersion, parameters);
		}

		public void DeserializeRequest (Message message, object [] parameters)
		{
			impl.DeserializeRequest (message, parameters);
		}

		public Message SerializeReply (MessageVersion messageVersion, object [] parameters, object result)
		{
			return impl.SerializeReply (messageVersion, parameters, result);
		}
	}

	internal abstract class BaseMessagesFormatter
		: IDispatchMessageFormatter, IClientMessageFormatter
	{
		MessageDescriptionCollection messages;
		bool isAsync;
		ParameterInfo [] requestMethodParams;
		ParameterInfo [] replyMethodParams;
		List<Type> operation_known_types = new List<Type> ();

		public BaseMessagesFormatter (MessageDescriptionCollection messages)
		{
			this.messages = messages;
		}

		public BaseMessagesFormatter (OperationDescription desc)
			: this (desc.Messages)
		{
			if (desc.SyncMethod != null)
			{
				isAsync = false;
				requestMethodParams = replyMethodParams = desc.SyncMethod.GetParameters ();
				return;
			}
			isAsync = true;
			ParameterInfo [] methodParams = desc.BeginMethod.GetParameters ();
			requestMethodParams = new ParameterInfo [methodParams.Length - 2];
			Array.Copy (methodParams, requestMethodParams, requestMethodParams.Length);
			methodParams = desc.EndMethod.GetParameters ();
			replyMethodParams = new ParameterInfo [methodParams.Length - 1];
			Array.Copy (methodParams, replyMethodParams, replyMethodParams.Length);
			operation_known_types.AddRange (desc.KnownTypes);
		}

		// FIXME: this should be refactored and eliminated.
		// XmlSerializerFormatAttribute and DataContractFormatAttribute
		// should be handled at ContractDescription.GetContract (to fill
		// IOperationBehavior for each).
		//
		// Fixing the issue above should also fix "Formatter is already filled at initial state" issue described in EndpointDispatcher.cs and ContractDescription.cs.
		public static BaseMessagesFormatter Create (OperationDescription desc)
		{
			MethodInfo attrProvider = desc.SyncMethod ?? desc.BeginMethod;
			object [] attrs;
			attrs = attrProvider.GetCustomAttributes (typeof (XmlSerializerFormatAttribute), false);
			if (attrs != null && attrs.Length > 0)
				return new XmlMessagesFormatter (desc, (XmlSerializerFormatAttribute) attrs [0]);

			attrs = attrProvider.GetCustomAttributes (typeof (DataContractFormatAttribute), false);
			DataContractFormatAttribute dataAttr = null;
			if (attrs != null && attrs.Length > 0)
				dataAttr = (DataContractFormatAttribute) attrs [0];
			return new DataContractMessagesFormatter (desc, dataAttr);
		}

		public IEnumerable<Type> OperationKnownTypes {
			get { return operation_known_types; }
		}

		protected abstract Message PartsToMessage (
			MessageDescription md, MessageVersion version, string action, object [] parts);
		protected abstract object [] MessageToParts (MessageDescription md, Message message);
		protected abstract Dictionary<MessageHeaderDescription,object> MessageToHeaderObjects (MessageDescription md, Message message);

		public Message SerializeRequest (
			MessageVersion version, object [] parameters)
		{
			MessageDescription md = null;
			foreach (MessageDescription mdi in messages)
				if (mdi.IsRequest)
					md = mdi;

			object [] parts = CreatePartsArray (md.Body);
			var headers = md.Headers.Count > 0 ? new Dictionary<MessageHeaderDescription,object> () : null;
			if (md.MessageType != null)
				MessageObjectToParts (md, parameters [0], headers, parts);
			else {
				int index = 0;
				foreach (ParameterInfo pi in requestMethodParams)
					if (!pi.IsOut)
						parts [index++] = parameters [pi.Position];
			}
			var msg = PartsToMessage (md, version, md.Action, parts);
			if (headers != null)
				foreach (var pair in headers)
					if (pair.Value != null)
						msg.Headers.Add (CreateHeader (pair.Key, pair.Value));
			return msg;
		}

		public Message SerializeReply (
			MessageVersion version, object [] parameters, object result)
		{
			// use_response_converter

			MessageDescription md = null;
			foreach (MessageDescription mdi in messages)
				if (!mdi.IsRequest)
					md = mdi;

			object [] parts = CreatePartsArray (md.Body);
			var headers = md.Headers.Count > 0 ? new Dictionary<MessageHeaderDescription,object> () : null;
			if (md.MessageType != null)
				MessageObjectToParts (md, result, headers, parts);
			else {
				if (HasReturnValue (md.Body))
					parts [0] = result;
				int index = ParamsOffset (md.Body);
				int paramsIdx = 0;
				foreach (ParameterInfo pi in replyMethodParams)
					if (pi.IsOut || pi.ParameterType.IsByRef)
				parts [index++] = parameters [paramsIdx++];
			}
			string action = version.Addressing == AddressingVersion.None ? null : md.Action;
			var msg = PartsToMessage (md, version, action, parts);
			if (headers != null)
				foreach (var pair in headers)
					if (pair.Value != null)
						msg.Headers.Add (CreateHeader (pair.Key, pair.Value));
			return msg;
		}

		MessageHeader CreateHeader (MessageHeaderDescription mh, object value)
		{
			return MessageHeader.CreateHeader (mh.Name, mh.Namespace, value, mh.MustUnderstand, mh.Actor, mh.Relay);
		}

		public void DeserializeRequest (Message message, object [] parameters)
		{
			string action = message.Headers.Action;
			MessageDescription md = messages.Find (action);
			if (md == null)
				throw new ActionNotSupportedException (String.Format ("Action '{0}' is not supported by this operation.", action));

			var headers = MessageToHeaderObjects (md, message);
			object [] parts = MessageToParts (md, message);
			if (md.MessageType != null) {
#if MOBILE
				parameters [0] = Activator.CreateInstance (md.MessageType);
#else
				parameters [0] = Activator.CreateInstance (md.MessageType, true);
#endif
				PartsToMessageObject (md, headers, parts, parameters [0]);
			}
			else
			{
				int index = 0;
				foreach (ParameterInfo pi in requestMethodParams)
					if (!pi.IsOut) {
						parameters [index] = parts [index];
						index++;
					}
			}
		}

		public object DeserializeReply (Message message, object [] parameters)
		{
			MessageDescription md = null;
			foreach (MessageDescription mdi in messages)
				if (!mdi.IsRequest)
					md = mdi;

			var headers = MessageToHeaderObjects (md, message);
			object [] parts = MessageToParts (md, message);
			if (md.MessageType != null) {
#if MOBILE
				object msgObject = Activator.CreateInstance (md.MessageType);
#else
				object msgObject = Activator.CreateInstance (md.MessageType, true);
#endif
				PartsToMessageObject (md, headers, parts, msgObject);
				return msgObject;
			}
			else {
				int index = ParamsOffset (md.Body);
				foreach (ParameterInfo pi in replyMethodParams) {
					if (pi.IsOut || pi.ParameterType.IsByRef)
						parameters [pi.Position] = parts [index++];
				}
				return HasReturnValue (md.Body) ? parts [0] : null;
			}
		}

		void PartsToMessageObject (MessageDescription md, Dictionary<MessageHeaderDescription,object> headers, object [] parts, object msgObject)
		{
			if (headers != null)
				foreach (var pair in headers) {
					var mi = pair.Key.MemberInfo;
					if (mi is FieldInfo)
						((FieldInfo) mi).SetValue (msgObject, pair.Value);
					else
						((PropertyInfo) mi).SetValue (msgObject, pair.Value, null);
				}

			var l = new List<MessagePartDescription> (md.Body.Parts);
			if (md.Body.ReturnValue != null)
				l.Add (md.Body.ReturnValue);
			foreach (MessagePartDescription partDesc in l)
				if (partDesc.MemberInfo is FieldInfo)
					((FieldInfo) partDesc.MemberInfo).SetValue (msgObject, parts [partDesc.Index]);
				else if (partDesc.MemberInfo is PropertyInfo)
					((PropertyInfo) partDesc.MemberInfo).SetValue (msgObject, parts [partDesc.Index], null);
				// otherwise, it could be null (in case of undefined return value in MessageContract)
		}

		void MessageObjectToParts (MessageDescription md, object msgObject, Dictionary<MessageHeaderDescription,object> headers, object [] parts)
		{
			foreach (var headDesc in md.Headers) {
				var mi = headDesc.MemberInfo;
				if (mi is FieldInfo)
					headers [headDesc] = ((FieldInfo) mi).GetValue (msgObject);
				else
					headers [headDesc] = ((PropertyInfo) mi).GetValue (msgObject, null);
			}

			var l = new List<MessagePartDescription> (md.Body.Parts);
			if (md.Body.ReturnValue != null)
				l.Add (md.Body.ReturnValue);
			foreach (MessagePartDescription partDesc in l) {
				if (partDesc.MemberInfo == null)
					continue;
				if (partDesc.MemberInfo is FieldInfo)
					parts [partDesc.Index] = ((FieldInfo) partDesc.MemberInfo).GetValue (msgObject);
				else
					parts [partDesc.Index] = ((PropertyInfo) partDesc.MemberInfo).GetValue (msgObject, null);
			}
		}

		internal static bool HasReturnValue (MessageBodyDescription desc)
		{
			return desc.ReturnValue != null && desc.ReturnValue.Type != typeof (void);
		}

		protected static int ParamsOffset (MessageBodyDescription desc)
		{
			return HasReturnValue (desc) ? 1 : 0;
		}

		protected static object [] CreatePartsArray (MessageBodyDescription desc)
		{
			if (HasReturnValue (desc))
				return new object [desc.Parts.Count + 1];
			return new object [desc.Parts.Count];
		}
	}

	class DataContractMessagesFormatter : BaseMessagesFormatter
	{
		DataContractFormatAttribute attr;
#if !MOBILE
		DataContractSerializerOperationBehavior serializerBehavior;
#endif

		public DataContractMessagesFormatter (OperationDescription desc, DataContractFormatAttribute attr)
			: base (desc)
		{
#if !MOBILE
			this.serializerBehavior = desc.Behaviors.Find<DataContractSerializerOperationBehavior>();
#endif
			this.attr = attr;
		}

		public DataContractMessagesFormatter (MessageDescriptionCollection messages, DataContractFormatAttribute attr)
			: base (messages)
		{
			this.attr = attr;
		}

		Dictionary<MessagePartDescription, XmlObjectSerializer> serializers
			= new Dictionary<MessagePartDescription,XmlObjectSerializer> ();

		protected override Message PartsToMessage (
			MessageDescription md, MessageVersion version, string action, object [] parts)
		{
			return Message.CreateMessage (version, action, new DataContractBodyWriter (md.Body, this, parts));
		}

		protected override Dictionary<MessageHeaderDescription,object> MessageToHeaderObjects (MessageDescription md, Message message)
		{
			if (message.IsEmpty || md.Headers.Count == 0)
				return null;
			
			var dic = new Dictionary<MessageHeaderDescription,object> ();
			for (int i = 0; i < message.Headers.Count; i++) {
				var r = message.Headers.GetReaderAtHeader (i);
				var mh = md.Headers.FirstOrDefault (h => h.Name == r.LocalName && h.Namespace == r.NamespaceURI);
				if (mh != null)
					dic [mh] = ReadHeaderObject (mh.Type, GetSerializer (mh), r);
			}
			return dic;
		}

		protected override object [] MessageToParts (
			MessageDescription md, Message message)
		{
			if (message.IsEmpty)
				return null;

			int offset = ParamsOffset (md.Body);
			object [] parts = CreatePartsArray (md.Body);

			XmlDictionaryReader r = message.GetReaderAtBodyContents ();
			if (md.Body.WrapperName != null)
				r.ReadStartElement (md.Body.WrapperName, md.Body.WrapperNamespace);

			for (r.MoveToContent (); r.NodeType == XmlNodeType.Element; r.MoveToContent ()) {
				XmlQualifiedName key = new XmlQualifiedName (r.LocalName, r.NamespaceURI);
				MessagePartDescription rv = md.Body.ReturnValue;
				if (rv != null && rv.Name == key.Name && rv.Namespace == key.Namespace && rv.Type != typeof (void))
					parts [0] = ReadMessagePart (md.Body.ReturnValue, r);
				else if (md.Body.Parts.Contains (key)) {
					MessagePartDescription p = md.Body.Parts [key];
					parts [p.Index + offset] = ReadMessagePart (p, r);
				}
				else // Skip unknown elements
					r.Skip ();
			}

			if (md.Body.WrapperName != null && !r.EOF)
				r.ReadEndElement ();

			return parts;
		}

		object ReadMessagePart (MessagePartDescription part, XmlDictionaryReader r)
		{
			if (part.Type == typeof (Stream))
				// FIXME: it seems TransferMode.Streamed* has different serialization than .Buffered. Need to differentiate serialization somewhere (not limited to here).
				return new MemoryStream (Convert.FromBase64String (r.ReadElementContentAsString (part.Name, part.Namespace)));
			else
				return GetSerializer (part).ReadObject (r);
		}

		XmlObjectSerializer GetSerializer (MessagePartDescription partDesc)
		{
			if (!serializers.ContainsKey (partDesc))
#if !MOBILE
				if (serializerBehavior != null)
					serializers [partDesc] = serializerBehavior.CreateSerializer(
						partDesc.Type, partDesc.Name, partDesc.Namespace, OperationKnownTypes as IList<Type>);
				else
#endif
					serializers [partDesc] = new DataContractSerializer (
						partDesc.Type, partDesc.Name, partDesc.Namespace, OperationKnownTypes);
			return serializers [partDesc];
		}

		object ReadHeaderObject (Type type, XmlObjectSerializer serializer, XmlDictionaryReader reader)
		{
			// FIXME: it's a nasty workaround just to avoid UniqueId output as a string.
			// Seealso MessageHeader.DefaultMessageHeader.OnWriteHeaderContents().
			// Note that msg.Headers.GetHeader<UniqueId> () simply fails (on .NET too) and it is useless. The API is lame by design.
			if (type == typeof (UniqueId))
				return new UniqueId (reader.ReadElementContentAsString ());
			else
				return serializer.ReadObject (reader);
		}

		class DataContractBodyWriter : BodyWriter
		{
			MessageBodyDescription desc;
			object [] parts;
			DataContractMessagesFormatter parent;

			public DataContractBodyWriter (MessageBodyDescription desc, DataContractMessagesFormatter parent, object [] parts)
				: base (false)
			{
				this.desc = desc;
				this.parent = parent;
				this.parts = parts;
			}

			protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
			{
				int offset = HasReturnValue (desc) ? 1 : 0;
				if (desc.WrapperName != null)
					writer.WriteStartElement (desc.WrapperName, desc.WrapperNamespace);
				if (HasReturnValue (desc))
					WriteMessagePart (writer, desc, desc.ReturnValue, parts [0]);
				foreach (MessagePartDescription partDesc in desc.Parts)
					WriteMessagePart (writer, desc, partDesc, parts [partDesc.Index + offset]);
				if (desc.WrapperName != null)
					writer.WriteEndElement ();
			}

			void WriteMessagePart (
				XmlDictionaryWriter writer, MessageBodyDescription desc, MessagePartDescription partDesc, object obj)
			{
				// FIXME: it seems TransferMode.Streamed* has different serialization than .Buffered. Need to differentiate serialization somewhere (not limited to here).
				if (partDesc.Type == typeof (Stream)) {
					writer.WriteStartElement (partDesc.Name, partDesc.Namespace);
					writer.WriteValue (new StreamProvider ((Stream) obj));
					writer.WriteEndElement ();
				}
				else
					parent.GetSerializer (partDesc).WriteObject (writer, obj);
			}
		}
		
		class StreamProvider : IStreamProvider
		{
			Stream s;
			bool busy;
			
			public StreamProvider (Stream s)
			{
				this.s = s;
			}
			
			public Stream GetStream ()
			{
				if (busy)
					throw new InvalidOperationException ("Stream is already in use.");
				busy = true;
				return s;
			}
			
			public void ReleaseStream (Stream stream)
			{
				if (stream == null)
					throw new ArgumentNullException ("stream");
				if (this.s != stream)
					throw new ArgumentException ("Incorrect parameter stream");
				busy = false;
			}
		}
	}
}