2022-01-04 08:42:12 +00:00
|
|
|
using System.Net;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Workflows.Core.Attributes;
|
|
|
|
|
using Elsa.Workflows.Core.Models;
|
2022-01-04 08:42:12 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
2022-05-26 10:47:31 +00:00
|
|
|
namespace Elsa.Http;
|
2022-01-04 08:42:12 +00:00
|
|
|
|
2022-12-21 21:22:21 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Write a response to the current HTTP response object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Activity("Elsa", "HTTP", "Write a response to the current HTTP response object.", DisplayName = "HTTP Response")]
|
2022-01-04 08:42:12 +00:00
|
|
|
public class WriteHttpResponse : Activity
|
|
|
|
|
{
|
2022-12-21 21:22:21 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// The status code to return.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Input(DefaultValue = HttpStatusCode.OK, Description = "The status code to return.")]
|
2022-01-04 08:42:12 +00:00
|
|
|
public Input<HttpStatusCode> StatusCode { get; set; } = new(HttpStatusCode.OK);
|
2022-12-21 21:22:21 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The content to write back.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Input(Description = "The content to write back.")]
|
2022-01-04 08:42:12 +00:00
|
|
|
public Input<string?> Content { get; set; } = new("");
|
|
|
|
|
|
2022-12-21 21:22:21 +00:00
|
|
|
/// <inheritdoc />
|
2022-01-26 14:48:59 +00:00
|
|
|
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
|
2022-01-04 08:42:12 +00:00
|
|
|
{
|
|
|
|
|
var httpContextAccessor = context.GetRequiredService<IHttpContextAccessor>();
|
|
|
|
|
var httpContext = httpContextAccessor.HttpContext;
|
2022-10-04 09:57:01 +00:00
|
|
|
|
|
|
|
|
if (httpContext == null)
|
|
|
|
|
{
|
2022-12-21 21:22:21 +00:00
|
|
|
// We're executing in a non-HTTP context (e.g. in a virtual actor).
|
|
|
|
|
// Create a bookmark to allow the invoker to export the state and resume execution from there.
|
2022-10-04 09:57:01 +00:00
|
|
|
|
|
|
|
|
context.CreateBookmark(OnResumeAsync);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-21 21:22:21 +00:00
|
|
|
|
2022-10-04 09:57:01 +00:00
|
|
|
var response = httpContext.Response;
|
|
|
|
|
|
|
|
|
|
response.StatusCode = (int)context.Get(StatusCode);
|
|
|
|
|
|
|
|
|
|
var content = context.Get(Content);
|
|
|
|
|
|
|
|
|
|
if (content != null)
|
|
|
|
|
await response.WriteAsync(content, context.CancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async ValueTask OnResumeAsync(ActivityExecutionContext context)
|
|
|
|
|
{
|
|
|
|
|
var httpContextAccessor = context.GetRequiredService<IHttpContextAccessor>();
|
|
|
|
|
var httpContext = httpContextAccessor.HttpContext;
|
|
|
|
|
|
|
|
|
|
if (httpContext == null)
|
|
|
|
|
{
|
|
|
|
|
// We're not in an HTTP context, so let's fail.
|
|
|
|
|
throw new Exception("Cannot execute in a non-HTTP context");
|
|
|
|
|
}
|
2022-12-21 21:22:21 +00:00
|
|
|
|
2022-01-04 08:42:12 +00:00
|
|
|
var response = httpContext.Response;
|
|
|
|
|
|
|
|
|
|
response.StatusCode = (int)context.Get(StatusCode);
|
|
|
|
|
|
|
|
|
|
var content = context.Get(Content);
|
|
|
|
|
|
|
|
|
|
if (content != null)
|
|
|
|
|
await response.WriteAsync(content, context.CancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|