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