WebMessageEncodingElement.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // WebMessageEncodingElement.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 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. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.ComponentModel;
  33. using System.Configuration;
  34. using System.Net;
  35. using System.Net.Security;
  36. using System.Reflection;
  37. using System.ServiceModel;
  38. using System.ServiceModel.Channels;
  39. using System.ServiceModel.Description;
  40. using System.ServiceModel.Diagnostics;
  41. using System.ServiceModel.Dispatcher;
  42. using System.Runtime.Serialization;
  43. using System.Text;
  44. using System.Xml;
  45. namespace System.ServiceModel.Configuration
  46. {
  47. public sealed partial class WebMessageEncodingElement
  48. : BindingElementExtensionElement
  49. {
  50. // Static Fields
  51. static ConfigurationPropertyCollection properties;
  52. static ConfigurationProperty binding_element_type;
  53. static ConfigurationProperty max_read_pool_size;
  54. static ConfigurationProperty max_write_pool_size;
  55. static ConfigurationProperty reader_quotas;
  56. static ConfigurationProperty write_encoding;
  57. static ConfigurationProperty web_content_type_mapper_type;
  58. static WebMessageEncodingElement ()
  59. {
  60. properties = new ConfigurationPropertyCollection ();
  61. binding_element_type = new ConfigurationProperty ("",
  62. typeof (Type), null, new TypeConverter (), null,
  63. ConfigurationPropertyOptions.None);
  64. max_read_pool_size = new ConfigurationProperty ("maxReadPoolSize",
  65. typeof (int), "64", null/* FIXME: get converter for int*/, null,
  66. ConfigurationPropertyOptions.None);
  67. max_write_pool_size = new ConfigurationProperty ("maxWritePoolSize",
  68. typeof (int), "16", null/* FIXME: get converter for int*/, null,
  69. ConfigurationPropertyOptions.None);
  70. reader_quotas = new ConfigurationProperty ("readerQuotas",
  71. typeof (XmlDictionaryReaderQuotasElement), null, null/* FIXME: get converter for XmlDictionaryReaderQuotasElement*/, null,
  72. ConfigurationPropertyOptions.None);
  73. write_encoding = new ConfigurationProperty ("writeEncoding",
  74. typeof (Encoding), "utf-8", null/* FIXME: get converter for Encoding*/, null,
  75. ConfigurationPropertyOptions.None);
  76. web_content_type_mapper_type = new ConfigurationProperty ("",
  77. typeof (string), null, null /* FIXME: supply */, null,
  78. ConfigurationPropertyOptions.None);
  79. properties.Add (binding_element_type);
  80. properties.Add (max_read_pool_size);
  81. properties.Add (max_write_pool_size);
  82. properties.Add (reader_quotas);
  83. properties.Add (write_encoding);
  84. properties.Add (web_content_type_mapper_type);
  85. }
  86. public WebMessageEncodingElement ()
  87. {
  88. }
  89. // Properties
  90. public override Type BindingElementType {
  91. get { return (Type) base [binding_element_type]; }
  92. }
  93. [ConfigurationProperty ("maxReadPoolSize",
  94. Options = ConfigurationPropertyOptions.None,
  95. DefaultValue = "64")]
  96. [IntegerValidator ( MinValue = 1,
  97. MaxValue = int.MaxValue,
  98. ExcludeRange = false)]
  99. public int MaxReadPoolSize {
  100. get { return (int) base [max_read_pool_size]; }
  101. set { base [max_read_pool_size] = value; }
  102. }
  103. [ConfigurationProperty ("maxWritePoolSize",
  104. Options = ConfigurationPropertyOptions.None,
  105. DefaultValue = "16")]
  106. [IntegerValidator ( MinValue = 1,
  107. MaxValue = int.MaxValue,
  108. ExcludeRange = false)]
  109. public int MaxWritePoolSize {
  110. get { return (int) base [max_write_pool_size]; }
  111. set { base [max_write_pool_size] = value; }
  112. }
  113. [ConfigurationProperty ("readerQuotas",
  114. Options = ConfigurationPropertyOptions.None)]
  115. public XmlDictionaryReaderQuotasElement ReaderQuotas {
  116. get { return (XmlDictionaryReaderQuotasElement) base [reader_quotas]; }
  117. }
  118. [ConfigurationProperty ("writeEncoding",
  119. Options = ConfigurationPropertyOptions.None,
  120. DefaultValue = "utf-8")]
  121. [TypeConverter ()]
  122. public Encoding WriteEncoding {
  123. get { return (Encoding) base [write_encoding]; }
  124. set { base [write_encoding] = value; }
  125. }
  126. [ConfigurationProperty ("webContentTypeMapperType",
  127. Options = ConfigurationPropertyOptions.None,
  128. DefaultValue = "")]
  129. [StringValidator ( MinLength = 0,
  130. MaxLength = int.MaxValue)]
  131. public string WebContentTypeMapperType {
  132. get { return (string) base [web_content_type_mapper_type]; }
  133. set { base [web_content_type_mapper_type] = value; }
  134. }
  135. protected internal override BindingElement CreateBindingElement ()
  136. {
  137. var be = new WebMessageEncodingBindingElement ();
  138. ApplyConfiguration (be);
  139. return be;
  140. }
  141. public override void ApplyConfiguration (BindingElement bindingElement)
  142. {
  143. base.ApplyConfiguration (bindingElement);
  144. var b = (WebMessageEncodingBindingElement) bindingElement;
  145. b.ContentTypeMapper = (WebContentTypeMapper) Activator.CreateInstance (Type.GetType (WebContentTypeMapperType), true);
  146. b.MaxReadPoolSize = MaxReadPoolSize;
  147. b.MaxWritePoolSize = MaxWritePoolSize;
  148. b.WriteEncoding = WriteEncoding;
  149. ReaderQuotas.ApplyConfiguration (b.ReaderQuotas);
  150. }
  151. public override void CopyFrom (ServiceModelExtensionElement from)
  152. {
  153. base.CopyFrom (from);
  154. var c = (WebMessageEncodingElement) from;
  155. MaxReadPoolSize = c.MaxReadPoolSize;
  156. MaxWritePoolSize = c.MaxWritePoolSize;
  157. ReaderQuotas.CopyFrom (c.ReaderQuotas);
  158. WriteEncoding = c.WriteEncoding;
  159. }
  160. }
  161. static class Extensions
  162. {
  163. public static void ApplyConfiguration (this XmlDictionaryReaderQuotasElement e, XmlDictionaryReaderQuotas q)
  164. {
  165. q.MaxArrayLength = e.MaxArrayLength;
  166. q.MaxBytesPerRead = e.MaxBytesPerRead;
  167. q.MaxDepth = e.MaxDepth;
  168. q.MaxNameTableCharCount = e.MaxNameTableCharCount;
  169. q.MaxStringContentLength = e.MaxStringContentLength;
  170. }
  171. public static void CopyFrom (this XmlDictionaryReaderQuotasElement e, XmlDictionaryReaderQuotasElement o)
  172. {
  173. e.MaxArrayLength = o.MaxArrayLength;
  174. e.MaxBytesPerRead = o.MaxBytesPerRead;
  175. e.MaxDepth = o.MaxDepth;
  176. e.MaxNameTableCharCount = o.MaxNameTableCharCount;
  177. e.MaxStringContentLength = o.MaxStringContentLength;
  178. }
  179. }
  180. }