From aa2e380745e21486d5ac26ba79aff8e05153f6dd Mon Sep 17 00:00:00 2001 From: Schwirg László Date: Thu, 8 Sep 2022 18:40:30 +0200 Subject: [PATCH] v1.15.0 WebApplication/SendUrl xml elem megvalósítása --- Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Http.cs | 20 +++++++++----------- Vrh.Log4Pro.MaintenanceConsole/Manager - WebApplicationManager.cs | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------ Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs | 4 ++-- Vrh.Log4Pro.MaintenanceConsole/Vrh.Log4Pro.MaintenanceConsole.csproj | 4 ++-- Vrh.Log4Pro.MaintenanceConsole/packages.config | 2 +- 5 files changed, 164 insertions(+), 58 deletions(-) diff --git a/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Http.cs b/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Http.cs index fc2c7b5..f560593 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Http.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Http.cs @@ -23,21 +23,19 @@ using Vrh.Log4Pro.MaintenanceConsole.CommandLineParserNS; using System.Net.Http; using System.Net.Http.Headers; using System.Collections.Generic; -using System.Text.Json; -using System.Text.Json.Serialization; - +using Newtonsoft.Json; namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS { public static class HttpTools { public enum RequestType { GET,POST,} - public static ReturnInfoJSON GetReturninfoJSON(string url, RequestType mode = RequestType.GET) + public static ReturnInfoJSON GetReturninfoJSON(string url, RequestType mode = RequestType.GET, List dictparameterlist=null) { string returninfojsonstring = ""; try { - return mode == RequestType.GET? _GetReturninfoJSON(url,out returninfojsonstring) : _PostReturninfoJSON(url, out returninfojsonstring); + return mode == RequestType.GET? _GetReturninfoJSON(url,out returninfojsonstring) : _PostReturninfoJSON(url, out returninfojsonstring, dictparameterlist); } catch (Exception ex) { return new ReturnInfoJSON() { ReturnValue = -1, ReturnMessage = ex.Message + "\n" + returninfojsonstring, }; } } @@ -46,16 +44,18 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS { var returninfojsonstream = Task.Run(async () => await (new HttpClient()).GetStreamAsync(url)).GetAwaiter().GetResult(); returninfojsonstring = GetStreamAsString(returninfojsonstream); - ReturnInfoJSON returninfojson = JsonSerializer.Deserialize(returninfojsonstring); + ReturnInfoJSON returninfojson = (ReturnInfoJSON)JsonConvert.DeserializeObject(returninfojsonstring,typeof(ReturnInfoJSON)); //ReturnInfoJSON returninfojson = Task.Run(async () => await JsonSerializer.DeserializeAsync(returninfojsonstream)).GetAwaiter().GetResult(); return returninfojson; } - private static ReturnInfoJSON _PostReturninfoJSON(string url, out string returninfojsonstring) + private static ReturnInfoJSON _PostReturninfoJSON(string url, out string returninfojsonstring, List dictparameterlist = null) { - var returninfojsonhttpresponsemessage = Task.Run(async () => await (new HttpClient()).PostAsync(url, null)).GetAwaiter().GetResult(); + var jsonstring = JsonConvert.SerializeObject(dictparameterlist.Where(p => p.PassTo == Vrh.XmlProcessing.UrlElement.ParameterTypes.dict)); + HttpContent requestcontent = new StringContent(jsonstring); + var returninfojsonhttpresponsemessage = Task.Run(async () => await (new HttpClient()).PostAsync(url, requestcontent)).GetAwaiter().GetResult(); var returninfojsonstream = Task.Run(async () => await returninfojsonhttpresponsemessage.Content.ReadAsStreamAsync()).GetAwaiter().GetResult(); returninfojsonstring = GetStreamAsString(returninfojsonstream); - ReturnInfoJSON returninfojson = JsonSerializer.Deserialize(returninfojsonstring); + ReturnInfoJSON returninfojson = (ReturnInfoJSON)JsonConvert.DeserializeObject(returninfojsonstring, typeof(ReturnInfoJSON)); //var returninfojson = Task.Run(async () => await JsonSerializer.DeserializeAsync(returninfojsonstream)).GetAwaiter().GetResult(); return returninfojson; } @@ -72,10 +72,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS } public class ReturnInfoJSON { - [JsonPropertyName("ReturnValue")] public int ReturnValue { get; set; } - [JsonPropertyName("ReturnMessage")] public string ReturnMessage { get; set; } } } diff --git a/Vrh.Log4Pro.MaintenanceConsole/Manager - WebApplicationManager.cs b/Vrh.Log4Pro.MaintenanceConsole/Manager - WebApplicationManager.cs index de1034c..cc05d1d 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Manager - WebApplicationManager.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Manager - WebApplicationManager.cs @@ -88,23 +88,65 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WebApplicationManagerNS var config = (parameters as Menu.ExecutorParameter).GetConfig(); var args = (parameters as Menu.ExecutorParameter).Args; var selectedwaindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.WebApplicationManager.Function.CMD_WEBAPPS); + var menuwapps = DisplayWebApplicationMenu(config, "Select the web application(s) to manage!", silent: true); + menuwapps.SetSelectionMode(Menu.SelectionMode.Single); + Menu.Selection sr = menuwapps.Select(selectedwaindexes); - const string TESTURL = "http://localhost/Log4ProIS/Import/Log/PurgeGet?numofdays=120&xml=config=ALM_Import;"; + if (sr.Result == Menu.SelectionResult.Exit) { return o; } + else if (sr.Result == Menu.SelectionResult.None) { return o; } + else if (sr.Result == Menu.SelectionResult.Error) { return o; } + else if (sr.Result == Menu.SelectionResult.Ok) { } + else { } + + WebApplication selectedwebapplication = (sr.SelectedParameterList.First()).Parameters as WebApplication; + var therearepredefinedurls = selectedwebapplication.Xml_SendUrlList != null && selectedwebapplication.Xml_SendUrlList.Any(); + string urlname; + List sendurlnamelist=null; + if (therearepredefinedurls) + { + ColorConsole.WriteLine("Enter the name of the url to send, or * to enter url manually:", ConsoleColor.Yellow); + sendurlnamelist = selectedwebapplication.Xml_SendUrlList.Select(su => su.Name).ToList(); + string sendurlnamecommalist = string.Join(",", sendurlnamelist); + ColorConsole.WriteLine("Select names from this list:", ConsoleColor.Gray, suffix: sendurlnamecommalist); + foreach (var su in selectedwebapplication.Xml_SendUrlList) + { + ColorConsole.WriteLine(su.UrlElement.GetUrlAbsolute(), ConsoleColor.Gray, prefix: $" {su.Name}: "); + } + urlname = ColorConsole.ReadLine(); + } + else { urlname = "*"; } + if (urlname.ToLower() == "ex") return o; + string urltext; + HttpTools.RequestType gp; + List uplist=null; + if (urlname.ToLower() == "*") + { + const string TESTURL = "http://localhost/Log4ProIS/Import/Log/PurgeGet?numofdays=120&xml=config=ALM_Import;"; + ColorConsole.WriteLine("Enter url to send:", ConsoleColor.Yellow); + ColorConsole.WriteLine("Sample (Enter 'Sample' to use this):", ConsoleColor.Gray, suffix: TESTURL); + urltext = ColorConsole.ReadLine(); + if (urltext.ToLower() == "ex") return o; + if (urltext.ToLower() == "sample") urltext = TESTURL; + ColorConsole.WriteLine("Enter request type (GET/POST):", ConsoleColor.Yellow); + string gpstr = ColorConsole.ReadLine(); + gp = HttpTools.RequestType.GET; + if (gpstr.ToLower() == "ex") return o; + else if (gpstr.ToUpper() == nameof(HttpTools.RequestType.GET)) { gp = HttpTools.RequestType.GET; } + else if (gpstr.ToUpper() == nameof(HttpTools.RequestType.POST)) { gp = HttpTools.RequestType.POST; } - ColorConsole.WriteLine("Enter url to send:", ConsoleColor.Yellow); - ColorConsole.WriteLine("Sample (Enter 'Sample' to use this):", ConsoleColor.Gray, suffix: TESTURL); - string url = ColorConsole.ReadLine(); - if (url.ToLower() == "ex") return o; - if (url.ToLower() == "sample") url = TESTURL; - ColorConsole.WriteLine("Enter request type (GET/POST):", ConsoleColor.Yellow); - string gpstr = ColorConsole.ReadLine(); - HttpTools.RequestType gp = HttpTools.RequestType.GET; - if (gpstr.ToLower() == "ex") return o; - else if (gpstr.ToUpper() == nameof(HttpTools.RequestType.GET)) { gp = HttpTools.RequestType.GET; } - else if (gpstr.ToUpper() == nameof(HttpTools.RequestType.POST)) { gp = HttpTools.RequestType.POST; } + } + else if (sendurlnamelist!=null && !sendurlnamelist.Contains(urlname)) { ColorConsole.WriteLine("Invalid selection!", ConsoleColor.Red); return o; } + else + { + var sendurlobject = selectedwebapplication.Xml_SendUrlList.FirstOrDefault(u => u.Name == urlname); + var urlobject = sendurlobject.UrlElement; + urltext = urlobject.GetUrl(); + uplist = urlobject.UrlParameters.Where(p => p.PassTo == Vrh.XmlProcessing.UrlElement.ParameterTypes.dict).ToList(); + gp = sendurlobject.ForcePOST || uplist.Any() ? HttpTools.RequestType.POST : HttpTools.RequestType.GET; + } - ToolsNS.HttpTools.ReturnInfoJSON returninfojson = HttpTools.GetReturninfoJSON(url,gp); - ColorConsole.WriteLine("ReturnValue:",ConsoleColor.Yellow,suffix: returninfojson.ReturnValue.ToString()); + ToolsNS.HttpTools.ReturnInfoJSON returninfojson = HttpTools.GetReturninfoJSON(urltext, gp); + ColorConsole.WriteLine("ReturnValue:", ConsoleColor.Yellow, suffix: returninfojson.ReturnValue.ToString()); ColorConsole.WriteLine("ReturnMessage:", ConsoleColor.Yellow, suffix: returninfojson.ReturnMessage); ColorConsole.WriteLine(); @@ -748,30 +790,6 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WebApplicationManagerNS } #endregion private method:SetImpersonateIdentity #region private method:SetPoolUserAccount - //public static void SetPoolUserAccountOLD(string appname, string sitename, string username, string password, ProcessModelIdentityType poolidentitytype) - //{ - // using (var sm = new ServerManager()) - // { - // //var pool = GetPool(GetPoolName(sitename, appname)); - // var pool = GetPool(sm, sitename, appname); - // if (username == nameof(ProcessModelIdentityType.ApplicationPoolIdentity) - // || username == nameof(ProcessModelIdentityType.LocalService) - // || username == nameof(ProcessModelIdentityType.LocalSystem) - // || username == nameof(ProcessModelIdentityType.NetworkService)) - // { - // pool.ProcessModel.UserName = null; - // pool.ProcessModel.Password = null; - // pool.ProcessModel.IdentityType = (ProcessModelIdentityType)Enum.Parse(typeof(ProcessModelIdentityType), username); - // } - // else // if (ProcessModelIdentityType.SpecificUser) - // { - // pool.ProcessModel.UserName = username; - // pool.ProcessModel.Password = password; - // pool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser; - // } - // CommitChanges(sm, pool.Name); - // } - //} public static void SetPoolUserAccount(string appname, string sitename, string username, string password, ProcessModelIdentityType poolidentitytype) { using (var sm = new ServerManager()) @@ -1141,6 +1159,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WebApplicationManagerNS public bool Xml_ForceRemovePool; public bool Xml_ForceRemoveSite; public List Xml_VirtualDirectoryList; + public List Xml_SendUrlList; #endregion properties from Xml definition #region properties from environment @@ -1203,10 +1222,17 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WebApplicationManagerNS var vddefinitionXmlList = GetAllXElements(webappXml, nameof(XmlStructure.WebApplication.VirtualDirectory)); if (vddefinitionXmlList != null && vddefinitionXmlList.Any()) { - foreach (var vddefinitionXml in vddefinitionXmlList) - { - Xml_VirtualDirectoryList.Add(new VirtualDirectory(vddefinitionXml)); - } + foreach (var vddefinitionXml in vddefinitionXmlList) { Xml_VirtualDirectoryList.Add(new VirtualDirectory(vddefinitionXml)); } + } + } + catch { } + Xml_SendUrlList = new List(); + try + { + var sendurldefinitionXmlList = GetAllXElements(webappXml, nameof(XmlStructure.WebApplication.SendUrl)); + if (sendurldefinitionXmlList != null && sendurldefinitionXmlList.Any()) + { + foreach (var sendurldefinitionXml in sendurldefinitionXmlList) { Xml_SendUrlList.Add(new SendUrl(sendurldefinitionXml)); } } } catch { } @@ -1230,6 +1256,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WebApplicationManagerNS Xml_ForceRemovePool = wadef.Xml_ForceRemovePool; Xml_ForceRemoveSite = wadef.Xml_ForceRemoveSite; Xml_VirtualDirectoryList = wadef.Xml_VirtualDirectoryList.Select(x => new VirtualDirectory(x)).ToList(); + Xml_SendUrlList = wadef.Xml_SendUrlList.Select(x => new SendUrl(x)).ToList(); Xml_ImpersonateIdentityConfigFile = wadef.Xml_ImpersonateIdentityConfigFile; Xml_ImpersonateIdentity = wadef.Xml_ImpersonateIdentity; @@ -1249,6 +1276,64 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WebApplicationManagerNS PhysicalPath = GetValue(nameof(XmlStructure.WebApplication.VirtualDirectory.Attributes.PhysicalPath), vdxml, ""); } } + public class SendUrl : XmlLinqBase + { + public Vrh.XmlProcessing.UrlElement UrlElement; + public bool ForcePOST = false; + public string Name; + public bool IsOK + { + get + { + if (string.IsNullOrWhiteSpace(Name)) return false; + if (!string.IsNullOrWhiteSpace(UrlElement.Url)) return true; + if (string.IsNullOrWhiteSpace(UrlElement.Area) || string.IsNullOrWhiteSpace(UrlElement.Controller) || string.IsNullOrWhiteSpace(UrlElement.Action)) return false; + if (UrlElement.UrlParameters!=null && UrlElement.UrlParameters.Any()) + { + foreach (var urlparameter in UrlElement.UrlParameters) + { + if (string.IsNullOrWhiteSpace(urlparameter.Name)) { return false; } + if (urlparameter.PassTo != Vrh.XmlProcessing.UrlElement.ParameterTypes.url && urlparameter.PassTo != Vrh.XmlProcessing.UrlElement.ParameterTypes.dict) { return false; } + } + } + return true; + } + } + public SendUrl(SendUrl sendurl) + { + Name = sendurl.Name; + ForcePOST = sendurl.ForcePOST; + UrlElement = new UrlElement(sendurl.UrlElement); + } + public SendUrl(XElement sendurlxmlelement) + { + Name = GetValue(nameof(XmlStructure.WebApplication.SendUrl.Attributes.Name), sendurlxmlelement, ""); + ForcePOST = GetValue(nameof(XmlStructure.WebApplication.SendUrl.Attributes.ForcePost), sendurlxmlelement, XmlStructure.WebApplication.SendUrl.Attributes.ForcePost.Values.DEFAULT); + UrlElement = new Vrh.XmlProcessing.UrlElement(); + UrlElement.Url = GetValue(sendurlxmlelement, ""); + UrlElement.Protocol = GetValue(nameof(XmlStructure.WebApplication.SendUrl.Attributes.protocol), sendurlxmlelement, ""); + UrlElement.HostName = GetValue(nameof(XmlStructure.WebApplication.SendUrl.Attributes.hostname), sendurlxmlelement, ""); + UrlElement.AppName = GetValue(nameof(XmlStructure.WebApplication.SendUrl.Attributes.appname), sendurlxmlelement, ""); + UrlElement.Area = GetValue(nameof(XmlStructure.WebApplication.SendUrl.Attributes.area), sendurlxmlelement, ""); + UrlElement.Controller = GetValue(nameof(XmlStructure.WebApplication.SendUrl.Attributes.controller), sendurlxmlelement, ""); + UrlElement.Action = GetValue(nameof(XmlStructure.WebApplication.SendUrl.Attributes.action), sendurlxmlelement, ""); + UrlElement.Fragment = GetValue(nameof(XmlStructure.WebApplication.SendUrl.Attributes.fragment), sendurlxmlelement, ""); + UrlElement.UrlParameters = new List(); + var urlparameterxmllist = sendurlxmlelement.Elements(nameof(XmlStructure.WebApplication.SendUrl.Parameter)); + if (urlparameterxmllist != null) + { + foreach (var urlparameterxml in urlparameterxmllist) + { + UrlElement.UrlParameters.Add(new UrlElement.UrlParameter() + { + Name = GetValue(nameof(XmlStructure.WebApplication.SendUrl.Parameter.Attributes.Name), urlparameterxml, ""), + PassTo = GetValue(nameof(XmlStructure.WebApplication.SendUrl.Parameter.Attributes.PassTo), urlparameterxml, Vrh.XmlProcessing.UrlElement.ParameterTypes.url), + Value = GetValue(urlparameterxml, ""), + }); + } + } + } + } #region XmlStructure public static class XmlStructure @@ -1285,6 +1370,29 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WebApplicationManagerNS public static class PhysicalPath {} } } + public static class SendUrl + { + public static class Attributes + { + public static class Name { } + public static class ForcePost { public static class Values { public static bool DEFAULT = false; } } + public static class protocol { } + public static class hostname { } + public static class appname { } + public static class area { } + public static class controller { } + public static class action { } + public static class fragment { } + } + public static class Parameter + { + public static class Attributes + { + public static class PassTo { } + public static class Name { } + } + } + } } } #endregion XmlStructure diff --git a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs index 0119260..44ba967 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.14.0.0")] -[assembly: AssemblyFileVersion("1.14.0.0")] +[assembly: AssemblyVersion("1.15.0.0")] +[assembly: AssemblyFileVersion("1.15.0.0")] diff --git a/Vrh.Log4Pro.MaintenanceConsole/Vrh.Log4Pro.MaintenanceConsole.csproj b/Vrh.Log4Pro.MaintenanceConsole/Vrh.Log4Pro.MaintenanceConsole.csproj index f724301..e818e3d 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Vrh.Log4Pro.MaintenanceConsole.csproj +++ b/Vrh.Log4Pro.MaintenanceConsole/Vrh.Log4Pro.MaintenanceConsole.csproj @@ -353,8 +353,8 @@ ..\packages\VRH.Web.Providers.2.0.2\lib\net452\Vrh.Web.Providers.dll - - ..\packages\Vrh.XmlProcessing.1.30.0\lib\net45\Vrh.XmlProcessing.dll + + ..\packages\Vrh.XmlProcessing.1.30.1\lib\net45\Vrh.XmlProcessing.dll diff --git a/Vrh.Log4Pro.MaintenanceConsole/packages.config b/Vrh.Log4Pro.MaintenanceConsole/packages.config index 1248d45..55224a9 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/packages.config +++ b/Vrh.Log4Pro.MaintenanceConsole/packages.config @@ -75,5 +75,5 @@ - + \ No newline at end of file -- libgit2 0.21.2