summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth/Messaging/Channel.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2008-09-30 07:42:12 -0700
committerAndrew <andrewarnott@gmail.com>2008-10-02 07:33:57 -0700
commit08a2ca3fdbe337ba11575cf7893ab547536bb4e2 (patch)
tree450764d5a45091de0683d3f164a7efaa6164b22d /src/DotNetOAuth/Messaging/Channel.cs
parent5cbd5d7edb994f874b265ed2e7c43f0bcd27b6ac (diff)
downloadDotNetOpenAuth-08a2ca3fdbe337ba11575cf7893ab547536bb4e2.zip
DotNetOpenAuth-08a2ca3fdbe337ba11575cf7893ab547536bb4e2.tar.gz
DotNetOpenAuth-08a2ca3fdbe337ba11575cf7893ab547536bb4e2.tar.bz2
Split up the two OAuth message type providers into Consumer and Service Provider classes so they could actually work correctly..
Added Try methods to read messages that might not be there without throwing exceptions.
Diffstat (limited to 'src/DotNetOAuth/Messaging/Channel.cs')
-rw-r--r--src/DotNetOAuth/Messaging/Channel.cs75
1 files changed, 56 insertions, 19 deletions
diff --git a/src/DotNetOAuth/Messaging/Channel.cs b/src/DotNetOAuth/Messaging/Channel.cs
index e3e49d8..fff8f7c 100644
--- a/src/DotNetOAuth/Messaging/Channel.cs
+++ b/src/DotNetOAuth/Messaging/Channel.cs
@@ -158,11 +158,61 @@ namespace DotNetOAuth.Messaging {
/// Gets the protocol message embedded in the given HTTP request, if present.
/// </summary>
/// <typeparam name="TREQUEST">The expected type of the message to be received.</typeparam>
- /// <returns>The deserialized message, if one is found. Null otherwise.</returns>
+ /// <param name="request">The deserialized message, if one is found. Null otherwise.</param>
+ /// <returns>True if the expected message was recognized and deserialized. False otherwise.</returns>
+ /// <remarks>
+ /// Requires an HttpContext.Current context.
+ /// </remarks>
+ /// <exception cref="InvalidOperationException">Thrown when <see cref="HttpContext.Current"/> is null.</exception>
+ /// <exception cref="ProtocolException">Thrown when a request message of an unexpected type is received.</exception>
+ internal bool TryReadFromRequest<TREQUEST>(out TREQUEST request)
+ where TREQUEST : class, IProtocolMessage {
+ return TryReadFromRequest<TREQUEST>(this.GetRequestFromContext(), out request);
+ }
+
+ /// <summary>
+ /// Gets the protocol message embedded in the given HTTP request, if present.
+ /// </summary>
+ /// <typeparam name="TREQUEST">The expected type of the message to be received.</typeparam>
+ /// <param name="httpRequest">The request to search for an embedded message.</param>
+ /// <param name="request">The deserialized message, if one is found. Null otherwise.</param>
+ /// <returns>True if the expected message was recognized and deserialized. False otherwise.</returns>
/// <remarks>
/// Requires an HttpContext.Current context.
/// </remarks>
/// <exception cref="InvalidOperationException">Thrown when <see cref="HttpContext.Current"/> is null.</exception>
+ /// <exception cref="ProtocolException">Thrown when a request message of an unexpected type is received.</exception>
+ internal bool TryReadFromRequest<TREQUEST>(HttpRequestInfo httpRequest, out TREQUEST request)
+ where TREQUEST : class, IProtocolMessage {
+ IProtocolMessage untypedRequest = this.ReadFromRequest(httpRequest);
+ if (untypedRequest == null) {
+ request = null;
+ return false;
+ }
+
+ request = untypedRequest as TREQUEST;
+ if (request == null) {
+ throw new ProtocolException(
+ string.Format(
+ CultureInfo.CurrentCulture,
+ MessagingStrings.UnexpectedMessageReceived,
+ typeof(TREQUEST),
+ untypedRequest.GetType()));
+ }
+
+ return true;
+ }
+
+ /// <summary>
+ /// Gets the protocol message embedded in the given HTTP request, if present.
+ /// </summary>
+ /// <typeparam name="TREQUEST">The expected type of the message to be received.</typeparam>
+ /// <returns>The deserialized message.</returns>
+ /// <remarks>
+ /// Requires an HttpContext.Current context.
+ /// </remarks>
+ /// <exception cref="InvalidOperationException">Thrown when <see cref="HttpContext.Current"/> is null.</exception>
+ /// <exception cref="ProtocolException">Thrown if the expected message was not recognized in the response.</exception>
internal TREQUEST ReadFromRequest<TREQUEST>()
where TREQUEST : class, IProtocolMessage {
return this.ReadFromRequest<TREQUEST>(this.GetRequestFromContext());
@@ -174,32 +224,19 @@ namespace DotNetOAuth.Messaging {
/// <typeparam name="TREQUEST">The expected type of the message to be received.</typeparam>
/// <param name="httpRequest">The request to search for an embedded message.</param>
/// <returns>The deserialized message, if one is found. Null otherwise.</returns>
- /// <exception cref="ProtocolException">
- /// Thrown if no message is recognized in the request
- /// or an unexpected type of message is received.
- /// </exception>
+ /// <exception cref="ProtocolException">Thrown if the expected message was not recognized in the response.</exception>
protected internal TREQUEST ReadFromRequest<TREQUEST>(HttpRequestInfo httpRequest)
where TREQUEST : class, IProtocolMessage {
- IProtocolMessage request = this.ReadFromRequest(httpRequest);
- if (request == null) {
+ TREQUEST request;
+ if (this.TryReadFromRequest<TREQUEST>(httpRequest, out request)) {
+ return request;
+ } else {
throw new ProtocolException(
string.Format(
CultureInfo.CurrentCulture,
MessagingStrings.ExpectedMessageNotReceived,
typeof(TREQUEST)));
}
-
- var expectedRequest = request as TREQUEST;
- if (expectedRequest == null) {
- throw new ProtocolException(
- string.Format(
- CultureInfo.CurrentCulture,
- MessagingStrings.UnexpectedMessageReceived,
- typeof(TREQUEST),
- request.GetType()));
- }
-
- return expectedRequest;
}
/// <summary>