//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOAuth {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Linq;
///
/// Serializes/deserializes OAuth messages for/from transit.
///
/// The specific -derived type
/// that will be serialized and deserialized using this class.
internal class ProtocolMessageSerializer where T : IProtocolMessage {
///
/// Backing field for the property
///
private XName rootElement;
///
/// The serializer that will be used as a reflection engine to extract
/// the OAuth message properties out of their containing
/// objects.
///
private DataContractSerializer serializer;
///
/// Initializes a new instance of the ProtocolMessageSerializer class.
///
internal ProtocolMessageSerializer() {
this.serializer = new DataContractSerializer(
typeof(T), this.RootElement.LocalName, this.RootElement.NamespaceName);
}
///
/// Gets the XML element that is used to surround all the XML values from the dictionary.
///
private XName RootElement {
get {
if (this.rootElement == null) {
DataContractAttribute attribute = typeof(T).GetCustomAttributes(typeof(DataContractAttribute), false).OfType().Single();
this.rootElement = XName.Get("root", attribute.Namespace);
}
return this.rootElement;
}
}
///
/// Reads the data from a message instance and returns a series of name=value pairs for the fields that must be included in the message.
///
/// The message to be serialized.
/// The dictionary of values to send for the message.
internal IDictionary Serialize(T message) {
if (message == null) {
throw new ArgumentNullException("message");
}
var fields = new Dictionary(StringComparer.Ordinal);
this.Serialize(fields, message);
return fields;
}
///
/// Saves the [DataMember] properties of a message to an existing dictionary.
///
/// The dictionary to save values to.
/// The message to pull values from.
internal void Serialize(IDictionary fields, T message) {
if (fields == null) {
throw new ArgumentNullException("fields");
}
if (message == null) {
throw new ArgumentNullException("message");
}
message.EnsureValidMessage();
using (XmlWriter writer = DictionaryXmlWriter.Create(fields)) {
this.serializer.WriteObjectContent(writer, message);
}
}
///
/// Reads name=value pairs into an OAuth message.
///
/// The name=value pairs that were read in from the transport.
/// The instantiated and initialized instance.
internal T Deserialize(IDictionary fields) {
if (fields == null) {
throw new ArgumentNullException("fields");
}
var reader = DictionaryXmlReader.Create(this.RootElement, fields);
T result;
try {
result = (T)this.serializer.ReadObject(reader, false);
} catch (SerializationException ex) {
// Missing required fields is one cause of this exception.
throw new ProtocolException(Strings.InvalidIncomingMessage, ex);
}
result.EnsureValidMessage();
return result;
}
}
}