-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathHttpFeature.cs
More file actions
247 lines (211 loc) · 11 KB
/
Copy pathHttpFeature.cs
File metadata and controls
247 lines (211 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System.Net;
using CShells.AspNetCore.Features;
using CShells.Features;
using Elsa.Extensions;
using Elsa.Http.Bookmarks;
using Elsa.Http.ContentWriters;
using Elsa.Http.DownloadableContentHandlers;
using Elsa.Http.FileCaches;
using Elsa.Http.Handlers;
using Elsa.Http.Options;
using Elsa.Http.Parsers;
using Elsa.Http.PortResolvers;
using Elsa.Http.Resilience;
using Elsa.Http.Selectors;
using Elsa.Http.Services;
using Elsa.Http.Tasks;
using Elsa.Http.TriggerPayloadValidators;
using Elsa.Http.UIHints;
using Elsa.Resilience.Extensions;
using Elsa.Workflows;
using Elsa.Workflows.Management.Extensions;
using Elsa.Workflows.Options;
using FluentStorage;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Elsa.Common.Serialization;
namespace Elsa.Http.ShellFeatures;
/// <summary>
/// Installs services related to HTTP services and activities.
/// </summary>
[ShellFeature(
DisplayName = "HTTP",
Description = "Provides HTTP-related activities and services for workflow execution",
DependsOn = ["HttpJavaScript", "Resilience"])]
[UsedImplicitly]
public class HttpFeature : IMiddlewareShellFeature
{
/// <summary>
/// The <see cref="HttpActivityOptions"/> to configure.
/// </summary>
public HttpActivityOptions HttpActivityOptions { get; set; } = new();
/// <summary>
/// A delegate to configure <see cref="HttpFileCacheOptions"/>.
/// </summary>
public Action<HttpFileCacheOptions>? ConfigureHttpFileCacheOptions { get; set; }
/// <summary>
/// A delegate that is invoked when authorizing an inbound HTTP request.
/// </summary>
public Func<IServiceProvider, IHttpEndpointAuthorizationHandler> HttpEndpointAuthorizationHandler { get; set; } = sp => sp.GetRequiredService<AuthenticationBasedHttpEndpointAuthorizationHandler>();
/// <summary>
/// A delegate that is invoked when an HTTP workflow faults.
/// </summary>
public Func<IServiceProvider, IHttpEndpointFaultHandler> HttpEndpointWorkflowFaultHandler { get; set; } = sp => sp.GetRequiredService<DefaultHttpEndpointFaultHandler>();
/// <summary>
/// A delegate to configure the <see cref="IContentTypeProvider"/>.
/// </summary>
public Func<IServiceProvider, IContentTypeProvider> ContentTypeProvider { get; set; } = _ => new FileExtensionContentTypeProvider();
/// <summary>
/// A delegate to configure the <see cref="IFileCacheStorageProvider"/>.
/// </summary>
public Func<IServiceProvider, IFileCacheStorageProvider> FileCache { get; set; } = sp =>
{
var options = sp.GetRequiredService<IOptions<HttpFileCacheOptions>>().Value;
var blobStorage = StorageFactory.Blobs.DirectoryFiles(options.LocalCacheDirectory);
return new BlobFileCacheStorageProvider(blobStorage);
};
/// <summary>
/// A delegate to configure the <see cref="HttpClient"/> used when by the <see cref="FlowSendHttpRequest"/> and <see cref="SendHttpRequest"/> activities.
/// </summary>
public Action<IServiceProvider, HttpClient> HttpClient { get; set; } = (_, _) => { };
/// <summary>
/// A delegate to configure the <see cref="HttpClientBuilder"/> for <see cref="HttpClient"/>.
/// </summary>
public Action<IHttpClientBuilder> HttpClientBuilder { get; set; } = _ => { };
/// <summary>
/// A list of <see cref="IHttpCorrelationIdSelector"/> types to register with the service collection.
/// </summary>
public ICollection<Type> HttpCorrelationIdSelectorTypes { get; } = new List<Type>
{
typeof(HeaderHttpCorrelationIdSelector),
typeof(QueryStringHttpCorrelationIdSelector)
};
/// <summary>
/// A list of <see cref="IHttpWorkflowInstanceIdSelector"/> types to register with the service collection.
/// </summary>
public ICollection<Type> HttpWorkflowInstanceIdSelectorTypes { get; } = new List<Type>
{
typeof(HeaderHttpWorkflowInstanceIdSelector),
typeof(QueryStringHttpWorkflowInstanceIdSelector)
};
public void ConfigureServices(IServiceCollection services)
{
// Register HTTP activities.
services.AddActivitiesFrom<HttpFeature>();
// Register HTTP variable types.
services.AddVariableDescriptors([
new(typeof(HttpRouteData), "HTTP", null),
new(typeof(HttpRequest), "HTTP", null),
new(typeof(HttpResponse), "HTTP", null),
new(typeof(HttpResponseMessage), "HTTP", null),
new(typeof(HttpHeaders), "HTTP", null),
new(typeof(IFormFile), "HTTP", null),
new(typeof(HttpFile), "HTTP", null),
new(typeof(Downloadable), "HTTP", null),
]);
// Register the HTTP resilience strategy.
services.AddResilienceStrategy<HttpResilienceStrategy>();
var configureFileCacheOptions = ConfigureHttpFileCacheOptions ?? (options => { options.TimeToLive = TimeSpan.FromDays(7); });
services.Configure<HttpActivityOptions>(options =>
{
options.BasePath = HttpActivityOptions.BasePath;
options.BaseUrl = HttpActivityOptions.BaseUrl;
options.AvailableContentTypes = HttpActivityOptions.AvailableContentTypes;
options.WriteHttpResponseSynchronously = HttpActivityOptions.WriteHttpResponseSynchronously;
});
services.Configure(configureFileCacheOptions);
var httpClientBuilder = services.AddHttpClient<SendHttpRequestBase>(HttpClient);
HttpClientBuilder(httpClientBuilder);
services
.AddScoped<IRouteMatcher, RouteMatcher>()
.AddScoped<IRouteTable, RouteTable>()
.AddScoped<IAbsoluteUrlProvider, DefaultAbsoluteUrlProvider>()
.AddScoped<IRouteTableUpdater, DefaultRouteTableUpdater>()
.AddScoped<IHttpWorkflowLookupService, HttpWorkflowLookupService>()
.AddScoped(ContentTypeProvider)
.AddHttpContextAccessor()
// Handlers.
.AddNotificationHandler<UpdateRouteTable>()
// Content parsers.
.AddSingleton<IHttpContentParser, JsonHttpContentParser>()
.AddSingleton<IHttpContentParser, XmlHttpContentParser>()
.AddSingleton<IHttpContentParser, PlainTextHttpContentParser>()
.AddSingleton<IHttpContentParser, TextHtmlHttpContentParser>()
.AddSingleton<IHttpContentParser, FileHttpContentParser>()
// HTTP content factories.
.AddScoped<IHttpContentFactory, TextContentFactory>()
.AddScoped<IHttpContentFactory, JsonContentFactory>()
.AddScoped<IHttpContentFactory, XmlContentFactory>()
.AddScoped<IHttpContentFactory, FormUrlEncodedHttpContentFactory>()
.AddScoped<IHttpContentFactory, MultipartFormDataHttpContentFactory>()
// Activity property options providers.
.AddScoped<IPropertyUIHandler, HttpContentTypeOptionsProvider>()
.AddScoped<IPropertyUIHandler, HttpEndpointPathUIHandler>()
// Default providers.
.AddScoped<DefaultHttpEndpointBasePathProvider>()
.AddScoped<IHttpEndpointBasePathProvider>(sp => sp.GetRequiredService<DefaultHttpEndpointBasePathProvider>())
// Port resolvers.
.AddScoped<IActivityResolver, SendHttpRequestActivityResolver>()
// HTTP endpoint handlers.
.AddScoped<AuthenticationBasedHttpEndpointAuthorizationHandler>()
.AddScoped<AllowAnonymousHttpEndpointAuthorizationHandler>()
.AddScoped<DefaultHttpEndpointFaultHandler>()
.AddScoped<DefaultHttpEndpointRoutesProvider>()
.AddScoped(HttpEndpointWorkflowFaultHandler)
.AddScoped(HttpEndpointAuthorizationHandler)
.AddScoped<IHttpEndpointRoutesProvider>(sp => sp.GetRequiredService<DefaultHttpEndpointRoutesProvider>())
// Startup tasks.
.AddStartupTask<UpdateRouteTableStartupTask>()
// Downloadable content handlers.
.AddScoped<IDownloadableManager, DefaultDownloadableManager>()
.AddScoped<IDownloadableContentHandler, MultiDownloadableContentHandler>()
.AddScoped<IDownloadableContentHandler, BinaryDownloadableContentHandler>()
.AddScoped<IDownloadableContentHandler, StreamDownloadableContentHandler>()
.AddScoped<IDownloadableContentHandler, FormFileDownloadableContentHandler>()
.AddScoped<IDownloadableContentHandler, DownloadableDownloadableContentHandler>()
.AddScoped<IDownloadableContentHandler, UrlDownloadableContentHandler>()
.AddScoped<IDownloadableContentHandler, StringDownloadableContentHandler>()
.AddScoped<IDownloadableContentHandler, HttpFileDownloadableContentHandler>()
//Trigger payload validators.
.AddTriggerPayloadValidator<HttpEndpointTriggerPayloadValidator, HttpEndpointBookmarkPayload>()
// File caches.
.AddScoped(FileCache)
.AddScoped<ZipManager>()
// AuthenticationBasedHttpEndpointAuthorizationHandler requires Authorization services.
.AddAuthorization();
// HTTP clients.
services.AddHttpClient<IFileDownloader, HttpClientFileDownloader>();
// Add selectors.
foreach (var httpCorrelationIdSelectorType in HttpCorrelationIdSelectorTypes)
services.AddScoped(typeof(IHttpCorrelationIdSelector), httpCorrelationIdSelectorType);
foreach (var httpWorkflowInstanceIdSelectorType in HttpWorkflowInstanceIdSelectorTypes)
services.AddScoped(typeof(IHttpWorkflowInstanceIdSelector), httpWorkflowInstanceIdSelectorType);
services.Configure<SerializationTypeOptions>(options =>
{
options.RegisterTypeAlias(typeof(HttpRequest), "HttpRequest");
options.RegisterTypeAlias(typeof(HttpResponse), "HttpResponse");
options.RegisterTypeAlias(typeof(HttpResponseMessage), "HttpResponseMessage");
options.RegisterTypeAlias(typeof(HttpHeaders), "HttpHeaders");
options.RegisterTypeAlias(typeof(HttpRouteData), "RouteData");
options.RegisterTypeAlias(typeof(IFormFile), "FormFile");
options.RegisterTypeAlias(typeof(IFormFile[]), "FormFile[]");
options.RegisterTypeAlias(typeof(HttpFile), "HttpFile");
options.RegisterTypeAlias(typeof(HttpFile[]), "HttpFile[]");
options.RegisterTypeAlias(typeof(Downloadable), "Downloadable");
options.RegisterTypeAlias(typeof(Downloadable[]), "Downloadable[]");
options.RegisterTypeAlias(typeof(HttpStatusCode), nameof(HttpStatusCode));
options.RegisterTypeAlias(typeof(HttpRequestException), nameof(HttpRequestException));
options.RegisterTypeAlias(typeof(HttpEndpointBookmarkPayload), nameof(HttpEndpointBookmarkPayload));
});
}
/// <inheritdoc />
public void UseMiddleware(IApplicationBuilder app, IHostEnvironment? environment)
{
app.UseWorkflows();
}
}