MessageImpl.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // MessageImpl.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 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.Runtime.Serialization;
  30. using System.Xml;
  31. using System.IO;
  32. namespace System.ServiceModel.Channels
  33. {
  34. // Apparently Microsoft should have split Message class into
  35. // two diffferent public classes such as IncomingMessage and
  36. // OutgoingMessage.
  37. internal class XmlReaderMessage : Message
  38. {
  39. MessageVersion version;
  40. XmlDictionaryReader reader;
  41. MessageHeaders headers;
  42. MessageProperties properties = new MessageProperties ();
  43. bool is_empty, is_fault, body_started, body_consumed;
  44. int max_headers;
  45. string body;
  46. public XmlReaderMessage (MessageVersion version, XmlDictionaryReader reader, int maxSizeOfHeaders)
  47. {
  48. this.version = version;
  49. this.reader = reader;
  50. this.max_headers = maxSizeOfHeaders;
  51. ReadEnvelopeStart ();
  52. // Headers and IsEmpty are consumed at this stage.
  53. // Body content is not.
  54. ReadBodyStart ();
  55. StringWriter sw = new StringWriter ();
  56. using (XmlDictionaryWriter bodyXml = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw))) {
  57. while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement)
  58. bodyXml.WriteNode (reader, false);
  59. }
  60. this.body = sw.ToString ();
  61. }
  62. public override MessageHeaders Headers {
  63. get {
  64. if (headers == null)
  65. ReadHeaders ();
  66. return headers;
  67. }
  68. }
  69. public override bool IsEmpty {
  70. get {
  71. if (!body_started)
  72. ReadBodyStart ();
  73. return is_empty;
  74. }
  75. }
  76. public override bool IsFault {
  77. get {
  78. if (!body_started)
  79. ReadBodyStart ();
  80. return is_fault;
  81. }
  82. }
  83. public override MessageProperties Properties {
  84. get { return properties; }
  85. }
  86. public override MessageVersion Version {
  87. get { return version; }
  88. }
  89. protected override string OnGetBodyAttribute (
  90. string localName, string ns)
  91. {
  92. if (headers == null)
  93. ReadHeaders ();
  94. return reader.GetAttribute (localName, ns);
  95. }
  96. protected override XmlDictionaryReader OnGetReaderAtBodyContents ()
  97. {
  98. XmlDictionaryReader newReader = XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (this.body)));
  99. newReader.MoveToContent();
  100. return newReader;
  101. }
  102. protected override void OnWriteBodyContents (
  103. XmlDictionaryWriter writer)
  104. {
  105. XmlDictionaryReader reader = GetReaderAtBodyContents ();
  106. while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement)
  107. writer.WriteNode (reader, false);
  108. }
  109. static readonly char [] whitespaceChars = new char [] {' ', '\t', '\r', '\n'};
  110. void ReadEnvelopeStart ()
  111. {
  112. reader.MoveToContent ();
  113. if (reader.IsEmptyElement)
  114. throw new ArgumentException ("Missing message content XML.");
  115. reader.ReadStartElement ("Envelope", Version.Envelope.Namespace);
  116. // SOAP Header
  117. reader.MoveToContent ();
  118. }
  119. void ReadHeaders ()
  120. {
  121. if (headers != null)
  122. throw new InvalidOperationException ("XmlReader at headers is already consumed.");
  123. string envNS = Version.Envelope.Namespace;
  124. headers = new MessageHeaders (version, max_headers);
  125. if (reader.LocalName != "Header" || reader.NamespaceURI != envNS)
  126. return;
  127. bool isEmptyHeader = reader.IsEmptyElement;
  128. reader.ReadStartElement ("Header", envNS);
  129. reader.MoveToContent ();
  130. if (isEmptyHeader)
  131. return;
  132. #if NET_2_1 // FIXME: implement CreateInternalHeader() without XmlElement
  133. throw new NotImplementedException ();
  134. #else
  135. XmlDocument doc = null;
  136. while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement) {
  137. if (doc == null)
  138. doc = new XmlDocument ();
  139. XmlElement el = doc.ReadNode (reader) as XmlElement;
  140. if (el != null)
  141. headers.Add (MessageHeader.CreateInternalHeader (el, envNS));
  142. // FIXME: handle UnderstoodHeaders as well.
  143. reader.MoveToContent ();
  144. }
  145. #endif
  146. reader.ReadEndElement ();
  147. reader.MoveToContent ();
  148. }
  149. void ReadBodyStart ()
  150. {
  151. // read headers in advance.
  152. if (headers == null)
  153. ReadHeaders ();
  154. // SOAP Body
  155. body_started = true;
  156. is_empty = reader.IsEmptyElement;
  157. if (reader.MoveToAttribute ("Id", Constants.WsuNamespace)) {
  158. BodyId = reader.Value;
  159. reader.MoveToElement ();
  160. }
  161. reader.ReadStartElement ("Body", Version.Envelope.Namespace);
  162. if (reader.NodeType == XmlNodeType.EndElement) {
  163. is_empty = true;
  164. reader.Read ();
  165. } else {
  166. reader.MoveToContent ();
  167. if (reader.NodeType == XmlNodeType.Element &&
  168. reader.LocalName == "Fault" &&
  169. reader.NamespaceURI == Version.Envelope.Namespace)
  170. is_fault = true;
  171. }
  172. }
  173. }
  174. internal abstract class MessageImplBase : Message
  175. {
  176. MessageHeaders headers;
  177. MessageProperties properties = new MessageProperties ();
  178. public MessageImplBase (MessageVersion version, string action)
  179. {
  180. headers = new MessageHeaders (version);
  181. if (action != null)
  182. headers.Action = action;
  183. }
  184. public override MessageHeaders Headers {
  185. get { return headers; }
  186. }
  187. public override MessageProperties Properties {
  188. get { return properties; }
  189. }
  190. public override MessageVersion Version {
  191. get { return Headers.MessageVersion; }
  192. }
  193. }
  194. internal class EmptyMessage : MessageImplBase
  195. {
  196. public EmptyMessage (MessageVersion version, string action)
  197. : base (version, action)
  198. {
  199. }
  200. public override bool IsEmpty {
  201. get { return true; }
  202. }
  203. protected override void OnWriteBodyContents (
  204. XmlDictionaryWriter writer)
  205. {
  206. }
  207. protected override MessageBuffer OnCreateBufferedCopy (
  208. int maxBufferSize)
  209. {
  210. return new DefaultMessageBuffer (Headers, Properties);
  211. }
  212. }
  213. internal class SimpleMessage : MessageImplBase
  214. {
  215. BodyWriter body;
  216. bool is_fault;
  217. public SimpleMessage (MessageVersion version,
  218. string action, BodyWriter body, bool isFault)
  219. : base (version, action)
  220. {
  221. this.body = body;
  222. this.is_fault = isFault;
  223. }
  224. public override bool IsEmpty {
  225. get { return false; }
  226. }
  227. public override bool IsFault {
  228. get { return is_fault; }
  229. }
  230. protected override void OnWriteBodyContents (
  231. XmlDictionaryWriter writer)
  232. {
  233. body.WriteBodyContents (writer);
  234. }
  235. }
  236. }