215 lines
5.8 KiB
C#
215 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Web;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xIdentityService.Configuration;
|
|
using xIdentityService.Interfaces;
|
|
|
|
namespace xIdentityService.Providers {
|
|
public class XProxyHelper : IXProxyHelper {
|
|
//
|
|
#region Props ...
|
|
public XProxyConfiguration Configuration { get; }
|
|
#endregion
|
|
|
|
//
|
|
#region Constructor ...
|
|
public XProxyHelper (XProxyConfiguration configuration) {
|
|
this.Configuration = configuration;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Actions ...
|
|
/// <summary>
|
|
/// Validate XProxy Configuration for Client ...
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool ValidateConfiguration () {
|
|
//
|
|
var result = !Configuration.IsNull () &&
|
|
!Configuration.Host.IsNullOrEmpty () &&
|
|
Configuration.Host.IsValidUrl () &&
|
|
Configuration.Routes.HasChild ();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrie Normalize Host ...
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string NormalizeHost () {
|
|
//
|
|
var result = string.Empty;
|
|
|
|
//
|
|
if (!Configuration.IsNull () &&
|
|
!Configuration.Host.IsNullOrEmpty () &&
|
|
Configuration.Host.IsValidUrl ()
|
|
) {
|
|
//
|
|
result = Configuration.Host
|
|
.ToNormalString ();
|
|
|
|
//
|
|
if (result.EndsWith ("/")) {
|
|
result = result.Substring (0, result.Length - 1);
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieve all registred Routes ...
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public IEnumerable<XProxyRouteDescriptor> GetRoutes () {
|
|
//
|
|
// Validate Args ...
|
|
if (Configuration.IsNull ()) {
|
|
return null;
|
|
}
|
|
|
|
//
|
|
return Configuration.Routes;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve all Prepared url of Routes ...
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public IEnumerable<string> GetRouteUrls () {
|
|
//
|
|
var normalHost = NormalizeHost ();
|
|
if (normalHost.IsNullOrEmpty ()) {
|
|
return null;
|
|
}
|
|
|
|
//
|
|
var result = GetRoutes ()
|
|
.Select (r => $"{normalHost}{r.Route}");
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Find an Specific Route ...
|
|
/// </summary>
|
|
/// <param name="whereClause"></param>
|
|
/// <returns></returns>
|
|
public XProxyRouteDescriptor FindRoute (Expression<Func<XProxyRouteDescriptor, bool>> whereClause) {
|
|
//
|
|
// Validate Arg ...
|
|
if (whereClause.IsNull ()) {
|
|
return null;
|
|
}
|
|
|
|
//
|
|
var whereFunc = whereClause
|
|
.Compile ();
|
|
|
|
//
|
|
var result = GetRoutes ()
|
|
.FirstOrDefault (whereFunc);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prepare a request url for a Proxy Route ...
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="descriptor"></param>
|
|
/// <returns></returns>
|
|
public string PrepareActionUrlForXProxyRoute (
|
|
string url,
|
|
XProxyRouteDescriptor descriptor
|
|
) {
|
|
//
|
|
if (!url.IsValidUrl () ||
|
|
url.IsNullOrEmpty () ||
|
|
!ValidateConfiguration ()
|
|
) {
|
|
throw XException.InvalidArgs.ToException ();
|
|
}
|
|
|
|
//
|
|
// find Route ...
|
|
var routeDescriptor = GetRoutes ()
|
|
.FirstOrDefault (r => r
|
|
.IsSameContent (descriptor)
|
|
);
|
|
|
|
//
|
|
// Validate Route Descriptor ...
|
|
if (
|
|
routeDescriptor.IsNull () ||
|
|
routeDescriptor.Route.IsNullOrEmpty ()
|
|
) {
|
|
throw XException.InvalidArgs.ToException ();
|
|
}
|
|
|
|
//
|
|
// Retrieve and Validate Host Address ...
|
|
var host = NormalizeHost ();
|
|
if (!host.IsValidUrl () ||
|
|
host.IsNullOrEmpty ()
|
|
) {
|
|
throw XException.InvalidArgs.ToException ();
|
|
}
|
|
|
|
//
|
|
url = $"{host}{routeDescriptor.Route}/{HttpUtility.UrlEncode(url)}";
|
|
|
|
//
|
|
return url;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prepare a request url for a Selected Proxy Route ...
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="whereClause"></param>
|
|
/// <returns></returns>
|
|
public string PrepareActionUrlForXProxyRoute (
|
|
string url,
|
|
Expression<Func<XProxyRouteDescriptor, bool>> whereClause
|
|
) {
|
|
//
|
|
if (!url.IsValidUrl () ||
|
|
url.IsNullOrEmpty () ||
|
|
whereClause.IsNull () ||
|
|
!ValidateConfiguration ()
|
|
) {
|
|
throw XException.InvalidArgs.ToException ();
|
|
}
|
|
|
|
//
|
|
var descriptor = FindRoute (whereClause);
|
|
|
|
//
|
|
// find Route ...
|
|
var result = PrepareActionUrlForXProxyRoute (
|
|
url: url,
|
|
descriptor: descriptor
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Private ...
|
|
#endregion
|
|
}
|
|
} |