2025-02-11 23:12:06 +00:00
|
|
|
|
using System.Text.Json;
|
2025-01-07 09:05:25 +00:00
|
|
|
|
using Elsa.Connections.Attributes;
|
|
|
|
|
|
using Elsa.Connections.Models;
|
2025-02-11 23:12:06 +00:00
|
|
|
|
using Elsa.Extensions;
|
2025-01-07 09:05:25 +00:00
|
|
|
|
using Elsa.Workflows;
|
|
|
|
|
|
using Elsa.Workflows.Pipelines.ActivityExecution;
|
|
|
|
|
|
using JetBrains.Annotations;
|
2025-02-11 23:12:06 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using Elsa.Connections.Core.Extensions;
|
|
|
|
|
|
using Elsa.Connections.Converters;
|
|
|
|
|
|
using Elsa.Connections.Persistence.Contracts;
|
2025-01-07 09:05:25 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Connections.Middleware;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-23 13:07:32 +00:00
|
|
|
|
/// An activity execution middleware component that injects connection properties from Connection Name.
|
2025-01-07 09:05:25 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[UsedImplicitly]
|
2025-02-11 23:12:06 +00:00
|
|
|
|
public class ConnectionMiddleware(ActivityMiddlewareDelegate next
|
|
|
|
|
|
, IConnectionStore connectionStore
|
|
|
|
|
|
,ILogger<ConnectionMiddleware> logger)
|
|
|
|
|
|
: IActivityExecutionMiddleware
|
2025-01-07 09:05:25 +00:00
|
|
|
|
{
|
2025-02-11 23:12:06 +00:00
|
|
|
|
private static JsonSerializerOptions? _serializerOptions;
|
|
|
|
|
|
|
|
|
|
|
|
private static JsonSerializerOptions SerializerOptions =>
|
|
|
|
|
|
_serializerOptions ??= new JsonSerializerOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
|
}.WithConverters(new Int32Converter());
|
|
|
|
|
|
|
2025-01-07 09:05:25 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public async ValueTask InvokeAsync(ActivityExecutionContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var activityDescriptor = context.ActivityDescriptor;
|
|
|
|
|
|
|
|
|
|
|
|
if (activityDescriptor.Attributes.Any(attr => attr.GetType() == typeof(ConnectionActivityAttribute)))
|
|
|
|
|
|
{
|
2025-02-23 13:07:32 +00:00
|
|
|
|
var inputDescriptors = activityDescriptor.Inputs.Where(x => x.PropertyInfo?.PropertyType.GetGenericTypeDefinition() == typeof(ConnectionProperties<>)).ToList();
|
2025-01-07 09:05:25 +00:00
|
|
|
|
|
2025-02-23 13:07:32 +00:00
|
|
|
|
if (inputDescriptors.Count > 0)
|
2025-01-07 09:05:25 +00:00
|
|
|
|
{
|
2025-02-23 13:07:32 +00:00
|
|
|
|
var input = inputDescriptors[0];
|
|
|
|
|
|
var propertyType = input.PropertyInfo?.PropertyType.GetGenericArguments()[0];
|
2025-01-07 09:05:25 +00:00
|
|
|
|
|
2025-02-23 13:07:32 +00:00
|
|
|
|
dynamic inputValue = input.ValueGetter(context.Activity)!;
|
|
|
|
|
|
var connectionName = (string)inputValue.ConnectionName;
|
2025-01-07 09:05:25 +00:00
|
|
|
|
|
2025-02-11 23:12:06 +00:00
|
|
|
|
if (connectionName == null)
|
2025-02-23 13:07:32 +00:00
|
|
|
|
LogConnectionExtensions.LogConnectionIsNull(logger);
|
2025-02-11 23:12:06 +00:00
|
|
|
|
else
|
2025-01-07 09:05:25 +00:00
|
|
|
|
{
|
2025-04-18 12:29:18 +00:00
|
|
|
|
// Get connection from store, if exists.
|
|
|
|
|
|
var connectionConfiguration = await connectionStore.FindAsync(new() { Name = connectionName });
|
2025-02-11 23:12:06 +00:00
|
|
|
|
if (connectionConfiguration != null)
|
2025-01-07 09:05:25 +00:00
|
|
|
|
{
|
2025-04-18 12:29:18 +00:00
|
|
|
|
dynamic deserializedJson = connectionConfiguration.ConnectionConfiguration.Deserialize(propertyType, SerializerOptions)!;
|
2025-01-07 09:05:25 +00:00
|
|
|
|
|
2025-04-18 12:29:18 +00:00
|
|
|
|
inputValue.Properties = deserializedJson;
|
2025-02-11 23:12:06 +00:00
|
|
|
|
input.ValueSetter(context.Activity, inputValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2025-02-23 13:07:32 +00:00
|
|
|
|
LogConnectionExtensions.LogConnectionNotFound(logger, connectionName);
|
2025-01-07 09:05:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await next(context);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|