PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/WCFWebApi/src/Microsoft.Net.Http.Formatting/System/Net/Http/Formatting/MediaTypeMapping.cs

#
C# | 158 lines | 55 code | 14 blank | 89 comment | 9 complexity | c233a26350ec0a9c3c5eaca7e7ab90f4 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
  1. // <copyright>
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace System.Net.Http.Formatting
  5. {
  6. using System;
  7. using System.Net.Http;
  8. using System.Net.Http.Headers;
  9. /// <summary>
  10. /// An abstract base class used to create an association between <see cref="HttpRequestMessage"/> or
  11. /// <see cref="HttpResponseMessage"/> instances that have certain characteristics
  12. /// and a specific <see cref="MediaTypeHeaderValue"/>.
  13. /// </summary>
  14. public abstract class MediaTypeMapping
  15. {
  16. private static readonly Type httpRequestMessageType = typeof(HttpRequestMessage);
  17. private static readonly Type httpResponseMessageType = typeof(HttpResponseMessage);
  18. /// <summary>
  19. /// Initializes a new instance of a <see cref="MediaTypeMapping"/> with the
  20. /// given <paramref name="mediaType"/> value.
  21. /// </summary>
  22. /// <param name="mediaType">
  23. /// The <see cref="MediaTypeHeaderValue"/> that is associated with <see cref="HttpRequestMessage"/> or
  24. /// <see cref="HttpResponseMessage"/> instances that have the given characteristics of the
  25. /// <see cref="MediaTypeMapping"/>.
  26. /// </param>
  27. protected MediaTypeMapping(MediaTypeHeaderValue mediaType)
  28. {
  29. if (mediaType == null)
  30. {
  31. throw new ArgumentNullException("mediaType");
  32. }
  33. this.MediaType = mediaType;
  34. }
  35. /// <summary>
  36. /// Initializes a new instance of a <see cref="MediaTypeMapping"/> with the
  37. /// given <paramref name="mediaType"/> value.
  38. /// </summary>
  39. /// <param name="mediaType">
  40. /// The <see cref="string"/> that is associated with <see cref="HttpRequestMessage"/> or
  41. /// <see cref="HttpResponseMessage"/> instances that have the given characteristics of the
  42. /// <see cref="MediaTypeMapping"/>.
  43. /// </param>
  44. protected MediaTypeMapping(string mediaType)
  45. {
  46. if (string.IsNullOrWhiteSpace(mediaType))
  47. {
  48. throw new ArgumentNullException("mediaType");
  49. }
  50. this.MediaType = new MediaTypeHeaderValue(mediaType);
  51. }
  52. /// <summary>
  53. /// Gets the <see cref="MediaTypeHeaderValue"/> that is associated with <see cref="HttpRequestMessage"/> or
  54. /// <see cref="HttpResponseMessage"/> instances that have the given characteristics of the
  55. /// <see cref="MediaTypeMapping"/>.
  56. /// </summary>
  57. public MediaTypeHeaderValue MediaType { get; private set; }
  58. /// <summary>
  59. /// Returns the quality of the match of the <see cref="MediaTypeHeaderValue"/>
  60. /// associated with <paramref name="request"/>.
  61. /// </summary>
  62. /// <param name="request">
  63. /// The <see cref="HttpRequestMessage"/> to evaluate for the characteristics
  64. /// associated with the <see cref="MediaTypeHeaderValue"/>
  65. /// of the <see cref="MediaTypeMapping"/>.
  66. /// </param>
  67. /// <returns>
  68. /// The quality of the match. It must be between <c>0.0</c> and <c>1.0</c>.
  69. /// A value of <c>0.0</c> signifies no match.
  70. /// A value of <c>1.0</c> signifies a complete match.
  71. /// </returns>
  72. public double TryMatchMediaType(HttpRequestMessage request)
  73. {
  74. if (request == null)
  75. {
  76. throw new ArgumentNullException("request");
  77. }
  78. return this.OnTryMatchMediaType(request);
  79. }
  80. /// <summary>
  81. /// Returns the quality of the match of the <see cref="MediaTypeHeaderValue"/>
  82. /// associated with <paramref name="response"/>.
  83. /// </summary>
  84. /// <param name="response">
  85. /// The <see cref="HttpResponseMessage"/> to evaluate for the characteristics
  86. /// associated with the <see cref="MediaTypeHeaderValue"/>
  87. /// of the <see cref="MediaTypeMapping"/>.
  88. /// </param>
  89. /// <returns>
  90. /// The quality of the match. It must be between <c>0.0</c> and <c>1.0</c>.
  91. /// A value of <c>0.0</c> signifies no match.
  92. /// A value of <c>1.0</c> signifies a complete match.
  93. /// </returns>
  94. public double TryMatchMediaType(HttpResponseMessage response)
  95. {
  96. if (response == null)
  97. {
  98. throw new ArgumentNullException("response");
  99. }
  100. if (response.RequestMessage == null)
  101. {
  102. throw new InvalidOperationException(
  103. SR.ResponseMustReferenceRequest(
  104. httpResponseMessageType.Name,
  105. "response",
  106. httpRequestMessageType.Name,
  107. "RequestMessage"));
  108. }
  109. return this.OnTryMatchMediaType(response);
  110. }
  111. /// <summary>
  112. /// Implemented in a derived class to determine if the <see cref="HttpRequestMessage"/>
  113. /// should be associated with the <see cref="MediaTypeHeaderValue"/>
  114. /// of the <see cref="MediaTypeMapping"/>.
  115. /// </summary>
  116. /// <param name="request">
  117. /// The <see cref="HttpRequestMessage"/> to evaluate for the characteristics
  118. /// associated with the <see cref="MediaTypeHeaderValue"/>
  119. /// of the <see cref="MediaTypeMapping"/>.
  120. /// </param>
  121. /// <returns>
  122. /// The quality of the match. It must be between <c>0.0</c> and <c>1.0</c>.
  123. /// A value of <c>0.0</c> signifies no match.
  124. /// A value of <c>1.0</c> signifies a complete match.
  125. /// </returns>
  126. protected abstract double OnTryMatchMediaType(HttpRequestMessage request);
  127. /// <summary>
  128. /// Implemented in a derived class to determine if the <see cref="HttpResponseMessage"/>
  129. /// should be associated with the <see cref="MediaTypeHeaderValue"/>
  130. /// of the <see cref="MediaTypeMapping"/>.
  131. /// </summary>
  132. /// <param name="response">
  133. /// The <see cref="HttpResponseMessage"/> to evaluate for the characteristics
  134. /// associated with the <see cref="MediaTypeHeaderValue"/>
  135. /// of the <see cref="MediaTypeMapping"/>.
  136. /// </param>
  137. /// <returns>
  138. /// The quality of the match. It must be between <c>0.0</c> and <c>1.0</c>.
  139. /// A value of <c>0.0</c> signifies no match.
  140. /// A value of <c>1.0</c> signifies a complete match.
  141. /// </returns>
  142. protected abstract double OnTryMatchMediaType(HttpResponseMessage response);
  143. }
  144. }