SoapExtension.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. ArrayList exts = WSConfig.Instance.ExtensionTypes;
  99. if (exts == null) return;
  100. foreach (WSExtensionConfig econf in exts)
  101. {
  102. if (globalExtensions [(int)econf.Group] == null) globalExtensions [(int)econf.Group] = new ArrayList ();
  103. ArrayList destList = globalExtensions [(int) econf.Group];
  104. bool added = false;
  105. for (int n=0; n<destList.Count && !added; n++)
  106. if (((WSExtensionConfig)destList [n]).Priority > econf.Priority) {
  107. destList.Insert (n, econf);
  108. added = true;
  109. }
  110. if (!added) destList.Add (econf);
  111. }
  112. }
  113. internal static SoapExtensionRuntimeConfig[][] GetTypeExtensions (Type serviceType)
  114. {
  115. if (globalExtensions == null) InitializeGlobalExtensions();
  116. SoapExtensionRuntimeConfig[][] exts = new SoapExtensionRuntimeConfig[2][];
  117. for (int group = 0; group < 2; group++)
  118. {
  119. ArrayList globList = globalExtensions[group];
  120. if (globList == null) continue;
  121. exts [group] = new SoapExtensionRuntimeConfig [globList.Count];
  122. for (int n=0; n<globList.Count; n++)
  123. {
  124. WSExtensionConfig econf = (WSExtensionConfig) globList [n];
  125. SoapExtensionRuntimeConfig typeconf = new SoapExtensionRuntimeConfig ();
  126. typeconf.Type = econf.Type;
  127. SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
  128. typeconf.InitializationInfo = ext.GetInitializer (serviceType);
  129. exts [group][n] = typeconf;
  130. }
  131. }
  132. return exts;
  133. }
  134. internal static SoapExtensionRuntimeConfig[] GetMethodExtensions (LogicalMethodInfo method)
  135. {
  136. object[] ats = method.GetCustomAttributes (typeof (SoapExtensionAttribute));
  137. SoapExtensionRuntimeConfig[] exts = new SoapExtensionRuntimeConfig [ats.Length];
  138. int[] priorities = new int[ats.Length];
  139. for (int n=0; n<ats.Length; n++)
  140. {
  141. SoapExtensionAttribute at = (SoapExtensionAttribute) ats[n];
  142. SoapExtensionRuntimeConfig econf = new SoapExtensionRuntimeConfig ();
  143. econf.Type = at.ExtensionType;
  144. priorities [n] = at.Priority;
  145. SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
  146. econf.InitializationInfo = ext.GetInitializer (method, at);
  147. exts [n] = econf;
  148. }
  149. Array.Sort (priorities, exts);
  150. return exts;
  151. }
  152. internal static Stream ExecuteChainStream (SoapExtension[] extensions, Stream stream)
  153. {
  154. if (extensions == null) return stream;
  155. Stream newStream = stream;
  156. foreach (SoapExtension ext in extensions)
  157. newStream = ext.ChainStream (newStream);
  158. return newStream;
  159. }
  160. internal static void ExecuteProcessMessage(SoapExtension[] extensions, SoapMessage message, bool inverseOrder)
  161. {
  162. if (extensions == null) return;
  163. if (inverseOrder)
  164. {
  165. for (int n = extensions.Length-1; n >= 0; n--)
  166. extensions[n].ProcessMessage (message);
  167. }
  168. else
  169. {
  170. for (int n = 0; n < extensions.Length; n++)
  171. extensions[n].ProcessMessage (message);
  172. }
  173. }
  174. #endregion // Methods
  175. }
  176. internal class SoapExtensionRuntimeConfig
  177. {
  178. public Type Type;
  179. public int Priority;
  180. public object InitializationInfo;
  181. }
  182. }