internal static async Task <IList <string> > GetResponseBodyContentTypes( Type responseBodyType, IServiceProvider serviceProvider, IDeepSleepRequestConfiguration routeConfiguration, IDeepSleepMediaSerializerFactory formatterFactory) { var formatterTypes = routeConfiguration?.ReadWriteConfiguration?.WriteableMediaTypes ?? formatterFactory?.GetWriteableTypes(objType: responseBodyType, overridingFormatters: null) ?? new List <string>(); if (routeConfiguration?.ReadWriteConfiguration?.WriterResolver != null) { var overrides = await routeConfiguration.ReadWriteConfiguration.WriterResolver(serviceProvider).ConfigureAwait(false); if (overrides?.Formatters != null) { formatterTypes = routeConfiguration?.ReadWriteConfiguration?.WriteableMediaTypes ?? formatterFactory?.GetWriteableTypes(objType: responseBodyType, overridingFormatters: overrides.Formatters) ?? new List <string>(); } } return(formatterTypes?.ToList() ?? new List <string>()); }
/// <summary>Processes the HTTP request accept.</summary> /// <param name="context">The context.</param> /// <param name="contextResolver">The API request context resolver.</param> /// <param name="formatterFactory">The formatter factory.</param> /// <returns></returns> internal static async Task <bool> ProcessHttpRequestAccept(this ApiRequestContext context, IApiRequestContextResolver contextResolver, IDeepSleepMediaSerializerFactory formatterFactory) { if (!context.RequestAborted.IsCancellationRequested) { if (context.Request != null) { var returnType = context.Routing.Route.Location.MethodReturnType; if (returnType == typeof(void) || returnType == typeof(Task)) { return(true); } var accept = !string.IsNullOrWhiteSpace(context.Request.Accept) ? context.Request.Accept : AcceptHeader.All(); IDeepSleepMediaSerializer formatter = null; IList <IDeepSleepMediaSerializer> overridingFormatters = null; if (context.Configuration.ReadWriteConfiguration?.WriterResolver != null) { var overrides = await context.Configuration.ReadWriteConfiguration.WriterResolver(context?.RequestServices).ConfigureAwait(false); overridingFormatters = overrides?.Formatters; } if (formatterFactory != null) { formatter = await formatterFactory.GetAcceptableFormatter( acceptHeader : context.Configuration?.ReadWriteConfiguration?.AcceptHeaderOverride ?? accept, objType : returnType, writeableMediaTypes : context.Configuration?.ReadWriteConfiguration?.WriteableMediaTypes, writeableFormatters : overridingFormatters, formatterType : out var _).ConfigureAwait(false); if (formatter == null && context.Configuration?.ReadWriteConfiguration?.AcceptHeaderFallback != null as AcceptHeader) { formatter = await formatterFactory.GetAcceptableFormatter( acceptHeader : context.Configuration?.ReadWriteConfiguration.AcceptHeaderFallback, objType : returnType, writeableMediaTypes : context.Configuration?.ReadWriteConfiguration?.WriteableMediaTypes, writeableFormatters : overridingFormatters, formatterType : out var _).ConfigureAwait(false); } } if (formatter == null) { var formatterTypes = formatterFactory != null ? formatterFactory.GetWriteableTypes(objType : returnType, overridingFormatters : overridingFormatters) ?? new List <string>() : new DeepSleepMediaSerializerWriterFactory(context.RequestServices).GetWriteableTypes(objType: returnType, overridingFormatters) ?? new List <string>(); var writeableMediaTypes = context.Configuration?.ReadWriteConfiguration?.WriteableMediaTypes ?? formatterTypes ?? new List <string>(); var acceptableTypes = writeableMediaTypes .Where(w => formatterTypes.Any(f => string.Equals(f, w, System.StringComparison.InvariantCultureIgnoreCase))) .ToList(); string acceptable = (acceptableTypes != null && acceptableTypes.Count > 0) ? string.Join(", ", acceptableTypes) : string.Empty; context.Response.AddHeader("X-Allow-Accept", acceptable); context.Response.StatusCode = 406; return(false); } } return(true); } return(false); }