PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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