MessageProperties.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. public SecurityMessageProperty Security {
  92. get { return (SecurityMessageProperty) this ["Security"]; }
  93. set { this ["Security"] = value; }
  94. }
  95. public ICollection<object> Values {
  96. get { return new ParameterValueCollection (list); }
  97. }
  98. [MonoTODO ("This should actually be internal of a property.")]
  99. public Uri Via {
  100. get { return via; }
  101. set { via = value; }
  102. }
  103. public void Add (string name, object property)
  104. {
  105. list.Add (new Pair (name, property));
  106. }
  107. public void Clear ()
  108. {
  109. list.Clear ();
  110. }
  111. public bool ContainsKey (string name)
  112. {
  113. for (int i = 0; i < list.Count; i++)
  114. if (list [i].Key == name)
  115. return true;
  116. return false;
  117. }
  118. public void CopyProperties (MessageProperties properties)
  119. {
  120. list = new List<Pair> (properties.list);
  121. allow_output_batch = properties.allow_output_batch;
  122. encoder = properties.encoder;
  123. via = properties.via;
  124. }
  125. public void Dispose ()
  126. {
  127. }
  128. public bool Remove (string name)
  129. {
  130. for (int i = 0; i < list.Count; i++)
  131. if (list [i].Key == name) {
  132. list.RemoveAt (i);
  133. return true;
  134. }
  135. return false;
  136. }
  137. public bool TryGetValue (string name, out object value)
  138. {
  139. for (int i = 0; i < list.Count; i++)
  140. if (list [i].Key == name) {
  141. value = list [i].Value;
  142. return true;
  143. }
  144. value = null;
  145. return false;
  146. }
  147. void ICollection<Pair>.Add (Pair pair)
  148. {
  149. list.Add (pair);
  150. }
  151. bool ICollection<Pair>.Contains (Pair pair)
  152. {
  153. return list.Contains (pair);
  154. }
  155. void ICollection<Pair>.CopyTo (Pair [] array, int index)
  156. {
  157. list.CopyTo (array, index);
  158. }
  159. bool ICollection<Pair>.Remove (Pair pair)
  160. {
  161. return list.Remove (pair);
  162. }
  163. IEnumerator<Pair> IEnumerable<Pair>.GetEnumerator ()
  164. {
  165. return list.GetEnumerator ();
  166. }
  167. IEnumerator IEnumerable.GetEnumerator ()
  168. {
  169. return (IEnumerator) ((IEnumerable<Pair>) this).GetEnumerator ();
  170. }
  171. class ParameterKeyCollection : ICollection<string>
  172. {
  173. List<Pair> source;
  174. public ParameterKeyCollection (List<Pair> source)
  175. {
  176. this.source = source;
  177. }
  178. public int Count {
  179. get { return source.Count; }
  180. }
  181. public bool IsReadOnly {
  182. get { return true; }
  183. }
  184. public void Add (string item)
  185. {
  186. throw new InvalidOperationException ();
  187. }
  188. public void Clear ()
  189. {
  190. throw new InvalidOperationException ();
  191. }
  192. public bool Contains (string item)
  193. {
  194. for (int i = 0; i < source.Count; i++)
  195. if (source [i].Key == item)
  196. return true;
  197. return false;
  198. }
  199. public void CopyTo (string [] array, int index)
  200. {
  201. for (int i = 0; i < source.Count; i++)
  202. array [index + i] = source [i].Key;
  203. }
  204. public IEnumerator<string> GetEnumerator ()
  205. {
  206. foreach (Pair p in source)
  207. yield return p.Key;
  208. }
  209. IEnumerator IEnumerable.GetEnumerator ()
  210. {
  211. foreach (Pair p in source)
  212. yield return p.Key;
  213. }
  214. public bool Remove (string item)
  215. {
  216. throw new InvalidOperationException ();
  217. }
  218. }
  219. class ParameterValueCollection : ICollection<object>
  220. {
  221. List<Pair> source;
  222. public ParameterValueCollection (List<Pair> source)
  223. {
  224. this.source = source;
  225. }
  226. public int Count {
  227. get { return source.Count; }
  228. }
  229. public bool IsReadOnly {
  230. get { return true; }
  231. }
  232. public void Add (object item)
  233. {
  234. throw new InvalidOperationException ();
  235. }
  236. public void Clear ()
  237. {
  238. throw new InvalidOperationException ();
  239. }
  240. public bool Contains (object item)
  241. {
  242. for (int i = 0; i < source.Count; i++)
  243. if (source [i].Value == item)
  244. return true;
  245. return false;
  246. }
  247. public void CopyTo (object [] array, int index)
  248. {
  249. for (int i = 0; i < source.Count; i++)
  250. array [index + i] = source [i].Value;
  251. }
  252. public IEnumerator<object> GetEnumerator ()
  253. {
  254. foreach (Pair p in source)
  255. yield return p.Value;
  256. }
  257. IEnumerator IEnumerable.GetEnumerator ()
  258. {
  259. foreach (Pair p in source)
  260. yield return p.Key;
  261. }
  262. public bool Remove (object item)
  263. {
  264. throw new InvalidOperationException ();
  265. }
  266. }
  267. }
  268. }