using System;
using System.Threading.Tasks;
using Tapeti.Config;
using Tapeti.Helpers;
namespace Tapeti.Default
{
/// />
///
/// Handles methods marked with the ResponseHandler attribute.
///
internal class ResponseFilterMiddleware : IControllerFilterMiddleware//, IControllerMessageMiddleware
{
internal const string CorrelationIdRequestPrefix = "request|";
public async ValueTask Filter(IMessageContext context, Func next)
{
if (!context.TryGet(out var controllerPayload))
return;
// If no CorrelationId is present, this could be a request-response in flight from a previous version of
// Tapeti so we should not filter the response handler.
if (!string.IsNullOrEmpty(context.Properties.CorrelationId))
{
if (!context.Properties.CorrelationId.StartsWith(CorrelationIdRequestPrefix))
return;
var methodName = context.Properties.CorrelationId[CorrelationIdRequestPrefix.Length..];
if (methodName != MethodSerializer.Serialize(controllerPayload.Binding.Method))
return;
}
await next();
}
}
}