Message.cs 12 KB

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