Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / SoapExtension.cs
1 // 
2 // System.Web.Services.Protocols.SoapExtension.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002
8 //
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
31 using System.IO;
32 using System.Collections;
33 using System.Web.Services.Configuration;
34
35 namespace System.Web.Services.Protocols {
36         public abstract class SoapExtension {
37
38                 #region Fields
39
40                 Stream stream;
41
42                 #endregion
43
44                 #region Constructors
45
46                 protected SoapExtension ()
47                 {
48                 }
49
50                 #endregion // Constructors
51
52                 #region Methods
53
54                 public virtual Stream ChainStream (Stream stream)
55                 {
56                         return stream;
57                 }
58
59                 public abstract object GetInitializer (Type serviceType);
60                 public abstract object GetInitializer (LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute);
61                 public abstract void Initialize (object initializer);
62                 public abstract void ProcessMessage (SoapMessage message);
63
64
65 #if !TARGET_JVM
66                 static ArrayList[] globalExtensions;
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
78                 internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs)
79                 {
80                         if (extensionConfigs == null) return null;
81                         SoapExtension[] res = new SoapExtension [extensionConfigs.Length];
82                         CreateExtensionChain (extensionConfigs, res, 0);
83                         return res;
84                 }
85
86                 internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] hiPrioExts, SoapExtensionRuntimeConfig[] medPrioExts, SoapExtensionRuntimeConfig[] lowPrioExts)
87                 {
88                         int len = 0;
89                         if (hiPrioExts != null) len += hiPrioExts.Length;
90                         if (medPrioExts != null) len += medPrioExts.Length;
91                         if (lowPrioExts != null) len += lowPrioExts.Length;
92                         if (len == 0) return null;
93
94                         SoapExtension[] res = new SoapExtension [len];
95                         int pos = 0;
96                         if (hiPrioExts != null) pos = CreateExtensionChain (hiPrioExts, res, pos);
97                         if (medPrioExts != null) pos = CreateExtensionChain (medPrioExts, res, pos);
98                         if (lowPrioExts != null) pos = CreateExtensionChain (lowPrioExts, res, pos);
99                         return res;
100                 }
101
102                 static int CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs, SoapExtension[] destArray, int pos)
103                 {
104                         for (int n=0; n<extensionConfigs.Length; n++)
105                         {
106                                 SoapExtensionRuntimeConfig econf = extensionConfigs [n];
107                                 SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
108                                 ext.Initialize (econf.InitializationInfo);
109                                 destArray [pos++] = ext;
110                         }
111                         return pos;
112                 }
113
114 #if !MONOTOUCH
115                 static void InitializeGlobalExtensions ()
116                 {
117                         globalExtensions = new ArrayList[2];
118 #if NET_2_0 
119                         if (WebServicesSection.Current == null) return;
120
121                         SoapExtensionTypeElementCollection exts = WebServicesSection.Current.SoapExtensionTypes;
122 #else
123                         ArrayList exts = WSConfig.Instance.ExtensionTypes;
124 #endif
125                         if (exts == null) return;
126
127 #if NET_2_0
128                         foreach (SoapExtensionTypeElement econf in exts)
129 #else
130                         foreach (WSExtensionConfig econf in exts)
131 #endif
132                         {
133                                 if (globalExtensions [(int)econf.Group] == null) globalExtensions [(int)econf.Group] = new ArrayList ();
134                                 ArrayList destList = globalExtensions [(int) econf.Group];
135                                 bool added = false;
136                                 for (int n=0; n<destList.Count && !added; n++)
137 #if NET_2_0
138                                         if (((SoapExtensionTypeElement)destList [n]).Priority > econf.Priority) {
139 #else
140                                         if (((WSExtensionConfig)destList [n]).Priority > econf.Priority) {
141 #endif
142                                                 destList.Insert (n, econf);
143                                                 added = true;
144                                         }
145                                 if (!added) destList.Add (econf);
146                         }
147                 }
148
149                 internal static SoapExtensionRuntimeConfig[][] GetTypeExtensions (Type serviceType)
150                 {
151                         if (globalExtensions == null) InitializeGlobalExtensions();
152                         
153                         SoapExtensionRuntimeConfig[][] exts = new SoapExtensionRuntimeConfig[2][];
154
155                         for (int group = 0; group < 2; group++)
156                         {
157                                 ArrayList globList = globalExtensions[group];
158                                 if (globList == null) continue;
159                                 exts [group] = new SoapExtensionRuntimeConfig [globList.Count];
160                                 for (int n=0; n<globList.Count; n++)
161                                 {
162 #if NET_2_0
163                                         SoapExtensionTypeElement econf = (SoapExtensionTypeElement) globList [n];
164 #else
165                                         WSExtensionConfig econf = (WSExtensionConfig) globList [n];
166 #endif
167                                         SoapExtensionRuntimeConfig typeconf = new SoapExtensionRuntimeConfig ();
168                                         typeconf.Type = econf.Type;
169                                         SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
170                                         typeconf.InitializationInfo = ext.GetInitializer (serviceType);
171                                         exts [group][n] = typeconf;
172                                 }
173                         }
174                         return exts;
175                 }
176 #endif
177         
178                 internal static SoapExtensionRuntimeConfig[] GetMethodExtensions (LogicalMethodInfo method)
179                 {
180                         object[] ats = method.GetCustomAttributes (typeof (SoapExtensionAttribute));
181                         SoapExtensionRuntimeConfig[] exts = new SoapExtensionRuntimeConfig [ats.Length];
182                         int[] priorities = new int[ats.Length];
183
184                         for (int n=0; n<ats.Length; n++)
185                         {
186                                 SoapExtensionAttribute at = (SoapExtensionAttribute) ats[n];
187                                 SoapExtensionRuntimeConfig econf = new SoapExtensionRuntimeConfig ();
188                                 econf.Type = at.ExtensionType;
189                                 priorities [n] = at.Priority;
190                                 SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);
191                                 econf.InitializationInfo = ext.GetInitializer (method, at);
192                                 exts [n] = econf;
193                         }
194                         Array.Sort (priorities, exts);
195                         return exts;
196                 }
197
198                 internal static Stream ExecuteChainStream (SoapExtension[] extensions, Stream stream)
199                 {
200                         if (extensions == null) return stream;
201
202                         Stream newStream = stream;
203                         foreach (SoapExtension ext in extensions)
204                                 newStream = ext.ChainStream (newStream);
205                         return newStream;
206                 }
207
208                 internal static void ExecuteProcessMessage(SoapExtension[] extensions, SoapMessage message, Stream stream, bool inverseOrder) 
209                 {
210                         if (extensions == null) return;
211
212                         message.InternalStream = stream;
213
214                         if (inverseOrder)
215                         {
216                                 for (int n = extensions.Length-1; n >= 0; n--)
217                                         extensions[n].ProcessMessage (message);
218                         }
219                         else
220                         {
221                                 for (int n = 0; n < extensions.Length; n++)
222                                         extensions[n].ProcessMessage (message);
223                         }
224                 }
225
226                 #endregion // Methods
227         }
228
229         internal class SoapExtensionRuntimeConfig
230         {
231                 public Type Type;
232                 public int Priority;
233                 public object InitializationInfo;
234         }
235 }