SoapMessage.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 HeaderInfo = System.Web.Services.Protocols.SoapHeaderMapping;
  31. using System.ComponentModel;
  32. using System.IO;
  33. using System.Web.Services;
  34. namespace System.Web.Services.Protocols {
  35. public abstract class SoapMessage {
  36. #region Fields
  37. string content_type = "text/xml";
  38. string content_encoding;
  39. SoapException exception = null;
  40. SoapHeaderCollection headers;
  41. SoapMessageStage stage;
  42. Stream stream;
  43. object[] inParameters;
  44. object[] outParameters;
  45. #if NET_2_0
  46. SoapProtocolVersion soapVersion;
  47. #endif
  48. #endregion // Fields
  49. #region Constructors
  50. internal SoapMessage ()
  51. {
  52. headers = new SoapHeaderCollection ();
  53. }
  54. internal SoapMessage (Stream stream, SoapException exception)
  55. {
  56. this.exception = exception;
  57. this.stream = stream;
  58. headers = new SoapHeaderCollection ();
  59. }
  60. #endregion
  61. #region Properties
  62. internal object[] InParameters
  63. {
  64. get { return inParameters; }
  65. set { inParameters = value; }
  66. }
  67. internal object[] OutParameters
  68. {
  69. get { return outParameters; }
  70. set { outParameters = value; }
  71. }
  72. public abstract string Action
  73. {
  74. get;
  75. }
  76. public string ContentType {
  77. get { return content_type; }
  78. set { content_type = value; }
  79. }
  80. public SoapException Exception {
  81. get { return exception; }
  82. #if NET_2_0
  83. set { exception = value; }
  84. #endif
  85. }
  86. public SoapHeaderCollection Headers {
  87. get { return headers; }
  88. }
  89. public abstract LogicalMethodInfo MethodInfo {
  90. get;
  91. }
  92. public abstract bool OneWay {
  93. get;
  94. }
  95. public SoapMessageStage Stage {
  96. get { return stage; }
  97. }
  98. internal void SetStage (SoapMessageStage stage)
  99. {
  100. this.stage = stage;
  101. }
  102. public Stream Stream {
  103. get {
  104. return stream;
  105. }
  106. }
  107. public abstract string Url {
  108. get;
  109. }
  110. #if NET_1_1
  111. public string ContentEncoding
  112. {
  113. get { return content_encoding; }
  114. set { content_encoding = value; }
  115. }
  116. #else
  117. internal string ContentEncoding
  118. {
  119. get { return content_encoding; }
  120. set { content_encoding = value; }
  121. }
  122. #endif
  123. #if NET_2_0
  124. [System.Runtime.InteropServices.ComVisible(false)]
  125. [DefaultValue (SoapProtocolVersion.Default)]
  126. public virtual SoapProtocolVersion SoapVersion {
  127. get { return soapVersion; }
  128. }
  129. #endif
  130. internal Stream InternalStream
  131. {
  132. // for getter use public stream property
  133. set {
  134. stream = value;
  135. }
  136. }
  137. #endregion Properties
  138. #region Methods
  139. protected abstract void EnsureInStage ();
  140. protected abstract void EnsureOutStage ();
  141. protected void EnsureStage (SoapMessageStage stage)
  142. {
  143. if ((((int) stage) & ((int) Stage)) == 0)
  144. throw new InvalidOperationException ("The current SoapMessageStage is not the asserted stage or stages.");
  145. }
  146. public object GetInParameterValue (int index)
  147. {
  148. return inParameters [index];
  149. }
  150. public object GetOutParameterValue (int index)
  151. {
  152. if (MethodInfo.IsVoid) return outParameters [index];
  153. else return outParameters [index + 1];
  154. }
  155. public object GetReturnValue ()
  156. {
  157. if (!MethodInfo.IsVoid && exception == null) return outParameters [0];
  158. else return null;
  159. }
  160. internal void SetHeaders (SoapHeaderCollection headers)
  161. {
  162. this.headers = headers;
  163. }
  164. internal void SetException (SoapException ex)
  165. {
  166. exception = ex;
  167. }
  168. internal void CollectHeaders (object target, HeaderInfo[] headers, SoapHeaderDirection direction)
  169. {
  170. Headers.Clear ();
  171. foreach (HeaderInfo hi in headers)
  172. {
  173. if ((hi.Direction & direction) != 0 && !hi.Custom)
  174. {
  175. SoapHeader headerVal = hi.GetHeaderValue (target) as SoapHeader;
  176. if (headerVal != null)
  177. Headers.Add (headerVal);
  178. }
  179. }
  180. }
  181. internal void UpdateHeaderValues (object target, HeaderInfo[] headersInfo)
  182. {
  183. foreach (SoapHeader header in Headers)
  184. {
  185. HeaderInfo hinfo = FindHeader (headersInfo, header.GetType ());
  186. if (hinfo != null) {
  187. hinfo.SetHeaderValue (target, header);
  188. header.DidUnderstand = !hinfo.Custom;
  189. }
  190. }
  191. }
  192. HeaderInfo FindHeader (HeaderInfo[] headersInfo, Type headerType)
  193. {
  194. HeaderInfo unknownHeaderInfo = null;
  195. foreach (HeaderInfo headerInfo in headersInfo) {
  196. if (headerInfo.HeaderType == headerType)
  197. return headerInfo;
  198. else if (headerInfo.Custom)
  199. unknownHeaderInfo = headerInfo;
  200. }
  201. return unknownHeaderInfo;
  202. }
  203. #endregion // Methods
  204. }
  205. }