AddressHeader.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // System.ServiceModel.AddressHeader.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // Copyright (C) 2005,2010 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.IO;
  31. using System.Runtime.Serialization;
  32. using System.ServiceModel;
  33. using System.Xml;
  34. using System.Xml.Schema;
  35. namespace System.ServiceModel.Channels
  36. {
  37. public abstract class AddressHeader
  38. {
  39. protected AddressHeader () {}
  40. public static AddressHeader CreateAddressHeader (object value)
  41. {
  42. return new DefaultAddressHeader (value);
  43. }
  44. public static AddressHeader CreateAddressHeader (object value, XmlObjectSerializer formatter)
  45. {
  46. return new DefaultAddressHeader (value, formatter);
  47. }
  48. public static AddressHeader CreateAddressHeader (string name, string ns, object value)
  49. {
  50. return new DefaultAddressHeader (name, ns, value);
  51. }
  52. public static AddressHeader CreateAddressHeader (string name, string ns, object value,
  53. XmlObjectSerializer formatter)
  54. {
  55. if (formatter == null)
  56. throw new ArgumentNullException ("formatter");
  57. return new DefaultAddressHeader (name, ns, value, formatter);
  58. }
  59. public override bool Equals (object obj)
  60. {
  61. AddressHeader o = obj as AddressHeader;
  62. if (o == null)
  63. return false;
  64. return o.Name == this.Name && o.Namespace == this.Namespace;
  65. }
  66. public virtual XmlDictionaryReader GetAddressHeaderReader ()
  67. {
  68. var sw = new StringWriter ();
  69. var s = new XmlWriterSettings () { OmitXmlDeclaration = true };
  70. var xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, s));
  71. WriteAddressHeader (xw);
  72. xw.Close ();
  73. return XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (sw.ToString ())));
  74. }
  75. public override int GetHashCode ()
  76. {
  77. return this.Name.GetHashCode () + this.Namespace.GetHashCode ();
  78. }
  79. public T GetValue<T> ()
  80. {
  81. return GetValue<T> (new DataContractSerializer (typeof (T)));
  82. }
  83. public T GetValue<T> (XmlObjectSerializer formatter)
  84. {
  85. return (T) formatter.ReadObject (GetAddressHeaderReader ());
  86. }
  87. protected abstract void OnWriteAddressHeaderContents (XmlDictionaryWriter writer);
  88. protected virtual void OnWriteStartAddressHeader (XmlDictionaryWriter writer)
  89. {
  90. if (Name != null && Namespace != null)
  91. writer.WriteStartElement (Name, Namespace);
  92. }
  93. public MessageHeader ToMessageHeader ()
  94. {
  95. throw new NotImplementedException ();
  96. }
  97. public void WriteAddressHeader (XmlDictionaryWriter writer)
  98. {
  99. if (writer == null)
  100. throw new ArgumentNullException ("writer is null");
  101. this.WriteStartAddressHeader (writer);
  102. this.WriteAddressHeaderContents (writer);
  103. if (Name != null && Namespace != null)
  104. writer.WriteEndElement ();
  105. }
  106. public void WriteAddressHeader (XmlWriter writer)
  107. {
  108. this.WriteAddressHeader (XmlDictionaryWriter.CreateDictionaryWriter (writer));
  109. }
  110. public void WriteAddressHeaderContents (XmlDictionaryWriter writer)
  111. {
  112. this.OnWriteAddressHeaderContents (writer);
  113. }
  114. public void WriteStartAddressHeader (XmlDictionaryWriter writer)
  115. {
  116. this.OnWriteStartAddressHeader (writer);
  117. }
  118. public abstract string Name { get; }
  119. public abstract string Namespace { get; }
  120. internal class DefaultAddressHeader : AddressHeader
  121. {
  122. string name, ns;
  123. XmlObjectSerializer formatter;
  124. object value;
  125. internal DefaultAddressHeader (object value)
  126. : this (null, null, value) {}
  127. internal DefaultAddressHeader (object value, XmlObjectSerializer formatter)
  128. : this (null, null, value, formatter)
  129. {
  130. }
  131. internal DefaultAddressHeader (string name, string ns, object value)
  132. : this (name, ns, value, null) {}
  133. internal DefaultAddressHeader (string name, string ns, object value, XmlObjectSerializer formatter)
  134. {
  135. if (formatter == null)
  136. formatter = value != null ? new DataContractSerializer (value.GetType ()) : null;
  137. this.name = name;
  138. this.ns = ns;
  139. this.formatter = formatter;
  140. this.value = value;
  141. }
  142. public override string Name {
  143. get { return name; }
  144. }
  145. public override string Namespace {
  146. get { return ns; }
  147. }
  148. protected override void OnWriteAddressHeaderContents (XmlDictionaryWriter writer)
  149. {
  150. if (value == null)
  151. writer.WriteAttributeString ("i", "nil", XmlSchema.InstanceNamespace, "true");
  152. else
  153. this.formatter.WriteObject (writer, value);
  154. }
  155. }
  156. }
  157. }