private RemoteConfigManagerDto GetAllConfigs(RemoteConfigManagerDto remoteConfigManager)
        {
            RemoteConfigManagerDto result = new RemoteConfigManagerDto();

            try
            {
                result.Operation.Result = true;

                string folder = System.Configuration.ConfigurationManager.AppSettings["publishFolder"];
                folder = Path.Combine(folder, remoteConfigManager.Operation.Condition);

                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                string[] configs = Directory.GetDirectories(folder);
                if (configs.Length == 0)
                {
                    return(result);
                }
                foreach (string config in configs)
                {
                    string sectionName = config.Substring(folder.Length + 1);
                    result.RemoteConfigSections.AddSection(sectionName, 0, 0);
                }
            }
            catch (Exception ex)
            {
                result.Operation.Result     = false;
                result.Operation.ResultInfo = ex.ToString();
            }

            return(result);
        }
        private RemoteConfigManagerDto GetApplications()
        {
            RemoteConfigManagerDto result = new RemoteConfigManagerDto();

            try
            {
                result.Operation.Result = true;

                string folder = System.Configuration.ConfigurationManager.AppSettings["publishFolder"];
                if (!Directory.Exists(folder))
                {
                    return(result);
                }

                string[] applications = Directory.GetDirectories(folder);
                if (applications.Length == 0)
                {
                    return(result);
                }
                foreach (string application in applications)
                {
                    string sectionName = application.Substring(folder.Length + 1);
                    result.RemoteConfigSections.AddSection(sectionName, 0, 0);
                }
            }
            catch (Exception ex)
            {
                result.Operation.Result     = false;
                result.Operation.ResultInfo = ex.ToString();
            }

            return(result);
        }
        private RemoteConfigManagerDto GetLastVersionBySectionName(RemoteConfigManagerDto remoteConfigManager)
        {
            RemoteConfigManagerDto rcmDto = GetAllLastVersion(remoteConfigManager);
            RemoteConfigManagerDto result = new RemoteConfigManagerDto();

            string[] tmpParam                = remoteConfigManager.Operation.Condition.Split('|');
            string[] tmpAppList              = tmpParam[0].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] tmpsectionList          = tmpParam[1].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] majorList               = tmpParam[2].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            Dictionary <string, int> appList = new Dictionary <string, int>();

            for (int index = 0; index < tmpsectionList.Length; index++)
            {
                appList.Add(
                    Path.Combine(tmpsectionList[index], tmpAppList.Length <= index ? NoAppPath : tmpAppList[index]),
                    tmpAppList.Length <= index ? 1 : Convert.ToInt32(majorList[index]));
            }

            foreach (RemoteConfigSectionParam param in rcmDto.RemoteConfigSections)
            {
                if (appList.ContainsKey(param.SectionName) && appList[param.SectionName] == param.MajorVersion)
                {
                    result.RemoteConfigSections.AddSection(param.SectionName, param.MajorVersion, param.MinorVersion,
                                                           param.DownloadUrl);
                }
            }

            return(result);
        }
        private RemoteConfigManagerDto CreateMinor(RemoteConfigManagerDto remoteConfigManager)
        {
            RemoteConfigManagerDto result = new RemoteConfigManagerDto();

            try
            {
                result.Operation.Result = true;

                string[] strs            = remoteConfigManager.Operation.Condition.Split("/\\".ToCharArray());
                string   applicationName = strs[0];
                string   sectionName     = strs[1];
                int      major           = Convert.ToInt32(strs[2]);
                int      maxMinor        = GetMaxMinVersion(applicationName, sectionName, ref major);

                SaveConfig(applicationName, sectionName, major, maxMinor + 1, remoteConfigManager.Operation.Value,
                           remoteConfigManager.Operation.OperatorId);
                result.Operation.ResultInfo = GetDownloadUrl(applicationName, sectionName, major, maxMinor + 1);
                return(result);
            }
            catch (Exception ex)
            {
                result.Operation.Result     = false;
                result.Operation.ResultInfo = ex.ToString();
            }

            return(result);
        }
        private RemoteConfigManagerDto DeleteConfig(RemoteConfigManagerDto remoteConfigManager)
        {
            RemoteConfigManagerDto result = new RemoteConfigManagerDto();

            try
            {
                result.Operation.Result = true;

                string folder = System.Configuration.ConfigurationManager.AppSettings["publishFolder"];
                folder = Path.Combine(folder, remoteConfigManager.Operation.Condition);
                if (Directory.Exists(folder))
                {
                    string backupFolder = System.Configuration.ConfigurationManager.AppSettings["backupFolder"];
                    backupFolder = Path.Combine(backupFolder, remoteConfigManager.Operation.Condition);
                    if (!Directory.Exists(backupFolder))
                    {
                        Directory.CreateDirectory(backupFolder);
                    }

                    backupFolder = Path.Combine(backupFolder, DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss"));
                    Directory.Move(folder, backupFolder);
                }
            }
            catch (Exception ex)
            {
                result.Operation.Result     = false;
                result.Operation.ResultInfo = ex.ToString();
            }

            return(result);
        }
        private RemoteConfigManagerDto GetMajors(RemoteConfigManagerDto remoteConfigManager)
        {
            RemoteConfigManagerDto result = new RemoteConfigManagerDto();

            try
            {
                result.Operation.Result = true;

                string folder = System.Configuration.ConfigurationManager.AppSettings["resourceFolder"];
                folder = Path.Combine(folder, remoteConfigManager.Operation.Condition);
                if (!Directory.Exists(folder))
                {
                    return(result);
                }
                string[] majorVersions = Directory.GetDirectories(folder);
                if (majorVersions.Length == 0)
                {
                    return(result);
                }
                foreach (string majorVersion in majorVersions)
                {
                    int major;
                    if (int.TryParse(majorVersion.Substring(folder.Length + 1), out major))
                    {
                        result.RemoteConfigSections.AddSection(remoteConfigManager.Operation.Condition, major, 0);
                    }
                }
            }
            catch (Exception ex)
            {
                result.Operation.Result     = false;
                result.Operation.ResultInfo = ex.ToString();
            }
            return(result);
        }
        private RemoteConfigManagerDto GetMinors(RemoteConfigManagerDto remoteConfigManager)
        {
            RemoteConfigManagerDto result = new RemoteConfigManagerDto();

            try
            {
                result.Operation.Result = true;

                string folder = System.Configuration.ConfigurationManager.AppSettings["publishFolder"];
                folder = Path.Combine(folder, remoteConfigManager.Operation.Condition);
                if (Directory.Exists(folder))
                {
                    string[] minorVersions = Directory.GetFiles(folder);
                    if (minorVersions.Length == 0)
                    {
                        return(result);
                    }
                    foreach (string minorVersion in minorVersions)
                    {
                        string[] strs            = remoteConfigManager.Operation.Condition.Split("/\\".ToCharArray());
                        string   applicationName = strs[0];
                        string   sectionName     = strs[1];
                        int      major           = Convert.ToInt32(strs[2]);

                        string fileName = minorVersion.Substring(folder.Length + 1);
                        strs = fileName.Split(".".ToCharArray());
                        if (int.TryParse(strs[1], out var minor))
                        {
                            string downloadUrl = GetDownloadUrl(applicationName, sectionName, major, minor);
                            result.RemoteConfigSections.AddSection(Path.Combine(applicationName, sectionName), major,
                                                                   minor, downloadUrl);
                        }
                    }

                    string logFileName = folder + @"\log.txt";
                    if (File.Exists(logFileName))
                    {
                        FileStream   fs = new FileStream(logFileName, FileMode.Open, FileAccess.Read);
                        StreamReader sr = new StreamReader(fs);
                        result.Operation.Log = Encoding.Default.GetBytes(sr.ReadToEnd());
                        sr.Close();
                        fs.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                result.Operation.Result     = false;
                result.Operation.ResultInfo = ex.ToString();
            }

            return(result);
        }
        private RemoteConfigManagerDto GetAllLastVersion()
        {
            RemoteConfigManagerDto result = new RemoteConfigManagerDto();

            try
            {
                result.Operation.Result = true;

                string folder = System.Configuration.ConfigurationManager.AppSettings["resourceFolder"];
                if (Directory.Exists(folder))
                {
                    string[] sections = Directory.GetDirectories(folder);
                    foreach (string section in sections)
                    {
                        string   sectionName  = section.Substring(folder.Length + 1);
                        string[] applications = Directory.GetDirectories(section);
                        foreach (string application in applications)
                        {
                            string   applicationName = application.Substring(section.Length + 1);
                            string[] majorVersions   = Directory.GetDirectories(application);
                            foreach (string majorVersion in majorVersions)
                            {
                                int major;
                                if (int.TryParse(majorVersion.Substring(application.Length + 1), out major))
                                {
                                    int      maxMinor      = 0;
                                    string[] minorVersions = Directory.GetFiles(majorVersion);
                                    foreach (string minorVersion in minorVersions)
                                    {
                                        string   fileName = minorVersion.Substring(majorVersion.Length + 1);
                                        string[] strs     = fileName.Split(".".ToCharArray());
                                        int      minor;
                                        if (int.TryParse(strs[strs.Length - 1], out minor))
                                        {
                                            maxMinor = Math.Max(maxMinor, minor);
                                        }
                                    }
                                    string url = GetDownloadUrl(sectionName, applicationName, major, maxMinor);
                                    result.RemoteConfigSections.AddSection(Path.Combine(sectionName, applicationName), major, maxMinor, url);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Operation.Result     = false;
                result.Operation.ResultInfo = ex.ToString();
            }
            return(result);
        }
        private RemoteConfigManagerDto Manager(RemoteConfigManagerDto remoteConfigManager)
        {
            switch (remoteConfigManager.Operation.Command)
            {
            case "getAllConfigs":
            {
                return(GetAllConfigs(remoteConfigManager));
            }

            case "getApplications":
            {
                return(GetApplications());
            }

            case "getMajors":
            {
                return(GetMajors(remoteConfigManager));
            }

            case "getMinors":
            {
                return(GetMinors(remoteConfigManager));
            }

            case "createMinor":
            {
                return(CreateMinor(remoteConfigManager));
            }

            case "deleteConfig":
            {
                return(DeleteConfig(remoteConfigManager));
            }

            case "getAllLastVersion":
            {
                return(GetAllLastVersion(remoteConfigManager));
            }

            case "getLastVersionBySectionName":
                return(GetLastVersionBySectionName(remoteConfigManager));

            default:
            {
                var result = new RemoteConfigManagerDto {
                    Operation = { Result = false }
                };
                return(result);
            }
            }
        }
        private RemoteConfigManagerDto CreateMinor(RemoteConfigManagerDto remoteConfigManager)
        {
            RemoteConfigManagerDto result = new RemoteConfigManagerDto();

            try
            {
                result.Operation.Result = true;

                string folder = System.Configuration.ConfigurationManager.AppSettings["resourceFolder"];
                folder = Path.Combine(folder, remoteConfigManager.Operation.Condition);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                string[] strs            = remoteConfigManager.Operation.Condition.Split("/\\".ToCharArray());
                string   sectionName     = strs[0];
                string   applicationName = strs[1];
                int      major           = Convert.ToInt32(strs[2]);

                string[] minorVersions = Directory.GetFiles(folder);
                if (minorVersions.Length == 0)
                {
                    SaveConfig(sectionName, applicationName, major, 1, remoteConfigManager.Operation.Value, remoteConfigManager.Operation.OperatorId);
                    result.Operation.ResultInfo = GetDownloadUrl(sectionName, applicationName, major, 1);
                    return(result);
                }

                int maxMinor = 1;
                foreach (string minorVersion in minorVersions)
                {
                    string fileName = minorVersion.Substring(folder.Length + 1);
                    strs = fileName.Split(".".ToCharArray());
                    int minor;
                    if (int.TryParse(strs[strs.Length - 1], out minor))
                    {
                        maxMinor = Math.Max(maxMinor, minor);
                    }
                }

                SaveConfig(sectionName, applicationName, major, maxMinor + 1, remoteConfigManager.Operation.Value, remoteConfigManager.Operation.OperatorId);
                result.Operation.ResultInfo = GetDownloadUrl(sectionName, applicationName, major, maxMinor + 1);
                return(result);
            }
            catch (Exception ex)
            {
                result.Operation.Result     = false;
                result.Operation.ResultInfo = ex.ToString();
            }
            return(result);
        }
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.InputStream.Length == 0)
            {
                return;
            }
            XmlSerializer          xser = new XmlSerializer(typeof(RemoteConfigManagerDto));
            RemoteConfigManagerDto remoteConfigManager = (RemoteConfigManagerDto)xser.Deserialize(context.Request.InputStream);

            RemoteConfigManagerDto result = Manager(remoteConfigManager);

            context.Response.ContentType = "text/xml";
            xser.Serialize(context.Response.OutputStream, result);
        }
Example #12
0
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            string strCurrentPath = Request.Path.ToLower();

            if (bool.Parse(System.Configuration.ConfigurationManager.AppSettings["debug"]))
            {
                var info = string.Format("Begin Request: {0}", strCurrentPath);

                try
                {
                    if (strCurrentPath.ToLower().IndexOf("configversionhandler.ashx") >= 0)
                    {
                        XmlSerializer xser = new XmlSerializer(typeof(RemoteConfigSectionCollection));
                        RemoteConfigSectionCollection rcc = (RemoteConfigSectionCollection)xser.Deserialize(Request.InputStream);
                        Request.InputStream.Seek(0, SeekOrigin.Begin);
                        info += string.Format("\r\n\tApplication:{0} Machine:{1}", rcc.Application, rcc.Machine);
                    }
                    else if ((strCurrentPath.ToLower().IndexOf("resourcemanagerhandler.ashx") >= 0) || (strCurrentPath.ToLower().IndexOf("configmanagerhandler.ashx") >= 0))
                    {
                        XmlSerializer          xser = new XmlSerializer(typeof(RemoteConfigManagerDto));
                        RemoteConfigManagerDto rcm  = (RemoteConfigManagerDto)xser.Deserialize(Request.InputStream);
                        Request.InputStream.Seek(0, SeekOrigin.Begin);
                        info += string.Format("\r\n\tCommand:{0} OperatorID:{1} Application:{2} Machine:{3}", rcm.Operation.Command, rcm.Operation.OperatorId, rcm.RemoteConfigSections.Application, rcm.RemoteConfigSections.Machine);
                    }
                }
                catch (Exception ex)
                {
                    {
                        Request.InputStream.Seek(0, SeekOrigin.Begin);
                        byte[] bytes = new byte[Request.InputStream.Length + 1];
                        Request.InputStream.Read(bytes, 0, (int)(Request.InputStream.Length));
                        string streamContent = new UTF8Encoding().GetString(bytes);
                        Request.InputStream.Seek(0, SeekOrigin.Begin);

                        LoggerWrapper.Logger.Error(streamContent, ex);
                    }
                }

                LoggerWrapper.Logger.Info(info);
            }
        }