New test.
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / SoapExtension.cs
1 // \r
2 // System.Web.Services.Protocols.SoapExtension.cs\r
3 //\r
4 // Author:\r
5 //   Tim Coleman (tim@timcoleman.com)\r
6 //\r
7 // Copyright (C) Tim Coleman, 2002\r
8 //\r
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30 \r
31 using System.IO;\r
32 using System.Collections;\r
33 using System.Web.Services.Configuration;\r
34 \r
35 namespace System.Web.Services.Protocols {\r
36         public abstract class SoapExtension {\r
37 \r
38                 #region Fields\r
39 \r
40                 Stream stream;\r
41 \r
42                 #endregion\r
43 \r
44                 #region Constructors\r
45 \r
46                 protected SoapExtension ()\r
47                 {\r
48                 }\r
49 \r
50                 #endregion // Constructors\r
51 \r
52                 #region Methods\r
53 \r
54                 public virtual Stream ChainStream (Stream stream)\r
55                 {\r
56                         return stream;\r
57                 }\r
58 \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
63 \r
64 \r
65 #if !TARGET_JVM
66                 static ArrayList[] globalExtensions;\r
67 #else
68                 static ArrayList[] globalExtensions {
69                         get {
70                                 return (ArrayList[])AppDomain.CurrentDomain.GetData("SoapExtension.globalExtensions");
71                         }
72                         set {
73                                 AppDomain.CurrentDomain.SetData("SoapExtension.globalExtensions", value);
74                         }
75                 }
76 #endif
77 \r
78                 internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs)\r
79                 {\r
80                         if (extensionConfigs == null) return null;\r
81                         SoapExtension[] res = new SoapExtension [extensionConfigs.Length];\r
82                         CreateExtensionChain (extensionConfigs, res, 0);\r
83                         return res;\r
84                 }\r
85 \r
86                 internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] hiPrioExts, SoapExtensionRuntimeConfig[] medPrioExts, SoapExtensionRuntimeConfig[] lowPrioExts)\r
87                 {\r
88                         int len = 0;\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
93 \r
94                         SoapExtension[] res = new SoapExtension [len];\r
95                         int pos = 0;\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
99                         return res;\r
100                 }\r
101 \r
102                 static int CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs, SoapExtension[] destArray, int pos)\r
103                 {\r
104                         for (int n=0; n<extensionConfigs.Length; n++)\r
105                         {\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
110                         }\r
111                         return pos;\r
112                 }\r
113 \r
114                 static void InitializeGlobalExtensions ()\r
115                 {\r
116                         globalExtensions = new ArrayList[2];\r
117                         \r
118 #if NET_2_0
119                         SoapExtensionTypeElementCollection exts = WebServicesSection.Instance.SoapExtensionTypes;\r
120 #else
121                         ArrayList exts = WSConfig.Instance.ExtensionTypes;\r
122                         if (exts == null) return;\r
123 #endif
124 \r
125 #if NET_2_0
126                         foreach (SoapExtensionTypeElement econf in exts)\r
127 #else
128                         foreach (WSExtensionConfig econf in exts)\r
129 #endif
130                         {\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
135 #if NET_2_0
136                                         if (((SoapExtensionTypeElement)destList [n]).Priority > econf.Priority) {\r
137 #else
138                                         if (((WSExtensionConfig)destList [n]).Priority > econf.Priority) {\r
139 #endif
140                                                 destList.Insert (n, econf);\r
141                                                 added = true;\r
142                                         }\r
143                                 if (!added) destList.Add (econf);\r
144                         }\r
145                 }\r
146 \r
147                 internal static SoapExtensionRuntimeConfig[][] GetTypeExtensions (Type serviceType)\r
148                 {\r
149                         if (globalExtensions == null) InitializeGlobalExtensions();\r
150                         \r
151                         SoapExtensionRuntimeConfig[][] exts = new SoapExtensionRuntimeConfig[2][];\r
152 \r
153                         for (int group = 0; group < 2; group++)\r
154                         {\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
159                                 {\r
160 #if NET_2_0
161                                         SoapExtensionTypeElement econf = (SoapExtensionTypeElement) globList [n];\r
162 #else
163                                         WSExtensionConfig econf = (WSExtensionConfig) globList [n];\r
164 #endif
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
170                                 }\r
171                         }\r
172                         return exts;\r
173                 }\r
174 \r
175                 internal static SoapExtensionRuntimeConfig[] GetMethodExtensions (LogicalMethodInfo method)\r
176                 {\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
180 \r
181                         for (int n=0; n<ats.Length; n++)\r
182                         {\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
189                                 exts [n] = econf;\r
190                         }\r
191                         Array.Sort (priorities, exts);\r
192                         return exts;\r
193                 }\r
194 \r
195                 internal static Stream ExecuteChainStream (SoapExtension[] extensions, Stream stream)\r
196                 {\r
197                         if (extensions == null) return stream;\r
198 \r
199                         Stream newStream = stream;\r
200                         foreach (SoapExtension ext in extensions)\r
201                                 newStream = ext.ChainStream (newStream);\r
202                         return newStream;\r
203                 }\r
204 \r
205                 internal static void ExecuteProcessMessage(SoapExtension[] extensions, SoapMessage message, Stream stream, bool inverseOrder) \r
206                 {\r
207                         if (extensions == null) return;\r
208 \r
209                         message.InternalStream = stream;\r
210 \r
211                         if (inverseOrder)\r
212                         {\r
213                                 for (int n = extensions.Length-1; n >= 0; n--)\r
214                                         extensions[n].ProcessMessage (message);\r
215                         }\r
216                         else\r
217                         {\r
218                                 for (int n = 0; n < extensions.Length; n++)\r
219                                         extensions[n].ProcessMessage (message);\r
220                         }\r
221                 }
222 \r
223                 #endregion // Methods\r
224         }\r
225 \r
226         internal class SoapExtensionRuntimeConfig\r
227         {\r
228                 public Type Type;\r
229                 public int Priority;\r
230                 public object InitializationInfo;\r
231         }\r
232 }