sdk-csharp/samples/CloudNative.CloudEvents.Asp.../CloudEventJsonInputFormatte...

64 lines
2.4 KiB
C#

// 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
/// <summary>
/// A <see cref="TextInputFormatter"/> that parses HTTP requests into CloudEvents.
/// </summary>
public class CloudEventJsonInputFormatter : TextInputFormatter
{
private readonly CloudEventFormatter _formatter;
/// <summary>
/// Constructs a new instance that uses the given formatter for deserialization.
/// </summary>
/// <param name="formatter"></param>
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);
}
/// <inheritdoc />
public override async Task<InputFormatterResult> 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();
}
}
/// <inheritdoc />
protected override bool CanReadType(Type type)
=> type == typeof(CloudEvent) && base.CanReadType(type);
}
}