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