1
//-----------------------------------------------------------------------
2 // <copyright file="StoreResponse.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth
.OpenId
.Extensions
.AttributeExchange
{
9 using DotNetOpenAuth
.Messaging
;
10 using DotNetOpenAuth
.OpenId
.Messages
;
13 /// The Attribute Exchange Store message, response leg.
15 public sealed class StoreResponse
: ExtensionBase
{
17 /// The factory method that may be used in deserialization of this message.
19 internal static readonly OpenIdExtensionFactory
.CreateDelegate Factory
= (typeUri
, data
, baseMessage
) => {
20 if (typeUri
== Constants
.TypeUri
&& baseMessage
is IndirectSignedResponse
) {
22 if (data
.TryGetValue("mode", out mode
) && (mode
== SuccessMode
|| mode
== FailureMode
)) {
23 return new StoreResponse();
31 /// The value of the mode parameter used to express a successful store operation.
33 private const string SuccessMode
= "store_response_success";
36 /// The value of the mode parameter used to express a store operation failure.
38 private const string FailureMode
= "store_response_failure";
41 /// Initializes a new instance of the <see cref="StoreResponse"/> class
42 /// to represent a successful store operation.
44 public StoreResponse()
45 : base(new Version(1, 0), Constants
.TypeUri
, null) {
46 this.Succeeded
= true;
50 /// Initializes a new instance of the <see cref="StoreResponse"/> class
51 /// to represent a failed store operation.
53 /// <param name="failureReason">The reason for failure.</param>
54 public StoreResponse(string failureReason
)
56 this.Succeeded
= false;
57 this.FailureReason
= failureReason
;
61 /// Gets or sets a value indicating whether the storage request succeeded.
63 /// <value>Defaults to <c>true</c>.</value>
64 public bool Succeeded
{
65 get { return this.Mode == SuccessMode; }
66 set { this.Mode = value ? SuccessMode : FailureMode; }
70 /// Gets or sets the reason for the failure, if applicable.
72 [MessagePart("error", IsRequired
= false)]
73 public string FailureReason { get; set; }
76 /// Gets or sets the mode argument.
78 /// <value>One of 'store_response_success' or 'store_response_failure'.</value>
79 [MessagePart("mode", IsRequired
= true)]
80 private string Mode { get; set; }
83 /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
85 /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
87 /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
89 /// <exception cref="T:System.NullReferenceException">
90 /// The <paramref name="obj"/> parameter is null.
92 public override bool Equals(object obj
) {
93 var other
= obj
as StoreResponse
;
98 if (this.Version
!= other
.Version
) {
102 if (this.Succeeded
!= other
.Succeeded
) {
106 if (this.FailureReason
!= other
.FailureReason
) {
114 /// Serves as a hash function for a particular type.
117 /// A hash code for the current <see cref="T:System.Object"/>.
119 public override int GetHashCode() {
121 int hashCode
= this.Version
.GetHashCode();
122 hashCode
+= this.Succeeded
? 0 : 1;
123 if (this.FailureReason
!= null) {
124 hashCode
+= this.FailureReason
.GetHashCode();
132 /// Checks the message state for conformity to the protocol specification
133 /// and throws an exception if the message is invalid.
136 /// <para>Some messages have required fields, or combinations of fields that must relate to each other
137 /// in specialized ways. After deserializing a message, this method checks the state of the
138 /// message to see if it conforms to the protocol.</para>
139 /// <para>Note that this property should <i>not</i> check signatures or perform any state checks
140 /// outside this scope of this particular message.</para>
142 /// <exception cref="ProtocolException">Thrown if the message is invalid.</exception>
143 protected override void EnsureValidMessage() {
144 base.EnsureValidMessage();
146 ErrorUtilities
.VerifyProtocol(
147 this.Mode
== SuccessMode
|| this.Mode
== FailureMode
,
148 MessagingStrings
.UnexpectedMessagePartValue
,