2017-01-31 11:01:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Tapeti.Config;
|
|
|
|
|
using Tapeti.Flow.FlowHelpers;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti.Flow.Default
|
|
|
|
|
{
|
|
|
|
|
public class FlowBindingFilter : IBindingFilter
|
|
|
|
|
{
|
|
|
|
|
public async Task<bool> Accept(IMessageContext context, IBinding binding)
|
|
|
|
|
{
|
|
|
|
|
var flowContext = await GetFlowContext(context);
|
2017-02-05 22:22:34 +00:00
|
|
|
|
if (flowContext?.ContinuationMetadata == null)
|
2017-01-31 11:01:08 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
2017-02-05 22:22:34 +00:00
|
|
|
|
return flowContext.ContinuationMetadata.MethodName == MethodSerializer.Serialize(binding.Method);
|
2017-01-31 11:01:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static async Task<FlowContext> GetFlowContext(IMessageContext context)
|
|
|
|
|
{
|
|
|
|
|
if (context.Items.ContainsKey(ContextItems.FlowContext))
|
|
|
|
|
return (FlowContext)context.Items[ContextItems.FlowContext];
|
|
|
|
|
|
|
|
|
|
if (context.Properties.CorrelationId == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
Guid continuationID;
|
|
|
|
|
if (!Guid.TryParse(context.Properties.CorrelationId, out continuationID))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var flowStore = context.DependencyResolver.Resolve<IFlowStore>();
|
|
|
|
|
|
2017-02-05 22:22:34 +00:00
|
|
|
|
var flowID = await flowStore.FindFlowID(continuationID);
|
|
|
|
|
if (!flowID.HasValue)
|
2017-01-31 11:01:08 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
2017-02-05 22:22:34 +00:00
|
|
|
|
var flowStateLock = await flowStore.LockFlowState(flowID.Value);
|
2017-01-31 11:01:08 +00:00
|
|
|
|
if (flowStateLock == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var flowState = await flowStateLock.GetFlowState();
|
2017-02-05 22:22:34 +00:00
|
|
|
|
if (flowState == null)
|
|
|
|
|
return null;
|
2017-01-31 11:01:08 +00:00
|
|
|
|
|
2017-02-05 22:22:34 +00:00
|
|
|
|
ContinuationMetadata continuation;
|
2017-01-31 11:01:08 +00:00
|
|
|
|
|
|
|
|
|
var flowContext = new FlowContext
|
|
|
|
|
{
|
|
|
|
|
MessageContext = context,
|
2017-02-05 22:22:34 +00:00
|
|
|
|
|
2017-01-31 11:01:08 +00:00
|
|
|
|
FlowStateLock = flowStateLock,
|
|
|
|
|
FlowState = flowState,
|
2017-02-05 22:22:34 +00:00
|
|
|
|
|
|
|
|
|
ContinuationID = continuationID,
|
|
|
|
|
ContinuationMetadata = flowState.Continuations.TryGetValue(continuationID, out continuation) ? continuation : null
|
2017-01-31 11:01:08 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// IDisposable items in the IMessageContext are automatically disposed
|
|
|
|
|
context.Items.Add(ContextItems.FlowContext, flowContext);
|
|
|
|
|
return flowContext;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|