MessageProperties.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // MessageProperties.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.ServiceModel.Channels;
  33. using System.ServiceModel.Security;
  34. using Pair = System.Collections.Generic.KeyValuePair<string, object>;
  35. namespace System.ServiceModel.Channels
  36. {
  37. [MonoTODO ("it's untested")]
  38. public sealed class MessageProperties : IDictionary<string, object>,
  39. ICollection<Pair>, IEnumerable<Pair>, IEnumerable, IDisposable
  40. {
  41. bool allow_output_batch;
  42. MessageEncoder encoder;
  43. Uri via;
  44. List<Pair> list;
  45. public MessageProperties ()
  46. {
  47. list = new List<Pair> ();
  48. }
  49. public MessageProperties (MessageProperties properties)
  50. {
  51. properties.CopyProperties (this);
  52. }
  53. [MonoTODO ("This should actually be internal of a property.")]
  54. public bool AllowOutputBatching {
  55. get { return allow_output_batch; }
  56. set { allow_output_batch = value; }
  57. }
  58. public int Count {
  59. get { return list.Count; }
  60. }
  61. [MonoTODO ("This should actually be internal of a property.")]
  62. public MessageEncoder Encoder {
  63. get { return encoder; }
  64. set { encoder = value; }
  65. }
  66. public bool IsFixedSize {
  67. get { return false; }
  68. }
  69. public bool IsReadOnly {
  70. get { return false; }
  71. }
  72. public ICollection<string> Keys {
  73. get { return new ParameterKeyCollection (list); }
  74. }
  75. public object this [string name] {
  76. get {
  77. for (int i = 0; i < list.Count; i++)
  78. if (list [i].Key == name)
  79. return list [i].Value;
  80. return null;
  81. }
  82. set {
  83. for (int i = 0; i < list.Count; i++)
  84. if (list [i].Key == name) {
  85. list [i] = new Pair (name, value);
  86. return;
  87. }
  88. list.Add (new Pair (name, value));
  89. }
  90. }
  91. #if !NET_2_1
  92. public SecurityMessageProperty Security {
  93. get { return (SecurityMessageProperty) this ["Security"]; }
  94. set { this ["Security"] = value; }
  95. }
  96. #endif
  97. public ICollection<object> Values {
  98. get { return new ParameterValueCollection (list); }
  99. }
  100. [MonoTODO ("This should actually be internal of a property.")]
  101. public Uri Via {
  102. get { return via; }
  103. set { via = value; }
  104. }
  105. public void Add (string name, object property)
  106. {
  107. list.Add (new Pair (name, property));
  108. }
  109. public void Clear ()
  110. {
  111. list.Clear ();
  112. }
  113. public bool ContainsKey (string name)
  114. {
  115. for (int i = 0; i < list.Count; i++)
  116. if (list [i].Key == name)
  117. return true;
  118. return false;
  119. }
  120. public void CopyProperties (MessageProperties properties)
  121. {
  122. list = new List<Pair> (properties.list);
  123. allow_output_batch = properties.allow_output_batch;
  124. encoder = properties.encoder;
  125. via = properties.via;
  126. }
  127. public void Dispose ()
  128. {
  129. }
  130. public bool Remove (string name)
  131. {
  132. for (int i = 0; i < list.Count; i++)
  133. if (list [i].Key == name) {
  134. list.RemoveAt (i);
  135. return true;
  136. }
  137. return false;
  138. }
  139. public bool TryGetValue (string name, out object value)
  140. {
  141. for (int i = 0; i < list.Count; i++)
  142. if (list [i].Key == name) {
  143. value = list [i].Value;
  144. return true;
  145. }
  146. value = null;
  147. return false;
  148. }
  149. void ICollection<Pair>.Add (Pair pair)
  150. {
  151. list.Add (pair);
  152. }
  153. bool ICollection<Pair>.Contains (Pair pair)
  154. {
  155. return list.Contains (pair);
  156. }
  157. void ICollection<Pair>.CopyTo (Pair [] array, int index)
  158. {
  159. list.CopyTo (array, index);
  160. }
  161. bool ICollection<Pair>.Remove (Pair pair)
  162. {
  163. return list.Remove (pair);
  164. }
  165. IEnumerator<Pair> IEnumerable<Pair>.GetEnumerator ()
  166. {
  167. return list.GetEnumerator ();
  168. }
  169. IEnumerator IEnumerable.GetEnumerator ()
  170. {
  171. return (IEnumerator) ((IEnumerable<Pair>) this).GetEnumerator ();
  172. }
  173. class ParameterKeyCollection : ICollection<string>
  174. {
  175. List<Pair> source;
  176. public ParameterKeyCollection (List<Pair> source)
  177. {
  178. this.source = source;
  179. }
  180. public int Count {
  181. get { return source.Count; }
  182. }
  183. public bool IsReadOnly {
  184. get { return true; }
  185. }
  186. public void Add (string item)
  187. {
  188. throw new InvalidOperationException ();
  189. }
  190. public void Clear ()
  191. {
  192. throw new InvalidOperationException ();
  193. }
  194. public bool Contains (string item)
  195. {
  196. for (int i = 0; i < source.Count; i++)
  197. if (source [i].Key == item)
  198. return true;
  199. return false;
  200. }
  201. public void CopyTo (string [] array, int index)
  202. {
  203. for (int i = 0; i < source.Count; i++)
  204. array [index + i] = source [i].Key;
  205. }
  206. public IEnumerator<string> GetEnumerator ()
  207. {
  208. foreach (Pair p in source)
  209. yield return p.Key;
  210. }
  211. IEnumerator IEnumerable.GetEnumerator ()
  212. {
  213. foreach (Pair p in source)
  214. yield return p.Key;
  215. }
  216. public bool Remove (string item)
  217. {
  218. throw new InvalidOperationException ();
  219. }
  220. }
  221. class ParameterValueCollection : ICollection<object>
  222. {
  223. List<Pair> source;
  224. public ParameterValueCollection (List<Pair> source)
  225. {
  226. this.source = source;
  227. }
  228. public int Count {
  229. get { return source.Count; }
  230. }
  231. public bool IsReadOnly {
  232. get { return true; }
  233. }
  234. public void Add (object item)
  235. {
  236. throw new InvalidOperationException ();
  237. }
  238. public void Clear ()
  239. {
  240. throw new InvalidOperationException ();
  241. }
  242. public bool Contains (object item)
  243. {
  244. for (int i = 0; i < source.Count; i++)
  245. if (source [i].Value == item)
  246. return true;
  247. return false;
  248. }
  249. public void CopyTo (object [] array, int index)
  250. {
  251. for (int i = 0; i < source.Count; i++)
  252. array [index + i] = source [i].Value;
  253. }
  254. public IEnumerator<object> GetEnumerator ()
  255. {
  256. foreach (Pair p in source)
  257. yield return p.Value;
  258. }
  259. IEnumerator IEnumerable.GetEnumerator ()
  260. {
  261. foreach (Pair p in source)
  262. yield return p.Key;
  263. }
  264. public bool Remove (object item)
  265. {
  266. throw new InvalidOperationException ();
  267. }
  268. }
  269. }
  270. }