// Copyright (c) Cloud Native Foundation. // Licensed under the Apache 2.0 license. // See LICENSE file in the project root for full license information. using CloudNative.CloudEvents.AspNetCore; using CloudNative.CloudEvents.Core; using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.Net.Http.Headers; using System; using System.Text; using System.Threading.Tasks; namespace CloudNative.CloudEvents.AspNetCoreSample { // FIXME: This doesn't get called for binary CloudEvents without content, or with a different data content type. // FIXME: This shouldn't really be tied to JSON. We need to work out how we actually want this to be used. // See /// /// A that parses HTTP requests into CloudEvents. /// public class CloudEventJsonInputFormatter : TextInputFormatter { private readonly CloudEventFormatter _formatter; /// /// Constructs a new instance that uses the given formatter for deserialization. /// /// public CloudEventJsonInputFormatter(CloudEventFormatter formatter) { _formatter = Validation.CheckNotNull(formatter, nameof(formatter)); SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json")); SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/cloudevents+json")); SupportedEncodings.Add(Encoding.UTF8); SupportedEncodings.Add(Encoding.Unicode); } /// public override async Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { Validation.CheckNotNull(context, nameof(context)); Validation.CheckNotNull(encoding, nameof(encoding)); var request = context.HttpContext.Request; try { var cloudEvent = await request.ToCloudEventAsync(_formatter); return await InputFormatterResult.SuccessAsync(cloudEvent); } catch (Exception) { return await InputFormatterResult.FailureAsync(); } } /// protected override bool CanReadType(Type type) => type == typeof(CloudEvent) && base.CanReadType(type); } }