2 // System.Web.Services.Protocols.SoapExtension.cs
\r
5 // Tim Coleman (tim@timcoleman.com)
\r
7 // Copyright (C) Tim Coleman, 2002
\r
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
\r
33 using System.Web.Services.Configuration;
\r
35 namespace System.Web.Services.Protocols {
\r
36 public abstract class SoapExtension {
\r
44 #region Constructors
\r
46 protected SoapExtension ()
\r
50 #endregion // Constructors
\r
54 public virtual Stream ChainStream (Stream stream)
\r
59 public abstract object GetInitializer (Type serviceType);
\r
60 public abstract object GetInitializer (LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute);
\r
61 public abstract void Initialize (object initializer);
\r
62 public abstract void ProcessMessage (SoapMessage message);
\r
66 static ArrayList[] globalExtensions;
\r
68 static ArrayList[] globalExtensions {
70 return (ArrayList[])AppDomain.CurrentDomain.GetData("SoapExtension.globalExtensions");
73 AppDomain.CurrentDomain.SetData("SoapExtension.globalExtensions", value);
78 internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs)
\r
80 if (extensionConfigs == null) return null;
\r
81 SoapExtension[] res = new SoapExtension [extensionConfigs.Length];
\r
82 CreateExtensionChain (extensionConfigs, res, 0);
\r
86 internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] hiPrioExts, SoapExtensionRuntimeConfig[] medPrioExts, SoapExtensionRuntimeConfig[] lowPrioExts)
\r
89 if (hiPrioExts != null) len += hiPrioExts.Length;
\r
90 if (medPrioExts != null) len += medPrioExts.Length;
\r
91 if (lowPrioExts != null) len += lowPrioExts.Length;
\r
92 if (len == 0) return null;
\r
94 SoapExtension[] res = new SoapExtension [len];
\r
96 if (hiPrioExts != null) pos = CreateExtensionChain (hiPrioExts, res, pos);
\r
97 if (medPrioExts != null) pos = CreateExtensionChain (medPrioExts, res, pos);
\r
98 if (lowPrioExts != null) pos = CreateExtensionChain (lowPrioExts, res, pos);
\r
102 static int CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs, SoapExtension[] destArray, int pos)
\r
104 for (int n=0; n<extensionConfigs.Length; n++)
\r
106 SoapExtensionRuntimeConfig econf = extensionConfigs [n];
\r
107 SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
\r
108 ext.Initialize (econf.InitializationInfo);
\r
109 destArray [pos++] = ext;
\r
114 static void InitializeGlobalExtensions ()
\r
116 globalExtensions = new ArrayList[2];
\r
119 SoapExtensionTypeElementCollection exts = WebServicesSection.Instance.SoapExtensionTypes;
\r
121 ArrayList exts = WSConfig.Instance.ExtensionTypes;
\r
122 if (exts == null) return;
\r
126 foreach (SoapExtensionTypeElement econf in exts)
\r
128 foreach (WSExtensionConfig econf in exts)
\r
131 if (globalExtensions [(int)econf.Group] == null) globalExtensions [(int)econf.Group] = new ArrayList ();
\r
132 ArrayList destList = globalExtensions [(int) econf.Group];
\r
133 bool added = false;
\r
134 for (int n=0; n<destList.Count && !added; n++)
\r
136 if (((SoapExtensionTypeElement)destList [n]).Priority > econf.Priority) {
\r
138 if (((WSExtensionConfig)destList [n]).Priority > econf.Priority) {
\r
140 destList.Insert (n, econf);
\r
143 if (!added) destList.Add (econf);
\r
147 internal static SoapExtensionRuntimeConfig[][] GetTypeExtensions (Type serviceType)
\r
149 if (globalExtensions == null) InitializeGlobalExtensions();
\r
151 SoapExtensionRuntimeConfig[][] exts = new SoapExtensionRuntimeConfig[2][];
\r
153 for (int group = 0; group < 2; group++)
\r
155 ArrayList globList = globalExtensions[group];
\r
156 if (globList == null) continue;
\r
157 exts [group] = new SoapExtensionRuntimeConfig [globList.Count];
\r
158 for (int n=0; n<globList.Count; n++)
\r
161 SoapExtensionTypeElement econf = (SoapExtensionTypeElement) globList [n];
\r
163 WSExtensionConfig econf = (WSExtensionConfig) globList [n];
\r
165 SoapExtensionRuntimeConfig typeconf = new SoapExtensionRuntimeConfig ();
\r
166 typeconf.Type = econf.Type;
\r
167 SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
\r
168 typeconf.InitializationInfo = ext.GetInitializer (serviceType);
\r
169 exts [group][n] = typeconf;
\r
175 internal static SoapExtensionRuntimeConfig[] GetMethodExtensions (LogicalMethodInfo method)
\r
177 object[] ats = method.GetCustomAttributes (typeof (SoapExtensionAttribute));
\r
178 SoapExtensionRuntimeConfig[] exts = new SoapExtensionRuntimeConfig [ats.Length];
\r
179 int[] priorities = new int[ats.Length];
\r
181 for (int n=0; n<ats.Length; n++)
\r
183 SoapExtensionAttribute at = (SoapExtensionAttribute) ats[n];
\r
184 SoapExtensionRuntimeConfig econf = new SoapExtensionRuntimeConfig ();
\r
185 econf.Type = at.ExtensionType;
\r
186 priorities [n] = at.Priority;
\r
187 SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
\r
188 econf.InitializationInfo = ext.GetInitializer (method, at);
\r
191 Array.Sort (priorities, exts);
\r
195 internal static Stream ExecuteChainStream (SoapExtension[] extensions, Stream stream)
\r
197 if (extensions == null) return stream;
\r
199 Stream newStream = stream;
\r
200 foreach (SoapExtension ext in extensions)
\r
201 newStream = ext.ChainStream (newStream);
\r
205 internal static void ExecuteProcessMessage(SoapExtension[] extensions, SoapMessage message, Stream stream, bool inverseOrder)
\r
207 if (extensions == null) return;
\r
209 message.InternalStream = stream;
\r
213 for (int n = extensions.Length-1; n >= 0; n--)
\r
214 extensions[n].ProcessMessage (message);
\r
218 for (int n = 0; n < extensions.Length; n++)
\r
219 extensions[n].ProcessMessage (message);
\r
223 #endregion // Methods
\r
226 internal class SoapExtensionRuntimeConfig
\r
229 public int Priority;
\r
230 public object InitializationInfo;
\r