diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-09-18 16:17:42 -0700 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-09-18 16:17:42 -0700 |
commit | 11615a19bad90741c7a755eaf741b8683992c9ff (patch) | |
tree | 5ad7b033a81cf74192e31d496bdeb76401a6af44 /src/DotNetOAuth/Messaging | |
parent | 95e418aa02296a93587c4e07b0f3eec8cd379416 (diff) | |
download | DotNetOpenAuth-11615a19bad90741c7a755eaf741b8683992c9ff.zip DotNetOpenAuth-11615a19bad90741c7a755eaf741b8683992c9ff.tar.gz DotNetOpenAuth-11615a19bad90741c7a755eaf741b8683992c9ff.tar.bz2 |
Fixed a few bugs and got all tests passing.
Diffstat (limited to 'src/DotNetOAuth/Messaging')
-rw-r--r-- | src/DotNetOAuth/Messaging/Channel.cs | 18 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs | 9 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/MessagingStrings.resx | 3 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/Reflection/MessagePart.cs | 32 |
4 files changed, 57 insertions, 5 deletions
diff --git a/src/DotNetOAuth/Messaging/Channel.cs b/src/DotNetOAuth/Messaging/Channel.cs index e3b7a73..fd7e676 100644 --- a/src/DotNetOAuth/Messaging/Channel.cs +++ b/src/DotNetOAuth/Messaging/Channel.cs @@ -15,6 +15,7 @@ namespace DotNetOAuth.Messaging { using System.Net;
using System.Text;
using System.Web;
+ using DotNetOAuth.Messaging.Reflection;
/// <summary>
/// Manages sending direct messages to a remote party and receiving responses.
@@ -525,10 +526,21 @@ namespace DotNetOAuth.Messaging { throw new UnprotectedMessageException(message, appliedProtection);
}
- // TODO: call MessagePart.IsValidValue()
-
-
+ EnsureValidMessageParts(message);
message.EnsureValidMessage();
}
+
+ private void EnsureValidMessageParts(IProtocolMessage message) {
+ Debug.Assert(message != null, "message == null");
+ // TODO: call MessagePart.IsValidValue()
+ MessageDescription description = new MessageDescription(message.GetType());
+ List<MessagePart> invalidParts = description.Mapping.Values.Where(part => !part.IsValidValue(message)).ToList();
+ if (invalidParts.Count > 0) {
+ throw new ProtocolException(string.Format(
+ CultureInfo.CurrentCulture,
+ MessagingStrings.InvalidMessageParts,
+ string.Join(", ", invalidParts.Select(part => part.Name).ToArray())));
+ }
+ }
}
}
diff --git a/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs b/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs index 7852ea2..b8183c8 100644 --- a/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs +++ b/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs @@ -151,6 +151,15 @@ namespace DotNetOAuth.Messaging { }
/// <summary>
+ /// Looks up a localized string similar to Some part(s) of the message have invalid values: {0}.
+ /// </summary>
+ internal static string InvalidMessageParts {
+ get {
+ return ResourceManager.GetString("InvalidMessageParts", resourceCulture);
+ }
+ }
+
+ /// <summary>
/// Looks up a localized string similar to An item with the same key has already been added..
/// </summary>
internal static string KeyAlreadyExists {
diff --git a/src/DotNetOAuth/Messaging/MessagingStrings.resx b/src/DotNetOAuth/Messaging/MessagingStrings.resx index 121f9b8..b6b98d1 100644 --- a/src/DotNetOAuth/Messaging/MessagingStrings.resx +++ b/src/DotNetOAuth/Messaging/MessagingStrings.resx @@ -147,6 +147,9 @@ <data name="InsufficentMessageProtection" xml:space="preserve">
<value>The message required protections {0} but the channel could only apply {1}.</value>
</data>
+ <data name="InvalidMessageParts" xml:space="preserve">
+ <value>Some part(s) of the message have invalid values: {0}</value>
+ </data>
<data name="KeyAlreadyExists" xml:space="preserve">
<value>An item with the same key has already been added.</value>
</data>
diff --git a/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs b/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs index 2005370..b3c66d0 100644 --- a/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs +++ b/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs @@ -9,6 +9,8 @@ namespace DotNetOAuth.Messaging.Reflection { using System.Collections.Generic;
using System.Net.Security;
using System.Reflection;
+ using System.Xml;
+ using System.Globalization;
internal class MessagePart {
private static readonly Dictionary<Type, ValueMapping> converters = new Dictionary<Type, ValueMapping>();
@@ -25,6 +27,7 @@ namespace DotNetOAuth.Messaging.Reflection { static MessagePart() {
Map<Uri>(uri => uri.AbsoluteUri, str => new Uri(str));
+ Map<DateTime>(dt => XmlConvert.ToString(dt, XmlDateTimeSerializationMode.Utc), str => DateTime.Parse(str));
}
internal MessagePart(MemberInfo member, MessagePartAttribute attribute) {
@@ -53,6 +56,9 @@ namespace DotNetOAuth.Messaging.Reflection { obj => obj != null ? obj.ToString() : null,
str => str != null ? Convert.ChangeType(str, memberDeclaredType) : null);
}
+
+ // Validate a sane combination of settings
+ ValidateSettings();
}
internal string Name { get; set; }
@@ -109,12 +115,34 @@ namespace DotNetOAuth.Messaging.Reflection { }
}
- private static void Map<T>(Func<T, string> toString, Func<string, T> toValue) where T : class {
+ private static void Map<T>(Func<T, string> toString, Func<string, T> toValue) {
converters.Add(
typeof(T),
new ValueMapping(
obj => obj != null ? toString((T)obj) : null,
- str => str != null ? toValue(str) : null));
+ str => str != null ? toValue(str) : default(T)));
+ }
+
+ private void ValidateSettings() {
+ // An optional tag on a non-nullable value type is a contradiction.
+ if (!this.IsRequired && IsNonNullableValueType(this.memberDeclaredType)) {
+ MemberInfo member = (MemberInfo)this.field ?? this.property;
+ throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
+ "Invalid combination: {0} on message type {1} is a non-nullable value type but is marked as optional.",
+ member.Name, member.DeclaringType));
+ }
+ }
+
+ private static bool IsNonNullableValueType(Type type) {
+ if (!type.IsValueType) {
+ return false;
+ }
+
+ if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) {
+ return false;
+ }
+
+ return true;
}
}
}
|