Message.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //
  2. // Message.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005-2006,2010 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.Generic;
  30. using System.IO;
  31. using System.Runtime.Serialization;
  32. using System.Xml;
  33. using System.Xml.Schema;
  34. using Mono.Xml.XPath;
  35. namespace System.ServiceModel.Channels
  36. {
  37. public abstract class Message : IDisposable
  38. {
  39. bool disposed;
  40. string body_id;
  41. protected Message () {
  42. State = MessageState.Created;
  43. }
  44. public abstract MessageHeaders Headers { get; }
  45. Dictionary<XmlQualifiedName,string> attributes = new Dictionary<XmlQualifiedName,string> ();
  46. public virtual bool IsEmpty {
  47. get { return false; }
  48. }
  49. public virtual bool IsFault {
  50. get { return false; }
  51. }
  52. public abstract MessageProperties Properties { get; }
  53. MessageState ___state;
  54. public MessageState State {
  55. get { return ___state; }
  56. private set { ___state = value; }
  57. }
  58. public abstract MessageVersion Version { get; }
  59. protected bool IsDisposed {
  60. get { return disposed; }
  61. }
  62. public void Close ()
  63. {
  64. if (!disposed)
  65. OnClose ();
  66. State = MessageState.Closed;
  67. disposed = true;
  68. }
  69. public MessageBuffer CreateBufferedCopy (int maxBufferSize)
  70. {
  71. if (State != MessageState.Created)
  72. throw new InvalidOperationException (String.Format ("The message is already at {0} state", State));
  73. try {
  74. return OnCreateBufferedCopy (maxBufferSize);
  75. } finally {
  76. State = MessageState.Copied;
  77. }
  78. }
  79. void IDisposable.Dispose ()
  80. {
  81. Close ();
  82. }
  83. public T GetBody<T> ()
  84. {
  85. return GetBody<T> (new DataContractSerializer (typeof (T)));
  86. }
  87. public T GetBody<T> (XmlObjectSerializer xmlFormatter)
  88. {
  89. return (T) xmlFormatter.ReadObject (GetReaderAtBodyContents ());
  90. }
  91. public string GetBodyAttribute (string localName, string ns)
  92. {
  93. return OnGetBodyAttribute (localName, ns);
  94. }
  95. public XmlDictionaryReader GetReaderAtBodyContents ()
  96. {
  97. return OnGetReaderAtBodyContents ();
  98. }
  99. public override string ToString ()
  100. {
  101. MessageState tempState = State;
  102. try {
  103. StringWriter sw = new StringWriter ();
  104. XmlWriterSettings settings = new XmlWriterSettings ();
  105. settings.Indent = true;
  106. settings.OmitXmlDeclaration = true;
  107. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  108. WriteMessage (w);
  109. }
  110. return sw.ToString ();
  111. }
  112. finally {
  113. State = tempState;
  114. }
  115. }
  116. void WriteXsiNil (XmlDictionaryWriter writer)
  117. {
  118. var dic = Constants.SoapDictionary;
  119. writer.WriteStartElement ("z", dic.Add ("anyType"), dic.Add (Constants.MSSerialization));
  120. writer.WriteAttributeString ("i", dic.Add ("nil"), dic.Add ("http://www.w3.org/2001/XMLSchema-instance"), "true");
  121. writer.WriteEndElement ();
  122. }
  123. public void WriteBody (XmlDictionaryWriter writer)
  124. {
  125. if (Version.Envelope != EnvelopeVersion.None)
  126. WriteStartBody (writer);
  127. WriteBodyContents (writer);
  128. if (Version.Envelope != EnvelopeVersion.None)
  129. writer.WriteEndElement ();
  130. }
  131. public void WriteBody (XmlWriter writer)
  132. {
  133. WriteBody (XmlDictionaryWriter.CreateDictionaryWriter (writer));
  134. }
  135. public void WriteBodyContents (XmlDictionaryWriter writer)
  136. {
  137. if (!IsEmpty)
  138. OnWriteBodyContents (writer);
  139. else if (Version.Envelope == EnvelopeVersion.None)
  140. WriteXsiNil (writer);
  141. State = MessageState.Written;
  142. }
  143. public void WriteMessage (XmlDictionaryWriter writer)
  144. {
  145. if (State != MessageState.Created)
  146. throw new InvalidOperationException (String.Format ("The message is already at {0} state", State));
  147. OnWriteMessage (writer);
  148. }
  149. public void WriteMessage (XmlWriter writer)
  150. {
  151. WriteMessage (XmlDictionaryWriter.CreateDictionaryWriter (writer));
  152. }
  153. public void WriteStartBody (XmlDictionaryWriter writer)
  154. {
  155. if (State != MessageState.Created)
  156. throw new InvalidOperationException (String.Format ("The message is already at {0} state", State));
  157. OnWriteStartBody (writer);
  158. foreach (var a in attributes)
  159. writer.WriteAttributeString (a.Key.Name, a.Key.Namespace, a.Value);
  160. }
  161. public void WriteStartBody (XmlWriter writer)
  162. {
  163. WriteStartBody (
  164. XmlDictionaryWriter.CreateDictionaryWriter (writer));
  165. }
  166. public void WriteStartEnvelope (XmlDictionaryWriter writer)
  167. {
  168. if (State != MessageState.Created)
  169. throw new InvalidOperationException (String.Format ("The message is already at {0} state", State));
  170. OnWriteStartEnvelope (writer);
  171. }
  172. [MonoTODO]
  173. protected virtual void OnBodyToString (
  174. XmlDictionaryWriter writer)
  175. {
  176. throw new NotImplementedException ();
  177. }
  178. protected virtual void OnClose ()
  179. {
  180. }
  181. protected virtual MessageBuffer OnCreateBufferedCopy (
  182. int maxBufferSize)
  183. {
  184. var s = new XmlWriterSettings ();
  185. s.OmitXmlDeclaration = true;
  186. s.ConformanceLevel = ConformanceLevel.Auto;
  187. StringWriter sw = new StringWriter ();
  188. using (XmlDictionaryWriter w = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, s)))
  189. WriteBodyContents (w);
  190. var headers = new MessageHeaders (Headers);
  191. var props = new MessageProperties (Properties);
  192. return new DefaultMessageBuffer (maxBufferSize, headers, props, new XmlReaderBodyWriter (sw.ToString (), maxBufferSize, null), false, attributes);
  193. }
  194. protected virtual string OnGetBodyAttribute (
  195. string localName, string ns)
  196. {
  197. var q = new XmlQualifiedName (localName, ns);
  198. string v;
  199. return attributes.TryGetValue (q, out v) ? v : null;
  200. }
  201. protected virtual XmlDictionaryReader OnGetReaderAtBodyContents ()
  202. {
  203. var ws = new XmlWriterSettings ();
  204. ws.ConformanceLevel = ConformanceLevel.Auto;
  205. StringWriter sw = new StringWriter ();
  206. using (XmlDictionaryWriter body = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, ws))) {
  207. WriteBodyContents (body);
  208. }
  209. var rs = new XmlReaderSettings ();
  210. rs.ConformanceLevel = ConformanceLevel.Auto;
  211. return XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (sw.ToString ()), rs));
  212. }
  213. protected abstract void OnWriteBodyContents (
  214. XmlDictionaryWriter writer);
  215. protected virtual void OnWriteMessage (
  216. XmlDictionaryWriter writer)
  217. {
  218. if (Version.Envelope != EnvelopeVersion.None) {
  219. WriteStartEnvelope (writer);
  220. if (Headers.Count > 0) {
  221. OnWriteStartHeaders (writer);
  222. for (int i = 0, count = Headers.Count; i < count; i++)
  223. Headers.WriteHeader (i, writer);
  224. writer.WriteEndElement ();
  225. }
  226. }
  227. WriteBody (writer);
  228. if (Version.Envelope != EnvelopeVersion.None)
  229. writer.WriteEndElement ();
  230. }
  231. protected virtual void OnWriteStartBody (
  232. XmlDictionaryWriter writer)
  233. {
  234. var dic = Constants.SoapDictionary;
  235. writer.WriteStartElement ("s", dic.Add ("Body"), dic.Add (Version.Envelope.Namespace));
  236. }
  237. protected virtual void OnWriteStartEnvelope (
  238. XmlDictionaryWriter writer)
  239. {
  240. var dic = Constants.SoapDictionary;
  241. writer.WriteStartElement ("s", dic.Add ("Envelope"), dic.Add (Version.Envelope.Namespace));
  242. if (Headers.Action != null && Version.Addressing.Namespace != MessageVersion.None.Addressing.Namespace)
  243. writer.WriteXmlnsAttribute ("a", dic.Add (Version.Addressing.Namespace));
  244. foreach (MessageHeaderInfo h in Headers)
  245. if (h.Id != null) {
  246. writer.WriteXmlnsAttribute ("u", dic.Add (Constants.WsuNamespace));
  247. break;
  248. }
  249. }
  250. protected virtual void OnWriteStartHeaders (
  251. XmlDictionaryWriter writer)
  252. {
  253. var dic = Constants.SoapDictionary;
  254. writer.WriteStartElement ("s", dic.Add ("Header"), dic.Add (Version.Envelope.Namespace));
  255. }
  256. #region factory methods
  257. // 1) version, code, reason, action -> 3
  258. // 2) version, code, reason, detail, action -> 3
  259. // 3) version, fault, action -> SimpleMessage
  260. // 4) version, action, body -> 10 or 5
  261. // 5) version, action, body, formatter -> 10 or 9
  262. // 6) version, action, xmlReader -> 7
  263. // 7) version, action, reader -> 9
  264. // 8) xmlReader, maxSizeOfHeaders, version -> 11
  265. // 9) version, action, body -> SimpleMessage
  266. // 10) version, action -> EmptyMessage
  267. // 11) reader, maxSizeOfHeaders, version -> XmlReaderMessage
  268. // 1)
  269. public static Message CreateMessage (MessageVersion version,
  270. FaultCode code, string reason, string action)
  271. {
  272. MessageFault fault = MessageFault.CreateFault (code, reason);
  273. return CreateMessage (version, fault, action);
  274. }
  275. // 2)
  276. public static Message CreateMessage (MessageVersion version,
  277. FaultCode code, string reason, object detail,
  278. string action)
  279. {
  280. MessageFault fault = MessageFault.CreateFault (
  281. code, new FaultReason (reason), detail);
  282. return CreateMessage (version, fault, action);
  283. }
  284. // 3)
  285. public static Message CreateMessage (MessageVersion version,
  286. MessageFault fault, string action)
  287. {
  288. return new SimpleMessage (version, action,
  289. new MessageFaultBodyWriter (fault, version), true, empty_attributes);
  290. }
  291. // 4)
  292. public static Message CreateMessage (MessageVersion version,
  293. string action, object body)
  294. {
  295. return body == null ?
  296. CreateMessage (version, action) :
  297. CreateMessage (version, action, body, new DataContractSerializer (body.GetType ()));
  298. }
  299. // 5)
  300. public static Message CreateMessage (MessageVersion version,
  301. string action, object body, XmlObjectSerializer xmlFormatter)
  302. {
  303. return body == null ?
  304. CreateMessage (version, action) :
  305. CreateMessage (
  306. version, action,
  307. new XmlObjectSerializerBodyWriter (body, xmlFormatter));
  308. }
  309. // 6)
  310. public static Message CreateMessage (MessageVersion version,
  311. string action, XmlReader body)
  312. {
  313. return CreateMessage (version, action,
  314. XmlDictionaryReader.CreateDictionaryReader (body));
  315. }
  316. // 7)
  317. public static Message CreateMessage (MessageVersion version,
  318. string action, XmlDictionaryReader body)
  319. {
  320. return CreateMessage (version, action,
  321. new XmlReaderBodyWriter (body));
  322. }
  323. // 8)
  324. public static Message CreateMessage (XmlReader envelopeReader,
  325. int maxSizeOfHeaders, MessageVersion version)
  326. {
  327. return CreateMessage (
  328. XmlDictionaryReader.CreateDictionaryReader (envelopeReader),
  329. maxSizeOfHeaders,
  330. version);
  331. }
  332. // Core implementations of CreateMessage.
  333. static readonly Dictionary<XmlQualifiedName,string> empty_attributes = new Dictionary<XmlQualifiedName,string> ();
  334. // 9)
  335. public static Message CreateMessage (MessageVersion version,
  336. string action, BodyWriter body)
  337. {
  338. if (version == null)
  339. throw new ArgumentNullException ("version");
  340. if (body == null)
  341. throw new ArgumentNullException ("body");
  342. return new SimpleMessage (version, action, body, false, empty_attributes);
  343. }
  344. // 10)
  345. public static Message CreateMessage (MessageVersion version,
  346. string action)
  347. {
  348. if (version == null)
  349. throw new ArgumentNullException ("version");
  350. return new EmptyMessage (version, action);
  351. }
  352. // 11)
  353. public static Message CreateMessage (
  354. XmlDictionaryReader envelopeReader,
  355. int maxSizeOfHeaders,
  356. MessageVersion version)
  357. {
  358. if (envelopeReader == null)
  359. throw new ArgumentNullException ("envelopeReader");
  360. if (version == null)
  361. throw new ArgumentNullException ("version");
  362. return new XmlReaderMessage (version,
  363. envelopeReader, maxSizeOfHeaders);
  364. }
  365. #endregion
  366. }
  367. }