MessagePartSpecification.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.Xml;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Runtime.Serialization;
  12. public class MessagePartSpecification
  13. {
  14. List<XmlQualifiedName> headerTypes;
  15. bool isBodyIncluded;
  16. bool isReadOnly;
  17. static MessagePartSpecification noParts;
  18. public ICollection<XmlQualifiedName> HeaderTypes
  19. {
  20. get
  21. {
  22. if (headerTypes == null)
  23. {
  24. headerTypes = new List<XmlQualifiedName>();
  25. }
  26. if (isReadOnly)
  27. {
  28. return new ReadOnlyCollection<XmlQualifiedName>(headerTypes);
  29. }
  30. else
  31. {
  32. return headerTypes;
  33. }
  34. }
  35. }
  36. internal bool HasHeaders
  37. {
  38. get { return this.headerTypes != null && this.headerTypes.Count > 0; }
  39. }
  40. public bool IsBodyIncluded
  41. {
  42. get
  43. {
  44. return this.isBodyIncluded;
  45. }
  46. set
  47. {
  48. if (isReadOnly)
  49. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  50. this.isBodyIncluded = value;
  51. }
  52. }
  53. public bool IsReadOnly
  54. {
  55. get
  56. {
  57. return this.isReadOnly;
  58. }
  59. }
  60. static public MessagePartSpecification NoParts
  61. {
  62. get
  63. {
  64. if (noParts == null)
  65. {
  66. MessagePartSpecification parts = new MessagePartSpecification();
  67. parts.MakeReadOnly();
  68. noParts = parts;
  69. }
  70. return noParts;
  71. }
  72. }
  73. public void Clear()
  74. {
  75. if (isReadOnly)
  76. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  77. if (this.headerTypes != null)
  78. this.headerTypes.Clear();
  79. this.isBodyIncluded = false;
  80. }
  81. public void Union(MessagePartSpecification specification)
  82. {
  83. if (isReadOnly)
  84. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  85. if (specification == null)
  86. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("specification");
  87. this.isBodyIncluded |= specification.IsBodyIncluded;
  88. List<XmlQualifiedName> headerTypes = specification.headerTypes;
  89. if (headerTypes != null && headerTypes.Count > 0)
  90. {
  91. if (this.headerTypes == null)
  92. {
  93. this.headerTypes = new List<XmlQualifiedName>(headerTypes.Count);
  94. }
  95. for (int i = 0; i < headerTypes.Count; i++)
  96. {
  97. XmlQualifiedName qname = headerTypes[i];
  98. this.headerTypes.Add(qname);
  99. }
  100. }
  101. }
  102. public void MakeReadOnly()
  103. {
  104. if (isReadOnly)
  105. return;
  106. if (this.headerTypes != null)
  107. {
  108. List<XmlQualifiedName> noDuplicates = new List<XmlQualifiedName>(headerTypes.Count);
  109. for (int i = 0; i < headerTypes.Count; i++)
  110. {
  111. XmlQualifiedName qname = headerTypes[i];
  112. if (qname != null)
  113. {
  114. bool include = true;
  115. for (int j = 0; j < noDuplicates.Count; j++)
  116. {
  117. XmlQualifiedName qname1 = noDuplicates[j];
  118. if (qname.Name == qname1.Name && qname.Namespace == qname1.Namespace)
  119. {
  120. include = false;
  121. break;
  122. }
  123. }
  124. if (include)
  125. noDuplicates.Add(qname);
  126. }
  127. }
  128. this.headerTypes = noDuplicates;
  129. }
  130. this.isReadOnly = true;
  131. }
  132. public MessagePartSpecification()
  133. {
  134. // empty
  135. }
  136. public MessagePartSpecification(bool isBodyIncluded)
  137. {
  138. this.isBodyIncluded = isBodyIncluded;
  139. }
  140. public MessagePartSpecification(params XmlQualifiedName[] headerTypes)
  141. : this(false, headerTypes)
  142. {
  143. // empty
  144. }
  145. public MessagePartSpecification(bool isBodyIncluded, params XmlQualifiedName[] headerTypes)
  146. {
  147. this.isBodyIncluded = isBodyIncluded;
  148. if (headerTypes != null && headerTypes.Length > 0)
  149. {
  150. this.headerTypes = new List<XmlQualifiedName>(headerTypes.Length);
  151. for (int i = 0; i < headerTypes.Length; i++)
  152. {
  153. this.headerTypes.Add(headerTypes[i]);
  154. }
  155. }
  156. }
  157. internal bool IsHeaderIncluded(MessageHeader header)
  158. {
  159. if (header == null)
  160. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("header");
  161. return IsHeaderIncluded(header.Name, header.Namespace);
  162. }
  163. internal bool IsHeaderIncluded(string name, string ns)
  164. {
  165. if (name == null)
  166. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("name");
  167. if (ns == null)
  168. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("ns");
  169. if (this.headerTypes != null)
  170. {
  171. for (int i = 0; i < this.headerTypes.Count; i++)
  172. {
  173. XmlQualifiedName qname = this.headerTypes[i];
  174. // Name is an optional attribute. If not present, compare with only the namespace.
  175. if (String.IsNullOrEmpty(qname.Name))
  176. {
  177. if (qname.Namespace == ns)
  178. {
  179. return true;
  180. }
  181. }
  182. else
  183. {
  184. if (qname.Name == name && qname.Namespace == ns)
  185. {
  186. return true;
  187. }
  188. }
  189. }
  190. }
  191. return false;
  192. }
  193. internal bool IsEmpty()
  194. {
  195. if (this.headerTypes != null && this.headerTypes.Count > 0)
  196. return false;
  197. return !this.IsBodyIncluded;
  198. }
  199. }
  200. }