| | | 1 | | using System.Net.Mime; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Authorization; |
| | | 4 | | using Bpmn.Model; |
| | | 5 | | using Elsa.Abstractions; |
| | | 6 | | using Elsa.Bpmn.Interchange.Endpoints.Bpmn; |
| | | 7 | | using Elsa.Bpmn.Interchange.Services; |
| | | 8 | | using Elsa.Common.Models; |
| | | 9 | | using Elsa.Workflows.Management; |
| | | 10 | | using Elsa.Workflows.Models; |
| | | 11 | | using JetBrains.Annotations; |
| | | 12 | | using Microsoft.AspNetCore.Http; |
| | | 13 | | |
| | | 14 | | namespace Elsa.Bpmn.Interchange.Endpoints.Bpmn.Document.Get; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Reads a stored workflow definition's BPMN source back out as the library's own <c>bpmnDefinitions</c> JSON |
| | | 18 | | /// document — the shape <c>Endpoints.Bpmn.Document.Put.Put</c> accepts back — rather than as <c>.bpmn</c> XML. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks> |
| | | 21 | | /// A thin wrapper over <see cref="BpmnInterchangeDocumentService.ReadDocument"/>. Studio never holds the whole |
| | | 22 | | /// document as JSON; the round trip this endpoint and <c>Put</c> exist for is what lets an edit made there — a |
| | | 23 | | /// binding (W11), a moved shape (W14) — be written back without a client-side reimplementation of |
| | | 24 | | /// <c>BpmnXmlWriter</c>. Same missing/stale-source refusals as <c>Export</c>; see |
| | | 25 | | /// <see cref="BpmnInterchangeDocumentService"/>'s remarks for what each means. |
| | | 26 | | /// <para> |
| | | 27 | | /// The response body is written with <see cref="BpmnDocumentJsonOptions"/>, not through FastEndpoints' configured |
| | | 28 | | /// serializer — see that type's remarks for why the two disagree on shape. |
| | | 29 | | /// </para> |
| | | 30 | | /// </remarks> |
| | | 31 | | [UsedImplicitly] |
| | 80 | 32 | | internal sealed class Get(IWorkflowDefinitionStore store, BpmnInterchangeDocumentService documentService) : ElsaEndpoint |
| | | 33 | | { |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public override void Configure() |
| | | 36 | | { |
| | 46 | 37 | | Get("bpmn/definitions/{definitionId}/document"); |
| | 46 | 38 | | RequirePermission(Elsa.Bpmn.Interchange.Permissions.BpmnPermissions.Definitions, CoreVerbs.View); |
| | 46 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 43 | | { |
| | 34 | 44 | | var definitionId = Route<string>("definitionId")!; |
| | 34 | 45 | | var filter = WorkflowDefinitionHandle.ByDefinitionId(definitionId, VersionOptions.Latest).ToFilter(); |
| | 34 | 46 | | var definition = await store.FindAsync(filter, cancellationToken); |
| | | 47 | | |
| | 34 | 48 | | if (definition == null) |
| | | 49 | | { |
| | 1 | 50 | | await Send.NotFoundAsync(cancellationToken); |
| | 1 | 51 | | return; |
| | | 52 | | } |
| | | 53 | | |
| | 33 | 54 | | await BpmnExportErrorResponses.RunAsync( |
| | 33 | 55 | | async () => |
| | 33 | 56 | | { |
| | 33 | 57 | | var document = documentService.ReadDocument(definition); |
| | 31 | 58 | | var json = JsonSerializer.Serialize(document, BpmnDocumentJsonOptions.Value); |
| | 31 | 59 | | HttpContext.Response.Headers.ETag = BpmnDocumentETag.From(definition); |
| | 31 | 60 | | await Send.StringAsync(json, contentType: MediaTypeNames.Application.Json, cancellation: cancellationTok |
| | 31 | 61 | | }, |
| | 33 | 62 | | HttpContext.Response, |
| | 0 | 63 | | message => AddError(message), |
| | 33 | 64 | | Send.ErrorsAsync, |
| | 33 | 65 | | cancellationToken); |
| | 34 | 66 | | } |
| | | 67 | | } |