示例#1
0
 public CustomRedirect(CustomRedirect redirect)
 {
     _oldUrl            = redirect._oldUrl;
     _newUrl            = redirect._newUrl;
     WildCardSkipAppend = redirect.WildCardSkipAppend;
     SiteId             = redirect.SiteId;
 }
示例#2
0
        // TODO: If desired, change parameters to Find method to search based on a property of CustomRedirect.
        public CustomRedirect Find(Uri urlNotFound)
        {
            // Handle absolute addresses first
            string url    = urlNotFound.AbsoluteUri;
            int    siteId = DataHandler.GetSiteIdFromUrl(urlNotFound.Host);

            if (siteId == -1)
            {
                return(null);
            }

            //TODO: Since we shouldn't use the host in our redirects, we should be able to remove this step
            CustomRedirect foundRedirect = FindInternal(url, siteId);

            // Common case
            if (foundRedirect == null)
            {
                url           = urlNotFound.PathAndQuery;
                foundRedirect = FindInternal(url, siteId);
            }

            // Handle legacy databases with encoded values
            if (foundRedirect == null)
            {
                url           = HttpUtility.HtmlEncode(url);
                foundRedirect = FindInternal(url, siteId);
            }

            return(foundRedirect);
        }
示例#3
0
 public int IndexOf(CustomRedirect customRedirect)
 {
     for (int i = 0; i < List.Count; i++)
     {
         if (this[i] == customRedirect)    // Found it
         {
             return(i);
         }
     }
     return(-1);
 }
示例#4
0
        /// <summary>
        /// Parses the xml file and reads all redirects.
        /// </summary>
        /// <returns>A collection of CustomRedirect objects</returns>
        public CustomRedirectCollection Load(int siteId)
        {
            // ReSharper disable InconsistentNaming
            const string URLPATH      = "/redirects/urls/url";
            const string NEWURL       = "new";
            const string OLDURL       = "old";
            const string SKIPWILDCARD = "onWildCardMatchSkipAppend";
            // ReSharper restore InconsistentNaming

            CustomRedirectCollection redirects = new CustomRedirectCollection();

            // Parse all url nodes
            XmlNodeList nodes = _customRedirectsXmlFile.SelectNodes(URLPATH);

            if (nodes != null)
            {
                foreach (XmlNode node in nodes)
                {
                    // Each url new url can have several old values
                    // we need to create a redirect object for each pair
                    XmlNode newNode = node.SelectSingleNode(NEWURL);

                    XmlNodeList oldNodes = node.SelectNodes(OLDURL);
                    if (oldNodes != null)
                    {
                        foreach (XmlNode oldNode in oldNodes)
                        {
                            bool skipWildCardAppend = false;
                            if (oldNode.Attributes != null)
                            {
                                XmlAttribute skipWildCardAttr = oldNode.Attributes[SKIPWILDCARD];
                                if (skipWildCardAttr != null)
                                {
                                    // If value parsing fails, it will be false by default. We do
                                    // not really care to check if it fails, as we cannot do anything
                                    // about it (throwing an exception is not a good idea here)
                                    bool.TryParse(skipWildCardAttr.Value, out skipWildCardAppend);
                                }
                            }

                            // Create new custom redirect nodes
                            if (newNode != null)
                            {
                                CustomRedirect redirect = new CustomRedirect(oldNode.InnerText, newNode.InnerText, skipWildCardAppend, siteId);
                                redirects.Add(redirect);
                            }
                        }
                    }
                }
            }

            return(redirects);
        }
示例#5
0
        private CustomRedirect FindInternal(string url, int siteId)
        {
            object foundRedirect = _quickLookupTable[url];

            if (foundRedirect != null)
            {
                return(foundRedirect as CustomRedirect);
            }
            // No exact match could be done, so we'll check if the 404 url
            // starts with one of the urls we're matching against. This
            // will be kind of a wild card match (even though we only check
            // for the start of the url
            // Example: http://www.mysite.com/news/mynews.html is not found
            // We have defined an "<old>/news</old>" entry in the config
            // file. We will get a match on the /news part of /news/myne...
            // Depending on the skip wild card append setting, we will either
            // redirect using the <new> url as is, or we'll append the 404
            // url to the <new> url.
            IDictionaryEnumerator enumerator = _quickLookupTable.GetEnumerator();

            while (enumerator.MoveNext())
            {
                // See if this "old" url (the one that cannot be found) starts with one
                if (url.StartsWith(enumerator.Key.ToString(), StringComparison.InvariantCultureIgnoreCase))
                {
                    foundRedirect = _quickLookupTable[enumerator.Key];

                    CustomRedirect cr = foundRedirect as CustomRedirect;
                    if (cr != null && cr.State == (int)DataStoreHandler.State.Ignored)
                    {
                        return(null);
                    }
                    if (cr != null && cr.WildCardSkipAppend)
                    {
                        // We'll redirect without appending the 404 url
                        return(cr);
                    }
                    // We need to append the 404 to the end of the
                    // new one. Make a copy of the redir object as we
                    // are changing it.
                    CustomRedirect redirCopy = new CustomRedirect(cr);
                    redirCopy.NewUrl = redirCopy.NewUrl + url.Substring(enumerator.Key.ToString().Length);
                    return(redirCopy);
                }
            }
            return(null);
        }
示例#6
0
 public void Remove(CustomRedirect customRedirect)
 {
     _quickLookupTable.Remove(customRedirect);
     List.Remove(customRedirect);
 }
示例#7
0
 public void Insert(int index, CustomRedirect customRedirect)
 {
     _quickLookupTable.Add(customRedirect, customRedirect);
     List.Insert(index, customRedirect);
 }
示例#8
0
 // public methods...
 #region Add
 public int Add(CustomRedirect customRedirect)
 {
     _quickLookupTable.Add(customRedirect.OldUrl, customRedirect);
     return(List.Add(customRedirect));
 }