SoapMessage.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // System.Web.Services.Protocols.SoapMessage.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.IO;
  31. using System.Web.Services;
  32. namespace System.Web.Services.Protocols {
  33. public abstract class SoapMessage {
  34. #region Fields
  35. string content_type = "text/xml";
  36. string content_encoding;
  37. SoapException exception = null;
  38. SoapHeaderCollection headers;
  39. SoapMessageStage stage;
  40. Stream stream;
  41. object[] inParameters;
  42. object[] outParameters;
  43. #if NET_2_0
  44. SoapProtocolVersion soapVersion;
  45. #endif
  46. #endregion // Fields
  47. #region Constructors
  48. internal SoapMessage ()
  49. {
  50. headers = new SoapHeaderCollection ();
  51. }
  52. internal SoapMessage (Stream stream)
  53. {
  54. this.stream = stream;
  55. }
  56. internal SoapMessage (Stream stream, SoapException exception)
  57. {
  58. this.exception = exception;
  59. this.stream = stream;
  60. headers = new SoapHeaderCollection ();
  61. }
  62. #endregion
  63. #region Properties
  64. internal object[] InParameters
  65. {
  66. get { return inParameters; }
  67. set { inParameters = value; }
  68. }
  69. internal object[] OutParameters
  70. {
  71. get { return outParameters; }
  72. set { outParameters = value; }
  73. }
  74. public abstract string Action
  75. {
  76. get;
  77. }
  78. public string ContentType {
  79. get { return content_type; }
  80. set { content_type = value; }
  81. }
  82. public SoapException Exception {
  83. get { return exception; }
  84. #if NET_2_0
  85. set { exception = value; }
  86. #endif
  87. }
  88. public SoapHeaderCollection Headers {
  89. get { return headers; }
  90. }
  91. public abstract LogicalMethodInfo MethodInfo {
  92. get;
  93. }
  94. public abstract bool OneWay {
  95. get;
  96. }
  97. public SoapMessageStage Stage {
  98. get { return stage; }
  99. }
  100. internal void SetStage (SoapMessageStage stage)
  101. {
  102. this.stage = stage;
  103. }
  104. public Stream Stream {
  105. get {
  106. return stream;
  107. }
  108. }
  109. public abstract string Url {
  110. get;
  111. }
  112. #if NET_1_1
  113. public string ContentEncoding
  114. {
  115. get { return content_encoding; }
  116. set { content_encoding = value; }
  117. }
  118. #else
  119. internal string ContentEncoding
  120. {
  121. get { return content_encoding; }
  122. set { content_encoding = value; }
  123. }
  124. #endif
  125. #if NET_2_0
  126. [System.Runtime.InteropServices.ComVisible(false)]
  127. public virtual SoapProtocolVersion SoapVersion {
  128. get { return soapVersion; }
  129. }
  130. #endif
  131. #endregion Properties
  132. #region Methods
  133. protected abstract void EnsureInStage ();
  134. protected abstract void EnsureOutStage ();
  135. protected void EnsureStage (SoapMessageStage stage)
  136. {
  137. if ((((int) stage) & ((int) Stage)) == 0)
  138. throw new InvalidOperationException ("The current SoapMessageStage is not the asserted stage or stages.");
  139. }
  140. public object GetInParameterValue (int index)
  141. {
  142. return inParameters [index];
  143. }
  144. public object GetOutParameterValue (int index)
  145. {
  146. if (MethodInfo.IsVoid) return outParameters [index];
  147. else return outParameters [index + 1];
  148. }
  149. public object GetReturnValue ()
  150. {
  151. if (!MethodInfo.IsVoid && exception == null) return outParameters [0];
  152. else return null;
  153. }
  154. internal void SetHeaders (SoapHeaderCollection headers)
  155. {
  156. this.headers = headers;
  157. }
  158. internal void SetException (SoapException ex)
  159. {
  160. exception = ex;
  161. }
  162. internal void CollectHeaders (object target, HeaderInfo[] headers, SoapHeaderDirection direction)
  163. {
  164. Headers.Clear ();
  165. foreach (HeaderInfo hi in headers)
  166. {
  167. if ((hi.Direction & direction) != 0 && !hi.IsUnknownHeader)
  168. {
  169. SoapHeader headerVal = hi.GetHeaderValue (target) as SoapHeader;
  170. if (headerVal != null)
  171. Headers.Add (headerVal);
  172. }
  173. }
  174. }
  175. internal void UpdateHeaderValues (object target, HeaderInfo[] headersInfo)
  176. {
  177. foreach (SoapHeader header in Headers)
  178. {
  179. HeaderInfo hinfo = FindHeader (headersInfo, header.GetType ());
  180. if (hinfo != null) {
  181. hinfo.SetHeaderValue (target, header);
  182. header.DidUnderstand = !hinfo.IsUnknownHeader;
  183. }
  184. }
  185. }
  186. HeaderInfo FindHeader (HeaderInfo[] headersInfo, Type headerType)
  187. {
  188. HeaderInfo unknownHeaderInfo = null;
  189. foreach (HeaderInfo headerInfo in headersInfo) {
  190. if (headerInfo.HeaderType == headerType)
  191. return headerInfo;
  192. else if (headerInfo.IsUnknownHeader)
  193. unknownHeaderInfo = headerInfo;
  194. }
  195. return unknownHeaderInfo;
  196. }
  197. #endregion // Methods
  198. }
  199. }