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

# · C# · 134 lines · 77 code · 17 blank · 40 comment · 11 complexity · 4c4d4ffc210a51b03e572f3ac4ff9519 MD5 · raw file

  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.Collections.Specialized;
  8. using System.Diagnostics.Contracts;
  9. using System.Net.Http;
  10. using System.Net.Http.Headers;
  11. using Microsoft.Server.Common;
  12. /// <summary>
  13. /// Class that provides <see cref="MediaTypeHeaderValue"/>s from query strings.
  14. /// </summary>
  15. public sealed class QueryStringMapping : MediaTypeMapping
  16. {
  17. private static readonly Type queryStringMappingType = typeof(QueryStringMapping);
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="QueryStringMapping"/> class.
  20. /// </summary>
  21. /// <param name="queryStringParameterName">The name of the query string parameter to match, if present.</param>
  22. /// <param name="queryStringParameterValue">The value of the query string parameter specified by <paramref name="queryStringParameterName"/>.</param>
  23. /// <param name="mediaType">The media type to use if the query parameter specified by <paramref name="queryStringParameterName"/> is present
  24. /// and assigned the value specified by <paramref name="queryStringParameterValue"/>.</param>
  25. public QueryStringMapping(string queryStringParameterName, string queryStringParameterValue, string mediaType)
  26. : base(mediaType)
  27. {
  28. this.Initialize(queryStringParameterName, queryStringParameterValue);
  29. }
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="QueryStringMapping"/> class.
  32. /// </summary>
  33. /// <param name="queryStringParameterName">The name of the query string parameter to match, if present.</param>
  34. /// <param name="queryStringParameterValue">The value of the query string parameter specified by <paramref name="queryStringParameterName"/>.</param>
  35. /// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/> to use if the query parameter specified by <paramref name="queryStringParameterName"/> is present
  36. /// and assigned the value specified by <paramref name="queryStringParameterValue"/>.</param>
  37. public QueryStringMapping(string queryStringParameterName, string queryStringParameterValue, MediaTypeHeaderValue mediaType)
  38. : base(mediaType)
  39. {
  40. this.Initialize(queryStringParameterName, queryStringParameterValue);
  41. }
  42. /// <summary>
  43. /// Gets the query string parameter name.
  44. /// </summary>
  45. public string QueryStringParameterName { get; private set; }
  46. /// <summary>
  47. /// Gets the query string parameter value.
  48. /// </summary>
  49. public string QueryStringParameterValue { get; private set; }
  50. /// <summary>
  51. /// Returns a value indicating whether the current <see cref="QueryStringMapping"/>
  52. /// instance can return a <see cref="MediaTypeHeaderValue"/> from <paramref name="request"/>.
  53. /// </summary>
  54. /// <param name="request">The <see cref="HttpRequestMessage"/> to check.</param>
  55. /// <returns>If this instance can produce a <see cref="MediaTypeHeaderValue"/> from <paramref name="request"/>
  56. /// it returns <c>1.0</c> otherwise <c>0.0</c>.</returns>
  57. protected override sealed double OnTryMatchMediaType(HttpRequestMessage request)
  58. {
  59. Contract.Assert(request != null, "Base class ensures that the 'request' parameter will never be null.");
  60. NameValueCollection queryString = GetQueryString(request.RequestUri);
  61. return this.DoesQueryStringMatch(queryString) ? MediaTypeMatch.Match : MediaTypeMatch.NoMatch;
  62. }
  63. /// <summary>
  64. /// Returns a value indicating whether the current <see cref="QueryStringMapping"/>
  65. /// instance can return a <see cref="MediaTypeHeaderValue"/> from <paramref name="response"/>.
  66. /// </summary>
  67. /// <param name="response">The <see cref="HttpResponseMessage"/> to check.</param>
  68. /// <returns>If this instance can produce a <see cref="MediaTypeHeaderValue"/> from <paramref name="response"/>
  69. /// it returns <c>true</c> otherwise <c>false</c>.</returns>
  70. protected override sealed double OnTryMatchMediaType(HttpResponseMessage response)
  71. {
  72. Contract.Assert(response != null, "Base class ensures that the 'response' parameter will never be null.");
  73. Contract.Assert(response.RequestMessage != null, "Base class ensures that the 'response.RequestMessage' will never be null.");
  74. NameValueCollection queryString = GetQueryString(response.RequestMessage.RequestUri);
  75. return this.DoesQueryStringMatch(queryString) ? MediaTypeMatch.Match : MediaTypeMatch.NoMatch;
  76. }
  77. private static NameValueCollection GetQueryString(Uri uri)
  78. {
  79. if (uri == null)
  80. {
  81. throw new InvalidOperationException(SR.NonNullUriRequiredForMediaTypeMapping(queryStringMappingType.Name));
  82. }
  83. return UrlUtility.ParseQueryString(uri.Query);
  84. }
  85. private void Initialize(string queryStringParameterName, string queryStringParameterValue)
  86. {
  87. if (string.IsNullOrWhiteSpace(queryStringParameterName))
  88. {
  89. throw new ArgumentNullException("queryStringParameterName");
  90. }
  91. if (string.IsNullOrWhiteSpace(queryStringParameterValue))
  92. {
  93. throw new ArgumentNullException("queryStringParameterValue");
  94. }
  95. this.QueryStringParameterName = queryStringParameterName.Trim();
  96. this.QueryStringParameterValue = queryStringParameterValue.Trim();
  97. }
  98. private bool DoesQueryStringMatch(NameValueCollection queryString)
  99. {
  100. if (queryString != null)
  101. {
  102. foreach (string queryParameter in queryString.AllKeys)
  103. {
  104. if (string.Equals(queryParameter, this.QueryStringParameterName, StringComparison.Ordinal))
  105. {
  106. string queryValue = queryString[queryParameter];
  107. if (string.Equals(queryValue, this.QueryStringParameterValue, StringComparison.Ordinal))
  108. {
  109. return true;
  110. }
  111. }
  112. }
  113. }
  114. return false;
  115. }
  116. }
  117. }