Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using cdmdotnet.Logging;
11 : using Cqrs.Authentication;
12 : using Cqrs.Events;
13 : using Cqrs.WebApi.SignalR.Hubs;
14 :
15 : namespace Cqrs.WebApi.Events.Handlers
16 : {
17 : /// <summary>
18 : /// Proxies <see cref="IEvent{TAuthenticationToken}">events</see> from the event bus to the <see cref="INotificationHub"/>.
19 : /// This requires one or more <see cref="IEventHandler"/> implementations in order to be triggered.
20 : /// </summary>
21 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
22 : public abstract class EventToHubProxy<TAuthenticationToken>
23 1 : {
24 : /// <summary>
25 : /// Instantiates a new instance of <see cref="EventToHubProxy{TAuthenticationToken}"/>.
26 : /// </summary>
27 1 : protected EventToHubProxy(ILogger logger, INotificationHub notificationHub, IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper)
28 : {
29 : Logger = logger;
30 : NotificationHub = notificationHub;
31 : AuthenticationTokenHelper = authenticationTokenHelper;
32 : }
33 :
34 : /// <summary>
35 : /// Gets or sets the <see cref="ILogger"/>.
36 : /// </summary>
37 : protected ILogger Logger { get; private set; }
38 :
39 : /// <summary>
40 : /// The <see cref="INotificationHub"/> to proxy <see cref="IEvent{TAuthenticationToken}">events</see> to.
41 : /// </summary>
42 : protected INotificationHub NotificationHub { get; private set; }
43 :
44 : /// <summary>
45 : /// Gets or sets the <see cref="IAuthenticationTokenHelper{TAuthenticationToken}"/>.
46 : /// </summary>
47 : protected IAuthenticationTokenHelper<TAuthenticationToken> AuthenticationTokenHelper { get; private set; }
48 :
49 : /// <summary>
50 : /// Get the authentication token of the entity that triggered the request generating the provided <paramref name="message"/>
51 : /// Extract any proxy information attributes (<see cref="NotifyCallerEventAttribute"/>, <see cref="NotifyEveryoneEventAttribute"/> and <see cref="NotifyEveryoneExceptCallerEventAttribute"/>)
52 : /// then proxy the provided <paramref name="message"/> to <see cref="NotificationHub"/> if an attribute is present.
53 : /// </summary>
54 : /// <param name="message">The <see cref="IEvent{TAuthenticationToken}"/> to proxy.</param>
55 1 : protected virtual void HandleGenericEvent(IEvent<TAuthenticationToken> message)
56 : {
57 : Type eventType = message.GetType();
58 : var notifyCallerEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyCallerEventAttribute)) as NotifyCallerEventAttribute;
59 : var notifyEveryoneEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyEveryoneEventAttribute)) as NotifyEveryoneEventAttribute;
60 : var notifyEveryoneExceptCallerEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyEveryoneExceptCallerEventAttribute)) as NotifyEveryoneExceptCallerEventAttribute;
61 :
62 : string userToken = ((object)AuthenticationTokenHelper.GetAuthenticationToken() ?? string.Empty).ToString().Replace(".", string.Empty);
63 :
64 : if (notifyCallerEventAttribute != null)
65 : {
66 : NotificationHub.SendUserEvent(message, userToken);
67 : }
68 : if (notifyEveryoneEventAttribute != null)
69 : {
70 : NotificationHub.SendAllUsersEvent(message);
71 : }
72 : if (notifyEveryoneExceptCallerEventAttribute != null)
73 : {
74 : NotificationHub.SendExceptThisUserEvent(message, userToken);
75 : }
76 : }
77 : }
78 : }
|