Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Output notification handler messages as a header (#15786)
* Output notification handler messages as a header * Make the notification contract public * Moved the Notification type and added it to swagger schema * Update swagger docs --------- Co-authored-by: Bjarke Berg <mail@bergmania.dk>
- Loading branch information
Showing
7 changed files
with
5,480 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/Umbraco.Cms.Api.Management/Filters/AppendEventMessagesAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Umbraco.Cms.Api.Management.ViewModels; | ||
using Umbraco.Cms.Core; | ||
using Umbraco.Cms.Core.Events; | ||
using Umbraco.Cms.Core.Serialization; | ||
|
||
namespace Umbraco.Cms.Api.Management.Filters; | ||
|
||
/// <summary> | ||
/// When applied to a controller, this ensures that any event messages created during a request (e.g. by notification | ||
/// handlers) are appended to the response in a custom header. | ||
/// </summary> | ||
/// <remarks> | ||
/// GET operations are explicitly ignored by this, as they are not expected to generate event messages. | ||
/// </remarks> | ||
public sealed class AppendEventMessagesAttribute : TypeFilterAttribute | ||
{ | ||
public AppendEventMessagesAttribute() | ||
: base(typeof(AppendEventMessagesFilter)) | ||
{ | ||
} | ||
|
||
private class AppendEventMessagesFilter : IActionFilter | ||
{ | ||
private readonly IEventMessagesFactory _eventMessagesFactory; | ||
private readonly IJsonSerializer _jsonSerializer; | ||
|
||
public AppendEventMessagesFilter(IEventMessagesFactory eventMessagesFactory, IJsonSerializer jsonSerializer) | ||
{ | ||
_eventMessagesFactory = eventMessagesFactory; | ||
_jsonSerializer = jsonSerializer; | ||
} | ||
|
||
public void OnActionExecuted(ActionExecutedContext context) | ||
{ | ||
if (context.HttpContext.Response is null) | ||
{ | ||
return; | ||
} | ||
|
||
if (context.HttpContext.Request.Method == HttpMethod.Get.Method) | ||
{ | ||
return; | ||
} | ||
|
||
EventMessage[]? eventMessages = _eventMessagesFactory.GetOrDefault()?.GetAll()?.ToArray(); | ||
if (eventMessages is null || eventMessages.Any() is false) | ||
{ | ||
return; | ||
} | ||
|
||
var headerContent = _jsonSerializer.Serialize(eventMessages.Select(message => | ||
new NotificationHeaderModel | ||
{ | ||
Message = message.Message, Category = message.Category, Type = message.MessageType | ||
})); | ||
|
||
context.HttpContext.Response.Headers[Constants.Headers.Notifications] = headerContent; | ||
} | ||
|
||
public void OnActionExecuting(ActionExecutingContext context) | ||
{ | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.