Esempio n. 1
0
        /// <summary>Finds the content item and the template associated with an url.</summary>
        /// <param name="url">The url to the template to locate.</param>
        /// <returns>A TemplateData object. If no template was found the object will have empty properties.</returns>
        public PathData ResolvePath(Url url)
        {
            if (url == null)
            {
                return(PathData.Empty);
            }

            string key = url.ToString().ToLowerInvariant();

            PathData data = HttpRuntime.Cache[key] as PathData;

            if (data == null)
            {
                data = inner.ResolvePath(url);
                if (!data.IsEmpty() && data.IsCacheable)
                {
                    logger.Debug("Adding " + url + " to cache");
                    HttpRuntime.Cache.Add(key, data.Detach(), new ContentCacheDependency(persister), Cache.NoAbsoluteExpiration, SlidingExpiration, CacheItemPriority.Normal, null);
                }
            }
            else
            {
                logger.Debug("Retrieving " + url + " from cache");
                data = data.Attach(persister);
                data.UpdateParameters(Url.Parse(url).GetQueries());
            }

            return(data);
        }
        private string GetHostedUrl(ContentItem item, string url, Site site)
        {
        	if (string.IsNullOrEmpty(site.Authority))
				return item.FindPath(PathData.DefaultAction).GetRewrittenUrl();
        	
			return Url.Parse(url).SetAuthority(site.Authority);
        }
Esempio n. 3
0
        private void RedirectToFix(WrongClassException wex)
        {
            string url = Url.Parse(fixClassUrl).ResolveTokens().AppendQuery("id", wex.Identifier);

            logger.Warn("Redirecting to '" + url + "' to fix exception: " + wex);
            context.HttpContext.ClearError();
            context.HttpContext.Response.Redirect(url);
        }
Esempio n. 4
0
        private void RedirectToFix(WrongClassException wex)
        {
            string url = Url.Parse(editConfig.Installer.FixClassUrl).ResolveTokens().AppendQuery("id", wex.Identifier);

            Trace.WriteLine("Redirecting to '" + url + "' to fix exception: " + wex);
            context.ClearError();
            context.Response.Redirect(url);
        }
Esempio n. 5
0
        /// <summary>Creates the url used for rewriting friendly urls to the url of a template.</summary>
        /// <param name="path">The path containing item information to route.</param>
        /// <returns>The path to a template.</returns>
        /// <remarks>This method may throw <see cref="TemplateNotFoundException"/> if the template cannot be computed.</remarks>
        public static Url GetRewrittenUrl(this PathData path)
        {
            if (path.IsEmpty() || string.IsNullOrEmpty(path.TemplateUrl))
            {
                return(null);
            }

            if (path.CurrentPage.IsPage)
            {
                Url url = Url.Parse(path.TemplateUrl)
                          .UpdateQuery(path.QueryParameters);
                if (path.CurrentPage.ID == 0 && path.CurrentPage.VersionOf.HasValue)
                {
                    url = url.SetQueryParameter(PathData.PageQueryKey, path.CurrentPage.VersionOf.ID.Value)
                          .SetQueryParameter(PathData.VersionIndexQueryKey, path.CurrentPage.VersionIndex);
                }
                else
                {
                    url = url.SetQueryParameter(PathData.PageQueryKey, path.CurrentPage.ID);
                }

                if (!string.IsNullOrEmpty(path.Argument))
                {
                    url = url.SetQueryParameter("argument", path.Argument);
                }

                return(url.ResolveTokens());
            }

            for (ContentItem ancestor = path.CurrentItem.Parent; ancestor != null; ancestor = ancestor.Parent)
            {
                if (ancestor.IsPage)
                {
                    var url = ancestor.FindPath(PathData.DefaultAction).GetRewrittenUrl()
                              .UpdateQuery(path.QueryParameters);
                    if (path.CurrentItem.VersionOf.HasValue)
                    {
                        return(url.SetQueryParameter(PathData.ItemQueryKey, path.CurrentItem.VersionOf.ID.Value)
                               .SetQueryParameter(PathData.VersionIndexQueryKey, ancestor.VersionIndex));
                    }
                    else
                    {
                        return(url.SetQueryParameter(PathData.ItemQueryKey, path.CurrentItem.ID));
                    }
                }
            }
            if (path.CurrentItem.VersionOf.HasValue)
            {
                return(path.CurrentItem.VersionOf.FindPath(PathData.DefaultAction).GetRewrittenUrl()
                       .UpdateQuery(path.QueryParameters)
                       .SetQueryParameter(PathData.ItemQueryKey, path.CurrentItem.ID));
            }

            return(null);
        }
Esempio n. 6
0
        /// <summary>Finds an item by traversing names from the start page.</summary>
        /// <param name="url">The url that should be traversed.</param>
        /// <returns>The content item matching the supplied url.</returns>
        public override ContentItem Parse(string url)
        {
            if (url.StartsWith("/") || url.StartsWith("~/"))
            {
                return(base.Parse(url));
            }

            Site site = host.GetSite(url);

            if (site != null)
            {
                return(TryLoadingFromQueryString(url, PathData.ItemQueryKey, PathData.PageQueryKey)
                       ?? Parse(persister.Get(site.StartPageID), Url.Parse(url).PathAndQuery));
            }

            return(TryLoadingFromQueryString(url, PathData.ItemQueryKey, PathData.PageQueryKey));
        }
Esempio n. 7
0
 /// <summary>Converts a string to an <see cref="Url"/></summary>
 /// <param name="url">The url string.</param>
 /// <returns>The string parsed into an Url.</returns>
 public static Url ToUrl(this string url)
 {
     return(Url.Parse(url));
 }
Esempio n. 8
0
 /// <summary>Converts a string URI to an Url class. The method will make any app-relative managementUrls absolute and resolve tokens within the url string.</summary>
 /// <param name="url">The URI to parse.</param>
 /// <returns>An Url object or null if the input was null.</returns>
 public static Url ParseTokenized(string url)
 {
     return(Url.Parse(ResolveTokens(url)));
 }
Esempio n. 9
0
        public PathData ResolvePath(Url url, ContentItem startNode = null, string remainingPath = null)
        {
            if (url == null)
            {
                return(PathData.Empty);
            }

            url = url.GetNormalizedContentUrl();

            var urlKey = GetUrlLowerInvariantString(url);

            // Make sure the cached path data is initialized thread safely
            Dictionary <string, PathData> cachedPathData;

            if ((cachedPathData = cache.Get <Dictionary <string, PathData> >("N2.PathDataCache")) == null)
            {
                lock (classLock)
                {
                    if ((cachedPathData = cache.Get <Dictionary <string, PathData> >("N2.PathDataCache")) == null)
                    {
                        cachedPathData = new Dictionary <string, PathData>();
                        cache.Add("N2.PathDataCache", cachedPathData, new CacheOptions {
                            SlidingExpiration = SlidingExpiration
                        });
                    }
                }
            }

            PathData data;

            if (cachedPathData.TryGetValue(urlKey, out data))
            {
                data = data.Attach(persister);
                if (data == null || data.ID == 0)
                {
                    // Cached path has to CMS content
                    return(data);
                }

                logger.Debug("Retrieving " + url + " from cache");
                if (!string.IsNullOrEmpty(url.Query))
                {
                    data.UpdateParameters(Url.Parse(url).GetQueries());
                }
            }
            else
            {
                // The requested url doesn't exist in the cached path data
                lock (classLock)
                {
                    if (!cachedPathData.TryGetValue(urlKey, out data))
                    {
                        remainingPath = remainingPath ?? Url.ToRelative(url.Path).TrimStart('~');

                        string path     = remainingPath;
                        var    pathData = GetStartNode(url, cachedPathData, ref path, 0);

                        data = pathData.ID == 0
                                                        ? inner.ResolvePath(url)
                                                        : inner.ResolvePath(url, persister.Get(pathData.ID), remainingPath.Substring(path.Length, remainingPath.Length - path.Length));

                        if (data.IsCacheable)
                        {
                            logger.Debug("Adding " + urlKey + " to cache");
                            cachedPathData.Add(urlKey, data.Detach());
                        }
                    }
                }
            }

            return(data);
        }
Esempio n. 10
0
        public void TransferRequest(string path)
        {
            string url = Url.Parse(path).AppendQuery("postback", Url.LocalUrl);

            CurrentHttpContext.Server.TransferRequest(url, true);
        }
        /// <summary>Finds the path associated with an url.</summary>
        /// <param name="url">The url to the template to locate.</param>
        /// <param name="startNode">The node to start finding path from if none supplied will start from StartNode</param>
        /// <param name="remainingPath">The remaining path to search</param>
        /// <returns>A PathData object. If no template was found the object will have empty properties.</returns>
        public PathData FindPath(Url url, ContentItem startNode = null, string remainingPath = null)
        {
            if (url == null)
            {
                return(PathData.Empty);
            }

            url = url.GetNormalizedContentUrl();

            var urlKey = GetUrlLowerInvariantString(url);

            // Within the same request, we don't need to clone again!
            if (HttpContext.Current.Items[urlKey] != null)
            {
                return(((Tuple <PathData>)HttpContext.Current.Items[urlKey]).Item1);
            }

            // Make sure the cached path data is initialized thread safely
            Dictionary <string, PathData> cachedPathData = GetCachedPathData();

            PathData data;
            bool     pathDataFound;

            lock (pathLock)
            {
                pathDataFound = cachedPathData.TryGetValue(urlKey, out data);
            }

            if (pathDataFound)
            {
                logger.DebugFormat("Retrieving path {0} from cache for key {1} ({2})", data, urlKey, data.GetHashCode());

                data = data.Attach(persister);
                if (data == null || data.ID == 0)
                {
                    // Cached path has to CMS content
                    HttpContext.Current.Items[urlKey] = Tuple.Create(data);
                    return(data);
                }

                if (!string.IsNullOrEmpty(url.Query))
                {
                    data.UpdateParameters(Url.Parse(url).GetQueries());
                }
            }
            else
            {
                // The requested url doesn't exist in the cached path data
                lock (pathLock)
                {
                    if (cachedPathData.TryGetValue(urlKey, out data))
                    {
                        logger.DebugFormat("Retrieving path {0} from cache (second chance) for key {1} ({2})", data, urlKey, data.GetHashCode());

                        data = data.Attach(persister);
                        if (data == null || data.ID == 0)
                        {
                            // Cached path has to CMS content
                            HttpContext.Current.Items[urlKey] = Tuple.Create(data);
                            return(data);
                        }

                        if (!string.IsNullOrEmpty(url.Query))
                        {
                            data.UpdateParameters(Url.Parse(url).GetQueries());
                        }
                    }
                    else
                    {
                        remainingPath = remainingPath ?? Url.ToRelative(url.Path).TrimStart('~');

                        string   path        = remainingPath;
                        PathData partialPath = GetStartNode(url, cachedPathData, ref path, 0);

                        if (partialPath.ID == 0)
                        {
                            data = inner.FindPath(url);
                            logger.DebugFormat("Found path {0} for url {1}", data, url);
                        }
                        else
                        {
                            string subpath = remainingPath.Substring(path.Length, remainingPath.Length - path.Length);
                            data = inner.FindPath(url, persister.Get(partialPath.ID), subpath);
                            logger.DebugFormat("Found path {0} for subpath {1} below {2}", data, subpath, partialPath.ID);
                        }

                        if (data.IsCacheable)
                        {
                            var detached = data.Detach();
                            logger.DebugFormat("Adding {0} to cache for key {1} ({2})", detached, urlKey, detached.GetHashCode());
                            cachedPathData.Add(urlKey, detached);
                        }
                    }
                }
            }

            HttpContext.Current.Items[urlKey] = Tuple.Create(data);
            return(data);
        }
Esempio n. 12
0
        /// <summary>Finds the path associated with an url.</summary>
        /// <param name="url">The url to the template to locate.</param>
        /// <param name="startNode">The node to start finding path from if none supplied will start from StartNode</param>
        /// <param name="remainingPath">The remaining path to search</param>
        /// <returns>A PathData object. If no template was found the object will have empty properties.</returns>
        public PathData FindPath(Url url, ContentItem startNode = null, string remainingPath = null)
        {
            if (url == null)
            {
                return(PathData.Empty);
            }

            url = url.GetNormalizedContentUrl();

            var urlKey = GetUrlLowerInvariantString(url);

            // Make sure the cached path data is initialized thread safely
            Dictionary <string, PathData> cachedPathData;

            if ((cachedPathData = cache.Get <Dictionary <string, PathData> >("N2.PathDataCache")) == null)
            {
                lock (pathLock)
                {
                    if ((cachedPathData = cache.Get <Dictionary <string, PathData> >("N2.PathDataCache")) == null)
                    {
                        cachedPathData = new Dictionary <string, PathData>();
                        cache.Add("N2.PathDataCache", cachedPathData, new CacheOptions {
                            SlidingExpiration = SlidingExpiration
                        });
                    }
                }
            }

            PathData data;
            bool     pathDataFound;

            lock (pathLock)
            {
                pathDataFound = cachedPathData.TryGetValue(urlKey, out data);
            }

            if (pathDataFound)
            {
                logger.DebugFormat("Retrieving path {0} from cache for key {1} ({2})", data, urlKey, data.GetHashCode());

                data = data.Attach(persister);
                if (data == null || data.ID == 0)
                {
                    // Cached path has to CMS content
                    return(data);
                }

                if (!string.IsNullOrEmpty(url.Query))
                {
                    data.UpdateParameters(Url.Parse(url).GetQueries());
                }
            }
            else
            {
                // The requested url doesn't exist in the cached path data
                lock (pathLock)
                {
                    if (cachedPathData.TryGetValue(urlKey, out data))
                    {
                        logger.DebugFormat("Retrieving path {0} from cache (second chance) for key {1} ({2})", data, urlKey, data.GetHashCode());

                        data = data.Attach(persister);
                        if (data == null || data.ID == 0)
                        {
                            // Cached path has to CMS content
                            return(data);
                        }

                        if (!string.IsNullOrEmpty(url.Query))
                        {
                            data.UpdateParameters(Url.Parse(url).GetQueries());
                        }
                    }
                    else
                    {
                        remainingPath = remainingPath ?? Url.ToRelative(url.Path).TrimStart('~');

                        string   path        = remainingPath;
                        PathData partialPath = GetStartNode(url, cachedPathData, ref path, 0);

                        if (partialPath.ID == 0)
                        {
                            data = inner.FindPath(url);
                            logger.DebugFormat("Found path {0} for url {1}", data, url);
                        }
                        else
                        {
                            string subpath = remainingPath.Substring(path.Length, remainingPath.Length - path.Length);
                            data = inner.FindPath(url, persister.Get(partialPath.ID), subpath);
                            logger.DebugFormat("Found path {0} for subpath {1} below {2}", data, subpath, partialPath.ID);
                        }

                        if (data.IsCacheable)
                        {
                            var detached = data.Detach();
                            logger.DebugFormat("Adding {0} to cache for key {1} ({2})", detached, urlKey, detached.GetHashCode());
                            cachedPathData.Add(urlKey, detached);
                        }
                    }
                }
            }

            return(data);
        }