1 //----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------------------
4 namespace System.ServiceModel.Channels
6 using System.Collections.Generic;
7 using System.ServiceModel;
8 using System.Collections.ObjectModel;
9 using System.Diagnostics;
10 using System.Runtime.Serialization;
13 using System.Xml.Schema;
14 using System.Xml.Serialization;
15 using System.ServiceModel.Security;
16 using System.IdentityModel.Claims;
17 using System.IdentityModel.Policy;
19 public sealed class AddressHeaderCollection : ReadOnlyCollection<AddressHeader>
21 static AddressHeaderCollection emptyHeaderCollection = new AddressHeaderCollection();
23 public AddressHeaderCollection()
24 : base(new List<AddressHeader>())
28 public AddressHeaderCollection(IEnumerable<AddressHeader> addressHeaders)
29 : base(new List<AddressHeader>(addressHeaders))
31 // avoid allocating an enumerator when possible
32 IList<AddressHeader> collection = addressHeaders as IList<AddressHeader>;
33 if (collection != null)
35 for (int i = 0; i < collection.Count; i++)
37 if (collection[i] == null)
38 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.MessageHeaderIsNull0)));
43 foreach (AddressHeader addressHeader in addressHeaders)
45 if (addressHeaders == null)
46 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.MessageHeaderIsNull0)));
51 internal static AddressHeaderCollection EmptyHeaderCollection
53 get { return emptyHeaderCollection; }
60 if (this == (object)emptyHeaderCollection)
66 public void AddHeadersTo(Message message)
69 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
71 for (int i = 0; i < InternalCount; i++)
73 #pragma warning suppress 56506 // Microsoft, Message.Headers can never be null
74 message.Headers.Add(this[i].ToMessageHeader());
78 public AddressHeader[] FindAll(string name, string ns)
81 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("name"));
83 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("ns"));
85 List<AddressHeader> results = new List<AddressHeader>();
86 for (int i = 0; i < Count; i++)
88 AddressHeader header = this[i];
89 if (header.Name == name && header.Namespace == ns)
95 return results.ToArray();
98 public AddressHeader FindHeader(string name, string ns)
101 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("name"));
103 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("ns"));
105 AddressHeader matchingHeader = null;
107 for (int i = 0; i < Count; i++)
109 AddressHeader header = this[i];
110 if (header.Name == name && header.Namespace == ns)
112 if (matchingHeader != null)
113 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.MultipleMessageHeaders, name, ns)));
114 matchingHeader = header;
118 return matchingHeader;
121 internal bool IsEquivalent(AddressHeaderCollection col)
123 if (InternalCount != col.InternalCount)
126 StringBuilder builder = new StringBuilder();
127 Dictionary<string, int> myHeaders = new Dictionary<string, int>();
128 PopulateHeaderDictionary(builder, myHeaders);
130 Dictionary<string, int> otherHeaders = new Dictionary<string, int>();
131 col.PopulateHeaderDictionary(builder, otherHeaders);
133 if (myHeaders.Count != otherHeaders.Count)
136 foreach (KeyValuePair<string, int> pair in myHeaders)
139 if (otherHeaders.TryGetValue(pair.Key, out count))
141 if (count != pair.Value)
153 internal void PopulateHeaderDictionary(StringBuilder builder, Dictionary<string, int> headers)
156 for (int i = 0; i < InternalCount; ++i)
158 builder.Remove(0, builder.Length);
159 key = this[i].GetComparableForm(builder);
160 if (headers.ContainsKey(key))
162 headers[key] = headers[key] + 1;
171 internal static AddressHeaderCollection ReadServiceParameters(XmlDictionaryReader reader)
173 return ReadServiceParameters(reader, false);
176 internal static AddressHeaderCollection ReadServiceParameters(XmlDictionaryReader reader, bool isReferenceProperty)
178 reader.MoveToContent();
179 if (reader.IsEmptyElement)
186 reader.ReadStartElement();
187 List<AddressHeader> headerList = new List<AddressHeader>();
188 while (reader.IsStartElement())
190 headerList.Add(new BufferedAddressHeader(reader, isReferenceProperty));
192 reader.ReadEndElement();
193 return new AddressHeaderCollection(headerList);
197 internal bool HasReferenceProperties
201 for (int i = 0; i < InternalCount; i++)
202 if (this[i].IsReferenceProperty)
208 internal bool HasNonReferenceProperties
212 for (int i = 0; i < InternalCount; i++)
213 if (!this[i].IsReferenceProperty)
219 internal void WriteReferencePropertyContentsTo(XmlDictionaryWriter writer)
221 for (int i = 0; i < InternalCount; i++)
222 if (this[i].IsReferenceProperty)
223 this[i].WriteAddressHeader(writer);
226 internal void WriteNonReferencePropertyContentsTo(XmlDictionaryWriter writer)
228 for (int i = 0; i < InternalCount; i++)
229 if (!this[i].IsReferenceProperty)
230 this[i].WriteAddressHeader(writer);
233 internal void WriteContentsTo(XmlDictionaryWriter writer)
235 for (int i = 0; i < InternalCount; i++)
236 this[i].WriteAddressHeader(writer);