[corlib] Assume UTC if no $TZ set. Fixes #30360
[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 NET_2_1
29 using System;
30 using System.Collections.Generic;
31 using System.ServiceModel.Channels;
32 using System.Text;
33 using System.Xml;
34
35 namespace System.ServiceModel
36 {
37         // This class is to read ServiceReference.ClientConfig which is
38         // used to give default settings for ClientBase<T> configuration.
39         // It is only used in Silverlight application.
40         //
41         // Since System.Configuration is not supported in SL, this config
42         // loader has to be created without depending on it.
43
44         internal class SilverlightClientConfigLoader
45         {
46                 public SilverlightClientConfiguration Load (XmlReader reader)
47                 {
48                         var ret = new SilverlightClientConfiguration ();
49                         ret.Bindings = new BindingsConfiguration ();
50                         ret.Client = new ClientConfiguration ();
51
52                         reader.MoveToContent ();
53                         if (reader.IsEmptyElement)
54                                 return ret;
55                         reader.ReadStartElement ("configuration");
56
57                         for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
58                                 if (reader.NodeType != XmlNodeType.Element ||
59                                     reader.LocalName != "system.serviceModel" ||
60                                     reader.IsEmptyElement) {
61                                         reader.Skip ();
62                                         continue;
63                                 }
64                                 // in <system.serviceModel>
65                                 reader.ReadStartElement ();
66                                 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
67                                         if (reader.NodeType != XmlNodeType.Element ||
68                                             reader.IsEmptyElement) {
69                                                 reader.Skip ();
70                                                 continue;
71                                         }
72                                         switch (reader.LocalName) {
73                                         case "bindings":
74                                                 reader.ReadStartElement ();
75                                                 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
76                                                         if (reader.NodeType != XmlNodeType.Element ||
77                                                             reader.IsEmptyElement) {
78                                                                 reader.Skip ();
79                                                                 continue;
80                                                         }
81                                                         switch (reader.LocalName) {
82                                                         case "customBinding":
83                                                         reader.ReadStartElement ();
84                                                         for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
85                                                                 if (reader.NodeType != XmlNodeType.Element ||
86                                                                     reader.LocalName != "binding") {
87                                                                         reader.Skip ();
88                                                                         continue;
89                                                                 }
90                                                                 ret.Bindings.CustomBinding.Add (ReadCustomBinding (reader));
91                                                         }
92                                                         reader.ReadEndElement ();
93                                                         break;
94                                                         case "basicHttpBinding":
95                                                         reader.ReadStartElement ();
96                                                         for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
97                                                                 if (reader.NodeType != XmlNodeType.Element ||
98                                                                     reader.LocalName != "binding") {
99                                                                         reader.Skip ();
100                                                                         continue;
101                                                                 }
102                                                                 ret.Bindings.BasicHttpBinding.Add (ReadBasicHttpBinding (reader));
103                                                         }
104                                                         reader.ReadEndElement ();
105                                                         break;
106                                                         }
107                                                 }
108                                                 reader.ReadEndElement ();
109                                                 break;
110                                         case "client":
111                                                 reader.ReadStartElement ();
112                                                 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
113                                                         if (reader.NodeType != XmlNodeType.Element ||
114                                                             reader.LocalName != "endpoint") {
115                                                                 reader.Skip ();
116                                                                 continue;
117                                                         }
118                                                         ret.Client.Endpoints.Add (ReadEndpoint (reader));
119                                                 }
120                                                 reader.ReadEndElement ();
121                                                 break;
122                                         }
123                                 }
124                                 reader.ReadEndElement ();
125                                 // out <system.serviceModel>
126                         }
127                         reader.ReadEndElement ();
128                         // out <configuration>
129
130                         return ret;
131                 }
132
133                 CustomBindingConfiguration ReadCustomBinding (XmlReader reader)
134                 {
135                         string a;
136                         var b = new CustomBindingConfiguration ();
137                         if ((a = reader.GetAttribute ("name")) != null)
138                                 b.Name = a;
139
140                         if (!reader.IsEmptyElement) {
141                                 reader.ReadStartElement ();
142                                 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
143                                         if (reader.NodeType != XmlNodeType.Element) {
144                                                 reader.Skip ();
145                                                 continue;
146                                         }
147                                         switch (reader.LocalName) {
148                                         case "binaryMessageEncoding":
149                                                 b.MessageEncoding = new BinaryMessageEncodingBindingElement ();
150                                                 reader.Skip ();
151                                                 break;
152                                         case "textMessageEncoding":
153                                                 b.MessageEncoding = new TextMessageEncodingBindingElement ();
154                                                 reader.Skip ();
155                                                 break;
156                                         case "httpTransport":
157                                                 b.Transport = new HttpTransportBindingElement ();
158                                                 reader.Skip ();
159                                                 break;
160                                         case "httpsTransport":
161                                                 b.Transport = new HttpsTransportBindingElement ();
162                                                 reader.Skip ();
163                                                 break;
164                                         default:
165                                                 throw new XmlException (String.Format ("Unexpected configuration element '{0}'", reader.LocalName));
166                                         }
167                                 }
168                                 reader.ReadEndElement ();
169                         }
170                         
171                         return b;
172                 }
173
174                 BasicHttpBindingConfiguration ReadBasicHttpBinding (XmlReader reader)
175                 {
176                         string a;
177                         var b = new BasicHttpBindingConfiguration ();
178                         
179                         if ((a = reader.GetAttribute ("name")) != null)
180                                 b.Name = a;
181                         if ((a = reader.GetAttribute ("maxBufferPoolSize")) != null)
182                                 b.MaxBufferPoolSize = XmlConvert.ToInt32 (a);
183                         if ((a = reader.GetAttribute ("maxBufferSize")) != null)
184                                 b.MaxBufferSize = XmlConvert.ToInt32 (a);
185                         if ((a = reader.GetAttribute ("maxReceivedMessageSize")) != null)
186                                 b.MaxReceivedMessageSize = XmlConvert.ToInt32 (a);
187                         if ((a = reader.GetAttribute ("textEncoding")) != null)
188                                 b.TextEncoding = Encoding.GetEncoding (a);
189
190                         if (!reader.IsEmptyElement) {
191                                 reader.ReadStartElement ();
192                                 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
193                                         if (reader.NodeType != XmlNodeType.Element ||
194                                             reader.LocalName != "security") {
195                                                 reader.Skip ();
196                                                 continue;
197                                         }
198                                         if ((a = reader.GetAttribute ("mode")) != null)
199                                                 b.Security.Mode = (BasicHttpSecurityMode) Enum.Parse (typeof (BasicHttpSecurityMode), a, false);
200                                         reader.Skip ();
201                                 }
202                                 reader.ReadEndElement ();
203                         }
204                         else
205                                 reader.Read ();
206                         
207                         return b;
208                 }
209
210                 EndpointConfiguration ReadEndpoint (XmlReader reader)
211                 {
212                         string a;
213                         var e = new EndpointConfiguration ();
214
215                         if ((a = reader.GetAttribute ("name")) != null)
216                                 e.Name = a;
217                         if ((a = reader.GetAttribute ("address")) != null)
218                                 e.Address = new Uri (a);
219                         if ((a = reader.GetAttribute ("bindingConfiguration")) != null)
220                                 e.BindingConfiguration = a;
221                         if ((a = reader.GetAttribute ("contract")) != null)
222                                 e.Contract = a;
223                         reader.Skip ();
224
225                         return e;
226                 }
227
228                 public class SilverlightClientConfiguration
229                 {
230                         public SilverlightClientConfiguration ()
231                         {
232                         }
233
234                         public BindingsConfiguration Bindings { get; set; }
235                         public ClientConfiguration Client { get; set; }
236
237                         public ServiceEndpointConfiguration GetServiceEndpointConfiguration (string name)
238                         {
239                                 var s = new ServiceEndpointConfiguration ();
240                                 s.Name = name;
241                                 EndpointConfiguration e = GetEndpointConfiguration (name);
242                                 s.Address = new EndpointAddress (e.Address);
243                                 s.Binding = GetConfiguredHttpBinding (e).CreateBinding ();
244                                 return s;
245                         }
246
247                         EndpointConfiguration GetEndpointConfiguration (string name)
248                         {
249                                 foreach (var e in Client.Endpoints)
250                                         if (e.Name == name || name == "*")
251                                                 return e;
252                                 return Client.Endpoints [0];
253                         }
254
255                         BindingConfiguration GetConfiguredHttpBinding (EndpointConfiguration endpoint)
256                         {
257                                 foreach (var b in Bindings.All ())
258                                         if (b.Name == endpoint.BindingConfiguration)
259                                                 return b;
260                                 return Bindings.BasicHttpBinding [0];
261                         }
262                 }
263
264                 public class ServiceEndpointConfiguration
265                 {
266                         public string Name { get; set; }
267                         public EndpointAddress Address { get; set; }
268                         public Binding Binding { get; set; }
269                 }
270
271                 public class BindingsConfiguration
272                 {
273                         public BindingsConfiguration ()
274                         {
275                                 BasicHttpBinding = new List<BasicHttpBindingConfiguration> ();
276                                 CustomBinding = new List<CustomBindingConfiguration> ();
277                         }
278
279                         public IList<BasicHttpBindingConfiguration> BasicHttpBinding { get; private set; }
280                         public IList<CustomBindingConfiguration> CustomBinding { get; private set; }
281
282                         public IEnumerable<BindingConfiguration> All ()
283                         {
284                                 foreach (var b in BasicHttpBinding)
285                                         yield return b;
286                                 foreach (var b in CustomBinding)
287                                         yield return b;
288                         }
289                 }
290
291                 public abstract class BindingConfiguration
292                 {
293                         public string Name { get; set; }
294
295                         public abstract Binding CreateBinding ();
296                 }
297
298                 public class BasicHttpBindingConfiguration : BindingConfiguration
299                 {
300                         public BasicHttpBindingConfiguration ()
301                         {
302                                 Binding = new BasicHttpBinding ();
303                                 Security = new BasicHttpSecurityConfiguration (Binding.Security);
304                         }
305
306                         BasicHttpBinding Binding { get; set; }
307
308                         public override Binding CreateBinding ()
309                         {
310                                 return Binding;
311                         }
312
313                         public BasicHttpSecurityConfiguration Security { get; private set; }
314
315                         public long MaxBufferPoolSize {
316                                 get { return Binding.MaxBufferPoolSize; }
317                                 set { Binding.MaxBufferPoolSize = value; }
318                         }
319                         public int MaxBufferSize {
320                                 get { return Binding.MaxBufferSize; }
321                                 set { Binding.MaxBufferSize = value; }
322                         }
323                         public long MaxReceivedMessageSize {
324                                 get { return Binding.MaxReceivedMessageSize; }
325                                 set { Binding.MaxReceivedMessageSize = value; }
326                         }
327
328                         public Encoding TextEncoding {
329                                 get { return Binding.TextEncoding; }
330                                 set { Binding.TextEncoding = value; }
331                         }
332
333                         // public bool AllowCookies { get; set; }
334                         // public bool BypassProxyOnLocal { get; set; }
335                         // public HostNameComparisonMode HostNameComparisonMode { get; set; }
336                         // public WSMessageEncoding MessageEncoding { get; set; }
337                         // public Uri ProxyAddress { get; set; }
338                         // public XmlDictionaryReaderQuotasElement ReaderQuotas { get; }
339                         // public TransferMode TransferMode { get; set; }
340                         // public bool UseDefaultWebProxy { get; set; }
341                 }
342
343                 public class BasicHttpSecurityConfiguration
344                 {
345                         public BasicHttpSecurityConfiguration (BasicHttpSecurity security)
346                         {
347                                 Security = security;
348                         }
349
350                         public BasicHttpSecurity Security { get; private set; }
351
352                         public BasicHttpSecurityMode Mode {
353                                 get { return Security.Mode; }
354                                 set { Security.Mode = value; }
355                         }
356                 }
357
358                 public class CustomBindingConfiguration : BindingConfiguration
359                 {
360                         public override Binding CreateBinding ()
361                         {
362                                 return new CustomBinding (MessageEncoding, Transport);
363                         }
364
365                         public MessageEncodingBindingElement MessageEncoding { get; set; }
366                         public TransportBindingElement Transport { get; set; }
367                 }
368
369                 public class ClientConfiguration
370                 {
371                         public ClientConfiguration ()
372                         {
373                                 Endpoints = new List<EndpointConfiguration> ();
374                         }
375
376                         public IList<EndpointConfiguration> Endpoints { get; private set; }
377
378                         // (should be) no metadata element support unlike full WCF.
379                 }
380
381                 public class EndpointConfiguration
382                 {
383                         public EndpointConfiguration ()
384                         {
385                         }
386
387                         public string Name { get; set; }
388                         public Uri Address { get; set; }
389                         public string BindingConfiguration { get; set; }
390                         // public string BehaviorConfiguration { get; set; }
391                         public string Contract { get; set; }
392                         // public AddressHeaderCollection Headers { get; set; }
393                         // public EndpointIdentity Identity { get; set; }
394                 }
395         }
396 }
397 #endif