[mcs] Replace NET_2_1 by MOBILE
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / SilverlightClientConfigLoader.cs
1 //
2 // SilverlightClientConfigLoader.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2009 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 #if MOBILE || XAMMAC_4_5
29
30 using System;
31 using System.Collections.Generic;
32 using System.ServiceModel.Channels;
33 using System.Text;
34 using System.Xml;
35
36 namespace System.ServiceModel
37 {
38         // This class is to read ServiceReference.ClientConfig which is
39         // used to give default settings for ClientBase<T> configuration.
40         // It is only used in Silverlight application.
41         //
42         // Since System.Configuration is not supported in SL, this config
43         // loader has to be created without depending on it.
44
45         internal class SilverlightClientConfigLoader
46         {
47                 public SilverlightClientConfiguration Load (XmlReader reader)
48                 {
49                         var ret = new SilverlightClientConfiguration ();
50                         ret.Bindings = new BindingsConfiguration ();
51                         ret.Client = new ClientConfiguration ();
52
53                         reader.MoveToContent ();
54                         if (reader.IsEmptyElement)
55                                 return ret;
56                         reader.ReadStartElement ("configuration");
57
58                         for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
59                                 if (reader.NodeType != XmlNodeType.Element ||
60                                     reader.LocalName != "system.serviceModel" ||
61                                     reader.IsEmptyElement) {
62                                         reader.Skip ();
63                                         continue;
64                                 }
65                                 // in <system.serviceModel>
66                                 reader.ReadStartElement ();
67                                 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
68                                         if (reader.NodeType != XmlNodeType.Element ||
69                                             reader.IsEmptyElement) {
70                                                 reader.Skip ();
71                                                 continue;
72                                         }
73                                         switch (reader.LocalName) {
74                                         case "bindings":
75                                                 reader.ReadStartElement ();
76                                                 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
77                                                         if (reader.NodeType != XmlNodeType.Element ||
78                                                             reader.IsEmptyElement) {
79                                                                 reader.Skip ();
80                                                                 continue;
81                                                         }
82                                                         switch (reader.LocalName) {
83                                                         case "customBinding":
84                                                         reader.ReadStartElement ();
85                                                         for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
86                                                                 if (reader.NodeType != XmlNodeType.Element ||
87                                                                     reader.LocalName != "binding") {
88                                                                         reader.Skip ();
89                                                                         continue;
90                                                                 }
91                                                                 ret.Bindings.CustomBinding.Add (ReadCustomBinding (reader));
92                                                         }
93                                                         reader.ReadEndElement ();
94                                                         break;
95                                                         case "basicHttpBinding":
96                                                         reader.ReadStartElement ();
97                                                         for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
98                                                                 if (reader.NodeType != XmlNodeType.Element ||
99                                                                     reader.LocalName != "binding") {
100                                                                         reader.Skip ();
101                                                                         continue;
102                                                                 }
103                                                                 ret.Bindings.BasicHttpBinding.Add (ReadBasicHttpBinding (reader));
104                                                         }
105                                                         reader.ReadEndElement ();
106                                                         break;
107                                                         }
108                                                 }
109                                                 reader.ReadEndElement ();
110                                                 break;
111                                         case "client":
112                                                 reader.ReadStartElement ();
113                                                 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
114                                                         if (reader.NodeType != XmlNodeType.Element ||
115                                                             reader.LocalName != "endpoint") {
116                                                                 reader.Skip ();
117                                                                 continue;
118                                                         }
119                                                         ret.Client.Endpoints.Add (ReadEndpoint (reader));
120                                                 }
121                                                 reader.ReadEndElement ();
122                                                 break;
123                                         }
124                                 }
125                                 reader.ReadEndElement ();
126                                 // out <system.serviceModel>
127                         }
128                         reader.ReadEndElement ();
129                         // out <configuration>
130
131                         return ret;
132                 }
133
134                 CustomBindingConfiguration ReadCustomBinding (XmlReader reader)
135                 {
136                         string a;
137                         var b = new CustomBindingConfiguration ();
138                         if ((a = reader.GetAttribute ("name")) != null)
139                                 b.Name = a;
140
141                         if (!reader.IsEmptyElement) {
142                                 reader.ReadStartElement ();
143                                 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
144                                         if (reader.NodeType != XmlNodeType.Element) {
145                                                 reader.Skip ();
146                                                 continue;
147                                         }
148                                         switch (reader.LocalName) {
149                                         case "binaryMessageEncoding":
150                                                 b.MessageEncoding = new BinaryMessageEncodingBindingElement ();
151                                                 reader.Skip ();
152                                                 break;
153                                         case "textMessageEncoding":
154                                                 b.MessageEncoding = new TextMessageEncodingBindingElement ();
155                                                 reader.Skip ();
156                                                 break;
157                                         case "httpTransport":
158                                                 b.Transport = new HttpTransportBindingElement ();
159                                                 reader.Skip ();
160                                                 break;
161                                         case "httpsTransport":
162                                                 b.Transport = new HttpsTransportBindingElement ();
163                                                 reader.Skip ();
164                                                 break;
165                                         default:
166                                                 throw new XmlException (String.Format ("Unexpected configuration element '{0}'", reader.LocalName));
167                                         }
168                                 }
169                                 reader.ReadEndElement ();
170                         }
171                         
172                         return b;
173                 }
174
175                 BasicHttpBindingConfiguration ReadBasicHttpBinding (XmlReader reader)
176                 {
177                         string a;
178                         var b = new BasicHttpBindingConfiguration ();
179                         
180                         if ((a = reader.GetAttribute ("name")) != null)
181                                 b.Name = a;
182                         if ((a = reader.GetAttribute ("maxBufferPoolSize")) != null)
183                                 b.MaxBufferPoolSize = XmlConvert.ToInt32 (a);
184                         if ((a = reader.GetAttribute ("maxBufferSize")) != null)
185                                 b.MaxBufferSize = XmlConvert.ToInt32 (a);
186                         if ((a = reader.GetAttribute ("maxReceivedMessageSize")) != null)
187                                 b.MaxReceivedMessageSize = XmlConvert.ToInt32 (a);
188                         if ((a = reader.GetAttribute ("textEncoding")) != null)
189                                 b.TextEncoding = Encoding.GetEncoding (a);
190
191                         if (!reader.IsEmptyElement) {
192                                 reader.ReadStartElement ();
193                                 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
194                                         if (reader.NodeType != XmlNodeType.Element ||
195                                             reader.LocalName != "security") {
196                                                 reader.Skip ();
197                                                 continue;
198                                         }
199                                         if ((a = reader.GetAttribute ("mode")) != null)
200                                                 b.Security.Mode = (BasicHttpSecurityMode) Enum.Parse (typeof (BasicHttpSecurityMode), a, false);
201                                         reader.Skip ();
202                                 }
203                                 reader.ReadEndElement ();
204                         }
205                         else
206                                 reader.Read ();
207                         
208                         return b;
209                 }
210
211                 EndpointConfiguration ReadEndpoint (XmlReader reader)
212                 {
213                         string a;
214                         var e = new EndpointConfiguration ();
215
216                         if ((a = reader.GetAttribute ("name")) != null)
217                                 e.Name = a;
218                         if ((a = reader.GetAttribute ("address")) != null)
219                                 e.Address = new Uri (a);
220                         if ((a = reader.GetAttribute ("bindingConfiguration")) != null)
221                                 e.BindingConfiguration = a;
222                         if ((a = reader.GetAttribute ("contract")) != null)
223                                 e.Contract = a;
224                         reader.Skip ();
225
226                         return e;
227                 }
228
229                 public class SilverlightClientConfiguration
230                 {
231                         public SilverlightClientConfiguration ()
232                         {
233                         }
234
235                         public BindingsConfiguration Bindings { get; set; }
236                         public ClientConfiguration Client { get; set; }
237
238                         public ServiceEndpointConfiguration GetServiceEndpointConfiguration (string name)
239                         {
240                                 var s = new ServiceEndpointConfiguration ();
241                                 s.Name = name;
242                                 EndpointConfiguration e = GetEndpointConfiguration (name);
243                                 s.Address = new EndpointAddress (e.Address);
244                                 s.Binding = GetConfiguredHttpBinding (e).CreateBinding ();
245                                 return s;
246                         }
247
248                         EndpointConfiguration GetEndpointConfiguration (string name)
249                         {
250                                 foreach (var e in Client.Endpoints)
251                                         if (e.Name == name || name == "*")
252                                                 return e;
253                                 return Client.Endpoints [0];
254                         }
255
256                         BindingConfiguration GetConfiguredHttpBinding (EndpointConfiguration endpoint)
257                         {
258                                 foreach (var b in Bindings.All ())
259                                         if (b.Name == endpoint.BindingConfiguration)
260                                                 return b;
261                                 return Bindings.BasicHttpBinding [0];
262                         }
263                 }
264
265                 public class ServiceEndpointConfiguration
266                 {
267                         public string Name { get; set; }
268                         public EndpointAddress Address { get; set; }
269                         public Binding Binding { get; set; }
270                 }
271
272                 public class BindingsConfiguration
273                 {
274                         public BindingsConfiguration ()
275                         {
276                                 BasicHttpBinding = new List<BasicHttpBindingConfiguration> ();
277                                 CustomBinding = new List<CustomBindingConfiguration> ();
278                         }
279
280                         public IList<BasicHttpBindingConfiguration> BasicHttpBinding { get; private set; }
281                         public IList<CustomBindingConfiguration> CustomBinding { get; private set; }
282
283                         public IEnumerable<BindingConfiguration> All ()
284                         {
285                                 foreach (var b in BasicHttpBinding)
286                                         yield return b;
287                                 foreach (var b in CustomBinding)
288                                         yield return b;
289                         }
290                 }
291
292                 public abstract class BindingConfiguration
293                 {
294                         public string Name { get; set; }
295
296                         public abstract Binding CreateBinding ();
297                 }
298
299                 public class BasicHttpBindingConfiguration : BindingConfiguration
300                 {
301                         public BasicHttpBindingConfiguration ()
302                         {
303                                 Binding = new BasicHttpBinding ();
304                                 Security = new BasicHttpSecurityConfiguration (Binding.Security);
305                         }
306
307                         BasicHttpBinding Binding { get; set; }
308
309                         public override Binding CreateBinding ()
310                         {
311                                 return Binding;
312                         }
313
314                         public BasicHttpSecurityConfiguration Security { get; private set; }
315
316                         public long MaxBufferPoolSize {
317                                 get { return Binding.MaxBufferPoolSize; }
318                                 set { Binding.MaxBufferPoolSize = value; }
319                         }
320                         public int MaxBufferSize {
321                                 get { return Binding.MaxBufferSize; }
322                                 set { Binding.MaxBufferSize = value; }
323                         }
324                         public long MaxReceivedMessageSize {
325                                 get { return Binding.MaxReceivedMessageSize; }
326                                 set { Binding.MaxReceivedMessageSize = value; }
327                         }
328
329                         public Encoding TextEncoding {
330                                 get { return Binding.TextEncoding; }
331                                 set { Binding.TextEncoding = value; }
332                         }
333
334                         // public bool AllowCookies { get; set; }
335                         // public bool BypassProxyOnLocal { get; set; }
336                         // public HostNameComparisonMode HostNameComparisonMode { get; set; }
337                         // public WSMessageEncoding MessageEncoding { get; set; }
338                         // public Uri ProxyAddress { get; set; }
339                         // public XmlDictionaryReaderQuotasElement ReaderQuotas { get; }
340                         // public TransferMode TransferMode { get; set; }
341                         // public bool UseDefaultWebProxy { get; set; }
342                 }
343
344                 public class BasicHttpSecurityConfiguration
345                 {
346                         public BasicHttpSecurityConfiguration (BasicHttpSecurity security)
347                         {
348                                 Security = security;
349                         }
350
351                         public BasicHttpSecurity Security { get; private set; }
352
353                         public BasicHttpSecurityMode Mode {
354                                 get { return Security.Mode; }
355                                 set { Security.Mode = value; }
356                         }
357                 }
358
359                 public class CustomBindingConfiguration : BindingConfiguration
360                 {
361                         public override Binding CreateBinding ()
362                         {
363                                 return new CustomBinding (MessageEncoding, Transport);
364                         }
365
366                         public MessageEncodingBindingElement MessageEncoding { get; set; }
367                         public TransportBindingElement Transport { get; set; }
368                 }
369
370                 public class ClientConfiguration
371                 {
372                         public ClientConfiguration ()
373                         {
374                                 Endpoints = new List<EndpointConfiguration> ();
375                         }
376
377                         public IList<EndpointConfiguration> Endpoints { get; private set; }
378
379                         // (should be) no metadata element support unlike full WCF.
380                 }
381
382                 public class EndpointConfiguration
383                 {
384                         public EndpointConfiguration ()
385                         {
386                         }
387
388                         public string Name { get; set; }
389                         public Uri Address { get; set; }
390                         public string BindingConfiguration { get; set; }
391                         // public string BehaviorConfiguration { get; set; }
392                         public string Contract { get; set; }
393                         // public AddressHeaderCollection Headers { get; set; }
394                         // public EndpointIdentity Identity { get; set; }
395                 }
396         }
397 }
398 #endif