コード例 #1
0
 public override Boolean CanRead(InputFormatterContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     return(SupportedMediaTypes.Any(m => context.IsFromContentType(m)));
 }
コード例 #2
0
        /// <inheritdoc />
        public override bool CanRead([NotNull] InputFormatterContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            string contentType = context.HttpContext.Request.ContentType.ToNullIfEmpty();

            return(string.IsNullOrEmpty(contentType) || SupportedMediaTypes.Any(e => e.IsSame(contentType)));
        }
コード例 #3
0
        /// <inheritdoc />
        public bool CanRead(InputFormatterContext context)
        {
            var contentType = context.ActionContext.HttpContext.Request.ContentType;
            MediaTypeHeaderValue requestContentType;

            if (!MediaTypeHeaderValue.TryParse(contentType, out requestContentType))
            {
                return(false);
            }

            return(SupportedMediaTypes
                   .Any(supportedMediaType => supportedMediaType.IsSubsetOf(requestContentType)));
        }
コード例 #4
0
        public override bool CanWriteResult(OutputFormatterCanWriteContext context)
        {
            // Even though we do not support application/json replay we apparently get called anyway?
            var accept          = context.HttpContext.Request.GetTypedHeaders()?.Accept;
            var headerSupported = accept?.Any(a => SupportedMediaTypes.Any(m => a.MediaType == m)) ?? false;

            if (!headerSupported)
            {
                return(false);
            }
            var elementType = context.Object.ElementType();

            return(elementType != null);
        }
コード例 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PSMediaTypeFormatter"/> class.
        /// </summary>
        /// <param name="psConfiguration">PSConfiguration</param>
        public PSMediaTypeFormatter(PSConfiguration psConfiguration = null)
        {
            _psConfiguration = psConfiguration ?? new PSConfiguration();

            foreach (var converter in _psConfiguration.SupportedConverters)
            {
                foreach (var mediaType in converter.MediaTypes)
                {
                    if (!SupportedMediaTypes.Any(t => t.MediaType.Equals(mediaType.MediaType)))
                    {
                        SupportedMediaTypes.Add(mediaType);
                    }

                    if (!string.IsNullOrWhiteSpace(converter.UriPathExtension))
                    {
                        this.AddUriPathExtensionMapping(converter.UriPathExtension, mediaType);
                    }
                }
            }

            // Solving the 415 issue for GET request without Content-Type and Content-Length: 0
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream"));
        }
コード例 #6
0
        /// <inheritdoc/>
        public override bool CanWriteResult(OutputFormatterCanWriteContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            // Ensure we have a valid request.
            HttpRequest request = context.HttpContext.Request;

            if (request == null)
            {
                throw Error.InvalidOperation(SRResources.ReadFromStreamAsyncMustHaveRequest);
            }

            // Ignore non-OData requests.
            if (request.ODataFeature().Path == null)
            {
                return(false);
            }

            // The following base.CanWriteResult(context) will change the context.ContentType
            // If this formatter can't write the result, we should reset the context.ContentType to its original value.
            // So that, the other formatter can make a descison based on the original content type.
            // Be noted: in .NET 5, the context.ContentType is a new StringSegment everytime when goes into each formatter
            // formatterContext.ContentType = new StringSegment();
            // So, in .NET 5, we don't need to reset the contentType to backupContentType.
            StringSegment backupContentType = context.ContentType;

            // Allow the base class to make its determination, which includes
            // checks for SupportedMediaTypes.
            bool suportedMediaTypeFound = false;

            if (SupportedMediaTypes.Any())
            {
                suportedMediaTypeFound = base.CanWriteResult(context);
            }

            // See if the request satisfies any mappings.
            IEnumerable <MediaTypeMapping> matchedMappings = (MediaTypeMappings == null) ? null : MediaTypeMappings
                                                             .Where(m => (m.TryMatchMediaType(request) > 0));

            // Now pick the best content type. If a media mapping was found, use that and override the
            // value specified by the controller, if any. Otherwise, let the base class decide.
            if (matchedMappings != null && matchedMappings.Any())
            {
                context.ContentType = matchedMappings.First().MediaType.ToString();
            }
            else if (!suportedMediaTypeFound)
            {
                context.ContentType = backupContentType;
                return(false);
            }

            // We need the type in order to write it.
            Type type = context.ObjectType ?? context.Object?.GetType();

            if (type == null)
            {
                context.ContentType = backupContentType;
                return(false);
            }
            type = TypeHelper.GetTaskInnerTypeOrSelf(type);

            ODataSerializerProvider serializerProvider = request.GetRequestContainer().GetRequiredService <ODataSerializerProvider>();

            // See if this type is a SingleResult or is derived from SingleResult.
            bool isSingleResult = false;

            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                Type baseType    = TypeHelper.GetBaseType(type);
                isSingleResult = (genericType == typeof(SingleResult <>) || baseType == typeof(SingleResult));
            }

            bool result = ODataOutputFormatterHelper.CanWriteType(
                type,
                _payloadKinds,
                isSingleResult,
                new WebApiRequestMessage(request),
                (objectType) => serializerProvider.GetODataPayloadSerializer(objectType, request));

            if (!result)
            {
                context.ContentType = backupContentType;
            }

            return(result);
        }
コード例 #7
0
        /// <inheritdoc/>
        public override bool CanWriteResult(OutputFormatterCanWriteContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            // Ensure we have a valid request.
            HttpRequest request = context.HttpContext.Request;

            if (request == null)
            {
                throw Error.InvalidOperation(SRResources.ReadFromStreamAsyncMustHaveRequest);
            }

            // Ignore non-OData requests.
            if (request.ODataFeature().Path == null)
            {
                return(false);
            }

            // Allow the base class to make its determination, which includes
            // checks for SupportedMediaTypes.
            bool suportedMediaTypeFound = false;

            if (SupportedMediaTypes.Any())
            {
                suportedMediaTypeFound = base.CanWriteResult(context);
            }

            // See if the request satisfies any mappings.
            IEnumerable <MediaTypeMapping> matchedMappings = (MediaTypeMappings == null) ? null : MediaTypeMappings
                                                             .Where(m => m.TryMatchMediaType(request) > 0);

            // Now pick the best content type. If a media mapping was found, use that and override the
            // value specified by the controller, if any. Otherwise, let the base class decide.
            if (matchedMappings != null && matchedMappings.Any())
            {
                context.ContentType = matchedMappings.First().MediaType.ToString();
            }
            else if (!suportedMediaTypeFound)
            {
                return(false);
            }

            // We need the type in order to write it.
            Type type = context.ObjectType ?? context.Object?.GetType();

            if (type == null)
            {
                return(false);
            }
            type = TypeHelper.GetTaskInnerTypeOrSelf(type);

            ODataSerializerProvider serializerProvider = request.GetSubServiceProvider().GetRequiredService <ODataSerializerProvider>();

            // See if this type is a SingleResult or is derived from SingleResult.
            bool isSingleResult = false;

            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                Type baseType    = type.BaseType;
                isSingleResult = (genericType == typeof(SingleResult <>) || baseType == typeof(SingleResult));
            }

            ODataPayloadKind?payloadKind;

            Type elementType;

            if (typeof(IEdmObject).IsAssignableFrom(type) ||
                (TypeHelper.IsCollection(type, out elementType) && typeof(IEdmObject).IsAssignableFrom(elementType)))
            {
                payloadKind = GetEdmObjectPayloadKind(type, request);
            }
            else
            {
                payloadKind = GetClrObjectResponsePayloadKind(type, isSingleResult, serializerProvider, request);
            }

            return(payloadKind == null ? false : _payloadKinds.Contains(payloadKind.Value));
        }
コード例 #8
0
 private bool IsSupportedMediaType(MimeType mediaType)
 => SupportedMediaTypes.Any(x => x.Match(mediaType));