summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth/Messaging/Channel.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2008-09-24 20:57:49 -0700
committerAndrew <andrewarnott@gmail.com>2008-09-24 20:57:49 -0700
commitde6a705de31c11ee5892c94cc9afc5c8e49a90ce (patch)
treeace1f83f585c1e84da5f768cb6ed4dbeecb42d57 /src/DotNetOAuth/Messaging/Channel.cs
parent22341a07b0ba0dc685bb859b0ed82c22fc3c09db (diff)
downloadDotNetOpenAuth-de6a705de31c11ee5892c94cc9afc5c8e49a90ce.zip
DotNetOpenAuth-de6a705de31c11ee5892c94cc9afc5c8e49a90ce.tar.gz
DotNetOpenAuth-de6a705de31c11ee5892c94cc9afc5c8e49a90ce.tar.bz2
Added a scenario test from Appendix A (incomplete but passing so far).
Included in this change are a lot of fixes and additional implementation.
Diffstat (limited to 'src/DotNetOAuth/Messaging/Channel.cs')
-rw-r--r--src/DotNetOAuth/Messaging/Channel.cs71
1 files changed, 68 insertions, 3 deletions
diff --git a/src/DotNetOAuth/Messaging/Channel.cs b/src/DotNetOAuth/Messaging/Channel.cs
index d0ccccd..95efbae 100644
--- a/src/DotNetOAuth/Messaging/Channel.cs
+++ b/src/DotNetOAuth/Messaging/Channel.cs
@@ -166,11 +166,36 @@ namespace DotNetOAuth.Messaging {
/// </remarks>
/// <exception cref="InvalidOperationException">Thrown when <see cref="HttpContext.Current"/> is null.</exception>
internal IProtocolMessage ReadFromRequest() {
- if (HttpContext.Current == null) {
- throw new InvalidOperationException(MessagingStrings.HttpContextRequired);
+ return this.ReadFromRequest(GetRequestFromContext());
+ }
+
+ internal TREQUEST ReadFromRequest<TREQUEST>()
+ where TREQUEST : class, IProtocolMessage {
+ return this.ReadFromRequest<TREQUEST>(GetRequestFromContext());
+ }
+
+ protected internal TREQUEST ReadFromRequest<TREQUEST>(HttpRequestInfo httpRequest)
+ where TREQUEST : class, IProtocolMessage {
+ IProtocolMessage request = this.ReadFromRequest(httpRequest);
+ if (request == null) {
+ throw new ProtocolException(
+ string.Format(
+ CultureInfo.CurrentCulture,
+ MessagingStrings.ExpectedMessageNotReceived,
+ typeof(TREQUEST)));
}
- return this.ReadFromRequest(new HttpRequestInfo(HttpContext.Current.Request));
+ var expectedRequest = request as TREQUEST;
+ if (expectedRequest == null) {
+ throw new ProtocolException(
+ string.Format(
+ CultureInfo.CurrentCulture,
+ MessagingStrings.UnexpectedMessageReceived,
+ typeof(TREQUEST),
+ request.GetType()));
+ }
+
+ return expectedRequest;
}
/// <summary>
@@ -187,6 +212,30 @@ namespace DotNetOAuth.Messaging {
return requestMessage;
}
+ protected internal TRESPONSE Request<TRESPONSE>(IDirectedProtocolMessage request)
+ where TRESPONSE : class, IProtocolMessage {
+ IProtocolMessage response = this.Request(request);
+ if (response == null) {
+ throw new ProtocolException(
+ string.Format(
+ CultureInfo.CurrentCulture,
+ MessagingStrings.ExpectedMessageNotReceived,
+ typeof(TRESPONSE)));
+ }
+
+ var expectedResponse = response as TRESPONSE;
+ if (expectedResponse == null) {
+ throw new ProtocolException(
+ string.Format(
+ CultureInfo.CurrentCulture,
+ MessagingStrings.UnexpectedMessageReceived,
+ typeof(TRESPONSE),
+ response.GetType()));
+ }
+
+ return expectedResponse;
+ }
+
/// <summary>
/// Sends a direct message to a remote party and waits for the response.
/// </summary>
@@ -218,6 +267,22 @@ namespace DotNetOAuth.Messaging {
}
/// <summary>
+ /// Gets the current HTTP request being processed.
+ /// </summary>
+ /// <returns>The HttpRequestInfo for the current request.</returns>
+ /// <remarks>
+ /// Requires an HttpContext.Current context.
+ /// </remarks>
+ /// <exception cref="InvalidOperationException">Thrown when <see cref="HttpContext.Current"/> is null.</exception>
+ protected virtual HttpRequestInfo GetRequestFromContext() {
+ if (HttpContext.Current == null) {
+ throw new InvalidOperationException(MessagingStrings.HttpContextRequired);
+ }
+
+ return new HttpRequestInfo(HttpContext.Current.Request);
+ }
+
+ /// <summary>
/// Gets the protocol message that may be embedded in the given HTTP request.
/// </summary>
/// <param name="request">The request to search for an embedded message.</param>