/src/System.Net.Http.Formatting/Formatting/MediaTypeFormatterCollection.cs

https://bitbucket.org/lajjne/aspnetwebstack · C# · 183 lines · 120 code · 18 blank · 45 comment · 26 complexity · 7a583cab165758c09d46cb057571f9ec MD5 · raw file

  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Net.Http.Headers;
  6. using System.Web.Http;
  7. using System.Xml;
  8. using System.Xml.Linq;
  9. namespace System.Net.Http.Formatting
  10. {
  11. /// <summary>
  12. /// Collection class that contains <see cref="MediaTypeFormatter"/> instances.
  13. /// </summary>
  14. public class MediaTypeFormatterCollection : Collection<MediaTypeFormatter>
  15. {
  16. private static readonly Type _mediaTypeFormatterType = typeof(MediaTypeFormatter);
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="MediaTypeFormatterCollection"/> class.
  19. /// </summary>
  20. /// <remarks>
  21. /// This collection will be initialized to contain default <see cref="MediaTypeFormatter"/>
  22. /// instances for Xml, JsonValue and Json.
  23. /// </remarks>
  24. public MediaTypeFormatterCollection()
  25. : this(CreateDefaultFormatters())
  26. {
  27. }
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="MediaTypeFormatterCollection"/> class.
  30. /// </summary>
  31. /// <param name="formatters">A collection of <see cref="MediaTypeFormatter"/> instances to place in the collection.</param>
  32. public MediaTypeFormatterCollection(IEnumerable<MediaTypeFormatter> formatters)
  33. {
  34. VerifyAndSetFormatters(formatters);
  35. }
  36. /// <summary>
  37. /// Gets the <see cref="MediaTypeFormatter"/> to use for Xml.
  38. /// </summary>
  39. public XmlMediaTypeFormatter XmlFormatter
  40. {
  41. get { return Items.OfType<XmlMediaTypeFormatter>().FirstOrDefault(); }
  42. }
  43. /// <summary>
  44. /// Gets the <see cref="MediaTypeFormatter"/> to use for Json.
  45. /// </summary>
  46. public JsonMediaTypeFormatter JsonFormatter
  47. {
  48. get { return Items.OfType<JsonMediaTypeFormatter>().FirstOrDefault(); }
  49. }
  50. #if !NETFX_CORE
  51. /// <summary>
  52. /// Gets the <see cref="MediaTypeFormatter"/> to use for <c>application/x-www-form-urlencoded</c> data.
  53. /// </summary>
  54. public FormUrlEncodedMediaTypeFormatter FormUrlEncodedFormatter
  55. {
  56. get { return Items.OfType<FormUrlEncodedMediaTypeFormatter>().FirstOrDefault(); }
  57. }
  58. #endif
  59. /// <summary>
  60. /// Helper to search a collection for a formatter that can read the .NET type in the given mediaType.
  61. /// </summary>
  62. /// <param name="type">.NET type to read</param>
  63. /// <param name="mediaType">media type to match on.</param>
  64. /// <returns>Formatter that can read the type. Null if no formatter found.</returns>
  65. public MediaTypeFormatter FindReader(Type type, MediaTypeHeaderValue mediaType)
  66. {
  67. if (type == null)
  68. {
  69. throw Error.ArgumentNull("type");
  70. }
  71. if (mediaType == null)
  72. {
  73. throw Error.ArgumentNull("mediaType");
  74. }
  75. foreach (MediaTypeFormatter formatter in Items)
  76. {
  77. if (formatter != null && formatter.CanReadType(type))
  78. {
  79. foreach (MediaTypeHeaderValue supportedMediaType in formatter.SupportedMediaTypes)
  80. {
  81. if (supportedMediaType != null && supportedMediaType.IsSubsetOf(mediaType))
  82. {
  83. return formatter;
  84. }
  85. }
  86. }
  87. }
  88. return null;
  89. }
  90. /// <summary>
  91. /// Helper to search a collection for a formatter that can write the .NET type in the given mediaType.
  92. /// </summary>
  93. /// <param name="type">.NET type to read</param>
  94. /// <param name="mediaType">media type to match on.</param>
  95. /// <returns>Formatter that can write the type. Null if no formatter found.</returns>
  96. public MediaTypeFormatter FindWriter(Type type, MediaTypeHeaderValue mediaType)
  97. {
  98. if (type == null)
  99. {
  100. throw Error.ArgumentNull("type");
  101. }
  102. if (mediaType == null)
  103. {
  104. throw Error.ArgumentNull("mediaType");
  105. }
  106. foreach (MediaTypeFormatter formatter in Items)
  107. {
  108. if (formatter != null && formatter.CanWriteType(type))
  109. {
  110. foreach (MediaTypeHeaderValue supportedMediaType in formatter.SupportedMediaTypes)
  111. {
  112. if (supportedMediaType != null && supportedMediaType.IsSubsetOf(mediaType))
  113. {
  114. return formatter;
  115. }
  116. }
  117. }
  118. }
  119. return null;
  120. }
  121. /// <summary>
  122. /// Returns true if the type is one of those loosely defined types that should be excluded from validation
  123. /// </summary>
  124. /// <param name="type">.NET <see cref="Type"/> to validate</param>
  125. /// <returns><c>true</c> if the type should be excluded.</returns>
  126. public static bool IsTypeExcludedFromValidation(Type type)
  127. {
  128. return
  129. #if !NETFX_CORE
  130. typeof(XmlNode).IsAssignableFrom(type) || typeof(FormDataCollection).IsAssignableFrom(type) ||
  131. #endif
  132. FormattingUtilities.IsJTokenType(type) || typeof(XObject).IsAssignableFrom(type);
  133. }
  134. /// <summary>
  135. /// Creates a collection of new instances of the default <see cref="MediaTypeFormatter"/>s.
  136. /// </summary>
  137. /// <returns>The collection of default <see cref="MediaTypeFormatter"/> instances.</returns>
  138. private static IEnumerable<MediaTypeFormatter> CreateDefaultFormatters()
  139. {
  140. return new MediaTypeFormatter[]
  141. {
  142. new JsonMediaTypeFormatter(),
  143. new XmlMediaTypeFormatter(),
  144. #if !NETFX_CORE
  145. new FormUrlEncodedMediaTypeFormatter()
  146. #endif
  147. };
  148. }
  149. private void VerifyAndSetFormatters(IEnumerable<MediaTypeFormatter> formatters)
  150. {
  151. if (formatters == null)
  152. {
  153. throw Error.ArgumentNull("formatters");
  154. }
  155. foreach (MediaTypeFormatter formatter in formatters)
  156. {
  157. if (formatter == null)
  158. {
  159. throw Error.Argument("formatters", Properties.Resources.CannotHaveNullInList, _mediaTypeFormatterType.Name);
  160. }
  161. Add(formatter);
  162. }
  163. }
  164. }
  165. }