示例#1
0
 /// <summary>
 /// Gets the value associated with the specified key.
 /// </summary>
 /// <param name="key">The key whose value to get.</param>
 /// <param name="value">The value associated with the specified key, if the key is found; otherwise, null.</param>
 /// <returns>true if the cache contains an element with the specified key; otherwise, false.</returns>
 public bool TryGetValue(MatchInfoCacheKey key, out MediaTypeMatchInfo value)
 {
     lock (this.dict)
     {
         return(dict.TryGetValue(key, out value));
     }
 }
示例#2
0
            /// <summary>
            /// Returns a value indicating whether this instance is equal to a specified object.
            /// </summary>
            /// <param name="obj">An object to compare with this instance.</param>
            /// <returns>true if obj is equal to this instance; otherwise, false.</returns>
            public override bool Equals(object obj)
            {
                MatchInfoCacheKey cacheKey = obj as MatchInfoCacheKey;

                return(cacheKey != null &&
                       this.MediaTypeResolver == cacheKey.MediaTypeResolver &&
                       this.PayloadKind == cacheKey.PayloadKind &&
                       this.ContentTypeName == cacheKey.ContentTypeName);
            }
示例#3
0
            /// <summary>
            /// Adds an element with the provided key and value to the cache.
            /// </summary>
            /// <param name="key">The key of the element to add.</param>
            /// <param name="value">The value of the element to add.</param>
            public void Add(MatchInfoCacheKey key, MediaTypeMatchInfo value)
            {
                lock (this.dict)
                {
                    if (!dict.ContainsKey(key))
                    {
                        if (dict.Count == maxSize)
                        {
                            dict.Clear();
                        }

                        dict.Add(key, value);
                    }
                }
            }
示例#4
0
        /// <summary>
        /// Determine the <see cref="ODataFormat"/> to use for the given <paramref name="contentTypeName"/>. If no supported content type
        /// is found an exception is thrown.
        /// </summary>
        /// <param name="contentTypeName">The name of the content type to be checked.</param>
        /// <param name="supportedPayloadKinds">All possible kinds of payload that can be read with this content type.</param>
        /// <param name="mediaTypeResolver">The media type resolver to use when interpreting the content type.</param>
        /// <param name="mediaType">The media type parsed from the <paramref name="contentTypeName"/>.</param>
        /// <param name="encoding">The encoding from the content type or the default encoding for the <paramref name="mediaType" />.</param>
        /// <param name="selectedPayloadKind">
        /// The payload kind that was selected form the list of <paramref name="supportedPayloadKinds"/> for the
        /// specified <paramref name="contentTypeName"/>.
        /// </param>
        /// <returns>The <see cref="ODataFormat"/> for the <paramref name="contentTypeName"/>.</returns>
        internal static ODataFormat GetFormatFromContentType(string contentTypeName, ODataPayloadKind[] supportedPayloadKinds, ODataMediaTypeResolver mediaTypeResolver, out ODataMediaType mediaType, out Encoding encoding, out ODataPayloadKind selectedPayloadKind)
        {
            Debug.Assert(!supportedPayloadKinds.Contains(ODataPayloadKind.Unsupported), "!supportedPayloadKinds.Contains(ODataPayloadKind.Unsupported)");

            string charset;

            mediaType = ParseContentType(contentTypeName, out charset);

            IList <ODataMediaTypeFormat> supportedMediaTypes = null;

            for (int i = 0; i < supportedPayloadKinds.Length; ++i)
            {
                // get the supported and default media types for the current payload kind
                ODataPayloadKind supportedPayloadKind = supportedPayloadKinds[i];
                supportedMediaTypes = mediaTypeResolver.GetMediaTypeFormats(supportedPayloadKind).ToList();

                // match the specified media types against the supported/default ones
                // and get the format
                var cacheKey = new MatchInfoCacheKey(mediaTypeResolver, supportedPayloadKind, contentTypeName);
                MediaTypeMatchInfo matchInfo;

                if (!MatchInfoCache.TryGetValue(cacheKey, out matchInfo))
                {
                    matchInfo = MatchMediaTypes(supportedMediaTypes.Select(smt => smt.MediaType), new[] { mediaType });
                    MatchInfoCache.Add(cacheKey, matchInfo);
                }

                if (matchInfo != null)
                {
                    Debug.Assert(matchInfo.TargetTypeIndex == 0, "Invalid target type index detected.");
                    selectedPayloadKind = supportedPayloadKind;
                    encoding            = GetEncoding(charset, selectedPayloadKind, mediaType, /*useDefaultEncoding*/ false);
                    return(supportedMediaTypes[matchInfo.SourceTypeIndex].Format);
                }
            }

            // We're calling the ToArray here since not all platforms support the string.Join which takes IEnumerable.
            string supportedTypesAsString = String.Join(", ", supportedPayloadKinds.SelectMany(pk => mediaTypeResolver.GetMediaTypeFormats(pk).Select(mt => mt.MediaType.ToText())).ToArray());

            throw new ODataContentTypeException(Strings.MediaTypeUtils_CannotDetermineFormatFromContentType(supportedTypesAsString, contentTypeName));
        }