PageRenderTime 24ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 221 lines | 109 code | 17 blank | 95 comment | 16 complexity | c380a092ca2c6d5311671b20c45433ce 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.Diagnostics.CodeAnalysis;
  9    using System.Net.Http.Headers;
 10
 11    /// <summary>
 12    /// Extension methods to provide convenience in adding <see cref="MediaTypeMapping"/>
 13    /// items to a <see cref="MediaTypeFormatter"/>.
 14    /// </summary>
 15    public static class MediaTypeFormatterExtensionMethods
 16    {
 17        /// <summary>
 18        /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
 19        /// so that it associates the <paramref name="mediaType"/> with <see cref="Uri"/>s containing
 20        /// a specific query parameter and value.
 21        /// </summary>
 22        /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="QueryStringMapping"/> item.</param>
 23        /// <param name="queryStringParameterName">The name of the query parameter.</param>
 24        /// <param name="queryStringParameterValue">The value assigned to that query parameter.</param>
 25        /// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/> to associate 
 26        /// with a <see cref="Uri"/> containing a query string matching <paramref name="queryStringParameterName"/> 
 27        /// and <paramref name="queryStringParameterValue"/>.</param>
 28        public static void AddQueryStringMapping(
 29                                this MediaTypeFormatter formatter,
 30                                string queryStringParameterName,
 31                                string queryStringParameterValue,
 32                                MediaTypeHeaderValue mediaType)
 33        {
 34            if (formatter == null)
 35            {
 36                throw new ArgumentNullException("formatter");
 37            }
 38
 39            QueryStringMapping mapping = new QueryStringMapping(queryStringParameterName, queryStringParameterValue, mediaType);
 40            formatter.MediaTypeMappings.Add(mapping);
 41        }
 42
 43        /// <summary>
 44        /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
 45        /// so that it associates the <paramref name="mediaType"/> with <see cref="Uri"/>s containing
 46        /// a specific query parameter and value.
 47        /// </summary>
 48        /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="QueryStringMapping"/> item.</param>
 49        /// <param name="queryStringParameterName">The name of the query parameter.</param>
 50        /// <param name="queryStringParameterValue">The value assigned to that query parameter.</param>
 51        /// <param name="mediaType">The media type to associate 
 52        /// with a <see cref="Uri"/> containing a query string matching <paramref name="queryStringParameterName"/>
 53        /// and <paramref name="queryStringParameterValue"/>.</param>
 54        public static void AddQueryStringMapping(
 55                                this MediaTypeFormatter formatter,
 56                                string queryStringParameterName,
 57                                string queryStringParameterValue,
 58                                string mediaType)
 59        {
 60            if (formatter == null)
 61            {
 62                throw new ArgumentNullException("formatter");
 63            }
 64
 65            QueryStringMapping mapping = new QueryStringMapping(queryStringParameterName, queryStringParameterValue, mediaType);
 66            formatter.MediaTypeMappings.Add(mapping);
 67        }
 68
 69        /// <summary>
 70        /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
 71        /// so that it associates the <paramref name="mediaType"/> with <see cref="Uri"/>s ending with
 72        /// the given <paramref name="uriPathExtension"/>.
 73        /// </summary>
 74        /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="UriPathExtensionMapping"/> item.</param>
 75        /// <param name="uriPathExtension">The string of the <see cref="Uri"/> path extension.</param>
 76        /// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/> to associate with <see cref="Uri"/>s
 77        /// ending with <paramref name="uriPathExtension"/>.</param>
 78        [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", Justification = "There is no meaningful System.Uri representation for a path suffix such as '.xml'")]
 79        public static void AddUriPathExtensionMapping(
 80                                this MediaTypeFormatter formatter,
 81                                string uriPathExtension,
 82                                MediaTypeHeaderValue mediaType)
 83        {
 84            if (formatter == null)
 85            {
 86                throw new ArgumentNullException("formatter");
 87            }
 88
 89            UriPathExtensionMapping mapping = new UriPathExtensionMapping(uriPathExtension, mediaType);
 90            formatter.MediaTypeMappings.Add(mapping);
 91        }
 92
 93        /// <summary>
 94        /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
 95        /// so that it associates the <paramref name="mediaType"/> with <see cref="Uri"/>s ending with
 96        /// the given <paramref name="uriPathExtension"/>.
 97        /// </summary>
 98        /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="UriPathExtensionMapping"/> item.</param>
 99        /// <param name="uriPathExtension">The string of the <see cref="Uri"/> path extension.</param>
100        /// <param name="mediaType">The string media type to associate with <see cref="Uri"/>s
101        /// ending with <paramref name="uriPathExtension"/>.</param>
102        [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", Justification = "There is no meaningful System.Uri representation for a path suffix such as '.xml'")]
103        public static void AddUriPathExtensionMapping(this MediaTypeFormatter formatter, string uriPathExtension, string mediaType)
104        {
105            if (formatter == null)
106            {
107                throw new ArgumentNullException("formatter");
108            }
109
110            UriPathExtensionMapping mapping = new UriPathExtensionMapping(uriPathExtension, mediaType);
111            formatter.MediaTypeMappings.Add(mapping);
112        }
113
114        /// <summary>
115        /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
116        /// so that it associates the <paramref name="mediaType"/> with requests or responses containing
117        /// <paramref name="mediaRange"/> in the content headers.
118        /// </summary>
119        /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="MediaRangeMapping"/> item.</param>
120        /// <param name="mediaRange">The media range that will appear in the content headers.</param>
121        /// <param name="mediaType">The media type to associate with that <paramref name="mediaRange"/>.</param>
122        public static void AddMediaRangeMapping(this MediaTypeFormatter formatter, string mediaRange, string mediaType)
123        {
124            if (formatter == null)
125            {
126                throw new ArgumentNullException("formatter");
127            }
128
129            MediaRangeMapping mapping = new MediaRangeMapping(mediaRange, mediaType);
130            formatter.MediaTypeMappings.Add(mapping);
131        }
132
133        /// <summary>
134        /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
135        /// so that it associates the <paramref name="mediaType"/> with requests or responses containing
136        /// <paramref name="mediaRange"/> in the content headers.
137        /// </summary>
138        /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="MediaRangeMapping"/> item.</param>
139        /// <param name="mediaRange">The media range that will appear in the content headers.</param>
140        /// <param name="mediaType">The media type to associate with that <paramref name="mediaRange"/>.</param>
141        public static void AddMediaRangeMapping(
142                                this MediaTypeFormatter formatter,
143                                MediaTypeHeaderValue mediaRange,
144                                MediaTypeHeaderValue mediaType)
145        {
146            if (formatter == null)
147            {
148                throw new ArgumentNullException("formatter");
149            }
150
151            MediaRangeMapping mapping = new MediaRangeMapping(mediaRange, mediaType);
152            formatter.MediaTypeMappings.Add(mapping);
153        }
154
155        /// <summary>
156        /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
157        /// so that it associates the <paramref name="mediaType"/> with a specific HTTP request header field
158        /// with a specific value.
159        /// </summary>
160        /// <remarks><see cref="RequestHeaderMapping"/> checks header fields associated with <see cref="M:HttpRequestMessage.Headers"/> for a match. It does
161        /// not check header fields associated with <see cref="M:HttpResponseMessage.Headers"/> or <see cref="M:HttpContent.Headers"/> instances.</remarks>
162        /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="MediaRangeMapping"/> item.</param>
163        /// <param name="headerName">Name of the header to match.</param>
164        /// <param name="headerValue">The header value to match.</param>
165        /// <param name="valueComparison">The <see cref="StringComparison"/> to use when matching <paramref name="headerValue"/>.</param>
166        /// <param name="isValueSubstring">if set to <c>true</c> then <paramref name="headerValue"/> is 
167        /// considered a match if it matches a substring of the actual header value.</param>
168        /// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/> to associate 
169        /// with a <see cref="M:HttpRequestMessage.Header"/> entry with a name matching <paramref name="headerName"/>
170        /// and a value matching <paramref name="headerValue"/>.</param>
171        public static void AddRequestHeaderMapping(
172            this MediaTypeFormatter formatter,
173            string headerName,
174            string headerValue,
175            StringComparison valueComparison, 
176            bool isValueSubstring, 
177            MediaTypeHeaderValue mediaType)
178        {
179            if (formatter == null)
180            {
181                throw new ArgumentNullException("formatter");
182            }
183
184            RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, valueComparison, isValueSubstring, mediaType);
185            formatter.MediaTypeMappings.Add(mapping);
186        }
187
188        /// <summary>
189        /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
190        /// so that it associates the <paramref name="mediaType"/> with a specific HTTP request header field
191        /// with a specific value.
192        /// </summary>
193        /// <remarks><see cref="RequestHeaderMapping"/> checks header fields associated with <see cref="M:HttpRequestMessage.Headers"/> for a match. It does
194        /// not check header fields associated with <see cref="M:HttpResponseMessage.Headers"/> or <see cref="M:HttpContent.Headers"/> instances.</remarks>
195        /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="MediaRangeMapping"/> item.</param>
196        /// <param name="headerName">Name of the header to match.</param>
197        /// <param name="headerValue">The header value to match.</param>
198        /// <param name="valueComparison">The <see cref="StringComparison"/> to use when matching <paramref name="headerValue"/>.</param>
199        /// <param name="isValueSubstring">if set to <c>true</c> then <paramref name="headerValue"/> is 
200        /// considered a match if it matches a substring of the actual header value.</param>
201        /// <param name="mediaType">The media type to associate 
202        /// with a <see cref="M:HttpRequestMessage.Header"/> entry with a name matching <paramref name="headerName"/>
203        /// and a value matching <paramref name="headerValue"/>.</param>
204        public static void AddRequestHeaderMapping(
205            this MediaTypeFormatter formatter,
206            string headerName,
207            string headerValue,
208            StringComparison valueComparison,
209            bool isValueSubstring,
210            string mediaType)
211        {
212            if (formatter == null)
213            {
214                throw new ArgumentNullException("formatter");
215            }
216
217            RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, valueComparison, isValueSubstring, mediaType);
218            formatter.MediaTypeMappings.Add(mapping);
219        }
220    }
221}