| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Authorization; |
| | | 3 | | using Bpmn.Model; |
| | | 4 | | using Elsa.Abstractions; |
| | | 5 | | using Elsa.Bpmn.Interchange.Endpoints.Bpmn; |
| | | 6 | | using Elsa.Bpmn.Interchange.Endpoints.Bpmn.Import; |
| | | 7 | | using Elsa.Bpmn.Interchange.Services; |
| | | 8 | | using Elsa.Common.Models; |
| | | 9 | | using Elsa.Extensions; |
| | | 10 | | using Elsa.Workflows.Management; |
| | | 11 | | using Elsa.Workflows.Models; |
| | | 12 | | using JetBrains.Annotations; |
| | | 13 | | using Microsoft.AspNetCore.Http; |
| | | 14 | | |
| | | 15 | | namespace Elsa.Bpmn.Interchange.Endpoints.Bpmn.Document.Put; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Accepts the whole <c>bpmnDefinitions</c> JSON document for an existing workflow definition, writes it back out as |
| | | 19 | | /// BPMN 2.0 XML, and imports it through the same path <c>Endpoints.Bpmn.Import.Import</c> runs — analyze, capability |
| | | 20 | | /// check, bind, persist as a draft, refresh the stored source. A published version is never edited in place: like |
| | | 21 | | /// <c>Import</c>, this always produces a new draft. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <remarks> |
| | | 24 | | /// A thin wrapper over <see cref="BpmnInterchangeDocumentService.ImportDocumentAsync"/>. The request body is bound as |
| | | 25 | | /// a raw string and deserialized explicitly with <see cref="BpmnDocumentJsonOptions"/>, not through FastEndpoints' |
| | | 26 | | /// configured serializer — see that type's remarks for why the two disagree on shape, and |
| | | 27 | | /// <c>Endpoints.Bpmn.Document.Get.Get</c> for the read side of this round trip. |
| | | 28 | | /// </remarks> |
| | | 29 | | [UsedImplicitly] |
| | 76 | 30 | | internal sealed class Put(IWorkflowDefinitionStore store, BpmnInterchangeDocumentService documentService) : ElsaEndpoint |
| | | 31 | | { |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public override void Configure() |
| | | 34 | | { |
| | 46 | 35 | | Put("bpmn/definitions/{definitionId}/document"); |
| | 46 | 36 | | RequirePermission(Elsa.Bpmn.Interchange.Permissions.BpmnPermissions.Definitions, CoreVerbs.Write); |
| | 46 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | | 40 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 41 | | { |
| | 30 | 42 | | var definitionId = Route<string>("definitionId")!; |
| | 30 | 43 | | var filter = WorkflowDefinitionHandle.ByDefinitionId(definitionId, VersionOptions.Latest).ToFilter(); |
| | 30 | 44 | | var definition = await store.FindAsync(filter, cancellationToken); |
| | | 45 | | |
| | 30 | 46 | | if (definition == null) |
| | | 47 | | { |
| | 1 | 48 | | await Send.NotFoundAsync(cancellationToken); |
| | 1 | 49 | | return; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | // Optimistic concurrency: a client that GETs the document, then PUTs it back after someone else has written the |
| | | 53 | | // definition in between, must not silently replace that intervening write. The client is required to carry the |
| | | 54 | | // ETag its GET returned as If-Match. A missing header cannot express "I know what I'm overwriting" at all, and |
| | | 55 | | // neither can "*", which matches whatever is stored — so both are refused as 428 rather than honoured. Anything |
| | | 56 | | // else must be exactly the current strong ETag (a weak W/ tag or a list never is), or it proves the client's co |
| | | 57 | | // is no longer current. The header is checked here so a stale client is refused before import work runs, and |
| | | 58 | | // again inside ImportDocumentAsync's compare-and-swap so a write that lands in that window is 412, not a silent |
| | | 59 | | // overwrite. Metadata carried forward is read in that same swap, not from this lookup. |
| | 29 | 60 | | var ifMatch = HttpContext.Request.Headers.IfMatch.ToString().Trim(); |
| | | 61 | | |
| | 29 | 62 | | if (ifMatch is "" or "*") |
| | | 63 | | { |
| | 2 | 64 | | await BpmnErrorResponse.SendAsync( |
| | 2 | 65 | | HttpContext.Response, |
| | 2 | 66 | | BpmnErrorResponse.Create( |
| | 2 | 67 | | "An If-Match header carrying the ETag from a prior GET of this document is required to PUT it back, |
| | 2 | 68 | | BpmnErrorCodes.DocumentPreconditionRequired, |
| | 2 | 69 | | StatusCodes.Status428PreconditionRequired), |
| | 2 | 70 | | cancellationToken); |
| | 2 | 71 | | return; |
| | | 72 | | } |
| | | 73 | | |
| | 27 | 74 | | if (!string.Equals(ifMatch, BpmnDocumentETag.From(definition), StringComparison.Ordinal)) |
| | | 75 | | { |
| | 3 | 76 | | await BpmnErrorResponse.SendAsync( |
| | 3 | 77 | | HttpContext.Response, |
| | 3 | 78 | | BpmnErrorResponse.Create( |
| | 3 | 79 | | "The workflow definition has been written since the ETag in If-Match was issued. GET the document ag |
| | 3 | 80 | | BpmnErrorCodes.DocumentPreconditionFailed, |
| | 3 | 81 | | StatusCodes.Status412PreconditionFailed), |
| | 3 | 82 | | cancellationToken); |
| | 3 | 83 | | return; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | string body; |
| | | 87 | | |
| | 24 | 88 | | using (var reader = new StreamReader(HttpContext.Request.Body)) |
| | 24 | 89 | | body = await reader.ReadToEndAsync(cancellationToken); |
| | | 90 | | |
| | | 91 | | BpmnDefinitions? document; |
| | | 92 | | |
| | | 93 | | try |
| | | 94 | | { |
| | 24 | 95 | | document = JsonSerializer.Deserialize<BpmnDefinitions>(body, BpmnDocumentJsonOptions.Value); |
| | 23 | 96 | | } |
| | | 97 | | catch (JsonException exception) |
| | | 98 | | { |
| | 1 | 99 | | AddError($"The request body is not a valid BPMN document: {exception.Message}"); |
| | 1 | 100 | | await Send.ErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken); |
| | 1 | 101 | | return; |
| | | 102 | | } |
| | | 103 | | |
| | 23 | 104 | | if (document is null) |
| | | 105 | | { |
| | 0 | 106 | | AddError("The request body must be a BPMN document, not JSON null."); |
| | 0 | 107 | | await Send.ErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken); |
| | 0 | 108 | | return; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | // The process this definition was imported from, when the document that produced it declared more than one: |
| | | 112 | | // reused here so a multi-process document keeps importing the same process on every edit, without asking the |
| | | 113 | | // caller to say so again. See BpmnInterchangeDocumentService.SourceProcessIdCustomPropertyKey. |
| | 23 | 114 | | var processId = definition.CustomProperties.TryGetValue<string>(BpmnInterchangeDocumentService.SourceProcessIdCu |
| | 23 | 115 | | ? storedProcessId |
| | 23 | 116 | | : null; |
| | | 117 | | |
| | 23 | 118 | | var result = await BpmnImportErrorResponses.RunAsync( |
| | 23 | 119 | | () => documentService.ImportDocumentAsync(document, definitionId, processId, cancellationToken, ifMatch), |
| | 23 | 120 | | HttpContext.Response, |
| | 1 | 121 | | message => AddError(message), |
| | 23 | 122 | | Send.ErrorsAsync, |
| | 23 | 123 | | cancellationToken); |
| | | 124 | | |
| | 23 | 125 | | if (result is null) |
| | 4 | 126 | | return; |
| | | 127 | | |
| | 19 | 128 | | var persisted = result.ImportResult.WorkflowDefinition; |
| | | 129 | | |
| | | 130 | | // The definition exactly as ImportDocumentAsync's final save wrote it, so the ETag names the state this PUT |
| | | 131 | | // produced. Reloading it from the store instead could pick up a write that landed after that save and hand the |
| | | 132 | | // client a validator for content it never saw, letting its next PUT overwrite that write silently. |
| | 19 | 133 | | HttpContext.Response.Headers.ETag = BpmnDocumentETag.From(persisted); |
| | | 134 | | |
| | 19 | 135 | | await Send.OkAsync(new Response |
| | 19 | 136 | | { |
| | 19 | 137 | | Id = persisted.Id, |
| | 19 | 138 | | DefinitionId = persisted.DefinitionId, |
| | 19 | 139 | | Version = persisted.Version, |
| | 19 | 140 | | Analysis = BpmnImportAnalysisModel.From(result.Analysis) |
| | 19 | 141 | | }, cancellationToken); |
| | 30 | 142 | | } |
| | | 143 | | } |