//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OpenId.Extensions.AttributeExchange {
using System;
using System.Collections.ObjectModel;
using System.Linq;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId.Messages;
///
/// The Attribute Exchange Store message, request leg.
///
[Serializable]
public sealed class StoreRequest : ExtensionBase, IMessageWithEvents {
///
/// The factory method that may be used in deserialization of this message.
///
internal static readonly StandardOpenIdExtensionFactory.CreateDelegate Factory = (typeUri, data, baseMessage, isProviderRole) => {
if (typeUri == Constants.TypeUri && isProviderRole) {
string mode;
if (data.TryGetValue("mode", out mode) && mode == Mode) {
return new StoreRequest();
}
}
return null;
};
///
/// The value of the 'mode' parameter.
///
[MessagePart("mode", IsRequired = true)]
private const string Mode = "store_request";
///
/// The collection of provided attribute values. This field will never be null.
///
private readonly KeyedCollection attributesProvided = new KeyedCollectionDelegate(av => av.TypeUri);
///
/// Initializes a new instance of the class.
///
public StoreRequest()
: base(new Version(1, 0), Constants.TypeUri, null) {
}
///
/// Gets the collection of all the attributes that are included in the store request.
///
public KeyedCollection Attributes {
get { return this.attributesProvided; }
}
#region IMessageWithEvents Members
///
/// Called when the message is about to be transmitted,
/// before it passes through the channel binding elements.
///
void IMessageWithEvents.OnSending() {
var fields = ((IMessage)this).ExtraData;
fields.Clear();
AXUtilities.SerializeAttributes(fields, this.attributesProvided);
}
///
/// Called when the message has been received,
/// after it passes through the channel binding elements.
///
void IMessageWithEvents.OnReceiving() {
var fields = ((IMessage)this).ExtraData;
foreach (var att in AXUtilities.DeserializeAttributes(fields)) {
this.Attributes.Add(att);
}
}
#endregion
///
/// Determines whether the specified is equal to the current .
///
/// The to compare with the current .
///
/// true if the specified is equal to the current ; otherwise, false.
///
///
/// The parameter is null.
///
public override bool Equals(object obj) {
var other = obj as StoreRequest;
if (other == null) {
return false;
}
if (this.Version != other.Version) {
return false;
}
if (!MessagingUtilities.AreEquivalentUnordered(this.Attributes.ToList(), other.Attributes.ToList())) {
return false;
}
return true;
}
///
/// Serves as a hash function for a particular type.
///
///
/// A hash code for the current .
///
public override int GetHashCode() {
unchecked {
int hashCode = this.Version.GetHashCode();
foreach (AttributeValues att in this.Attributes) {
hashCode += att.GetHashCode();
}
return hashCode;
}
}
}
}