SoapExtension.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. static ArrayList[] globalExtensions;
  52. internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs)
  53. {
  54. if (extensionConfigs == null) return null;
  55. SoapExtension[] res = new SoapExtension [extensionConfigs.Length];
  56. CreateExtensionChain (extensionConfigs, res, 0);
  57. return res;
  58. }
  59. internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] hiPrioExts, SoapExtensionRuntimeConfig[] medPrioExts, SoapExtensionRuntimeConfig[] lowPrioExts)
  60. {
  61. int len = 0;
  62. if (hiPrioExts != null) len += hiPrioExts.Length;
  63. if (medPrioExts != null) len += medPrioExts.Length;
  64. if (lowPrioExts != null) len += lowPrioExts.Length;
  65. if (len == 0) return null;
  66. SoapExtension[] res = new SoapExtension [len];
  67. int pos = 0;
  68. if (hiPrioExts != null) pos = CreateExtensionChain (hiPrioExts, res, pos);
  69. if (medPrioExts != null) pos = CreateExtensionChain (medPrioExts, res, pos);
  70. if (lowPrioExts != null) pos = CreateExtensionChain (lowPrioExts, res, pos);
  71. return res;
  72. }
  73. static int CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs, SoapExtension[] destArray, int pos)
  74. {
  75. for (int n=0; n<extensionConfigs.Length; n++)
  76. {
  77. SoapExtensionRuntimeConfig econf = extensionConfigs [n];
  78. SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
  79. ext.Initialize (econf.InitializationInfo);
  80. destArray [pos++] = ext;
  81. }
  82. return pos;
  83. }
  84. static void InitializeGlobalExtensions ()
  85. {
  86. globalExtensions = new ArrayList[2];
  87. ArrayList exts = WSConfig.Instance.ExtensionTypes;
  88. if (exts == null) return;
  89. foreach (WSExtensionConfig econf in exts)
  90. {
  91. if (globalExtensions [(int)econf.Group] == null) globalExtensions [(int)econf.Group] = new ArrayList ();
  92. ArrayList destList = globalExtensions [(int) econf.Group];
  93. bool added = false;
  94. for (int n=0; n<destList.Count && !added; n++)
  95. if (((WSExtensionConfig)destList [n]).Priority > econf.Priority) {
  96. destList.Insert (n, econf);
  97. added = true;
  98. }
  99. if (!added) destList.Add (econf);
  100. }
  101. }
  102. internal static SoapExtensionRuntimeConfig[][] GetTypeExtensions (Type serviceType)
  103. {
  104. if (globalExtensions == null) InitializeGlobalExtensions();
  105. SoapExtensionRuntimeConfig[][] exts = new SoapExtensionRuntimeConfig[2][];
  106. for (int group = 0; group < 2; group++)
  107. {
  108. ArrayList globList = globalExtensions[group];
  109. if (globList == null) continue;
  110. exts [group] = new SoapExtensionRuntimeConfig [globList.Count];
  111. for (int n=0; n<globList.Count; n++)
  112. {
  113. WSExtensionConfig econf = (WSExtensionConfig) globList [n];
  114. SoapExtensionRuntimeConfig typeconf = new SoapExtensionRuntimeConfig ();
  115. typeconf.Type = econf.Type;
  116. SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
  117. typeconf.InitializationInfo = ext.GetInitializer (serviceType);
  118. exts [group][n] = typeconf;
  119. }
  120. }
  121. return exts;
  122. }
  123. internal static SoapExtensionRuntimeConfig[] GetMethodExtensions (LogicalMethodInfo method)
  124. {
  125. object[] ats = method.GetCustomAttributes (typeof (SoapExtensionAttribute));
  126. SoapExtensionRuntimeConfig[] exts = new SoapExtensionRuntimeConfig [ats.Length];
  127. int[] priorities = new int[ats.Length];
  128. for (int n=0; n<ats.Length; n++)
  129. {
  130. SoapExtensionAttribute at = (SoapExtensionAttribute) ats[n];
  131. SoapExtensionRuntimeConfig econf = new SoapExtensionRuntimeConfig ();
  132. econf.Type = at.ExtensionType;
  133. priorities [n] = at.Priority;
  134. SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
  135. econf.InitializationInfo = ext.GetInitializer (method, at);
  136. exts [n] = econf;
  137. }
  138. Array.Sort (priorities, exts);
  139. return exts;
  140. }
  141. internal static Stream ExecuteChainStream (SoapExtension[] extensions, Stream stream)
  142. {
  143. if (extensions == null) return stream;
  144. Stream newStream = stream;
  145. foreach (SoapExtension ext in extensions)
  146. newStream = ext.ChainStream (newStream);
  147. return newStream;
  148. }
  149. internal static void ExecuteProcessMessage(SoapExtension[] extensions, SoapMessage message, bool inverseOrder)
  150. {
  151. if (extensions == null) return;
  152. if (inverseOrder)
  153. {
  154. for (int n = extensions.Length-1; n >= 0; n--)
  155. extensions[n].ProcessMessage (message);
  156. }
  157. else
  158. {
  159. for (int n = 0; n < extensions.Length; n++)
  160. extensions[n].ProcessMessage (message);
  161. }
  162. }
  163. #endregion // Methods
  164. }
  165. internal class SoapExtensionRuntimeConfig
  166. {
  167. public Type Type;
  168. public int Priority;
  169. public object InitializationInfo;
  170. }
  171. }