SoapExtension.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // System.Web.Services.Protocols.SoapExtension.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.IO;
  30. using System.Collections;
  31. using System.Web.Services.Configuration;
  32. namespace System.Web.Services.Protocols {
  33. public abstract class SoapExtension {
  34. #region Fields
  35. Stream stream;
  36. #endregion
  37. #region Constructors
  38. protected SoapExtension ()
  39. {
  40. }
  41. #endregion // Constructors
  42. #region Methods
  43. public virtual Stream ChainStream (Stream stream)
  44. {
  45. return stream;
  46. }
  47. public abstract object GetInitializer (Type serviceType);
  48. public abstract object GetInitializer (LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute);
  49. public abstract void Initialize (object initializer);
  50. public abstract void ProcessMessage (SoapMessage message);
  51. #if !TARGET_JVM
  52. static ArrayList[] globalExtensions;
  53. #else
  54. static ArrayList[] globalExtensions {
  55. get {
  56. return (ArrayList[])AppDomain.CurrentDomain.GetData("SoapExtension.globalExtensions");
  57. }
  58. set {
  59. AppDomain.CurrentDomain.SetData("SoapExtension.globalExtensions", value);
  60. }
  61. }
  62. #endif
  63. internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs)
  64. {
  65. if (extensionConfigs == null) return null;
  66. SoapExtension[] res = new SoapExtension [extensionConfigs.Length];
  67. CreateExtensionChain (extensionConfigs, res, 0);
  68. return res;
  69. }
  70. internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] hiPrioExts, SoapExtensionRuntimeConfig[] medPrioExts, SoapExtensionRuntimeConfig[] lowPrioExts)
  71. {
  72. int len = 0;
  73. if (hiPrioExts != null) len += hiPrioExts.Length;
  74. if (medPrioExts != null) len += medPrioExts.Length;
  75. if (lowPrioExts != null) len += lowPrioExts.Length;
  76. if (len == 0) return null;
  77. SoapExtension[] res = new SoapExtension [len];
  78. int pos = 0;
  79. if (hiPrioExts != null) pos = CreateExtensionChain (hiPrioExts, res, pos);
  80. if (medPrioExts != null) pos = CreateExtensionChain (medPrioExts, res, pos);
  81. if (lowPrioExts != null) pos = CreateExtensionChain (lowPrioExts, res, pos);
  82. return res;
  83. }
  84. static int CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs, SoapExtension[] destArray, int pos)
  85. {
  86. for (int n=0; n<extensionConfigs.Length; n++)
  87. {
  88. SoapExtensionRuntimeConfig econf = extensionConfigs [n];
  89. SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
  90. ext.Initialize (econf.InitializationInfo);
  91. destArray [pos++] = ext;
  92. }
  93. return pos;
  94. }
  95. static void InitializeGlobalExtensions ()
  96. {
  97. globalExtensions = new ArrayList[2];
  98. #if NET_2_0
  99. SoapExtensionTypeElementCollection exts = WebServicesSection.Current.SoapExtensionTypes;
  100. #else
  101. ArrayList exts = WSConfig.Instance.ExtensionTypes;
  102. if (exts == null) return;
  103. #endif
  104. #if NET_2_0
  105. foreach (SoapExtensionTypeElement econf in exts)
  106. #else
  107. foreach (WSExtensionConfig econf in exts)
  108. #endif
  109. {
  110. if (globalExtensions [(int)econf.Group] == null) globalExtensions [(int)econf.Group] = new ArrayList ();
  111. ArrayList destList = globalExtensions [(int) econf.Group];
  112. bool added = false;
  113. for (int n=0; n<destList.Count && !added; n++)
  114. #if NET_2_0
  115. if (((SoapExtensionTypeElement)destList [n]).Priority > econf.Priority) {
  116. #else
  117. if (((WSExtensionConfig)destList [n]).Priority > econf.Priority) {
  118. #endif
  119. destList.Insert (n, econf);
  120. added = true;
  121. }
  122. if (!added) destList.Add (econf);
  123. }
  124. }
  125. internal static SoapExtensionRuntimeConfig[][] GetTypeExtensions (Type serviceType)
  126. {
  127. if (globalExtensions == null) InitializeGlobalExtensions();
  128. SoapExtensionRuntimeConfig[][] exts = new SoapExtensionRuntimeConfig[2][];
  129. for (int group = 0; group < 2; group++)
  130. {
  131. ArrayList globList = globalExtensions[group];
  132. if (globList == null) continue;
  133. exts [group] = new SoapExtensionRuntimeConfig [globList.Count];
  134. for (int n=0; n<globList.Count; n++)
  135. {
  136. #if NET_2_0
  137. SoapExtensionTypeElement econf = (SoapExtensionTypeElement) globList [n];
  138. #else
  139. WSExtensionConfig econf = (WSExtensionConfig) globList [n];
  140. #endif
  141. SoapExtensionRuntimeConfig typeconf = new SoapExtensionRuntimeConfig ();
  142. typeconf.Type = econf.Type;
  143. SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
  144. typeconf.InitializationInfo = ext.GetInitializer (serviceType);
  145. exts [group][n] = typeconf;
  146. }
  147. }
  148. return exts;
  149. }
  150. internal static SoapExtensionRuntimeConfig[] GetMethodExtensions (LogicalMethodInfo method)
  151. {
  152. object[] ats = method.GetCustomAttributes (typeof (SoapExtensionAttribute));
  153. SoapExtensionRuntimeConfig[] exts = new SoapExtensionRuntimeConfig [ats.Length];
  154. int[] priorities = new int[ats.Length];
  155. for (int n=0; n<ats.Length; n++)
  156. {
  157. SoapExtensionAttribute at = (SoapExtensionAttribute) ats[n];
  158. SoapExtensionRuntimeConfig econf = new SoapExtensionRuntimeConfig ();
  159. econf.Type = at.ExtensionType;
  160. priorities [n] = at.Priority;
  161. SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
  162. econf.InitializationInfo = ext.GetInitializer (method, at);
  163. exts [n] = econf;
  164. }
  165. Array.Sort (priorities, exts);
  166. return exts;
  167. }
  168. internal static Stream ExecuteChainStream (SoapExtension[] extensions, Stream stream)
  169. {
  170. if (extensions == null) return stream;
  171. Stream newStream = stream;
  172. foreach (SoapExtension ext in extensions)
  173. newStream = ext.ChainStream (newStream);
  174. return newStream;
  175. }
  176. internal static void ExecuteProcessMessage(SoapExtension[] extensions, SoapMessage message, Stream stream, bool inverseOrder)
  177. {
  178. if (extensions == null) return;
  179. message.InternalStream = stream;
  180. if (inverseOrder)
  181. {
  182. for (int n = extensions.Length-1; n >= 0; n--)
  183. extensions[n].ProcessMessage (message);
  184. }
  185. else
  186. {
  187. for (int n = 0; n < extensions.Length; n++)
  188. extensions[n].ProcessMessage (message);
  189. }
  190. }
  191. #endregion // Methods
  192. }
  193. internal class SoapExtensionRuntimeConfig
  194. {
  195. public Type Type;
  196. public int Priority;
  197. public object InitializationInfo;
  198. }
  199. }