コード例 #1
0
        public override RedirectRule Add(RedirectRule redirectRule)
        {
            redirectRule.Id = Identity.NewIdentity();
            _redirectsHashSet.Add(redirectRule);

            return(redirectRule);
        }
コード例 #2
0
        public RedirectRule GetRedirect(int id)
        {
            var rule = _repository.Get(id);

            return(rule == null ? null :
                   RedirectRule.Copy(rule));
        }
コード例 #3
0
        public void RedirectsAreCreatedCorrectly()
        {
            Assert.That(_redirectService.GetRedirectsTotalCount(), Is.EqualTo(0));
            var role = new RedirectRule {
                SourceUrl      = "sourceUrl",
                DestinationUrl = "destinationUrl",
                IsPermanent    = false
            };

            Assert.That(_redirectService.GetRedirectsTotalCount(), Is.EqualTo(0));

            PopulateTable(6);
            Assert.That(_redirectService.GetRedirectsTotalCount(), Is.EqualTo(6));

            var created = _redirectService.GetRedirects().ToArray();

            Assert.That(created.Length, Is.EqualTo(6));

            var sameObjects = true;

            for (int i = 0; i < created.Length; i++)
            {
                sameObjects &= created[i].SourceUrl == ("sourceUrl" + i.ToString()) &&
                               created[i].DestinationUrl == ("destinationUrl" + i.ToString()) &&
                               created[i].IsPermanent == (i % 2 == 0);
            }

            Assert.That(sameObjects);
        }
コード例 #4
0
        protected IRedirect ResolveRule <T>(RedirectRule rule, Func <RedirectRule, T> constructRedirect) where T : IRedirect
        {
            if (rule == null)
            {
                return(new NullRedirectRule());
            }

            if (!rule.ContentId.HasValue)
            {
                return(constructRedirect(rule));
            }

            if (!_contentLoader.TryGet <PageData>(new ContentReference(rule.ContentId.Value), out var content))
            {
                return(new NullRedirectRule());
            }

            var filter = new FilterPublished();

            if (filter.ShouldFilter(content))
            {
                return(new NullRedirectRule());
            }

            return(constructRedirect(rule));
        }
コード例 #5
0
        public RedirectBuilder WithContentRedirectRule(out RedirectRule redirectRule, int?contentReferenceId = null)
        {
            _redirectRule.ContentId = contentReferenceId ?? new Random().Next(1, 1000);

            redirectRule = _redirectRule;

            _redirect = new ExactMatchRedirect(_redirectRule);
            return(this);
        }
コード例 #6
0
        private PortListener(RedirectRule redirectRule)
        {
            TcpActor.TcpActorStoppedEvent += this.TcpActor_TcpActorStoppedEvent;

            _listenerTask  = new Task(ListenForClients);
            _redirectRules = new List <RedirectRule>();
            Add(redirectRule);
            _tcpListener = TcpListenerProvider.Create();
        }
コード例 #7
0
        public IEnumerable <RedirectRule> GetRedirects(int startIndex = 0, int pageSize = 0)
        {
            var result = _repository.Table.Skip(startIndex >= 0 ? startIndex : 0);

            if (pageSize > 0)
            {
                return(RedirectRule.Copy(result.Take(pageSize)));
            }
            return(RedirectRule.Copy(result.ToList()));
        }
コード例 #8
0
        public RedirectBuilder WithExactMatchRedirectRule(out RedirectRule redirectRule, string newPattern)
        {
            _redirectRule.RedirectRuleType = RedirectRuleType.ExactMatch;
            _redirectRule.NewPattern       = newPattern;

            redirectRule = _redirectRule;

            _redirect = new ExactMatchRedirect(_redirectRule);
            return(this);
        }
コード例 #9
0
 public RedirectRule Add(RedirectRule redirectRule)
 {
     //FixRedirect(redirectRule);
     if (GetSameSourceUrlIds(redirectRule).Any())
     {
         throw new RedirectRuleDuplicateException(T("Rules with same SourceURL are not valid."));
     }
     _repository.Create(redirectRule);
     return(redirectRule);
 }
コード例 #10
0
        public bool Remove(RedirectRule redirectRule)
        {
            if (!_redirectRules.Remove(redirectRule))
            {
                return(false);
            }

            //LogHelper.LogMessage(string.Format("Host {0} removed from listener with port {1}. Redirects to {2}:{3}.", string.IsNullOrEmpty(redirectRule.PublicRequestHost) ? "'Any'" : redirectRule.PublicRequestHost, redirectRule.PublicRequestPort, redirectRule.InternalTargetAddress, redirectRule.InternalTargetPort), Issue.IssueLevel.Information);

            return(true);
        }
コード例 #11
0
 public RedirectRule Update(RedirectRule redirectRule)
 {
     //FixRedirect(redirectRule);
     if (GetSameSourceUrlIds(redirectRule).Any(id => id != redirectRule.Id))
     {
         throw new RedirectRuleDuplicateException(T("Rules with same SourceURL are not valid."));
     }
     _repository.Update(redirectRule);
     _signals.Trigger("Laser.Orchard.Redirects.Changed");
     return(redirectRule);
 }
コード例 #12
0
        public RedirectBuilder WithWildcardRedirectRule(out RedirectRule redirectRule, string oldPattern, string newPattern)
        {
            _redirectRule.RedirectRuleType = RedirectRuleType.Wildcard;
            _redirectRule.OldPattern       = oldPattern;
            _redirectRule.NewPattern       = newPattern;

            redirectRule = _redirectRule;

            _redirect = new WildcardRedirect(_redirectRule);
            return(this);
        }
コード例 #13
0
 protected static void WriteToModel(RedirectRule redirectRule, RedirectRule redirectRuleToUpdate)
 {
     redirectRuleToUpdate.Id               = redirectRule.Id;
     redirectRuleToUpdate.OldPattern       = redirectRule.OldPattern;
     redirectRuleToUpdate.NewPattern       = redirectRule.NewPattern;
     redirectRuleToUpdate.RedirectType     = redirectRule.RedirectType;
     redirectRuleToUpdate.RedirectRuleType = redirectRule.RedirectRuleType;
     redirectRuleToUpdate.IsActive         = redirectRule.IsActive;
     redirectRuleToUpdate.Notes            = redirectRule.Notes;
     redirectRuleToUpdate.Priority         = redirectRule.Priority;
     redirectRuleToUpdate.ContentId        = redirectRule.ContentId;
 }
コード例 #14
0
        public ActionResult AddPost()
        {
            var redirect = new RedirectRule();

            //thanks to the _includeProperties, Update succeeds even if we are not adding the id from UI
            if (!TryUpdateModel(redirect, _includeProperties))
            {
                _orchardServices.TransactionManager.Cancel();
                return(View(redirect));
            }
            return(Validate(redirect, (red) => _redirectService.Add(red)));
        }
コード例 #15
0
        public override RedirectRule Update(RedirectRule redirectRule)
        {
            var redirectRuleToUpdate =
                _redirectsHashSet.FirstOrDefault(r => r.Id == redirectRule.Id);

            if (redirectRuleToUpdate == null)
            {
                throw new KeyNotFoundException("No existing redirect with this GUID");
            }

            WriteToModel(redirectRule, redirectRuleToUpdate);
            return(redirectRule);
        }
コード例 #16
0
        public void Add(RedirectRule redirectRule)
        {
            if (this._redirectRules.Count > 0)
            {
                if (PublicPortRequested != redirectRule.PublicRequestPort)
                {
                    throw new InvalidOperationException(string.Format("Cannot add redirect rule on different ports to the same listener. Current port is {0} and the requests added port {1}.", PublicPortRequested, redirectRule.PublicRequestPort));
                }
            }

            _redirectRules.Add(redirectRule);
            //LogHelper.LogMessage(string.Format("Host {0} added to listener with port {1}. Redirects to {2}:{3}.", string.IsNullOrEmpty(redirectRule.PublicRequestHost) ? "'Any'" : redirectRule.PublicRequestHost, redirectRule.PublicRequestPort, redirectRule.InternalTargetAddress, redirectRule.InternalTargetPort), Issue.IssueLevel.Information);
        }
コード例 #17
0
        public override RedirectRule Update(RedirectRule redirectRule)
        {
            var redirectRuleToUpdate = GetById(redirectRule.Id.ExternalId);

            if (redirectRuleToUpdate == null)
            {
                throw new Exception("No existing redirect with this GUID");
            }

            WriteToModel(redirectRule, redirectRuleToUpdate);

            DynamicDataStore.Save(redirectRuleToUpdate);

            return(redirectRuleToUpdate);
        }
コード例 #18
0
        private static RedirectRule DtoToModelDelegate(RedirectRuleDto source)
        {
            var destination = new RedirectRule();

            destination.Id               = source.Id;
            destination.OldPattern       = UrlPath.ExtractRelativePath(source.OldPattern);
            destination.NewPattern       = UrlPath.ExtractRelativePath(source.NewPattern);
            destination.RedirectType     = source.RedirectType;
            destination.RedirectRuleType = source.RedirectRuleType;
            destination.IsActive         = source.IsActive;
            destination.Notes            = source.Notes;
            destination.ContentId        = source.ContentId;
            destination.Priority         = (source.Priority.HasValue && source.Priority > 0) ? source.Priority.Value : Configuration.Configuration.DefaultRedirectRulePriority;

            return(destination);
        }
コード例 #19
0
        private static RedirectRule MapUrlRewriteToRedirectRule(UrlRewriteModel urlRewriteModel)
        {
            var redirectRule = new RedirectRule
            {
                OldPattern       = UrlPath.NormalizePath(urlRewriteModel.OldUrl),
                NewPattern       = urlRewriteModel.NewUrl,
                RedirectType     = MapStatusCodeToRedirectType(urlRewriteModel.RedirectStatusCode),
                CreatedOn        = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Utc),
                IsActive         = IsMigratedRedirectRuleActive(urlRewriteModel.ContentId),
                RedirectOrigin   = urlRewriteModel.ContentId != 0 ? RedirectOrigin.System : RedirectOrigin.Import,
                Notes            = "Migrated from old redirects module",
                RedirectRuleType = MapUrlRewriteTypeToRedirectRuleType(urlRewriteModel.Type)
            };

            return(redirectRule);
        }
コード例 #20
0
 public RedirectRule GetRedirect(string path)
 {
     //path = path.TrimStart('/');
     try {
         var rule = _repository.Get(x => x.SourceUrl == path);
         return(rule == null ? null :
                RedirectRule.Copy(rule));
     } catch (Exception) {
         //sqlCE doe not support using strings properly when their length is such that the column
         //in the record is of type ntext.
         var rules = _repository.Fetch(rr =>
                                       rr.SourceUrl.StartsWith(path) && rr.SourceUrl.EndsWith(path));
         var rule = rules.ToList().Where(rr => rr.SourceUrl == path).FirstOrDefault();
         return(rule == null ? null :
                RedirectRule.Copy(rule));
     }
 }
コード例 #21
0
        private ActionResult RedirectIfUrlsAreSame(RedirectRule redirect, Func <RedirectRule, RedirectRule> doOnSuccess)
        {
            if (redirect.SourceUrl == redirect.DestinationUrl)
            {
                ModelState.AddModelError("SourceUrl", T("Source url is equal to Destination url").Text);
                _orchardServices.TransactionManager.Cancel();
                return(View(redirect));
            }

            try {
                var resultRule = doOnSuccess(redirect);
            } catch (RedirectRuleDuplicateException) {
                _orchardServices.Notifier.Error(T("A rule for this Source URL already exists."));
            }


            return(RedirectToAction("Index"));
        }
コード例 #22
0
        private RedirectRule CreateRedirectRule(RedirectRuleImportRow redirectRow)
        {
            var matchToContent = redirectRow.ContentId.HasValue;

            return(matchToContent == false
                ? RedirectRule.NewFromImport(redirectRow.OldPattern, redirectRow.NewPattern,
                                             Parser.ParseRedirectType(redirectRow.RedirectType),
                                             Parser.ParseRedirectRuleType(redirectRow.RedirectRuleType),
                                             Parser.ParseBoolean(redirectRow.IsActive),
                                             redirectRow.Notes,
                                             redirectRow.Priority)
                : RedirectRule.NewFromImport(redirectRow.OldPattern, redirectRow.ContentId.Value,
                                             Parser.ParseRedirectType(redirectRow.RedirectType),
                                             Parser.ParseRedirectRuleType(redirectRow.RedirectRuleType),
                                             Parser.ParseBoolean(redirectRow.IsActive),
                                             redirectRow.Notes,
                                             redirectRow.Priority));
        }
コード例 #23
0
 private IEnumerable <int> GetSameSourceUrlIds(RedirectRule redirectRule)
 {
     try {
         return(_repository.Table
                .Where(rr => rr.SourceUrl == redirectRule.SourceUrl)
                .ToList() //need to force execution of the query, so that it can fail in sqlCE
                .Select(rr => rr.Id));
     } catch (Exception) {
         //sqlCE doe not support using strings properly when their length is such that the column
         //in the record is of type ntext.
         var rules = _repository.Fetch(rr =>
                                       rr.SourceUrl.StartsWith(redirectRule.SourceUrl) &&
                                       rr.SourceUrl.EndsWith(redirectRule.SourceUrl));
         return(rules.ToList()
                .Where(rr => rr.SourceUrl == redirectRule.SourceUrl)
                .Select(rr => rr.Id));
     }
 }
コード例 #24
0
        private static RedirectRuleDto ModelToDtoDelegate(RedirectRule source)
        {
            var destination = new RedirectRuleDto();

            destination.Id               = source.Id.ExternalId;
            destination.OldPattern       = source.OldPattern;
            destination.NewPattern       = source.NewPattern;
            destination.ContentId        = source.ContentId;
            destination.RedirectType     = source.RedirectType;
            destination.RedirectRuleType = source.RedirectRuleType;
            destination.RedirectOrigin   = source.RedirectOrigin;
            destination.IsActive         = source.IsActive;
            destination.Notes            = source.Notes;
            destination.CreatedOn        = DateTime.SpecifyKind(source.CreatedOn, DateTimeKind.Utc);
            destination.CreatedBy        = source.CreatedBy;
            destination.Priority         = source.Priority;

            return(destination);
        }
コード例 #25
0
        public _301Redirection()
        {
            string cacheName = "TTOL_Cache_OldUrlRedirectionRules";

            RedirectRules = HttpContext.Current.Cache[cacheName] as List <RedirectRule>;
            if (null == RedirectRules)
            {
                RedirectRules = new List <RedirectRule>();

                try
                {
                    string      configFilePath = HttpContext.Current.Server.MapPath("/Config/OldRewriteRules.config");
                    XmlDocument xmlDoc         = new XmlDocument();
                    xmlDoc.Load(configFilePath);

                    XmlNodeList nlstRules = xmlDoc.DocumentElement.SelectNodes("//rules/rule");

                    for (int i = 0; i < nlstRules.Count; i++)
                    {
                        RedirectRule rule = new RedirectRule();
                        rule.Url        = nlstRules[i].SelectSingleNode("url").InnerText;
                        rule.Parameters = nlstRules[i].SelectSingleNode("params").InnerText;
                        rule.Method     = nlstRules[i].SelectSingleNode("method").InnerText;

                        RedirectRules.Add(rule);
                    }

                    XmlNode nodeFileSettingCacheExpire = xmlDoc.DocumentElement.SelectSingleNode("//Configuration/RedirectRulesFile");
                    long    fileSettingCacheExpire     = Lib.Object2Long(nodeFileSettingCacheExpire.Attributes["cacheExpire"].Value);
                    if (fileSettingCacheExpire <= 0)
                    {
                        fileSettingCacheExpire = 3600;// default 1h
                    }

                    CacheDependency fileDependency = new CacheDependency(configFilePath);
                    HttpContext.Current.Cache.Insert(cacheName, RedirectRules, fileDependency, DateTime.Now.AddSeconds(fileSettingCacheExpire), TimeSpan.Zero, CacheItemPriority.Normal, null);
                }
                catch (Exception ex)
                {
                }
            }
        }
コード例 #26
0
        private ActionResult Validate(RedirectRule redirect, Func <RedirectRule, RedirectRule> doOnSuccess)
        {
            if (redirect.SourceUrl != "*/" && redirect.DestinationUrl != "*")
            {
                redirect.SourceUrl      = redirect.SourceUrl.TrimEnd('/');
                redirect.DestinationUrl = redirect.DestinationUrl.TrimEnd('/');
            }
            if (redirect.SourceUrl == redirect.DestinationUrl)
            {
                ModelState.AddModelError("SourceUrl", T("Source url is equal to Destination url").Text);
                _orchardServices.TransactionManager.Cancel();
                return(View(redirect));
            }

            try {
                var resultRule = doOnSuccess(redirect);
            }
            catch (RedirectRuleDuplicateException) {
                _orchardServices.Notifier.Error(T("A rule for this Source URL already exists."));
            }
            return(RedirectToAction("Index", new { page = Request["page"] }));
        }
コード例 #27
0
        public bool Redirect(string currentUrl)
        {
            bool mustRedirect = false;

            Regex rex;

            for (int i = 0; i < RedirectRules.Count; i++)
            {
                RedirectRule rule = RedirectRules[i];
                rex = new Regex(rule.Url, RegexOptions.IgnoreCase);
                Match match = rex.Match(currentUrl);

                if (match.Success)
                {
                    string parameters = rex.Replace(currentUrl, rule.Parameters);
                    mustRedirect = (bool)this.GetType().InvokeMember(rule.Method, System.Reflection.BindingFlags.InvokeMethod, null, this, new object[] { parameters.Split(',') });
                    break;
                }
            }

            return(mustRedirect);
        }
コード例 #28
0
        private static void AddRedirects(PageData pageData, string oldUrl, SystemRedirectReason systemRedirectReason)
        {
            if (!(pageData.Status == VersionStatus.PreviouslyPublished || pageData.Status == VersionStatus.Published))
            {
                return;
            }

            var redirectRule = RedirectRule.NewFromSystem(
                UrlPath.NormalizePath(oldUrl),
                pageData.ContentLink.ID,
                RedirectType.Permanent,
                RedirectRuleType.ExactMatch,
                SystemRedirectsHelper.GetSystemRedirectReason(systemRedirectReason));

            var redirectRuleRepository = ServiceLocator.Current.GetInstance <IRedirectRuleRepository>();

            try
            {
                redirectRuleRepository.Add(redirectRule);
            }
            catch (ApplicationException) { }
        }
コード例 #29
0
        public ActionResult Add(FormCollection formCollection)
        {
            var redirect = new RedirectRule();

            if (!TryUpdateModel(redirect, _includeProperties))
            {
                _orchardServices.TransactionManager.Cancel();
                return(View(redirect));
            }

            if (redirect.SourceUrl == redirect.DestinationUrl)
            {
                ModelState.AddModelError("SourceUrl", "Source url is equal to Destination url");
                _orchardServices.TransactionManager.Cancel();
                return(View(redirect));
            }

            _routingAppService.Add(redirect);

            _orchardServices.Notifier.Add(NotifyType.Information, T("Redirect record was added"));

            return(RedirectToAction("List"));
        }
コード例 #30
0
ファイル: RoutingAppService.cs プロジェクト: YSRE/SuperRocket
 public void Delete(RedirectRule redirectRule)
 {
     _repository.Delete(redirectRule);
 }
コード例 #31
0
ファイル: PageRedirection.cs プロジェクト: bneuhold/pb-dev
    System.Collections.Generic.List<RedirectRule> GetRules()
    {
        System.Collections.Generic.List<RedirectRule> col = new List<RedirectRule>();
        System.Xml.XmlNode _oRules = oDoc.SelectSingleNode("//urlrewrites");

        foreach (System.Xml.XmlNode oNode in _oRules.SelectNodes("rule"))
        {
            //url/text()
            if ((oNode.SelectSingleNode("url/text()") != null) && (oNode.SelectSingleNode("rewrite/text()") != null))
            {
                RedirectRule oR = new RedirectRule();
                oR.Name = oNode.Attributes["name"].Value;
                oR.URL = oNode.SelectSingleNode("url/text()").Value;
                oR.Rewrite = oNode.SelectSingleNode("rewrite/text()").Value;
                col.Add(oR);
            }
        }
        return col;
    }