summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth/Messaging/Reflection/MessageDescription.cs
blob: 9442f153085610a7a6bb7742189c403cc8ed0156 (plain)
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
//-----------------------------------------------------------------------
// <copyright file="MessageDescription.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOAuth.Messaging.Reflection {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Reflection;
	using System.Globalization;
	using System.Diagnostics;

	internal class MessageDescription {
		private static Dictionary<Type, MessageDescription> reflectedMessageTypes = new Dictionary<Type,MessageDescription>();
		private Type messageType;
		private Dictionary<string, MessagePart> mapping;

		private MessageDescription(Type messageType) {
			Debug.Assert(messageType != null, "messageType == null");

			if (!typeof(IProtocolMessage).IsAssignableFrom(messageType)) {
				throw new ArgumentException(string.Format(
					CultureInfo.CurrentCulture,
					MessagingStrings.UnexpectedType,
					typeof(IProtocolMessage),
					messageType));
			}

			this.messageType = messageType;
			this.ReflectMessageType();
		}

		internal static MessageDescription Get(Type messageType) {
			if (messageType == null) {
				throw new ArgumentNullException("messageType");
			}

			MessageDescription result;
			if (!reflectedMessageTypes.TryGetValue(messageType, out result)) {
				lock (reflectedMessageTypes) {
					if (!reflectedMessageTypes.TryGetValue(messageType, out result)) {
						reflectedMessageTypes[messageType] = result = new MessageDescription(messageType);
					}
				}
			}

			return result;
		}

		internal IDictionary<string, MessagePart> Mapping {
			get { return this.mapping; }
		}

		internal void ReflectMessageType() {
			this.mapping = new Dictionary<string, MessagePart>();

			Type currentType = this.messageType;
			do {
				foreach (MemberInfo member in currentType.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) {
					if (member is PropertyInfo || member is FieldInfo) {
						MessagePartAttribute partAttribute = member.GetCustomAttributes(typeof(MessagePartAttribute), true).OfType<MessagePartAttribute>().FirstOrDefault();
						if (partAttribute != null) {
							MessagePart part = new MessagePart(member, partAttribute);
							this.mapping.Add(part.Name, part);
						}
					}
				}
				currentType = currentType.BaseType;
			} while (currentType != null);
		}

		/// <summary>
		/// Verifies that a given set of keys include all the required parameters
		/// for this message type or throws an exception.
		/// </summary>
		/// <exception cref="ProtocolException">Thrown when required parts of a message are not in <paramref name="keys"/></exception>
		internal void EnsureRequiredMessagePartsArePresent(IEnumerable<string> keys) {
			var missingKeys = (from part in Mapping.Values
							   where part.IsRequired && !keys.Contains(part.Name)
							   select part.Name).ToArray();
			if (missingKeys.Length > 0) {
				throw new ProtocolException(string.Format(CultureInfo.CurrentCulture,
					MessagingStrings.RequiredParametersMissing,
					this.messageType.FullName,
					string.Join(", ", missingKeys)));
			}
		}
	}
}