2019-01-01 23:09:13 +00:00
|
|
|
using System;
|
2019-02-17 14:28:23 +00:00
|
|
|
using System.Collections.Generic;
|
2019-01-01 23:09:13 +00:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-12-31 09:59:35 +00:00
|
|
|
using Elsa.Activities.Http.Activities;
|
2019-01-01 23:09:13 +00:00
|
|
|
using Elsa.Activities.Http.Extensions;
|
|
|
|
|
using Elsa.Activities.Http.Models;
|
2018-12-31 09:59:35 +00:00
|
|
|
using Elsa.Handlers;
|
|
|
|
|
using Elsa.Models;
|
|
|
|
|
using Elsa.Results;
|
2018-12-30 17:20:12 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
2019-01-05 11:50:08 +00:00
|
|
|
namespace Elsa.Activities.Http.Drivers
|
2018-12-30 17:20:12 +00:00
|
|
|
{
|
2019-01-05 11:50:08 +00:00
|
|
|
public class HttpRequestTriggerDriver : ActivityDriver<HttpRequestTrigger>
|
2018-12-30 17:20:12 +00:00
|
|
|
{
|
|
|
|
|
private readonly IHttpContextAccessor httpContextAccessor;
|
|
|
|
|
private readonly IWorkflowExpressionEvaluator expressionEvaluator;
|
2019-02-17 14:28:23 +00:00
|
|
|
private readonly IEnumerable<IContentFormatter> contentFormatters;
|
2018-12-30 17:20:12 +00:00
|
|
|
|
2019-01-05 11:50:08 +00:00
|
|
|
public HttpRequestTriggerDriver(
|
2018-12-30 17:20:12 +00:00
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2019-02-17 14:28:23 +00:00
|
|
|
IWorkflowExpressionEvaluator expressionEvaluator,
|
|
|
|
|
IEnumerable<IContentFormatter> contentFormatters)
|
2018-12-30 17:20:12 +00:00
|
|
|
{
|
|
|
|
|
this.httpContextAccessor = httpContextAccessor;
|
|
|
|
|
this.expressionEvaluator = expressionEvaluator;
|
2019-02-17 14:28:23 +00:00
|
|
|
this.contentFormatters = contentFormatters;
|
2018-12-30 17:20:12 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-16 10:00:30 +00:00
|
|
|
protected override ActivityExecutionResult OnExecute(HttpRequestTrigger activity, WorkflowExecutionContext workflowContext)
|
|
|
|
|
{
|
|
|
|
|
return Halt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task<ActivityExecutionResult> OnResumeAsync(HttpRequestTrigger activity, WorkflowExecutionContext workflowContext, CancellationToken cancellationToken)
|
2018-12-30 17:20:12 +00:00
|
|
|
{
|
|
|
|
|
var request = httpContextAccessor.HttpContext.Request;
|
2019-01-01 23:09:13 +00:00
|
|
|
var model = new HttpRequestModel
|
2018-12-30 17:20:12 +00:00
|
|
|
{
|
2019-01-01 23:09:13 +00:00
|
|
|
Path = new Uri(request.Path.ToString(), UriKind.Relative),
|
|
|
|
|
QueryString = request.Query.ToDictionary(x => x.Key, x => x.Value),
|
|
|
|
|
Headers = request.Headers.ToDictionary(x => x.Key, x => x.Value),
|
|
|
|
|
Method = request.Method
|
2018-12-30 17:20:12 +00:00
|
|
|
};
|
2019-01-01 23:09:13 +00:00
|
|
|
|
|
|
|
|
if (activity.ReadContent)
|
|
|
|
|
{
|
|
|
|
|
if (request.HasFormContentType)
|
|
|
|
|
{
|
|
|
|
|
model.Form = (await request.ReadFormAsync(cancellationToken)).ToDictionary(x => x.Key, x => x.Value);
|
|
|
|
|
}
|
2019-02-17 14:28:23 +00:00
|
|
|
|
|
|
|
|
var formatter = SelectContentFormatter(request.ContentType);
|
|
|
|
|
var content = await request.ReadBodyAsync();
|
|
|
|
|
model.Content = content;
|
|
|
|
|
model.FormattedContent = await formatter.FormatAsync(content, request.ContentType);
|
2019-01-01 23:09:13 +00:00
|
|
|
}
|
2019-02-16 10:00:30 +00:00
|
|
|
|
2019-01-01 23:09:13 +00:00
|
|
|
workflowContext.CurrentScope.LastResult = model;
|
2019-02-16 10:00:30 +00:00
|
|
|
|
|
|
|
|
return Endpoint("Done");
|
2018-12-30 17:20:12 +00:00
|
|
|
}
|
2019-02-17 14:28:23 +00:00
|
|
|
|
|
|
|
|
private IContentFormatter SelectContentFormatter(string contentType)
|
|
|
|
|
{
|
|
|
|
|
var formatters = contentFormatters.OrderByDescending(x => x.Priority).ToList();
|
|
|
|
|
return formatters.FirstOrDefault(x => x.SupportedContentTypes.Contains(contentType, StringComparer.OrdinalIgnoreCase)) ?? formatters.Last();
|
|
|
|
|
}
|
2018-12-30 17:20:12 +00:00
|
|
|
}
|
|
|
|
|
}
|