2022-11-13 11:45:34 +00:00
|
|
|
|
using System.Text.Json.Serialization;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
using Elsa.Expressions.Models;
|
2022-12-31 15:16:43 +00:00
|
|
|
|
using Elsa.Extensions;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
using Elsa.Http.Models;
|
|
|
|
|
|
using Elsa.Workflows.Core.Attributes;
|
|
|
|
|
|
using Elsa.Workflows.Core.Models;
|
|
|
|
|
|
using Elsa.Workflows.Management.Models;
|
2022-01-04 08:42:12 +00:00
|
|
|
|
|
2022-05-26 10:47:31 +00:00
|
|
|
|
namespace Elsa.Http;
|
2022-01-04 08:42:12 +00:00
|
|
|
|
|
2022-05-03 07:46:27 +00:00
|
|
|
|
[Activity("Elsa", "HTTP", "Wait for an inbound HTTP request that matches the specified path and methods.")]
|
2022-03-09 23:19:00 +00:00
|
|
|
|
public class HttpEndpoint : Trigger<HttpRequestModel>
|
2022-01-04 08:42:12 +00:00
|
|
|
|
{
|
2022-03-07 14:08:01 +00:00
|
|
|
|
public const string InputKey = "HttpRequest";
|
2022-03-09 23:19:00 +00:00
|
|
|
|
|
2022-04-07 10:51:08 +00:00
|
|
|
|
[JsonConstructor]
|
|
|
|
|
|
public HttpEndpoint()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-04 08:42:12 +00:00
|
|
|
|
[Input] public Input<string> Path { get; set; } = default!;
|
|
|
|
|
|
|
|
|
|
|
|
[Input(
|
2022-04-04 14:00:10 +00:00
|
|
|
|
Options = new[] { "GET", "POST", "PUT", "HEAD", "DELETE" },
|
2022-01-04 08:42:12 +00:00
|
|
|
|
UIHint = InputUIHints.CheckList
|
|
|
|
|
|
)]
|
|
|
|
|
|
public Input<ICollection<string>> SupportedMethods { get; set; } = new(new[] { HttpMethod.Get.Method });
|
|
|
|
|
|
|
2022-03-09 23:19:00 +00:00
|
|
|
|
[Input(
|
|
|
|
|
|
Description = "Allow authenticated requests only",
|
|
|
|
|
|
Category = "Security"
|
|
|
|
|
|
)]
|
|
|
|
|
|
public Input<bool> Authorize { get; set; } = new(false);
|
|
|
|
|
|
|
|
|
|
|
|
[Input(
|
|
|
|
|
|
Description = "Provide a policy to evaluate. If the policy fails, the request is forbidden.",
|
|
|
|
|
|
Category = "Security"
|
|
|
|
|
|
)]
|
|
|
|
|
|
public Input<string?> Policy { get; set; } = new(default(string?));
|
2022-01-04 08:42:12 +00:00
|
|
|
|
|
2022-11-23 10:20:00 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
protected override IEnumerable<object> GetTriggerPayloads(TriggerIndexingContext context) => GetBookmarkPayloads(context.ExpressionExecutionContext);
|
2022-03-09 23:19:00 +00:00
|
|
|
|
|
2022-11-23 10:20:00 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
|
2022-03-07 14:08:01 +00:00
|
|
|
|
{
|
2022-04-30 09:10:08 +00:00
|
|
|
|
// If we did not receive external input, it means we are just now encountering this activity and we need to block execution by creating a bookmark.
|
2022-06-06 18:14:51 +00:00
|
|
|
|
if (!context.TryGetInput<HttpRequestModel>(InputKey, out var request))
|
2022-03-07 14:08:01 +00:00
|
|
|
|
{
|
|
|
|
|
|
// Create bookmarks for when we receive the expected HTTP request.
|
2022-11-23 10:20:00 +00:00
|
|
|
|
context.CreateBookmarks(GetBookmarkPayloads(context.ExpressionExecutionContext));
|
2022-03-07 14:08:01 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-03-09 23:19:00 +00:00
|
|
|
|
|
2022-03-07 14:08:01 +00:00
|
|
|
|
// Provide the received HTTP request as output.
|
2022-03-09 23:19:00 +00:00
|
|
|
|
context.Set(Result, request);
|
2022-11-23 10:20:00 +00:00
|
|
|
|
|
|
|
|
|
|
// Complete.
|
|
|
|
|
|
await context.CompleteActivityAsync();
|
2022-03-07 14:08:01 +00:00
|
|
|
|
}
|
2022-01-04 08:42:12 +00:00
|
|
|
|
|
2022-11-23 10:20:00 +00:00
|
|
|
|
private IEnumerable<object> GetBookmarkPayloads(ExpressionExecutionContext context)
|
2022-01-04 08:42:12 +00:00
|
|
|
|
{
|
2022-03-07 11:01:27 +00:00
|
|
|
|
// Generate bookmark data for path and selected methods.
|
2022-01-13 21:38:01 +00:00
|
|
|
|
var path = context.Get(Path);
|
|
|
|
|
|
var methods = context.Get(SupportedMethods);
|
2022-10-04 09:57:01 +00:00
|
|
|
|
return methods!.Select(x => new HttpEndpointBookmarkPayload(path!, x.ToLowerInvariant())).Cast<object>().ToArray();
|
2022-01-04 08:42:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|