コード例 #1
0
        public static UrlRuleConfiguration GenerateConfig(int portalId)
        {
            //string cacheKey = "UrlRuleConfig" + portalId;


            var config = new UrlRuleConfiguration();

            config.Rules = new List <UrlRule>();

            var UltimaUrlRule     = new UrlRule();
            var UltimaUrlRuleInfo = new UrlRuleInfo();
            var routine           = new int();
            var conta             = new int();

            try
            {
                // 1 cache by portal
                //Logger.Trace("Get cache " + portalId );
                //config = (UrlRuleConfiguration)DataCache.GetCache(cacheKey);

                //if ((config == null))
                {
                    Stopwatch timer = new Stopwatch();
                    timer.Start();
                    Logger.Info("Rebuild cache start " + portalId + " at " + DateTime.Now);

                    //config = new UrlRuleConfiguration { Rules = new List<UrlRule>() };

                    // generate admin page
                    GenerateAdminTab(portalId);
                    //if (false)
                    {
                        var builder = new UrlBuilder();
                        var Rules   = builder.BuildUrlMap(portalId);
                        config.Rules.AddRange(Rules);



                        var storedRules = UrlRuleController.GetUrlRules(portalId);
                        foreach (UrlRuleInfo storedRule in storedRules)
                        {
                            routine           = 1;
                            UltimaUrlRuleInfo = storedRule;

                            if (storedRule.CultureCode == "")
                            {
                                storedRule.CultureCode = null;
                            }
                            if (storedRule.RedirectDestination == "")
                            {
                                storedRule.RedirectDestination = null;
                            }
                            if (storedRule.RedirectDestination != null)
                            {
                                storedRule.RedirectDestination = storedRule.RedirectDestination.Trim();
                            }
                            if (storedRule.Url != null)
                            {
                                storedRule.Url = storedRule.Url.Trim();
                            }
                        }

                        // add custom rules to cache
                        foreach (UrlRuleInfo storedRule in storedRules.Where(r => r.RuleType == (int)UrlRuleType.Custom /* && r.RuleAction == (int)UrlRuleAction.Redirect */)) // custom rule
                        {
                            routine           = 2;
                            UltimaUrlRuleInfo = storedRule;

                            UrlRule rule = new UrlRule()
                            {
                                RuleType            = (UrlRuleType)storedRule.RuleType,
                                CultureCode         = storedRule.CultureCode,
                                TabId               = storedRule.TabId,
                                Parameters          = storedRule.Parameters,
                                Action              = (UrlRuleAction)storedRule.RuleAction,
                                RemoveTab           = storedRule.RemoveTab,
                                RedirectDestination = storedRule.RedirectDestination,
                                RedirectStatus      = storedRule.RedirectStatus,
                                Url = storedRule.Url
                            };
                            config.Rules.Add(rule);
                        }

                        // add rule for sitemap.xml rewrite to sitemap.aspx

                        /* replaced by a handler
                         * UrlRule sitemapRule = new UrlRule()
                         * {
                         *  RuleType = UrlRuleType.Custom,
                         *  Action = UrlRuleAction.Rewrite,
                         *  RedirectDestination = "~/Sitemap.aspx",
                         *  Url = ".*sitemap.xml"
                         * };
                         * config.Rules.Add(sitemapRule);
                         */
                        foreach (UrlRule rule in config.Rules)
                        {
                            routine       = 3;
                            UltimaUrlRule = rule;

                            if (rule.CultureCode == "")
                            {
                                rule.CultureCode = null;
                            }
                            if (rule.RedirectDestination == "")
                            {
                                rule.RedirectDestination = null;
                            }
                            rule.Url = rule.Url.ToLower();
                            // set RedirectDestination for automatic redirections corresponding to rewrite rule
                            if (rule.RuleType == UrlRuleType.Module && rule.Action == UrlRuleAction.Rewrite && string.IsNullOrEmpty(rule.RedirectDestination))
                            {
                                rule.RedirectDestination = rule.Parameters.Replace('=', '/').Replace('&', '/');
                            }
                            else if (rule.RuleType == UrlRuleType.Module && rule.Action == UrlRuleAction.Redirect && string.IsNullOrEmpty(rule.RedirectDestination))
                            {
                                var rewriteRule = config.Rules.FirstOrDefault(r => r.RuleType == UrlRuleType.Module && r.Parameters == rule.Parameters && r.Action == UrlRuleAction.Rewrite);
                                if (rewriteRule != null)
                                {
                                    rule.RedirectDestination = rewriteRule.Url;
                                }
                            }
                            if (rule.RedirectDestination != null)
                            {
                                rule.RedirectDestination = rule.RedirectDestination.ToLower();
                            }
                        }

                        var OldRules = new List <UrlRuleInfo>(storedRules);

                        var DistuctRules = config.Rules.Where(r => r.RuleType != UrlRuleType.Custom)
                                           .GroupBy(r => new { r.RuleType, r.CultureCode, r.TabId, r.Url })
                                           .Select(g => g.First());


                        conta = 0;
                        foreach (UrlRule rule in DistuctRules)
                        {
                            routine       = 4;
                            UltimaUrlRule = rule;
                            conta         = conta + 1;



                            var ruleInfoLst = storedRules.Where(r => r.RuleType == (int)rule.RuleType &&
                                                                r.CultureCode == rule.CultureCode &&
                                                                r.TabId == rule.TabId &&
                                                                r.Url == rule.Url);

                            UrlRuleInfo ruleInfo = ruleInfoLst.FirstOrDefault();
                            if (ruleInfo == null)
                            {
                                if (!string.IsNullOrEmpty(rule.Url) && rule.Url.Length < 500)
                                {
                                    ruleInfo = new UrlRuleInfo()
                                    {
                                        PortalId    = portalId,
                                        DateTime    = DateTime.Now,
                                        RuleType    = (int)rule.RuleType,
                                        CultureCode = rule.CultureCode,
                                        TabId       = rule.TabId,
                                        Url         = rule.Url,

                                        Parameters          = rule.Parameters,
                                        RuleAction          = (int)rule.Action,
                                        RemoveTab           = rule.RemoveTab,
                                        RedirectDestination = rule.RedirectDestination,
                                        RedirectStatus      = rule.RedirectStatus
                                    };
                                    ruleInfo.UrlRuleId = UrlRuleController.AddUrlRule(ruleInfo);
                                    Logger.Info("AddUrlRule (UrlRuleId=" + ruleInfo.UrlRuleId + ")");
                                    storedRules.Add(ruleInfo);
                                }
                            }
                            else
                            {
                                bool FirstOne = true; // for auto correction of double stored rules
                                foreach (var r in ruleInfoLst)
                                {
                                    OldRules.Remove(r);
                                    if (FirstOne)
                                    {
                                        if (ruleInfo.Parameters != rule.Parameters ||
                                            ruleInfo.RuleAction != (int)rule.Action ||
                                            ruleInfo.RemoveTab != rule.RemoveTab ||
                                            ruleInfo.RedirectDestination != rule.RedirectDestination ||
                                            ruleInfo.RedirectStatus != rule.RedirectStatus)
                                        {
                                            Logger.Info("UpdateUrlRule (UrlRuleId=" + ruleInfo.UrlRuleId +

                                                        (ruleInfo.Parameters == rule.Parameters) + "/" +
                                                        (ruleInfo.RuleAction == (int)rule.Action) + "/" +
                                                        (ruleInfo.RemoveTab == rule.RemoveTab) + "/" +
                                                        (ruleInfo.RedirectDestination == rule.RedirectDestination) + "/" +
                                                        (ruleInfo.RedirectStatus == rule.RedirectStatus) + "-" +

                                                        ruleInfo.Parameters + "/" + rule.Parameters + "/" +
                                                        ruleInfo.RuleAction + "/" + (int)rule.Action + "/" +
                                                        ruleInfo.RemoveTab + "/" + rule.RemoveTab + "/" +
                                                        ruleInfo.RedirectDestination + "/" + rule.RedirectDestination + "/" +
                                                        ruleInfo.RedirectStatus + "/" + rule.RedirectStatus +

                                                        ")");

                                            ruleInfo.Parameters          = rule.Parameters;
                                            ruleInfo.RuleAction          = (int)rule.Action;
                                            ruleInfo.RemoveTab           = rule.RemoveTab;
                                            ruleInfo.RedirectDestination = rule.RedirectDestination;
                                            ruleInfo.RedirectStatus      = rule.RedirectStatus;
                                            UrlRuleController.UpdateUrlRule(ruleInfo);
                                        }
                                    }
                                    else
                                    {
                                        UrlRuleController.DeleteUrlRule(ruleInfo.UrlRuleId);
                                        Logger.Info("DeleteUrlRule (UrlRuleId=" + ruleInfo.UrlRuleId + ")");
                                    }

                                    FirstOne = false;
                                }
                            }
                        }

                        foreach (var storedRule in OldRules.Where(r => r.RuleType == (int)UrlRuleType.Tab ||
                                                                  r.RuleType == (int)UrlRuleType.Module))
                        {
                            routine           = 5;
                            UltimaUrlRuleInfo = storedRule;

                            if (storedRule.RuleAction == (int)UrlRuleAction.Rewrite)
                            {
                                var actualRule = Rules.FirstOrDefault(r => r.RuleType == (UrlRuleType)storedRule.RuleType &&
                                                                      r.CultureCode == storedRule.CultureCode &&
                                                                      r.TabId == storedRule.TabId &&
                                                                      r.Parameters == storedRule.Parameters);

                                if (actualRule != null)
                                {
                                    UrlRule rule = new UrlRule()
                                    {
                                        RuleType            = (UrlRuleType)storedRule.RuleType,
                                        CultureCode         = storedRule.CultureCode,
                                        TabId               = storedRule.TabId,
                                        Parameters          = storedRule.Parameters,
                                        Action              = UrlRuleAction.Redirect,
                                        RemoveTab           = storedRule.RemoveTab,
                                        RedirectDestination = actualRule.Url,
                                        RedirectStatus      = storedRule.RedirectStatus,
                                        Url = storedRule.Url
                                    };
                                    config.Rules.Add(rule);
                                }
                            }
                        }
                    }
                    //Logger.MethodExit();
                    //DataCache.SetCache("UrlRuleConfig", config, TimeSpan.FromDays(1));
                    //int intCacheTimeout = 20 * Convert.ToInt32(DotNetNuke.Entities.Host.Host.PerformanceSetting);

                    timer.Stop();
                    double responseTime = timer.ElapsedMilliseconds / 1000.0;



                    Logger.Info("Rebuild cache end " + portalId + " (" + responseTime + ")" + " at " + DateTime.Now);
                    //DataCache.SetCache(cacheKey, config, TimeSpan.FromMinutes(intCacheTimeout));

                    //var onRemove = new CacheItemRemovedCallback(config.RemovedCallBack);

                    //DateTime absoluteExpiration = DateTime.UtcNow.Add(TimeSpan.FromMinutes(intCacheTimeout));

                    //DataCache.SetCache(cacheKey, config, null, absoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, onRemove, false);
                }
            }
            catch (Exception ex)
            {
                //log it
                var objEventLog     = new EventLogController();
                var objEventLogInfo = new LogInfo()
                {
                    LogTypeKey = "GENERAL_EXCEPTION"
                };
                objEventLogInfo.AddProperty("UrlRewriter.UrlRuleConfig", "GetConfig Failed");
                objEventLogInfo.AddProperty("UrlRewriter.RedirectDestination", UltimaUrlRule.RedirectDestination);
                objEventLogInfo.AddProperty("UrlRewriter.UrlRule", UltimaUrlRule.Url);
                objEventLogInfo.AddProperty("UrlRewriter.UrlRuleConfig", "GetConfig Failed");
                objEventLogInfo.AddProperty("ExceptionMessage", ex.Message);
                objEventLogInfo.AddProperty("PortalId", portalId.ToString());
                //objEventLogInfo.LogTypeKey = EventLogController.EventLogType.HOST_ALERT.ToString();


                objEventLogInfo.AddProperty("Exception Type", ex.GetType().ToString());
                objEventLogInfo.AddProperty("Message", ex.Message);
                objEventLogInfo.AddProperty("Stack Trace", ex.StackTrace);
                if (ex.InnerException != null)
                {
                    objEventLogInfo.AddProperty("Inner Exception Message", ex.InnerException.Message);
                    objEventLogInfo.AddProperty("Inner Exception Stacktrace", ex.InnerException.StackTrace);
                }
                objEventLogInfo.BypassBuffering = true;

                objEventLog.AddLog(objEventLogInfo);
                Logger.Error(ex);
                //DataCache.SetCache(cacheKey, config, TimeSpan.FromMinutes(60));
            }


            return(config);
        }
コード例 #2
0
        public static UrlRuleConfiguration GenerateConfig(int portalId)
        {
            //string cacheKey = "UrlRuleConfig" + portalId;

            var config = new UrlRuleConfiguration();
            config.Rules = new List<UrlRule>();

            try
            {
                // 1 cache by portal
                //DnnLog.Trace("Get cache " + portalId );
                //config = (UrlRuleConfiguration)DataCache.GetCache(cacheKey);

                //if ((config == null))
                {
                    Stopwatch timer = new Stopwatch();
                    timer.Start();
                    Logger.Info("Rebuild cache start " + portalId);

                    //config = new UrlRuleConfiguration { Rules = new List<UrlRule>() };

                    // generate admin page
                    GenerateAdminTab(portalId);
                    //if (false)
                    {

                        var builder = new UrlBuilder();
                        var Rules = builder.BuildUrlMap(portalId);
                        config.Rules.AddRange(Rules);

                        var storedRules = UrlRuleController.GetUrlRules(portalId);
                        foreach (UrlRuleInfo storedRule in storedRules)
                        {
                            if (storedRule.CultureCode == "") storedRule.CultureCode = null;
                            if (storedRule.RedirectDestination == "") storedRule.RedirectDestination = null;
                            if (storedRule.RedirectDestination != null) storedRule.RedirectDestination = storedRule.RedirectDestination.Trim();
                            if (storedRule.Url != null) storedRule.Url = storedRule.Url.Trim();
                        }

                        // add custom rules to cache
                        foreach (UrlRuleInfo storedRule in storedRules.Where(r => r.RuleType == (int)UrlRuleType.Custom /* && r.RuleAction == (int)UrlRuleAction.Redirect */)) // custom rule
                        {
                            UrlRule rule = new UrlRule()
                            {
                                RuleType = (UrlRuleType)storedRule.RuleType,
                                CultureCode = storedRule.CultureCode,
                                TabId = storedRule.TabId,
                                Parameters = storedRule.Parameters,
                                Action = (UrlRuleAction)storedRule.RuleAction,
                                RemoveTab = storedRule.RemoveTab,
                                RedirectDestination = storedRule.RedirectDestination,
                                RedirectStatus = storedRule.RedirectStatus,
                                Url = storedRule.Url
                            };
                            config.Rules.Add(rule);
                        }

                        // add rule for sitemap.xml rewrite to sitemap.aspx
                        /* replaced by a handler
                        UrlRule sitemapRule = new UrlRule()
                        {
                            RuleType = UrlRuleType.Custom,
                            Action = UrlRuleAction.Rewrite,
                            RedirectDestination = "~/Sitemap.aspx",
                            Url = ".*sitemap.xml"
                        };
                        config.Rules.Add(sitemapRule);
                        */
                        foreach (UrlRule rule in config.Rules)
                        {
                            if (rule.CultureCode == "") rule.CultureCode = null;
                            if (rule.RedirectDestination == "") rule.RedirectDestination = null;
                            rule.Url = rule.Url.ToLower();
                            // set RedirectDestination for automatic redirections corresponding to rewrite rule
                            if (rule.RuleType == UrlRuleType.Module && rule.Action == UrlRuleAction.Rewrite && string.IsNullOrEmpty(rule.RedirectDestination))
                            {
                                rule.RedirectDestination = rule.Parameters.Replace('=', '/').Replace('&', '/');
                            }
                            else if (rule.RuleType == UrlRuleType.Module && rule.Action == UrlRuleAction.Redirect && string.IsNullOrEmpty(rule.RedirectDestination))
                            {
                                var rewriteRule = config.Rules.FirstOrDefault(r => r.RuleType == UrlRuleType.Module && r.Parameters == rule.Parameters && r.Action == UrlRuleAction.Rewrite);
                                if (rewriteRule != null)
                                {
                                    rule.RedirectDestination = rewriteRule.Url;
                                }
                            }
                            if (rule.RedirectDestination != null)
                            {
                                rule.RedirectDestination = rule.RedirectDestination.ToLower();
                            }
                        }

                        var OldRules = new List<UrlRuleInfo>(storedRules);

                        var DistuctRules = config.Rules.Where(r => r.RuleType != UrlRuleType.Custom)
                                                        .GroupBy(r => new { r.RuleType, r.CultureCode, r.TabId, r.Url })
                                                        .Select(g => g.First());

                        foreach (UrlRule rule in DistuctRules)
                        {
                            var ruleInfoLst = storedRules.Where(r => r.RuleType == (int)rule.RuleType &&
                                                                                r.CultureCode == rule.CultureCode &&
                                                                                r.TabId == rule.TabId &&
                                                                                r.Url == rule.Url);

                            UrlRuleInfo ruleInfo = ruleInfoLst.FirstOrDefault();
                            if (ruleInfo == null)
                            {
                                ruleInfo = new UrlRuleInfo()
                                {
                                    PortalId = portalId,
                                    DateTime = DateTime.Now,
                                    RuleType = (int)rule.RuleType,
                                    CultureCode = rule.CultureCode,
                                    TabId = rule.TabId,
                                    Url = rule.Url,

                                    Parameters = rule.Parameters,
                                    RuleAction = (int)rule.Action,
                                    RemoveTab = rule.RemoveTab,
                                    RedirectDestination = rule.RedirectDestination,
                                    RedirectStatus = rule.RedirectStatus
                                };
                                ruleInfo.UrlRuleId = UrlRuleController.AddUrlRule(ruleInfo);
                                Logger.Info("AddUrlRule (UrlRuleId=" + ruleInfo.UrlRuleId + ")");
                                storedRules.Add(ruleInfo);
                            }
                            else
                            {
                                bool FirstOne = true; // for auto correction of double stored rules
                                foreach (var r in ruleInfoLst)
                                {
                                    OldRules.Remove(r);
                                    if (FirstOne)
                                    {
                                        if (ruleInfo.Parameters != rule.Parameters ||
                                            ruleInfo.RuleAction != (int)rule.Action ||
                                            ruleInfo.RemoveTab != rule.RemoveTab ||
                                            ruleInfo.RedirectDestination != rule.RedirectDestination ||
                                            ruleInfo.RedirectStatus != rule.RedirectStatus)
                                        {
                                            Logger.Info("UpdateUrlRule (UrlRuleId=" + ruleInfo.UrlRuleId +

                                                (ruleInfo.Parameters == rule.Parameters) + "/" +
                                                (ruleInfo.RuleAction == (int)rule.Action) + "/" +
                                                (ruleInfo.RemoveTab == rule.RemoveTab) + "/" +
                                                (ruleInfo.RedirectDestination == rule.RedirectDestination) + "/" +
                                                (ruleInfo.RedirectStatus == rule.RedirectStatus) + "-" +

                                                ruleInfo.Parameters + "/" + rule.Parameters + "/" +
                                                ruleInfo.RuleAction + "/" + (int)rule.Action + "/" +
                                                ruleInfo.RemoveTab + "/" + rule.RemoveTab + "/" +
                                                ruleInfo.RedirectDestination + "/" + rule.RedirectDestination + "/" +
                                                ruleInfo.RedirectStatus + "/" + rule.RedirectStatus +

                                                ")");

                                            ruleInfo.Parameters = rule.Parameters;
                                            ruleInfo.RuleAction = (int)rule.Action;
                                            ruleInfo.RemoveTab = rule.RemoveTab;
                                            ruleInfo.RedirectDestination = rule.RedirectDestination;
                                            ruleInfo.RedirectStatus = rule.RedirectStatus;
                                            UrlRuleController.UpdateUrlRule(ruleInfo);

                                        }
                                    }
                                    else
                                    {
                                        UrlRuleController.DeleteUrlRule(ruleInfo.UrlRuleId);
                                        Logger.Info("DeleteUrlRule (UrlRuleId=" + ruleInfo.UrlRuleId + ")");
                                    }

                                    FirstOne = false;
                                }
                            }
                        }

                        foreach (var storedRule in OldRules.Where(r => r.RuleType == (int)UrlRuleType.Tab ||
                                                                        r.RuleType == (int)UrlRuleType.Module))
                        {
                            if (storedRule.RuleAction == (int)UrlRuleAction.Rewrite)
                            {
                                var actualRule = Rules.FirstOrDefault(r => r.RuleType == (UrlRuleType)storedRule.RuleType &&
                                                                            r.CultureCode == storedRule.CultureCode &&
                                                                            r.TabId == storedRule.TabId &&
                                                                            r.Parameters == storedRule.Parameters);

                                if (actualRule != null)
                                {
                                    UrlRule rule = new UrlRule()
                                    {
                                        RuleType = (UrlRuleType)storedRule.RuleType,
                                        CultureCode = storedRule.CultureCode,
                                        TabId = storedRule.TabId,
                                        Parameters = storedRule.Parameters,
                                        Action = UrlRuleAction.Redirect,
                                        RemoveTab = storedRule.RemoveTab,
                                        RedirectDestination = actualRule.Url,
                                        RedirectStatus = storedRule.RedirectStatus,
                                        Url = storedRule.Url
                                    };
                                    config.Rules.Add(rule);
                                }
                            }
                        }
                    }
                    //DnnLog.MethodExit();
                    //DataCache.SetCache("UrlRuleConfig", config, TimeSpan.FromDays(1));
                    //int intCacheTimeout = 20 * Convert.ToInt32(DotNetNuke.Entities.Host.Host.PerformanceSetting);

                    timer.Stop();
                    double responseTime = timer.ElapsedMilliseconds / 1000.0;

                    Logger.Info("Rebuild cache " + portalId + " (" + responseTime + ")");
                    //DataCache.SetCache(cacheKey, config, TimeSpan.FromMinutes(intCacheTimeout));

                    //var onRemove = new CacheItemRemovedCallback(config.RemovedCallBack);

                    //DateTime absoluteExpiration = DateTime.UtcNow.Add(TimeSpan.FromMinutes(intCacheTimeout));

                    //DataCache.SetCache(cacheKey, config, null, absoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, onRemove, false);

                }
            }
            catch (Exception ex)
            {
                //log it
                var objEventLog = new EventLogController();
                var objEventLogInfo = new LogInfo() { LogTypeKey = "GENERAL_EXCEPTION" };
                objEventLogInfo.AddProperty("UrlRewriter.UrlRuleConfig", "GetConfig Failed");
                objEventLogInfo.AddProperty("ExceptionMessage", ex.Message);
                objEventLogInfo.AddProperty("PortalId", portalId.ToString());
                //objEventLogInfo.LogTypeKey = EventLogController.EventLogType.HOST_ALERT.ToString();

                objEventLogInfo.AddProperty("Exception Type", ex.GetType().ToString());
                objEventLogInfo.AddProperty("Message", ex.Message);
                objEventLogInfo.AddProperty("Stack Trace", ex.StackTrace);
                if (ex.InnerException != null)
                {
                    objEventLogInfo.AddProperty("Inner Exception Message", ex.InnerException.Message);
                    objEventLogInfo.AddProperty("Inner Exception Stacktrace", ex.InnerException.StackTrace);
                }
                objEventLogInfo.BypassBuffering = true;

                objEventLog.AddLog(objEventLogInfo);
                Logger.Error(ex);
                //DataCache.SetCache(cacheKey, config, TimeSpan.FromMinutes(60));
            }

            return config;
        }