Message.cs 13 KB

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