diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/DotNetOAuth.Test/Messaging/ChannelTests.cs | 9 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Mocks/TestBadChannel.cs | 8 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Mocks/TestChannel.cs | 12 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Mocks/TestDirectedMessage.cs | 8 | ||||
-rw-r--r-- | src/DotNetOAuth/DotNetOAuth.csproj | 2 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/Channel.cs | 76 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/IProtocolMessage.cs | 1 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs | 18 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/MessagingStrings.resx | 6 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/ProtocolException.cs | 135 | ||||
-rw-r--r-- | src/DotNetOAuth/OAuthChannel.cs | 17 | ||||
-rw-r--r-- | src/DotNetOAuth/ProtocolException.cs | 45 |
12 files changed, 201 insertions, 136 deletions
diff --git a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs index 6750739..26155cb 100644 --- a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs +++ b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs @@ -53,14 +53,9 @@ namespace DotNetOAuth.Test.Messaging { this.channel.Send(null);
}
- [TestMethod, ExpectedException(typeof(ArgumentNullException))]
- public void SendNull2() {
- this.channel.Send(null, new TestDirectedMessage());
- }
-
[TestMethod]
public void SendIndirectMessage301Get() {
- IProtocolMessage message = new TestDirectedMessage {
+ IProtocolMessage message = new TestDirectedMessage(MessageTransport.Indirect) {
Age = 15,
Name = "Andrew",
Location = new Uri("http://host/path"),
@@ -80,7 +75,7 @@ namespace DotNetOAuth.Test.Messaging { // We craft a very large message to force fallback to form POST.
// We'll also stick some HTML reserved characters in the string value
// to test proper character escaping.
- var message = new TestDirectedMessage {
+ var message = new TestDirectedMessage(MessageTransport.Indirect) {
Age = 15,
Name = "c<b" + new string('a', 10 * 1024),
Location = new Uri("http://host/path"),
diff --git a/src/DotNetOAuth.Test/Mocks/TestBadChannel.cs b/src/DotNetOAuth.Test/Mocks/TestBadChannel.cs index 20fe03a..4c06980 100644 --- a/src/DotNetOAuth.Test/Mocks/TestBadChannel.cs +++ b/src/DotNetOAuth.Test/Mocks/TestBadChannel.cs @@ -30,13 +30,5 @@ namespace DotNetOAuth.Test.Mocks { protected override void SendDirectMessageResponse(IProtocolMessage response) {
throw new NotImplementedException();
}
-
- protected override void ReportErrorToUser(ProtocolException exception) {
- throw new NotImplementedException();
- }
-
- protected override void ReportErrorAsDirectResponse(ProtocolException exception) {
- throw new NotImplementedException();
- }
}
}
diff --git a/src/DotNetOAuth.Test/Mocks/TestChannel.cs b/src/DotNetOAuth.Test/Mocks/TestChannel.cs index 632257e..359019f 100644 --- a/src/DotNetOAuth.Test/Mocks/TestChannel.cs +++ b/src/DotNetOAuth.Test/Mocks/TestChannel.cs @@ -17,23 +17,15 @@ namespace DotNetOAuth.Test.Mocks { }
protected internal override IProtocolMessage Request(IDirectedProtocolMessage request) {
- throw new NotImplementedException();
+ throw new NotImplementedException("Request");
}
protected internal override IProtocolMessage ReadFromResponse(System.IO.Stream responseStream) {
- throw new NotImplementedException();
+ throw new NotImplementedException("ReadFromResponse");
}
protected override void SendDirectMessageResponse(IProtocolMessage response) {
throw new NotImplementedException("SendDirectMessageResponse");
}
-
- protected override void ReportErrorToUser(ProtocolException exception) {
- throw new NotImplementedException();
- }
-
- protected override void ReportErrorAsDirectResponse(ProtocolException exception) {
- throw new NotImplementedException();
- }
}
}
diff --git a/src/DotNetOAuth.Test/Mocks/TestDirectedMessage.cs b/src/DotNetOAuth.Test/Mocks/TestDirectedMessage.cs index 6972624..0e600b5 100644 --- a/src/DotNetOAuth.Test/Mocks/TestDirectedMessage.cs +++ b/src/DotNetOAuth.Test/Mocks/TestDirectedMessage.cs @@ -11,6 +11,12 @@ namespace DotNetOAuth.Test.Mocks { [DataContract(Namespace = Protocol.DataContractNamespaceV10)]
internal class TestDirectedMessage : IDirectedProtocolMessage {
+ private MessageTransport transport;
+
+ internal TestDirectedMessage(MessageTransport transport) {
+ this.transport = transport;
+ }
+
[DataMember(Name = "age", IsRequired = true)]
public int Age { get; set; }
[DataMember]
@@ -33,7 +39,7 @@ namespace DotNetOAuth.Test.Mocks { }
MessageTransport IProtocolMessage.Transport {
- get { return MessageTransport.Direct; }
+ get { return this.transport; }
}
void IProtocolMessage.EnsureValidMessage() {
diff --git a/src/DotNetOAuth/DotNetOAuth.csproj b/src/DotNetOAuth/DotNetOAuth.csproj index 4db5eb0..b710061 100644 --- a/src/DotNetOAuth/DotNetOAuth.csproj +++ b/src/DotNetOAuth/DotNetOAuth.csproj @@ -90,7 +90,7 @@ <Compile Include="Messaging\MessageScheme.cs" />
<Compile Include="Messaging\MessageTransport.cs" />
<Compile Include="OAuthMessageTypeProvider.cs" />
- <Compile Include="ProtocolException.cs" />
+ <Compile Include="Messaging\ProtocolException.cs" />
<Compile Include="Messaging\MessageSerializer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Util.cs" />
diff --git a/src/DotNetOAuth/Messaging/Channel.cs b/src/DotNetOAuth/Messaging/Channel.cs index 2c5e225..2ab3636 100644 --- a/src/DotNetOAuth/Messaging/Channel.cs +++ b/src/DotNetOAuth/Messaging/Channel.cs @@ -7,6 +7,8 @@ namespace DotNetOAuth.Messaging {
using System;
using System.Collections.Generic;
+ using System.Diagnostics;
+ using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
@@ -93,44 +95,33 @@ namespace DotNetOAuth.Messaging { /// </summary>
/// <param name="message">The one-way message to send</param>
internal void Send(IProtocolMessage message) {
- this.Send(message, null);
- }
-
- /// <summary>
- /// Queues an indirect message (either a request or response)
- /// or direct message response for transmission to a remote party.
- /// </summary>
- /// <param name="message">The one-way message to send</param>
- /// <param name="inResponseTo">
- /// If <paramref name="message"/> is a response to an incoming message, this is the incoming message.
- /// This is useful for error scenarios in deciding just how to send the response message.
- /// May be null.
- /// </param>
- internal void Send(IProtocolMessage message, IProtocolMessage inResponseTo) {
if (message == null) {
throw new ArgumentNullException("message");
}
- var directedMessage = message as IDirectedProtocolMessage;
- if (directedMessage == null) {
- // This is a response to a direct message.
- this.SendDirectMessageResponse(message);
- } else {
- if (directedMessage.Recipient != null) {
- // This is an indirect message request or reply.
- this.SendIndirectMessage(directedMessage);
- } else {
- ProtocolException exception = message as ProtocolException;
- if (exception != null) {
- if (inResponseTo is IDirectedProtocolMessage) {
- this.ReportErrorAsDirectResponse(exception);
- } else {
- this.ReportErrorToUser(exception);
- }
- } else {
- throw new InvalidOperationException(MessagingStrings.DirectedMessageMissingRecipient);
+ switch (message.Transport) {
+ case MessageTransport.Direct:
+ // This is a response to a direct message.
+ this.SendDirectMessageResponse(message);
+ break;
+ case MessageTransport.Indirect:
+ var directedMessage = message as IDirectedProtocolMessage;
+ if (directedMessage == null) {
+ throw new ArgumentException(
+ string.Format(
+ CultureInfo.CurrentCulture,
+ MessagingStrings.IndirectMessagesMustImplementIDirectedProtocolMessage,
+ typeof(IDirectedProtocolMessage).FullName),
+ "message");
}
- }
+ if (directedMessage.Recipient == null) {
+ throw new ArgumentException(MessagingStrings.DirectedMessageMissingRecipient, "message");
+ }
+ this.SendIndirectMessage(directedMessage);
+ break;
+ default:
+ Debug.Fail("Unrecogized MessageTransport value.");
+ throw new ArgumentException();
}
}
@@ -249,6 +240,9 @@ namespace DotNetOAuth.Messaging { if (message == null) {
throw new ArgumentNullException("message");
}
+ if (message.Recipient == null) {
+ throw new ArgumentException(MessagingStrings.DirectedMessageMissingRecipient, "message");
+ }
if (fields == null) {
throw new ArgumentNullException("fields");
}
@@ -279,6 +273,9 @@ namespace DotNetOAuth.Messaging { if (message == null) {
throw new ArgumentNullException("message");
}
+ if (message.Recipient == null) {
+ throw new ArgumentException(MessagingStrings.DirectedMessageMissingRecipient, "message");
+ }
if (fields == null) {
throw new ArgumentNullException("fields");
}
@@ -319,19 +316,6 @@ namespace DotNetOAuth.Messaging { protected abstract void SendDirectMessageResponse(IProtocolMessage response);
/// <summary>
- /// Reports an error to the user via the user agent.
- /// </summary>
- /// <param name="exception">The error information.</param>
- protected abstract void ReportErrorToUser(ProtocolException exception);
-
- /// <summary>
- /// Sends an error result directly to the calling remote party according to the
- /// rules of the protocol.
- /// </summary>
- /// <param name="exception">The error information.</param>
- protected abstract void ReportErrorAsDirectResponse(ProtocolException exception);
-
- /// <summary>
/// Calculates a fairly accurate estimation on the size of a message that contains
/// a given set of fields.
/// </summary>
diff --git a/src/DotNetOAuth/Messaging/IProtocolMessage.cs b/src/DotNetOAuth/Messaging/IProtocolMessage.cs index 3d9f82e..76079c0 100644 --- a/src/DotNetOAuth/Messaging/IProtocolMessage.cs +++ b/src/DotNetOAuth/Messaging/IProtocolMessage.cs @@ -22,7 +22,6 @@ namespace DotNetOAuth.Messaging { /// <summary>
/// Gets whether this is a direct or indirect message.
/// </summary>
- [Obsolete("Are we using this anywhere?")]
MessageTransport Transport { get; }
/// <summary>
diff --git a/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs b/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs index 98e8cc6..cf722bd 100644 --- a/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs +++ b/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs @@ -88,6 +88,24 @@ namespace DotNetOAuth.Messaging { }
/// <summary>
+ /// Looks up a localized string similar to This exception was not constructed with a root request message that caused it..
+ /// </summary>
+ internal static string ExceptionNotConstructedForTransit {
+ get {
+ return ResourceManager.GetString("ExceptionNotConstructedForTransit", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Messages that indicate indirect transport must implement the {0} interface..
+ /// </summary>
+ internal static string IndirectMessagesMustImplementIDirectedProtocolMessage {
+ get {
+ return ResourceManager.GetString("IndirectMessagesMustImplementIDirectedProtocolMessage", resourceCulture);
+ }
+ }
+
+ /// <summary>
/// Looks up a localized string similar to A message response is already queued for sending in the response stream..
/// </summary>
internal static string QueuedMessageResponseAlreadyExists {
diff --git a/src/DotNetOAuth/Messaging/MessagingStrings.resx b/src/DotNetOAuth/Messaging/MessagingStrings.resx index 2125e15..abc913b 100644 --- a/src/DotNetOAuth/Messaging/MessagingStrings.resx +++ b/src/DotNetOAuth/Messaging/MessagingStrings.resx @@ -126,6 +126,12 @@ <data name="ErrorInRequestReplyMessage" xml:space="preserve">
<value>Error occurred while sending a direct message or gettings the response.</value>
</data>
+ <data name="ExceptionNotConstructedForTransit" xml:space="preserve">
+ <value>This exception was not constructed with a root request message that caused it.</value>
+ </data>
+ <data name="IndirectMessagesMustImplementIDirectedProtocolMessage" xml:space="preserve">
+ <value>Messages that indicate indirect transport must implement the {0} interface.</value>
+ </data>
<data name="QueuedMessageResponseAlreadyExists" xml:space="preserve">
<value>A message response is already queued for sending in the response stream.</value>
</data>
diff --git a/src/DotNetOAuth/Messaging/ProtocolException.cs b/src/DotNetOAuth/Messaging/ProtocolException.cs new file mode 100644 index 0000000..10cb884 --- /dev/null +++ b/src/DotNetOAuth/Messaging/ProtocolException.cs @@ -0,0 +1,135 @@ +//-----------------------------------------------------------------------
+// <copyright file="ProtocolException.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Messaging {
+ using System;
+
+ /// <summary>
+ /// An exception to represent errors in the local or remote implementation of the protocol.
+ /// </summary>
+ [Serializable]
+ public class ProtocolException : Exception, IDirectedProtocolMessage {
+ /// <summary>
+ /// The request message being processed when this exception was generated, if any.
+ /// </summary>
+ private IProtocolMessage inResponseTo;
+
+ /// <summary>
+ /// The indirect message receiver this exception should be delivered to, if any.
+ /// </summary>
+ private Uri recipient;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ProtocolException"/> class.
+ /// </summary>
+ public ProtocolException() { }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ProtocolException"/> class.
+ /// </summary>
+ /// <param name="message">A message describing the specific error the occurred or was detected.</param>
+ public ProtocolException(string message) : base(message) { }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ProtocolException"/> class.
+ /// </summary>
+ /// <param name="message">A message describing the specific error the occurred or was detected.</param>
+ /// <param name="inner">The inner exception to include.</param>
+ public ProtocolException(string message, Exception inner) : base(message, inner) { }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ProtocolException"/> class
+ /// such that it can be sent as a protocol message response to a remote caller.
+ /// </summary>
+ /// <param name="message">The human-readable exception message.</param>
+ /// <param name="inResponseTo">
+ /// If <paramref name="message"/> is a response to an incoming message, this is the incoming message.
+ /// This is useful for error scenarios in deciding just how to send the response message.
+ /// May be null.
+ /// </param>
+ /// <param name="remoteIndirectReceiver">
+ /// In the case of exceptions that will be sent as indirect messages to the original calling
+ /// remote party, this is the URI of that remote site's receiver.
+ /// May be null only if the <paramref name="inResponseTo"/> message is a direct request.
+ /// </param>
+ internal ProtocolException(string message, IProtocolMessage inResponseTo, Uri remoteIndirectReceiver)
+ : this(message) {
+ if (inResponseTo == null) {
+ throw new ArgumentNullException("inResponseTo");
+ }
+ this.inResponseTo = inResponseTo;
+
+ if (remoteIndirectReceiver == null && inResponseTo.Transport != MessageTransport.Direct) {
+ // throw an exception, with ourselves as the inner exception (as fully initialized as we can be).
+ throw new ArgumentNullException("remoteIndirectReceiver");
+ }
+ this.recipient = remoteIndirectReceiver;
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ProtocolException"/> class.
+ /// </summary>
+ /// <param name="info">The <see cref="System.Runtime.Serialization.SerializationInfo"/>
+ /// that holds the serialized object data about the exception being thrown.</param>
+ /// <param name="context">The System.Runtime.Serialization.StreamingContext
+ /// that contains contextual information about the source or destination.</param>
+ protected ProtocolException(
+ System.Runtime.Serialization.SerializationInfo info,
+ System.Runtime.Serialization.StreamingContext context)
+ : base(info, context) { }
+
+ #region IDirectedProtocolMessage Members
+
+ /// <summary>
+ /// Gets the URL of the intended receiver of this message.
+ /// </summary>
+ /// <remarks>
+ /// This property should only be called when the error is being sent as an indirect response.
+ /// </remarks>
+ Uri IDirectedProtocolMessage.Recipient {
+ get { return this.recipient; }
+ }
+
+ #endregion
+
+ #region IProtocolMessage Members
+
+ /// <summary>
+ /// Gets the version of the protocol this message is prepared to implement.
+ /// </summary>
+ Protocol IProtocolMessage.Protocol {
+ get {
+ if (this.inResponseTo == null) {
+ throw new InvalidOperationException(MessagingStrings.ExceptionNotConstructedForTransit);
+ }
+ return this.inResponseTo.Protocol;
+ }
+ }
+
+ /// <summary>
+ /// Gets whether this is a direct or indirect message.
+ /// </summary>
+ MessageTransport IProtocolMessage.Transport {
+ get {
+ if (this.inResponseTo == null) {
+ throw new InvalidOperationException(MessagingStrings.ExceptionNotConstructedForTransit);
+ }
+ return this.inResponseTo.Transport;
+ }
+ }
+
+ /// <summary>
+ /// See <see cref="IProtocolMessage.EnsureValidMessage"/>.
+ /// </summary>
+ void IProtocolMessage.EnsureValidMessage() {
+ if (this.inResponseTo == null) {
+ throw new InvalidOperationException(MessagingStrings.ExceptionNotConstructedForTransit);
+ }
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOAuth/OAuthChannel.cs b/src/DotNetOAuth/OAuthChannel.cs index 6271954..4014ecf 100644 --- a/src/DotNetOAuth/OAuthChannel.cs +++ b/src/DotNetOAuth/OAuthChannel.cs @@ -152,23 +152,6 @@ namespace DotNetOAuth { }
/// <summary>
- /// Reports an error to the user via the user agent.
- /// </summary>
- /// <param name="exception">The error information.</param>
- protected override void ReportErrorToUser(ProtocolException exception) {
- throw new NotImplementedException();
- }
-
- /// <summary>
- /// Sends an error result directly to the calling remote party according to the
- /// rules of the protocol.
- /// </summary>
- /// <param name="exception">The error information.</param>
- protected override void ReportErrorAsDirectResponse(ProtocolException exception) {
- throw new NotImplementedException();
- }
-
- /// <summary>
/// Prepares to send a request to the Service Provider via the Authorization header.
/// </summary>
/// <param name="requestMessage">The message to be transmitted to the ServiceProvider.</param>
diff --git a/src/DotNetOAuth/ProtocolException.cs b/src/DotNetOAuth/ProtocolException.cs deleted file mode 100644 index 4505c59..0000000 --- a/src/DotNetOAuth/ProtocolException.cs +++ /dev/null @@ -1,45 +0,0 @@ -//-----------------------------------------------------------------------
-// <copyright file="ProtocolException.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOAuth {
- using System;
-
- /// <summary>
- /// An exception to represent errors in the local or remote implementation of the protocol.
- /// </summary>
- [Serializable]
- public class ProtocolException : Exception {
- /// <summary>
- /// Initializes a new instance of the <see cref="ProtocolException"/> class.
- /// </summary>
- public ProtocolException() { }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="ProtocolException"/> class.
- /// </summary>
- /// <param name="message">A message describing the specific error the occurred or was detected.</param>
- public ProtocolException(string message) : base(message) { }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="ProtocolException"/> class.
- /// </summary>
- /// <param name="message">A message describing the specific error the occurred or was detected.</param>
- /// <param name="inner">The inner exception to include.</param>
- public ProtocolException(string message, Exception inner) : base(message, inner) { }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="ProtocolException"/> class.
- /// </summary>
- /// <param name="info">The <see cref="System.Runtime.Serialization.SerializationInfo"/>
- /// that holds the serialized object data about the exception being thrown.</param>
- /// <param name="context">The System.Runtime.Serialization.StreamingContext
- /// that contains contextual information about the source or destination.</param>
- protected ProtocolException(
- System.Runtime.Serialization.SerializationInfo info,
- System.Runtime.Serialization.StreamingContext context)
- : base(info, context) { }
- }
-}
|