MessageFault.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. //
  2. // MessageFault.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005-2009 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. namespace System.ServiceModel.Channels
  34. {
  35. public abstract class MessageFault
  36. {
  37. // type members
  38. public static MessageFault CreateFault (Message message, int maxBufferSize)
  39. {
  40. try {
  41. if (message.Version.Envelope == EnvelopeVersion.Soap11)
  42. return CreateFault11 (message, maxBufferSize);
  43. else // common to None and SOAP12
  44. return CreateFault (message, maxBufferSize, message.Version.Envelope.Namespace);
  45. } catch (XmlException ex) {
  46. throw new CommunicationException ("Received an invalid SOAP Fault message", ex);
  47. }
  48. throw new InvalidOperationException ("The input message is not a SOAP envelope.");
  49. }
  50. static MessageFault CreateFault11 (Message message, int maxBufferSize)
  51. {
  52. FaultCode fc = null;
  53. FaultReason fr = null;
  54. object details = null;
  55. XmlDictionaryReader r = message.GetReaderAtBodyContents ();
  56. r.ReadStartElement ("Fault", message.Version.Envelope.Namespace);
  57. r.MoveToContent ();
  58. while (r.NodeType != XmlNodeType.EndElement) {
  59. switch (r.LocalName) {
  60. case "faultcode":
  61. fc = ReadFaultCode11 (r);
  62. break;
  63. case "faultstring":
  64. fr = new FaultReason (r.ReadElementContentAsString());
  65. break;
  66. case "detail":
  67. //BUGBUG: Handle children of type other than ExceptionDetail, in order to comply with
  68. // FaultContractAttribute.
  69. r.ReadStartElement ();
  70. r.MoveToContent();
  71. details = new DataContractSerializer (typeof (ExceptionDetail)).ReadObject (r);
  72. break;
  73. case "faultactor":
  74. default:
  75. throw new NotImplementedException ();
  76. }
  77. r.MoveToContent ();
  78. }
  79. r.ReadEndElement ();
  80. if (fr == null)
  81. throw new XmlException ("Reason is missing in the Fault message");
  82. if (details == null)
  83. return CreateFault (fc, fr);
  84. return CreateFault (fc, fr, details);
  85. }
  86. static MessageFault CreateFault (Message message, int maxBufferSize, string ns)
  87. {
  88. FaultCode fc = null;
  89. FaultReason fr = null;
  90. XmlDictionaryReader r = message.GetReaderAtBodyContents ();
  91. r.ReadStartElement ("Fault", ns);
  92. r.MoveToContent ();
  93. while (r.NodeType != XmlNodeType.EndElement) {
  94. switch (r.LocalName) {
  95. case "Code":
  96. fc = ReadFaultCode (r, ns);
  97. break;
  98. case "Reason":
  99. fr = ReadFaultReason (r, ns);
  100. break;
  101. default:
  102. throw new XmlException (String.Format ("Unexpected node {0} name {1}", r.NodeType, r.Name));
  103. }
  104. r.MoveToContent ();
  105. }
  106. if (fr == null)
  107. throw new XmlException ("Reason is missing in the Fault message");
  108. r.ReadEndElement ();
  109. return CreateFault (fc, fr);
  110. }
  111. static FaultCode ReadFaultCode11 (XmlDictionaryReader r)
  112. {
  113. FaultCode subcode = null;
  114. XmlQualifiedName value = XmlQualifiedName.Empty;
  115. if (r.IsEmptyElement)
  116. throw new ArgumentException ("Fault Code is mandatory in SOAP fault message.");
  117. r.ReadStartElement ("faultcode");
  118. r.MoveToContent ();
  119. while (r.NodeType != XmlNodeType.EndElement) {
  120. if (r.NodeType == XmlNodeType.Element)
  121. subcode = ReadFaultCode11 (r);
  122. else
  123. value = (XmlQualifiedName) r.ReadContentAs (typeof (XmlQualifiedName), r as IXmlNamespaceResolver);
  124. r.MoveToContent ();
  125. }
  126. r.ReadEndElement ();
  127. return new FaultCode (value.Name, value.Namespace, subcode);
  128. }
  129. static FaultCode ReadFaultCode (XmlDictionaryReader r, string ns)
  130. {
  131. FaultCode subcode = null;
  132. XmlQualifiedName value = XmlQualifiedName.Empty;
  133. if (r.IsEmptyElement)
  134. throw new ArgumentException ("either SubCode or Value element is mandatory in SOAP fault code.");
  135. r.ReadStartElement (); // could be either Code or SubCode
  136. r.MoveToContent ();
  137. while (r.NodeType != XmlNodeType.EndElement) {
  138. switch (r.LocalName) {
  139. case "Subcode":
  140. subcode = ReadFaultCode (r, ns);
  141. break;
  142. case "Value":
  143. value = (XmlQualifiedName) r.ReadElementContentAs (typeof (XmlQualifiedName), r as IXmlNamespaceResolver, "Value", ns);
  144. break;
  145. default:
  146. throw new ArgumentException (String.Format ("Unexpected Fault Code subelement: '{0}'", r.LocalName));
  147. }
  148. r.MoveToContent ();
  149. }
  150. r.ReadEndElement ();
  151. return new FaultCode (value.Name, value.Namespace, subcode);
  152. }
  153. static FaultReason ReadFaultReason (XmlDictionaryReader r, string ns)
  154. {
  155. List<FaultReasonText> l = new List<FaultReasonText> ();
  156. if (r.IsEmptyElement)
  157. throw new ArgumentException ("One or more Text element is mandatory in SOAP fault reason text.");
  158. r.ReadStartElement ("Reason", ns);
  159. for (r.MoveToContent ();
  160. r.NodeType != XmlNodeType.EndElement;
  161. r.MoveToContent ()) {
  162. string lang = r.GetAttribute ("lang", "http://www.w3.org/XML/1998/namespace");
  163. if (lang == null)
  164. throw new XmlException ("xml:lang is mandatory on fault reason Text");
  165. l.Add (new FaultReasonText (r.ReadElementContentAsString ("Text", ns), lang));
  166. }
  167. return new FaultReason (l);
  168. }
  169. public static MessageFault CreateFault (FaultCode code,
  170. string reason)
  171. {
  172. return CreateFault (code, new FaultReason (reason));
  173. }
  174. public static MessageFault CreateFault (FaultCode code,
  175. FaultReason reason)
  176. {
  177. return new SimpleMessageFault (code, reason,
  178. false, null, null, null, null);
  179. }
  180. public static MessageFault CreateFault (FaultCode code,
  181. FaultReason reason, object detail)
  182. {
  183. return new SimpleMessageFault (code, reason,
  184. true, detail, new DataContractSerializer (detail.GetType ()), null, null);
  185. }
  186. public static MessageFault CreateFault (FaultCode code,
  187. FaultReason reason, object detail,
  188. XmlObjectSerializer formatter)
  189. {
  190. return new SimpleMessageFault (code, reason, true,
  191. detail, formatter, String.Empty, String.Empty);
  192. }
  193. public static MessageFault CreateFault (FaultCode code,
  194. FaultReason reason, object detail,
  195. XmlObjectSerializer formatter, string actor)
  196. {
  197. return new SimpleMessageFault (code, reason,
  198. true, detail, formatter, actor, String.Empty);
  199. }
  200. public static MessageFault CreateFault (FaultCode code,
  201. FaultReason reason, object detail,
  202. XmlObjectSerializer formatter, string actor, string node)
  203. {
  204. return new SimpleMessageFault (code, reason,
  205. true, detail, formatter, actor, node);
  206. }
  207. // pretty simple implementation class
  208. internal class SimpleMessageFault : MessageFault
  209. {
  210. bool has_detail;
  211. string actor, node;
  212. FaultCode code;
  213. FaultReason reason;
  214. object detail;
  215. XmlObjectSerializer formatter;
  216. public SimpleMessageFault (FaultCode code,
  217. FaultReason reason, bool has_detail,
  218. object detail, XmlObjectSerializer formatter,
  219. string actor, string node)
  220. : this (code, reason, detail, formatter, actor, node)
  221. {
  222. this.has_detail = has_detail;
  223. }
  224. public SimpleMessageFault (FaultCode code,
  225. FaultReason reason,
  226. object detail, XmlObjectSerializer formatter,
  227. string actor, string node)
  228. {
  229. if (code == null)
  230. throw new ArgumentNullException ("code");
  231. if (reason == null)
  232. throw new ArgumentNullException ("reason");
  233. this.code = code;
  234. this.reason = reason;
  235. this.detail = detail;
  236. this.formatter = formatter;
  237. this.actor = actor;
  238. this.node = node;
  239. }
  240. public override string Actor {
  241. get { return actor; }
  242. }
  243. public override FaultCode Code {
  244. get { return code; }
  245. }
  246. public override bool HasDetail {
  247. // it is not simply "detail != null" since
  248. // null detail could become <ms:anyType xsi:nil="true" />
  249. get { return has_detail; }
  250. }
  251. public override string Node {
  252. get { return node; }
  253. }
  254. public override FaultReason Reason {
  255. get { return reason; }
  256. }
  257. protected override XmlDictionaryReader OnGetReaderAtDetailContents ()
  258. {
  259. // FIXME: use XmlObjectSerializer
  260. return base.OnGetReaderAtDetailContents ();
  261. }
  262. protected override void OnWriteDetailContents (XmlDictionaryWriter writer)
  263. {
  264. formatter.WriteObject (writer, detail);
  265. }
  266. public object Detail {
  267. get { return detail; }
  268. }
  269. }
  270. // instance members
  271. protected MessageFault ()
  272. {
  273. }
  274. [MonoTODO ("is this true?")]
  275. public virtual string Actor {
  276. get { return String.Empty; }
  277. }
  278. public abstract FaultCode Code { get; }
  279. public abstract bool HasDetail { get; }
  280. [MonoTODO ("is this true?")]
  281. public virtual string Node {
  282. get { return String.Empty; }
  283. }
  284. public abstract FaultReason Reason { get; }
  285. public T GetDetail<T> ()
  286. {
  287. return GetDetail<T> (new DataContractSerializer (typeof (T)));
  288. }
  289. public T GetDetail<T> (XmlObjectSerializer formatter)
  290. {
  291. if (!HasDetail)
  292. throw new InvalidOperationException ("This message does not have details.");
  293. return (T) formatter.ReadObject (GetReaderAtDetailContents ());
  294. }
  295. public XmlDictionaryReader GetReaderAtDetailContents ()
  296. {
  297. return OnGetReaderAtDetailContents ();
  298. }
  299. public void WriteTo (XmlDictionaryWriter writer,
  300. EnvelopeVersion version)
  301. {
  302. writer.WriteStartElement ("Fault", version.Namespace);
  303. WriteFaultCode (writer, version, Code, false);
  304. WriteReason (writer, version);
  305. if (HasDetail)
  306. OnWriteDetail (writer, version);
  307. writer.WriteEndElement ();
  308. }
  309. private void WriteFaultCode (XmlDictionaryWriter writer,
  310. EnvelopeVersion version, FaultCode code, bool sub)
  311. {
  312. if (version == EnvelopeVersion.Soap11) {
  313. writer.WriteStartElement ("", "faultcode", String.Empty);
  314. if (code.Namespace.Length > 0 && String.IsNullOrEmpty (writer.LookupPrefix (code.Namespace)))
  315. writer.WriteXmlnsAttribute ("a", code.Namespace);
  316. writer.WriteQualifiedName (code.Name, code.Namespace);
  317. writer.WriteEndElement ();
  318. } else { // Soap12
  319. writer.WriteStartElement (sub ? "Subcode" : "Code", version.Namespace);
  320. writer.WriteStartElement ("Value", version.Namespace);
  321. if (code.Namespace.Length > 0 && String.IsNullOrEmpty (writer.LookupPrefix (code.Namespace)))
  322. writer.WriteXmlnsAttribute ("a", code.Namespace);
  323. writer.WriteQualifiedName (code.Name, code.Namespace);
  324. writer.WriteEndElement ();
  325. if (code.SubCode != null)
  326. WriteFaultCode (writer, version, code.SubCode, true);
  327. writer.WriteEndElement ();
  328. }
  329. }
  330. private void WriteReason (XmlDictionaryWriter writer,
  331. EnvelopeVersion version)
  332. {
  333. if (version == EnvelopeVersion.Soap11) {
  334. foreach (FaultReasonText t in Reason.Translations) {
  335. writer.WriteStartElement ("", "faultstring", String.Empty);
  336. if (t.XmlLang != null)
  337. writer.WriteAttributeString ("xml", "lang", null, t.XmlLang);
  338. writer.WriteString (t.Text);
  339. writer.WriteEndElement ();
  340. }
  341. } else { // Soap12
  342. writer.WriteStartElement ("Reason", version.Namespace);
  343. foreach (FaultReasonText t in Reason.Translations) {
  344. writer.WriteStartElement ("Text", version.Namespace);
  345. if (t.XmlLang != null)
  346. writer.WriteAttributeString ("xml", "lang", null, t.XmlLang);
  347. writer.WriteString (t.Text);
  348. writer.WriteEndElement ();
  349. }
  350. writer.WriteEndElement ();
  351. }
  352. }
  353. public void WriteTo (XmlWriter writer, EnvelopeVersion version)
  354. {
  355. WriteTo (XmlDictionaryWriter.CreateDictionaryWriter (
  356. writer), version);
  357. }
  358. protected virtual XmlDictionaryReader OnGetReaderAtDetailContents ()
  359. {
  360. MemoryStream ms = new MemoryStream ();
  361. using (XmlDictionaryWriter dw =
  362. XmlDictionaryWriter.CreateDictionaryWriter (
  363. XmlWriter.Create (ms))) {
  364. OnWriteDetailContents (dw);
  365. }
  366. ms.Seek (0, SeekOrigin.Begin);
  367. return XmlDictionaryReader.CreateDictionaryReader (
  368. XmlReader.Create (ms));
  369. }
  370. protected virtual void OnWriteDetail (XmlDictionaryWriter writer, EnvelopeVersion version)
  371. {
  372. OnWriteStartDetail (writer, version);
  373. OnWriteDetailContents (writer);
  374. writer.WriteEndElement ();
  375. }
  376. protected virtual void OnWriteStartDetail (XmlDictionaryWriter writer, EnvelopeVersion version)
  377. {
  378. if (version == EnvelopeVersion.Soap11)
  379. writer.WriteStartElement ("detail", String.Empty);
  380. else // Soap12
  381. writer.WriteStartElement ("Detail", version.Namespace);
  382. }
  383. protected abstract void OnWriteDetailContents (XmlDictionaryWriter writer);
  384. }
  385. }