summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth/Messaging/Reflection
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2008-09-17 17:49:14 -0700
committerAndrew <andrewarnott@gmail.com>2008-09-17 17:49:14 -0700
commitea7cae52f40770d1023362dadd234b8038d9e68b (patch)
tree2bf9a9bfa01de5887be1336f4a9ece33cbf171e3 /src/DotNetOAuth/Messaging/Reflection
parent2dcfb4843722237aa214294b59ed9be782ea0cec (diff)
downloadDotNetOpenAuth-ea7cae52f40770d1023362dadd234b8038d9e68b.zip
DotNetOpenAuth-ea7cae52f40770d1023362dadd234b8038d9e68b.tar.gz
DotNetOpenAuth-ea7cae52f40770d1023362dadd234b8038d9e68b.tar.bz2
Added MessageDictionary and supporting classes and tests.
Very little documentation is there until I can prove the idea works to solve our signing design problem.
Diffstat (limited to 'src/DotNetOAuth/Messaging/Reflection')
-rw-r--r--src/DotNetOAuth/Messaging/Reflection/MessageDescription.cs56
-rw-r--r--src/DotNetOAuth/Messaging/Reflection/MessageDictionary.cs195
-rw-r--r--src/DotNetOAuth/Messaging/Reflection/MessagePart.cs91
-rw-r--r--src/DotNetOAuth/Messaging/Reflection/ValueMapping.cs27
4 files changed, 369 insertions, 0 deletions
diff --git a/src/DotNetOAuth/Messaging/Reflection/MessageDescription.cs b/src/DotNetOAuth/Messaging/Reflection/MessageDescription.cs
new file mode 100644
index 0000000..ef73a31
--- /dev/null
+++ b/src/DotNetOAuth/Messaging/Reflection/MessageDescription.cs
@@ -0,0 +1,56 @@
+//-----------------------------------------------------------------------
+// <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;
+
+ internal class MessageDescription {
+ private Type messageType;
+ private Dictionary<string, MessagePart> mapping;
+
+ internal MessageDescription(Type messageType) {
+ if (messageType == null) {
+ throw new ArgumentNullException("messageType");
+ }
+
+ if (!typeof(IProtocolMessage).IsAssignableFrom(messageType)) {
+ throw new ArgumentOutOfRangeException(); // TODO: better message
+ }
+
+ this.messageType = messageType;
+ this.ReflectMessageType();
+ }
+
+ internal Type MessageType {
+ get { return this.messageType; }
+ }
+
+ 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);
+ }
+ }
+}
diff --git a/src/DotNetOAuth/Messaging/Reflection/MessageDictionary.cs b/src/DotNetOAuth/Messaging/Reflection/MessageDictionary.cs
new file mode 100644
index 0000000..eca7a9b
--- /dev/null
+++ b/src/DotNetOAuth/Messaging/Reflection/MessageDictionary.cs
@@ -0,0 +1,195 @@
+//-----------------------------------------------------------------------
+// <copyright file="MessageDictionary.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Messaging.Reflection {
+ using System;
+ using System.Collections;
+ using System.Collections.Generic;
+
+ /// <summary>
+ /// Wraps an <see cref="IProtocolMessage"/> instance in a dictionary that
+ /// provides access to both well-defined message properties and "extra"
+ /// name/value pairs that have no properties associated with them.
+ /// </summary>
+ internal class MessageDictionary : IDictionary<string, string> {
+ private IProtocolMessage message;
+
+ private MessageDescription description;
+
+ internal MessageDictionary(IProtocolMessage message) {
+ if (message == null) {
+ throw new ArgumentNullException("message");
+ }
+
+ this.message = message;
+ this.description = new MessageDescription(message.GetType());
+ }
+
+ #region IDictionary<string,string> Members
+
+ void IDictionary<string, string>.Add(string key, string value) {
+ MessagePart part;
+ if (this.description.Mapping.TryGetValue(key, out part)) {
+ if (part.GetValue(this.message) != null) {
+ throw new ArgumentException(MessagingStrings.KeyAlreadyExists);
+ }
+ part.SetValue(this.message, value);
+ } else {
+ this.message.ExtraData.Add(key, value);
+ }
+ }
+
+ bool IDictionary<string, string>.ContainsKey(string key) {
+ return this.message.ExtraData.ContainsKey(key) || this.description.Mapping.ContainsKey(key);
+ }
+
+ ICollection<string> IDictionary<string, string>.Keys {
+ get {
+ string[] keys = new string[this.message.ExtraData.Count + this.description.Mapping.Count];
+ int i = 0;
+ foreach (string key in this.description.Mapping.Keys) {
+ keys[i++] = key;
+ }
+
+ foreach (string key in this.message.ExtraData.Keys) {
+ keys[i++] = key;
+ }
+
+ return keys;
+ }
+ }
+
+ bool IDictionary<string, string>.Remove(string key) {
+ if (this.message.ExtraData.Remove(key)) {
+ return true;
+ } else {
+ MessagePart part;
+ if (this.description.Mapping.TryGetValue(key, out part)) {
+ if (part.GetValue(this.message) != null) {
+ part.SetValue(this.message, null);
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ bool IDictionary<string, string>.TryGetValue(string key, out string value) {
+ MessagePart part;
+ if (this.description.Mapping.TryGetValue(key, out part)) {
+ value = part.GetValue(this.message);
+ return true;
+ }
+ return this.message.ExtraData.TryGetValue(key, out value);
+ }
+
+ ICollection<string> IDictionary<string, string>.Values {
+ get {
+ string[] values = new string[this.message.ExtraData.Count + this.description.Mapping.Count];
+ int i = 0;
+ foreach (MessagePart part in this.description.Mapping.Values) {
+ values[i++] = part.GetValue(this.message);
+ }
+
+ foreach (string value in this.message.ExtraData.Values) {
+ values[i++] = value;
+ }
+
+ return values;
+ }
+ }
+
+ string IDictionary<string, string>.this[string key] {
+ get {
+ MessagePart part;
+ if (this.description.Mapping.TryGetValue(key, out part)) {
+ return part.GetValue(this.message);
+ } else {
+ return this.message.ExtraData[key];
+ }
+ }
+
+ set {
+ MessagePart part;
+ if (this.description.Mapping.TryGetValue(key, out part)) {
+ part.SetValue(this.message, value);
+ } else {
+ this.message.ExtraData[key] = value;
+ }
+ }
+ }
+
+ #endregion
+
+ #region ICollection<KeyValuePair<string,string>> Members
+
+ void ICollection<KeyValuePair<string, string>>.Add(KeyValuePair<string, string> item) {
+ ((IDictionary<string, string>)this).Add(item.Key, item.Value);
+ }
+
+ void ICollection<KeyValuePair<string, string>>.Clear() {
+ foreach (string key in ((IDictionary<string, string>)this).Keys) {
+ ((IDictionary<string, string>)this).Remove(key);
+ }
+ }
+
+ bool ICollection<KeyValuePair<string, string>>.Contains(KeyValuePair<string, string> item) {
+ MessagePart part;
+ if (this.description.Mapping.TryGetValue(item.Key, out part)) {
+ return string.Equals(part.GetValue(this.message), item.Value, StringComparison.Ordinal);
+ } else {
+ return this.message.ExtraData.Contains(item);
+ }
+ }
+
+ void ICollection<KeyValuePair<string, string>>.CopyTo(KeyValuePair<string, string>[] array, int arrayIndex) {
+ foreach (var pair in (IDictionary<string, string>)this) {
+ array[arrayIndex++] = pair;
+ }
+ }
+
+ int ICollection<KeyValuePair<string, string>>.Count {
+ get { return this.description.Mapping.Count + this.message.ExtraData.Count; }
+ }
+
+ bool ICollection<KeyValuePair<string, string>>.IsReadOnly {
+ get { return false; }
+ }
+
+ bool ICollection<KeyValuePair<string, string>>.Remove(KeyValuePair<string, string> item) {
+ // We use contains because that checks that the value is equal as well.
+ if (((ICollection<KeyValuePair<string, string>>)this).Contains(item)) {
+ ((IDictionary<string, string>)this).Remove(item.Key);
+ return true;
+ }
+ return false;
+ }
+
+ #endregion
+
+ #region IEnumerable<KeyValuePair<string,string>> Members
+
+ IEnumerator<KeyValuePair<string, string>> IEnumerable<KeyValuePair<string, string>>.GetEnumerator() {
+ foreach (MessagePart part in this.description.Mapping.Values) {
+ yield return new KeyValuePair<string, string>(part.Name, part.GetValue(this.message));
+ }
+
+ foreach (var pair in this.message.ExtraData) {
+ yield return pair;
+ }
+ }
+
+ #endregion
+
+ #region IEnumerable Members
+
+ IEnumerator System.Collections.IEnumerable.GetEnumerator() {
+ return ((IEnumerable<KeyValuePair<string, string>>)this).GetEnumerator();
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs b/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs
new file mode 100644
index 0000000..09f959b
--- /dev/null
+++ b/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs
@@ -0,0 +1,91 @@
+//-----------------------------------------------------------------------
+// <copyright file="MessagePart.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Messaging.Reflection {
+ using System;
+ using System.Collections.Generic;
+ using System.Net.Security;
+ using System.Reflection;
+
+ internal class MessagePart {
+ private static readonly Dictionary<Type, ValueMapping> converters = new Dictionary<Type, ValueMapping>();
+
+ private ValueMapping converter;
+
+ private PropertyInfo property;
+
+ private FieldInfo field;
+
+ static MessagePart() {
+ Map<Uri>(uri => uri.AbsoluteUri, str => new Uri(str));
+ }
+
+ internal MessagePart(MemberInfo member, MessagePartAttribute attribute) {
+ if (member == null) {
+ throw new ArgumentNullException("member");
+ }
+
+ this.field = member as FieldInfo;
+ this.property = member as PropertyInfo;
+ if (this.field == null && this.property == null) {
+ throw new ArgumentOutOfRangeException("member"); // TODO: add descriptive message
+ }
+
+ if (attribute == null) {
+ throw new ArgumentNullException("attribute");
+ }
+
+ this.Name = attribute.Name ?? member.Name;
+ this.Signed = attribute.Signed;
+ this.IsRequired = !attribute.Optional;
+
+ if (!converters.TryGetValue(member.DeclaringType, out this.converter)) {
+ Type memberDeclaredType = (this.field != null) ? this.field.FieldType : this.property.PropertyType;
+ this.converter = new ValueMapping(
+ obj => obj != null ? obj.ToString() : null,
+ str => str != null ? Convert.ChangeType(str, memberDeclaredType) : null);
+ }
+ }
+
+ internal string Name { get; set; }
+
+ internal ProtectionLevel Signed { get; set; }
+
+ internal bool IsRequired { get; set; }
+
+ internal object ToValue(string value) {
+ return this.converter.StringToValue(value);
+ }
+
+ internal string ToString(object value) {
+ return this.converter.ValueToString(value);
+ }
+
+ internal void SetValue(IProtocolMessage message, string value) {
+ if (this.property != null) {
+ this.property.SetValue(message, this.ToValue(value), null);
+ } else {
+ this.field.SetValue(message, this.ToValue(value));
+ }
+ }
+
+ internal string GetValue(IProtocolMessage message) {
+ if (this.property != null) {
+ return this.ToString(this.property.GetValue(message, null));
+ } else {
+ return this.ToString(this.field.GetValue(message));
+ }
+ }
+
+ private static void Map<T>(Func<T, string> toString, Func<string, T> toValue) where T : class {
+ converters.Add(
+ typeof(T),
+ new ValueMapping(
+ obj => obj != null ? toString((T)obj) : null,
+ str => str != null ? toValue(str) : null));
+ }
+ }
+}
diff --git a/src/DotNetOAuth/Messaging/Reflection/ValueMapping.cs b/src/DotNetOAuth/Messaging/Reflection/ValueMapping.cs
new file mode 100644
index 0000000..2371b49
--- /dev/null
+++ b/src/DotNetOAuth/Messaging/Reflection/ValueMapping.cs
@@ -0,0 +1,27 @@
+//-----------------------------------------------------------------------
+// <copyright file="ValueMapping.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Messaging.Reflection {
+ using System;
+
+ internal struct ValueMapping {
+ internal Func<object, string> ValueToString;
+ internal Func<string, object> StringToValue;
+
+ internal ValueMapping(Func<object, string> toString, Func<string, object> toValue) {
+ if (toString == null) {
+ throw new ArgumentNullException("toString");
+ }
+
+ if (toValue == null) {
+ throw new ArgumentNullException("toValue");
+ }
+
+ this.ValueToString = toString;
+ this.StringToValue = toValue;
+ }
+ }
+}