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 ... /// /// Validate XProxy Configuration for Client ... /// /// public bool ValidateConfiguration () { // var result = !Configuration.IsNull () && !Configuration.Host.IsNullOrEmpty () && Configuration.Host.IsValidUrl () && Configuration.Routes.HasChild (); // return result; } /// /// retrie Normalize Host ... /// /// 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; } /// /// retrieve all registred Routes ... /// /// public IEnumerable GetRoutes () { // // Validate Args ... if (Configuration.IsNull ()) { return null; } // return Configuration.Routes; } /// /// Retrieve all Prepared url of Routes ... /// /// public IEnumerable GetRouteUrls () { // var normalHost = NormalizeHost (); if (normalHost.IsNullOrEmpty ()) { return null; } // var result = GetRoutes () .Select (r => $"{normalHost}{r.Route}"); // return result; } /// /// Find an Specific Route ... /// /// /// public XProxyRouteDescriptor FindRoute (Expression> whereClause) { // // Validate Arg ... if (whereClause.IsNull ()) { return null; } // var whereFunc = whereClause .Compile (); // var result = GetRoutes () .FirstOrDefault (whereFunc); // return result; } /// /// Prepare a request url for a Proxy Route ... /// /// /// /// 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; } /// /// Prepare a request url for a Selected Proxy Route ... /// /// /// /// public string PrepareActionUrlForXProxyRoute ( string url, Expression> 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 } }