Example #1
0
 /// <summary>
 /// Returns the appropriate content-type for this format.
 /// </summary>
 /// <param name="mediaType">The specified media type.</param>
 /// <param name="encoding">The specified encoding.</param>
 /// <param name="writingResponse">True if the message writer is being used to write a response.</param>
 /// <param name="mediaTypeParameters"> The resultant parameters list of the media type. Parameters list could be updated
 /// when getting content type and should be returned if that is the case.
 /// </param>
 /// <returns>The content-type value for the format.</returns>
 internal virtual string GetContentType(ODataMediaType mediaType, Encoding encoding,
                                        bool writingResponse, out IEnumerable <KeyValuePair <string, string> > mediaTypeParameters)
 {
     mediaTypeParameters = mediaType.Parameters;
     return(HttpUtils.BuildContentType(mediaType, encoding));
 }
Example #2
0
        /// <summary>
        /// Append default values required by OData to specified HTTP header.
        ///
        /// When header name is ODataConstants.ContentTypeHeader:
        ///     If header value is application/json, append the following default values:
        ///         odata.metadata=minimal
        ///         odata.streaming=true
        ///         IEEE754Compatible=false
        /// </summary>
        /// <param name="headerName">The name of the header to append default values.</param>
        /// <param name="headerValue">The original header value string.</param>
        /// <returns>The header value string with appended default values.</returns>
        public static string AppendDefaultHeaderValue(string headerName, string headerValue)
        {
            if (string.CompareOrdinal(headerName, ODataConstants.ContentTypeHeader) != 0)
            {
                return(headerValue);
            }

            if (headerValue == null)
            {
                return(null);
            }

            var mediaTypeList = HttpUtils.MediaTypesFromString(headerValue);
            var mediaType     = mediaTypeList.Single().Key;
            var encoding      = HttpUtils.GetEncodingFromCharsetName(mediaTypeList.Single().Value);

            if (string.CompareOrdinal(mediaType.FullTypeName, MimeConstants.MimeApplicationJson) != 0)
            {
                return(headerValue);
            }

            var extendedParameters = new List <KeyValuePair <string, string> >();
            var extendedMediaType  = new ODataMediaType(mediaType.Type, mediaType.SubType, extendedParameters);

            var hasMetadata          = false;
            var hasStreaming         = false;
            var hasIeee754Compatible = false;

            if (mediaType.Parameters != null)
            {
                foreach (var parameter in mediaType.Parameters)
                {
                    extendedParameters.Add(parameter);

                    if (string.CompareOrdinal(parameter.Key, MimeConstants.MimeMetadataParameterName) == 0)
                    {
                        hasMetadata = true;
                    }

                    if (string.CompareOrdinal(parameter.Key, MimeConstants.MimeStreamingParameterName) == 0)
                    {
                        hasStreaming = true;
                    }

                    if (string.CompareOrdinal(parameter.Key, MimeConstants.MimeIeee754CompatibleParameterName) == 0)
                    {
                        hasIeee754Compatible = true;
                    }
                }
            }

            if (!hasMetadata)
            {
                extendedParameters.Add(new KeyValuePair <string, string>(
                                           MimeConstants.MimeMetadataParameterName,
                                           MimeConstants.MimeMetadataParameterValueMinimal));
            }

            if (!hasStreaming)
            {
                extendedParameters.Add(new KeyValuePair <string, string>(
                                           MimeConstants.MimeStreamingParameterName,
                                           MimeConstants.MimeParameterValueTrue));
            }

            if (!hasIeee754Compatible)
            {
                extendedParameters.Add(new KeyValuePair <string, string>(
                                           MimeConstants.MimeIeee754CompatibleParameterName,
                                           MimeConstants.MimeParameterValueFalse));
            }

            return(extendedMediaType.ToText(encoding));
        }