Message.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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", XmlSchema.InstanceNamespace, "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. DTMXPathDocumentWriter2 pw = new DTMXPathDocumentWriter2 (new NameTable (), 100);
  187. XmlDictionaryWriter w = XmlDictionaryWriter.CreateDictionaryWriter (pw);
  188. WriteMessage (w);
  189. return new XPathMessageBuffer (pw.CreateDocument (), Version, Headers.Count, this.Properties);
  190. }
  191. protected virtual string OnGetBodyAttribute (
  192. string localName, string ns)
  193. {
  194. // other than XmlReaderMessage it cannot return anything
  195. return null;
  196. }
  197. [MonoTODO]
  198. protected virtual XmlDictionaryReader OnGetReaderAtBodyContents ()
  199. {
  200. XmlDictionaryWriter writer = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (TextWriter.Null));
  201. if (Version.Envelope != EnvelopeVersion.None) {
  202. WriteStartEnvelope (writer);
  203. OnWriteStartHeaders (writer);
  204. for (int i = 0, count = Headers.Count; i < count; i++)
  205. Headers.WriteHeader (i, writer);
  206. writer.WriteEndElement ();
  207. WriteStartBody (writer);
  208. }
  209. StringWriter sw = new StringWriter ();
  210. using (XmlDictionaryWriter body = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw))) {
  211. WriteBodyContents (body);
  212. }
  213. if (Version.Envelope != EnvelopeVersion.None) {
  214. writer.WriteEndElement ();
  215. writer.WriteEndElement ();
  216. }
  217. return XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (sw.ToString ())));
  218. }
  219. protected abstract void OnWriteBodyContents (
  220. XmlDictionaryWriter writer);
  221. protected virtual void OnWriteMessage (
  222. XmlDictionaryWriter writer)
  223. {
  224. if (Version.Envelope != EnvelopeVersion.None) {
  225. WriteStartEnvelope (writer);
  226. OnWriteStartHeaders (writer);
  227. for (int i = 0, count = Headers.Count; i < count; i++)
  228. Headers.WriteHeader (i, writer);
  229. writer.WriteEndElement ();
  230. }
  231. WriteBody (writer);
  232. if (Version.Envelope != EnvelopeVersion.None)
  233. writer.WriteEndElement ();
  234. }
  235. [MonoTODO]
  236. protected virtual void OnWriteStartBody (
  237. XmlDictionaryWriter writer)
  238. {
  239. writer.WriteStartElement ("s", "Body", Version.Envelope.Namespace);
  240. if (BodyId != null)
  241. writer.WriteAttributeString ("u", "Id", Constants.WsuNamespace, BodyId);
  242. }
  243. protected virtual void OnWriteStartEnvelope (
  244. XmlDictionaryWriter writer)
  245. {
  246. writer.WriteStartElement ("s", "Envelope", Version.Envelope.Namespace);
  247. if (Headers.Action != null && Version.Addressing.Namespace != MessageVersion.None.Addressing.Namespace)
  248. writer.WriteXmlnsAttribute ("a", Version.Addressing.Namespace);
  249. foreach (MessageHeaderInfo h in Headers)
  250. if (h.Id != null) {
  251. writer.WriteXmlnsAttribute ("u", Constants.WsuNamespace);
  252. break;
  253. }
  254. }
  255. protected virtual void OnWriteStartHeaders (
  256. XmlDictionaryWriter writer)
  257. {
  258. writer.WriteStartElement ("s", "Header", Version.Envelope.Namespace);
  259. }
  260. #region factory methods
  261. // 1) fault -> 4
  262. // 2) action -> 5
  263. // 3) fault, action -> 10
  264. // 4) version, fault -> 10
  265. // 5) version, action -> EmptyMessage
  266. // 6) action, body -> 12
  267. // 7) action, xmlReader -> 8
  268. // 8) action, reader -> 16
  269. // 10) version, fault, action -> 20
  270. // 11) version, action, body -> 14
  271. // 12) action, body, formatter -> 14
  272. // 13) version, action, body -> 14
  273. // 14) version, action, body, formatter -> 20
  274. // 15) version, action, xmlReader -> 16
  275. // 16) version, action, reader -> 20
  276. // 17) xmlReader, maxSizeOfHeaders, version -> 18
  277. // 18) reader, maxSizeOfHeaders, version -> ForwardingMessage
  278. // 19) action, bodyWriter -> 20
  279. // 20) version, action, bodyWriter -> SimpleMessage
  280. public static Message CreateMessage (MessageVersion version,
  281. FaultCode code, string reason, string action)
  282. {
  283. MessageFault fault = MessageFault.CreateFault (code, reason);
  284. return CreateMessage (version, fault, action);
  285. }
  286. public static Message CreateMessage (MessageVersion version,
  287. FaultCode code, string reason, object detail,
  288. string action)
  289. {
  290. MessageFault fault = MessageFault.CreateFault (
  291. code, new FaultReason (reason), detail);
  292. return CreateMessage (version, fault, action);
  293. }
  294. public static Message CreateMessage (MessageVersion version,
  295. MessageFault fault, string action)
  296. {
  297. return new SimpleMessage (version, action,
  298. new MessageFaultBodyWriter (fault, version), true);
  299. }
  300. public static Message CreateMessage (MessageVersion version,
  301. string action, object body)
  302. {
  303. return body == null ?
  304. CreateMessage (version, action) :
  305. CreateMessage (version, action, body, new DataContractSerializer (body.GetType ()));
  306. }
  307. public static Message CreateMessage (MessageVersion version,
  308. string action, object body, XmlObjectSerializer xmlFormatter)
  309. {
  310. return body == null ?
  311. CreateMessage (version, action) :
  312. CreateMessage (
  313. version, action,
  314. new XmlObjectSerializerBodyWriter (body, xmlFormatter));
  315. }
  316. public static Message CreateMessage (MessageVersion version,
  317. string action, XmlReader body)
  318. {
  319. return CreateMessage (version, action,
  320. XmlDictionaryReader.CreateDictionaryReader (body));
  321. }
  322. public static Message CreateMessage (MessageVersion version,
  323. string action, XmlDictionaryReader body)
  324. {
  325. return CreateMessage (version, action,
  326. new XmlReaderBodyWriter (body));
  327. }
  328. public static Message CreateMessage (XmlReader envelopeReader,
  329. int maxSizeOfHeaders, MessageVersion version)
  330. {
  331. return CreateMessage (
  332. XmlDictionaryReader.CreateDictionaryReader (envelopeReader),
  333. maxSizeOfHeaders,
  334. version);
  335. }
  336. // Core implementations of CreateMessage.
  337. public static Message CreateMessage (MessageVersion version,
  338. string action, BodyWriter body)
  339. {
  340. if (version == null)
  341. throw new ArgumentNullException ("version");
  342. if (body == null)
  343. throw new ArgumentNullException ("body");
  344. return new SimpleMessage (version, action, body, false);
  345. }
  346. public static Message CreateMessage (MessageVersion version,
  347. string action)
  348. {
  349. if (version == null)
  350. throw new ArgumentNullException ("version");
  351. return new EmptyMessage (version, action);
  352. }
  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. }