344 lines
9.1 KiB
C#
344 lines
9.1 KiB
C#
using System.Linq;
|
|
using xCommons.Extensions;
|
|
using xEventService.Models;
|
|
using xExceptions.Constants;
|
|
using xEventService.Constants;
|
|
using xEventService.Configuration;
|
|
|
|
namespace xEventService.Extensions
|
|
{
|
|
/// <summary>
|
|
/// Extensions for Event Service ...
|
|
/// </summary>
|
|
public static class XEventServiceExtensions
|
|
{
|
|
/// <summary>
|
|
/// Validate Event Broker ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValid(this XEventBroker source)
|
|
{
|
|
//
|
|
var result =
|
|
!source.IsNull() &&
|
|
source != XEventBroker.None;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate Event Service Configuration ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValid(this XEventServiceConfiguration source)
|
|
{
|
|
//
|
|
var result =
|
|
!source.IsNullOrDefault() &&
|
|
source.Broker.IsValid() &&
|
|
!source.Url.IsNullOrEmpty() &&
|
|
source.Url.IsValidUrl();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate RabbitMQ Exchange Type ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValid(this XRabbitMQExchangeType source)
|
|
{
|
|
//
|
|
var result = source != XRabbitMQExchangeType.None;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate RabbitMQ Configuration ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValid(this XRabbitMQOptions source)
|
|
{
|
|
//
|
|
var result =
|
|
!source.IsNullOrDefault() &&
|
|
!source.Exchange.IsNullOrEmpty() &&
|
|
source.ExchangeType.IsValid();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate an Event Message ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValid(this XEventMessage source)
|
|
{
|
|
//
|
|
var result =
|
|
!source.IsNullOrDefault() &&
|
|
!source.Action.IsNullOrEmpty() &&
|
|
!source.Sender.IsNullOrEmpty();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate an Event Request ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValid(this XEventRequest source)
|
|
{
|
|
//
|
|
var result =
|
|
!source.IsNullOrDefault() &&
|
|
!source.Topic.IsNullOrEmpty() &&
|
|
source.Message.IsValid();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate ZeroMQ Pattern ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValid(this XZeroMQPattern source)
|
|
{
|
|
//
|
|
var result = source != XZeroMQPattern.None;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate ZeroMQ Options ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValid(this XZeroMQOptions source)
|
|
{
|
|
//
|
|
var result =
|
|
!source.IsNullOrDefault() &&
|
|
source.Pattern.IsValid();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate an Array is Wildcard passed or not ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsAllowedWildcard(this string[] source)
|
|
{
|
|
//
|
|
var result =
|
|
source is not null &&
|
|
source.Length == 1 &&
|
|
source.Any(s =>
|
|
s == XEventServiceConstants.XWildcard);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate a Collection is Valid for Kafka Consumer ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsAllowedCollection(
|
|
this string[] source
|
|
)
|
|
{
|
|
//
|
|
var result = source.HasChild();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate Sender ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="allowedSenders"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValidSender(
|
|
this XEventMessage source,
|
|
string[] allowedSenders
|
|
)
|
|
{
|
|
//
|
|
// Validate Source and Collection ...
|
|
var result =
|
|
source.IsValid() &&
|
|
allowedSenders
|
|
.IsAllowedCollection();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Validate Sender ...
|
|
result =
|
|
allowedSenders.IsAllowedWildcard() ||
|
|
allowedSenders.Any(x =>
|
|
x.Equals(
|
|
source.Sender,
|
|
System.StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate Action ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="allowedActions"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValidAction(
|
|
this XEventMessage source,
|
|
string[] allowedActions
|
|
)
|
|
{
|
|
//
|
|
// Validate Source and Collection ...
|
|
var result =
|
|
source.IsValid() &&
|
|
allowedActions
|
|
.IsAllowedCollection();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Validate Action ...
|
|
result =
|
|
allowedActions.IsAllowedWildcard() ||
|
|
allowedActions.Any(x =>
|
|
x.Equals(
|
|
source.Action,
|
|
System.StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Completely Validate a Message, based on providing:
|
|
/// - Allowed Actions
|
|
/// - Allowed Senders
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="allowedSenders"></param>
|
|
/// <param name="allowedActions"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValid(
|
|
this XEventMessage source,
|
|
string[] allowedSenders,
|
|
string[] allowedActions
|
|
)
|
|
{
|
|
//
|
|
var result =
|
|
source.IsValid() &&
|
|
source.IsValidAction(allowedActions) &&
|
|
source.IsValidSender(allowedSenders);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parse Url and Extract Host address ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static string GetRabbitMQHost(this XEventServiceConfiguration source)
|
|
{
|
|
//
|
|
var result = string.Empty;
|
|
|
|
//
|
|
if (!source.IsValid())
|
|
{
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
|
|
//
|
|
var parts = source.Url.Split(':');
|
|
if (parts.Length != 2)
|
|
{
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
|
|
//
|
|
result = parts[0];
|
|
if (result.IsNullOrEmpty() ||
|
|
!result.IsValidUrl()
|
|
)
|
|
{
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parse Url and Extract Port Number ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static int GetRabbitMQPort(this XEventServiceConfiguration source)
|
|
{
|
|
//
|
|
int result = -1;
|
|
|
|
//
|
|
if (!source.IsValid())
|
|
{
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
|
|
//
|
|
var parts = source.Url.Split(':');
|
|
if (parts.Length != 2)
|
|
{
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
|
|
//
|
|
// Try Parse Port as Integer ...
|
|
int.TryParse(parts[1], out result);
|
|
if (result <= 0)
|
|
{
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |