Exemplo n.º 1
0
        /// <summary>
        /// Returns a RedirectionMap created from the specified datafile.  If avaialble, a cached
        /// copy of the map is returned; otherwise, the data file is parsed and a new map created.
        /// </summary>
        /// <param name="datafile">Location of a data file creating comma-separated pairs of
        /// old URLs and new URLs for use as redirection targets.</param>
        /// <param name="context">The current HTTP Context object.</param>
        /// <returns>A (possibly empty) RedirectionMap.</returns>
        public static RedirectionMap GetMap(String datafile, HttpContext context)
        {
            log.Trace("Enter GetMap().");

            RedirectionMap map;
            Cache          cache = context.Cache;

            Object lockObj = new Object();

            try
            {
                log.DebugFormat("Load cache for '{0}'.", datafile);
                map = (RedirectionMap)cache[datafile];
                if (map == null)
                {
                    lock (lockObj)
                    {
                        // Check whether the cache was loaded while we waited for the lock.
                        map = (RedirectionMap)cache[datafile];
                        if (map == null)
                        {
                            // There was no cached redirection map.  Load it from the file system.
                            log.DebugFormat("Cache miss. Loading redirection map from '{0}'.", datafile);

                            CacheItemRemovedCallback onRemove       = new CacheItemRemovedCallback(RemovedItemCallback);
                            CacheDependency          fileDependency = new CacheDependency(datafile);

                            map = LoadMapFromFile(datafile);
                            cache.Add(datafile, map, fileDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, onRemove);
                        }
                        else
                        {
                            log.Debug("Cached redirection map found on second chance retrieval.");
                        }
                    }
                }
                else
                {
                    // A cached redirection map was found. Return it.
                    log.Debug("Loading cached redirection map.");
                }
            }
            catch (Exception ex)
            {
                log.Error("Error while getting the redirection map.", ex);

                // Instead of letting the request die,
                // swallow the exception and return an empty map.
                map = new RedirectionMap();
            }

            return(map);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Private helper method to encapsulate the logic for loading and parsing the datafile.
        /// </summary>
        /// <param name="datafile">Location of a data file creating comma-separated pairs of
        /// old URLs and new URLs for use as redirection targets.</param>
        /// <returns></returns>
        private static RedirectionMap LoadMapFromFile(String datafile)
        {
            log.Trace("Enter LoadMapFromFile().");

            SimpleRedirectorConfigurationSection config = SimpleRedirectorConfigurationSection.Get();

            char[] separators = new char[1];
            separators[0] = config.DataSource.Separator;

            RedirectionMap map = new RedirectionMap();

            try
            {
                if (!File.Exists(datafile))
                {
                    log.ErrorFormat("Datafile '{0}' not found.", datafile);
                    throw new FileNotFoundException(datafile);
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error while loading urls from {0}.", ex, datafile);
                // Swallow the exception.  The worst case is we return an empty dictionary
                // and nothing gets redirected.
            }

            String[] listOfUrlPairs = File.ReadAllLines(datafile);
            foreach (String urlPair in listOfUrlPairs)
            {
                String[] urls = urlPair.Trim().Split(separators);
                if (urls.Length >= 2)
                {
                    try
                    {
                        map.Add(urls[0], urls[1]);
                    }
                    catch (Exception ex)
                    {
                        log.ErrorFormat("Duplicate URL found in RedirectMap: {0}", ex, urls[0]);
                    }
                }

                if (urls.Length != 2)
                {
                    // We can recover from this problem. No exception needed.
                    log.WarnFormat("Expected only two urls, found {0} in '{1}'.", urls.Length, urlPair);
                }
            }

            return(map);
        }