SoapMessage.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. using System.IO;
  11. using System.Web.Services;
  12. namespace System.Web.Services.Protocols {
  13. public abstract class SoapMessage {
  14. #region Fields
  15. string content_type = "text/xml";
  16. SoapException exception = null;
  17. SoapHeaderCollection headers;
  18. SoapMessageStage stage;
  19. Stream stream;
  20. object[] inParameters;
  21. object[] outParameters;
  22. #endregion // Fields
  23. #region Constructors
  24. internal SoapMessage ()
  25. {
  26. headers = new SoapHeaderCollection ();
  27. }
  28. internal SoapMessage (Stream stream)
  29. {
  30. this.stream = stream;
  31. }
  32. internal SoapMessage (Stream stream, SoapException exception)
  33. {
  34. this.exception = exception;
  35. this.stream = stream;
  36. headers = new SoapHeaderCollection ();
  37. }
  38. #endregion
  39. #region Properties
  40. internal object[] InParameters
  41. {
  42. get { return inParameters; }
  43. set { inParameters = value; }
  44. }
  45. internal object[] OutParameters
  46. {
  47. get { return outParameters; }
  48. set { outParameters = value; }
  49. }
  50. public abstract string Action
  51. {
  52. get;
  53. }
  54. public string ContentType {
  55. get { return content_type; }
  56. set { content_type = value; }
  57. }
  58. public SoapException Exception {
  59. get { return exception; }
  60. }
  61. public SoapHeaderCollection Headers {
  62. get { return headers; }
  63. }
  64. public abstract LogicalMethodInfo MethodInfo {
  65. get;
  66. }
  67. public abstract bool OneWay {
  68. get;
  69. }
  70. public SoapMessageStage Stage {
  71. get { return stage; }
  72. }
  73. internal void SetStage (SoapMessageStage stage)
  74. {
  75. this.stage = stage;
  76. }
  77. public Stream Stream {
  78. get {
  79. return stream;
  80. }
  81. }
  82. public abstract string Url {
  83. get;
  84. }
  85. #if NET_1_1
  86. [MonoTODO]
  87. public string ContentEncoding
  88. {
  89. get { throw new NotImplementedException (); }
  90. set { throw new NotImplementedException (); }
  91. }
  92. #endif
  93. #endregion Properties
  94. #region Methods
  95. protected abstract void EnsureInStage ();
  96. protected abstract void EnsureOutStage ();
  97. protected void EnsureStage (SoapMessageStage stage)
  98. {
  99. if ((((int) stage) & ((int) Stage)) == 0)
  100. throw new InvalidOperationException ("The current SoapMessageStage is not the asserted stage or stages.");
  101. }
  102. public object GetInParameterValue (int index)
  103. {
  104. return inParameters [index];
  105. }
  106. public object GetOutParameterValue (int index)
  107. {
  108. if (MethodInfo.IsVoid) return outParameters [index];
  109. else return outParameters [index + 1];
  110. }
  111. public object GetReturnValue ()
  112. {
  113. if (!MethodInfo.IsVoid && exception == null) return outParameters [0];
  114. else return null;
  115. }
  116. internal void SetHeaders (SoapHeaderCollection headers)
  117. {
  118. this.headers = headers;
  119. }
  120. internal void SetException (SoapException ex)
  121. {
  122. exception = ex;
  123. }
  124. internal void CollectHeaders (object target, HeaderInfo[] headers, SoapHeaderDirection direction)
  125. {
  126. Headers.Clear ();
  127. foreach (HeaderInfo hi in headers)
  128. {
  129. if ((hi.Direction & direction) != 0)
  130. {
  131. SoapHeader headerVal = hi.GetHeaderValue (target) as SoapHeader;
  132. if (headerVal != null)
  133. Headers.Add (headerVal);
  134. }
  135. }
  136. }
  137. internal void UpdateHeaderValues (object target, HeaderInfo[] headersInfo)
  138. {
  139. foreach (SoapHeader header in Headers)
  140. {
  141. HeaderInfo hinfo = FindHeader (headersInfo, header.GetType ());
  142. if (hinfo != null)
  143. hinfo.SetHeaderValue (target, header);
  144. else
  145. if (header.MustUnderstand)
  146. throw new SoapHeaderException ("Unknown header", SoapException.MustUnderstandFaultCode);
  147. header.DidUnderstand = false;
  148. }
  149. }
  150. HeaderInfo FindHeader (HeaderInfo[] headersInfo, Type headerType)
  151. {
  152. foreach (HeaderInfo headerInfo in headersInfo)
  153. if (headerInfo.HeaderType == headerType) return headerInfo;
  154. return null;
  155. }
  156. #endregion // Methods
  157. }
  158. }