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