From 3546b3e9547728ae0d4c0d16058a4d247937e245 Mon Sep 17 00:00:00 2001 From: sgay Date: Sun, 22 Jul 2012 14:01:18 -0200 Subject: [PATCH] refactor routing --- src/Umbraco.Core/ReadLock.cs | 40 ++++ src/Umbraco.Core/StringExtensions.cs | 10 + src/Umbraco.Core/Umbraco.Core.csproj | 2 + src/Umbraco.Core/UpgradeableReadLock.cs | 34 +++ src/Umbraco.Web/LegacyRequestInitializer.cs | 2 +- src/Umbraco.Web/NiceUrlResolver.cs | 219 +++++++++--------- src/Umbraco.Web/PluginResolverExtensions.cs | 10 +- src/Umbraco.Web/Routing/DefaultRoutesCache.cs | 23 +- src/Umbraco.Web/Routing/DocumentRequest.cs | 14 +- src/Umbraco.Web/Routing/Domains.cs | 3 +- src/Umbraco.Web/Routing/ILookup.cs | 9 - src/Umbraco.Web/Routing/ILookupNotFound.cs | 7 - .../IRequestDocumentLastChanceResolver.cs | 16 ++ .../Routing/IRequestDocumentResolver.cs | 16 ++ .../Routing/LookupWeightAttribute.cs | 32 --- .../RequestDocumentResolverWeightAttribute.cs | 31 +++ .../{LookupByAlias.cs => ResolveByAlias.cs} | 9 +- .../Routing/{LookupById.cs => ResolveById.cs} | 9 +- .../{LookupByPath.cs => ResolveByNiceUrl.cs} | 9 +- ...late.cs => ResolveByNiceUrlAndTemplate.cs} | 8 +- ...LookupByProfile.cs => ResolveByProfile.cs} | 8 +- .../{LookupFor404.cs => ResolveLastChance.cs} | 7 +- src/Umbraco.Web/Routing/RouteLookups.cs | 35 +-- src/Umbraco.Web/Routing/RoutingContext.cs | 4 +- src/Umbraco.Web/Umbraco.Web.csproj | 21 +- src/Umbraco.Web/UmbracoContext.cs | 42 +++- src/Umbraco.Web/UmbracoModule.cs | 2 +- src/Umbraco.Web/UriUtility.cs | 4 +- .../umbraco.presentation/default.aspx.cs | 4 +- .../umbraco.presentation/library.cs | 29 ++- 30 files changed, 408 insertions(+), 251 deletions(-) create mode 100644 src/Umbraco.Core/ReadLock.cs create mode 100644 src/Umbraco.Core/UpgradeableReadLock.cs delete mode 100644 src/Umbraco.Web/Routing/ILookup.cs delete mode 100644 src/Umbraco.Web/Routing/ILookupNotFound.cs create mode 100644 src/Umbraco.Web/Routing/IRequestDocumentLastChanceResolver.cs create mode 100644 src/Umbraco.Web/Routing/IRequestDocumentResolver.cs delete mode 100644 src/Umbraco.Web/Routing/LookupWeightAttribute.cs create mode 100644 src/Umbraco.Web/Routing/RequestDocumentResolverWeightAttribute.cs rename src/Umbraco.Web/Routing/{LookupByAlias.cs => ResolveByAlias.cs} (79%) rename src/Umbraco.Web/Routing/{LookupById.cs => ResolveById.cs} (80%) rename src/Umbraco.Web/Routing/{LookupByPath.cs => ResolveByNiceUrl.cs} (86%) rename src/Umbraco.Web/Routing/{LookupByPathWithTemplate.cs => ResolveByNiceUrlAndTemplate.cs} (81%) rename src/Umbraco.Web/Routing/{LookupByProfile.cs => ResolveByProfile.cs} (84%) rename src/Umbraco.Web/Routing/{LookupFor404.cs => ResolveLastChance.cs} (93%) diff --git a/src/Umbraco.Core/ReadLock.cs b/src/Umbraco.Core/ReadLock.cs new file mode 100644 index 0000000000..4b36860c2d --- /dev/null +++ b/src/Umbraco.Core/ReadLock.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace Umbraco.Core +{ + /// + /// Provides a convenience methodology for implementing locked access to resources. + /// + /// + /// Intended as an infrastructure class. + /// + public class UpgradeableReadLock : IDisposable + { + private readonly ReaderWriterLockSlim _rwLock; + private bool _upgraded = false; + + /// + /// Initializes a new instance of the class. + /// + /// The rw lock. + public UpgradeableReadLock(ReaderWriterLockSlim rwLock) + { + _rwLock = rwLock; + _rwLock.EnterUpgradeableReadLock(); + } + + public void UpgradeToWriteLock() + { + _rwLock.EnterWriteLock(); + } + + void IDisposable.Dispose() + { + _rwLock.ExitUpgradeableReadLock(); + } + } +} diff --git a/src/Umbraco.Core/StringExtensions.cs b/src/Umbraco.Core/StringExtensions.cs index 4469af3176..0c2e227317 100644 --- a/src/Umbraco.Core/StringExtensions.cs +++ b/src/Umbraco.Core/StringExtensions.cs @@ -55,6 +55,16 @@ namespace Umbraco.Core return toStartWith + input.TrimStart(toStartWith.ToArray()); // Ensure each char is removed first from input, e.g. ~/ plus /Path will equal ~/Path not ~//Path } + public static string EnsureStartsWith(this string input, char value) + { + return input.StartsWith(value.ToString()) ? input : value + input; + } + + public static string EnsureEndsWith(this string input, char value) + { + return input.EndsWith(value.ToString()) ? input : input + value; + } + public static bool IsLowerCase(this char ch) { return ch.ToString(CultureInfo.InvariantCulture) == ch.ToString(CultureInfo.InvariantCulture).ToLower(); diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index ca5650e3d8..9c5101ff36 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -45,6 +45,8 @@ Properties\SolutionInfo.cs + + diff --git a/src/Umbraco.Core/UpgradeableReadLock.cs b/src/Umbraco.Core/UpgradeableReadLock.cs new file mode 100644 index 0000000000..d6787b56f8 --- /dev/null +++ b/src/Umbraco.Core/UpgradeableReadLock.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace Umbraco.Core +{ + /// + /// Provides a convenience methodology for implementing locked access to resources. + /// + /// + /// Intended as an infrastructure class. + /// + public class ReadLock : IDisposable + { + private readonly ReaderWriterLockSlim _rwLock; + + /// + /// Initializes a new instance of the class. + /// + /// The rw lock. + public ReadLock(ReaderWriterLockSlim rwLock) + { + _rwLock = rwLock; + _rwLock.EnterReadLock(); + } + + void IDisposable.Dispose() + { + _rwLock.ExitReadLock(); + } + } +} diff --git a/src/Umbraco.Web/LegacyRequestInitializer.cs b/src/Umbraco.Web/LegacyRequestInitializer.cs index 301c9308ad..bc54e36f43 100644 --- a/src/Umbraco.Web/LegacyRequestInitializer.cs +++ b/src/Umbraco.Web/LegacyRequestInitializer.cs @@ -14,7 +14,7 @@ namespace Umbraco.Web public void InitializeRequest() { - var uri = _umbracoContext.OriginalUrl; + var uri = _umbracoContext.RequestUrl; // legacy - umbOriginalUrl used by default.aspx to rewritepath so forms are happy // legacy - umbOriginalUrl used by presentation/umbraco/urlRewriter/UrlRewriterFormWriter which handles
/ eg "-1/", "-1/foo", "123/", "123/foo/bar"... int pos = route.IndexOf('/'); path = route.Substring(pos); - - if (UmbracoSettings.UseDomainPrefixes || forceDomain) - { - int rootNodeId = int.Parse(route.Substring(0, pos)); - if (rootNodeId > 0) - return DomainAtNode(rootNodeId) + path; - } - - return path; - } - - // else there was not route in the cache, must build route... - - var node = _contentStore.GetNodeById(nodeId); - if (node == null) - return "#"; // legacy wrote to the log here... - - var parts = new List(); - var depth = int.Parse(_contentStore.GetNodeProperty(node, "@level")); - var id = nodeId; - string domain = null; - while (depth >= 1) - { - // if not hiding that depth, add urlName - if (depth >= startNodeDepth) - parts.Add(_contentStore.GetNodeProperty(node, UrlNameProperty)); - - var tmp = DomainAtNode(id); - if (tmp != null) - { - if (UmbracoSettings.UseDomainPrefixes || forceDomain) - domain = tmp; - break; // break to capture the id - } - - node = _contentStore.GetNodeParent(node); - id = int.Parse(_contentStore.GetNodeProperty(node, "@id")); - depth--; - } - - parts.Reverse(); - path = "/" + string.Join("/", parts); - route = string.Format("{0}{1}", id, path); - - if (!_umbracoContext.InPreviewMode) + int id = int.Parse(route.Substring(0, pos)); // will be -1 or 1234 + domainUri = id > 0 ? DomainUriAtNode(id, current) : null; + } + else { - _umbracoContext.RoutesCache.Store(nodeId, route); // will not write if previewing - } + var node = _contentStore.GetNodeById(nodeId); + if (node == null) + return "#"; // legacy wrote to the log here... - return FormatUrl(domain, path); - } + var pathParts = new List(); + int id = nodeId; + domainUri = DomainUriAtNode(id, current); + while (domainUri == null && id > 0) + { + pathParts.Add(_contentStore.GetNodeProperty(node, UrlNameProperty)); + node = _contentStore.GetNodeParent(node); + id = int.Parse(_contentStore.GetNodeProperty(node, "@id")); // will be -1 or 1234 + domainUri = id > 0 ? DomainUriAtNode(id, current) : null; + } - protected string DomainAtNode(int nodeId) - { - // be safe - if (nodeId <= 0) - return null; + // no domain, respect HideTopLevelNodeFromPath for legacy purposes + if (domainUri == null && global::umbraco.GlobalSettings.HideTopLevelNodeFromPath) + pathParts.RemoveAt(pathParts.Count - 1); - // get domains defined on that node - Domain[] domains = Domain.GetDomainsById(nodeId); + pathParts.Reverse(); + path = "/" + string.Join("/", pathParts); // will be "/" or "/foo" or "/foo/bar" etc + route = id.ToString() + path; - // no domain set on that node, return null - if (domains.Length == 0) - return null; + if (!_umbracoContext.InPreviewMode) + _umbracoContext.RoutesCache.Store(nodeId, route); + } - // else try to find the first domain that matches the current request - // else take the first domain of the list - Domain domain = domains.FirstOrDefault(d => UrlUtility.IsBaseOf(d.Name, _umbracoContext.OriginalUrl)) ?? domains[0]; + return AssembleUrl(domainUri, path, current, absolute).ToString(); + } - var domainName = domain.Name.TrimEnd('/'); - domainName = UrlUtility.EnsureScheme(domainName, _umbracoContext.OriginalUrl.Scheme); - var pos = domainName.IndexOf("//"); - pos = domainName.IndexOf("/", pos + 2); - if (pos > 0) - domainName = domainName.Substring(0, pos); + Uri AssembleUrl(Uri domain, string path, Uri current, bool absolute) + { + Uri uri; - // return a scheme + host eg http://example.com with no trailing slash - return domainName; - } + if (domain == null) + { + // no domain was found : return a relative url, add vdir if any + uri = new Uri(global::umbraco.IO.SystemDirectories.Root + path, UriKind.Relative); + } + else + { + // a domain was found : return an absolute or relative url + // cannot handle vdir, has to be in domain uri + if (!absolute && current != null && domain.GetLeftPart(UriPartial.Authority) == current.GetLeftPart(UriPartial.Authority)) + uri = new Uri(domain.AbsolutePath.TrimEnd('/') + path, UriKind.Relative); // relative + else + uri = new Uri(domain.GetLeftPart(UriPartial.Path).TrimEnd('/') + path); // absolute + } - protected string FormatUrl(string domain, string path) - { - if (domain == null) // else vdir needs to be in the domain - { - // get the application virtual dir (empty if no vdir) - string vdir = SystemDirectories.Root; - if (!string.IsNullOrEmpty(vdir)) - domain = "/" + vdir; - } + return UriFromUmbraco(uri); + } - string url = (domain ?? "") + path; - if (path != "/") - { - // not at root - if (GlobalSettings.UseDirectoryUrls) - { - // add trailing / if required - if (UmbracoSettings.AddTrailingSlash) - url += "/"; - } - else - { - // add .aspx - url += ".aspx"; - } - } + Uri DomainUriAtNode(int nodeId, Uri current) + { + // be safe + if (nodeId <= 0) + return null; - return url; - } - } + // apply filter on domains defined on that node + var domainAndUri = Domains.ApplicableDomains(Domain.GetDomainsById(nodeId), current, true); + return domainAndUri == null ? null : domainAndUri.Uri; + } + + #endregion + + #region Map public urls to/from umbraco urls + + // fixme - what about vdir? + // path = path.Substring(UriUtility.AppVirtualPathPrefix.Length); // remove virtual directory + + public static Uri UriFromUmbraco(Uri uri) + { + var path = uri.GetSafeAbsolutePath(); + if (path == "/") + return uri; + + if (!global::umbraco.GlobalSettings.UseDirectoryUrls) + path += ".aspx"; + else if (global::umbraco.UmbracoSettings.AddTrailingSlash) + path += "/"; + + return uri.Rewrite(path); + } + + public static Uri UriToUmbraco(Uri uri) + { + var path = uri.GetSafeAbsolutePath(); + + path = path.ToLower(); + if (path != "/") + path = path.TrimEnd('/'); + if (path.EndsWith(".aspx")) + path = path.Substring(0, path.Length - ".aspx".Length); + + return uri.Rewrite(path); + } + + #endregion + } } \ No newline at end of file diff --git a/src/Umbraco.Web/PluginResolverExtensions.cs b/src/Umbraco.Web/PluginResolverExtensions.cs index 381fad68fb..3d4eaff11a 100644 --- a/src/Umbraco.Web/PluginResolverExtensions.cs +++ b/src/Umbraco.Web/PluginResolverExtensions.cs @@ -13,7 +13,7 @@ namespace Umbraco.Web public static class PluginResolverExtensions { - private static volatile IEnumerable _lookups; + private static volatile IEnumerable _lookups; private static readonly object Locker = new object(); /// @@ -21,7 +21,7 @@ namespace Umbraco.Web /// /// /// - internal static IEnumerable ResolveLookups(this PluginResolver plugins) + internal static IEnumerable ResolveLookups(this PluginResolver plugins) { if (_lookups == null) { @@ -29,13 +29,13 @@ namespace Umbraco.Web { if (_lookups == null) { - var lookupTypes = TypeFinder.FindClassesOfType(); - var lookups = new List(); + var lookupTypes = TypeFinder.FindClassesOfType(); + var lookups = new List(); foreach (var l in lookupTypes) { try { - var typeInstance = Activator.CreateInstance(l) as ILookup; + var typeInstance = Activator.CreateInstance(l) as IRequestDocumentResolver; lookups.Add(typeInstance); } catch (Exception ex) diff --git a/src/Umbraco.Web/Routing/DefaultRoutesCache.cs b/src/Umbraco.Web/Routing/DefaultRoutesCache.cs index a755e9c489..b4daefaea2 100644 --- a/src/Umbraco.Web/Routing/DefaultRoutesCache.cs +++ b/src/Umbraco.Web/Routing/DefaultRoutesCache.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Threading; +using Umbraco.Core; namespace Umbraco.Web.Routing { @@ -8,8 +10,8 @@ namespace Umbraco.Web.Routing /// internal class DefaultRoutesCache : IRoutesCache { - private readonly object _lock = new object(); - private Dictionary _routes; + private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(); + private Dictionary _routes; private Dictionary _nodeIds; public DefaultRoutesCache() @@ -21,11 +23,17 @@ namespace Umbraco.Web.Routing // // but really, we should do some partial refreshes! // otherwise, we could even cache 404 errors... + // + // these are the two events used by library in legacy code... + // is it enough? + // + global::umbraco.content.AfterRefreshContent += (sender, e) => Clear(); + global::umbraco.content.AfterUpdateDocumentCache += (sender, e) => Clear(); } public void Store(int nodeId, string route) { - lock (_lock) + using (new WriteLock(_lock)) { _routes[nodeId] = route; _nodeIds[route] = nodeId; @@ -34,7 +42,7 @@ namespace Umbraco.Web.Routing public string GetRoute(int nodeId) { - lock (_lock) + lock (new ReadLock(_lock)) { return _routes.ContainsKey(nodeId) ? _routes[nodeId] : null; } @@ -42,7 +50,7 @@ namespace Umbraco.Web.Routing public int GetNodeId(string route) { - lock (_lock) + using (new ReadLock(_lock)) { return _nodeIds.ContainsKey(route) ? _nodeIds[route] : 0; } @@ -50,10 +58,11 @@ namespace Umbraco.Web.Routing public void ClearNode(int nodeId) { - lock (_lock) + using (var lck = new UpgradeableReadLock(_lock)) { if (_routes.ContainsKey(nodeId)) { + lck.UpgradeToWriteLock(); _nodeIds.Remove(_routes[nodeId]); _routes.Remove(nodeId); } @@ -62,7 +71,7 @@ namespace Umbraco.Web.Routing public void Clear() { - lock (_lock) + using (new WriteLock(_lock)) { _routes = new Dictionary(); _nodeIds = new Dictionary(); diff --git a/src/Umbraco.Web/Routing/DocumentRequest.cs b/src/Umbraco.Web/Routing/DocumentRequest.cs index a1e153b342..99b42b7263 100644 --- a/src/Umbraco.Web/Routing/DocumentRequest.cs +++ b/src/Umbraco.Web/Routing/DocumentRequest.cs @@ -155,7 +155,7 @@ namespace Umbraco.Web.Routing Trace.TraceInformation("{0}Uri=\"{1}\"", tracePrefix, this.Uri); // try to find a domain matching the current request - var domainAndUri = Domains.ApplicableDomains(Domain.GetDomains(), RequestContext.Current.UmbracoUrl, false); + var domainAndUri = Domains.ApplicableDomains(Domain.GetDomains(), RoutingContext.UmbracoContext.UmbracoUrl, false); // handle domain if (domainAndUri != null) @@ -199,12 +199,12 @@ namespace Umbraco.Web.Routing Trace.TraceInformation("{0}Path=\"{1}\"", tracePrefix, this.Uri.AbsolutePath); // look for the document - // the first successful lookup, if any, will set this.Node, and may also set this.Template + // the first successful resolver, if any, will set this.Node, and may also set this.Template // some lookups may implement caching - Trace.TraceInformation("{0}Begin lookup", tracePrefix); - var lookups = RoutingContext.RouteLookups.GetLookups(); - lookups.Any(lookup => lookup.LookupDocument(this)); - Trace.TraceInformation("{0}End lookup, {1}", tracePrefix, (this.HasNode ? "a document was found" : "no document was found")); + Trace.TraceInformation("{0}Begin resolvers", tracePrefix); + var resolvers = RoutingContext.RouteLookups.GetLookups(); + resolvers.Any(resolver => resolver.TrySetDocument(this)); + Trace.TraceInformation("{0}End resolvers, {1}", tracePrefix, (this.HasNode ? "a document was found" : "no document was found")); // fixme - not handling umbracoRedirect // should come after internal redirects @@ -243,7 +243,7 @@ namespace Umbraco.Web.Routing Trace.TraceInformation("{0}No document, try notFound lookup", tracePrefix); // if it fails then give up, there isn't much more that we can do - if (RoutingContext.LookupNotFound == null || !RoutingContext.LookupNotFound.LookupDocument(this)) + if (RoutingContext.LookupNotFound == null || !RoutingContext.LookupNotFound.TrySetDocument(this)) { Trace.TraceInformation("{0}Failed to find a document, give up", tracePrefix); break; diff --git a/src/Umbraco.Web/Routing/Domains.cs b/src/Umbraco.Web/Routing/Domains.cs index 9fb27d5b06..01b5def78a 100644 --- a/src/Umbraco.Web/Routing/Domains.cs +++ b/src/Umbraco.Web/Routing/Domains.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Umbraco.Core; using umbraco.cms.businesslogic.web; namespace Umbraco.Web.Routing @@ -58,7 +59,7 @@ namespace Umbraco.Web.Routing public static string PathRelativeToDomain(Uri domainUri, string path) { - return path.Substring(domainUri.AbsolutePath.Length).AtStart('/'); + return path.Substring(domainUri.AbsolutePath.Length).EnsureStartsWith('/'); } } } diff --git a/src/Umbraco.Web/Routing/ILookup.cs b/src/Umbraco.Web/Routing/ILookup.cs deleted file mode 100644 index 3dc00d70ef..0000000000 --- a/src/Umbraco.Web/Routing/ILookup.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Umbraco.Web.Routing -{ - - - internal interface ILookup - { - bool LookupDocument(DocumentRequest docRequest); - } -} \ No newline at end of file diff --git a/src/Umbraco.Web/Routing/ILookupNotFound.cs b/src/Umbraco.Web/Routing/ILookupNotFound.cs deleted file mode 100644 index 1d1745e74f..0000000000 --- a/src/Umbraco.Web/Routing/ILookupNotFound.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Umbraco.Web.Routing -{ - internal interface ILookupNotFound - { - bool LookupDocument(DocumentRequest docRequest); - } -} \ No newline at end of file diff --git a/src/Umbraco.Web/Routing/IRequestDocumentLastChanceResolver.cs b/src/Umbraco.Web/Routing/IRequestDocumentLastChanceResolver.cs new file mode 100644 index 0000000000..4a7f7f7088 --- /dev/null +++ b/src/Umbraco.Web/Routing/IRequestDocumentLastChanceResolver.cs @@ -0,0 +1,16 @@ +namespace Umbraco.Web.Routing +{ + /// + /// Provides a method to try to find an assign an Umbraco document to a DocumentRequest when everything else has failed. + /// + internal interface IRequestDocumentLastChanceResolver + { + /// + /// Tries to find and assign an Umbraco document to a DocumentRequest. + /// + /// The DocumentRequest that is being resolved. + /// A value indicating whether an Umbraco document was found and assigned. + /// Optionally, can also assign the template, although that is not required. + bool TrySetDocument(DocumentRequest docRequest); + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Routing/IRequestDocumentResolver.cs b/src/Umbraco.Web/Routing/IRequestDocumentResolver.cs new file mode 100644 index 0000000000..f9785a6aa3 --- /dev/null +++ b/src/Umbraco.Web/Routing/IRequestDocumentResolver.cs @@ -0,0 +1,16 @@ +namespace Umbraco.Web.Routing +{ + /// + /// Provides a method to try to find an assign an Umbraco document to a DocumentRequest. + /// + internal interface IRequestDocumentResolver + { + /// + /// Tries to find and assign an Umbraco document to a DocumentRequest. + /// + /// The DocumentRequest that is being resolved. + /// A value indicating whether an Umbraco document was found and assigned. + /// Optionally, can also assign the template, although that is not required. + bool TrySetDocument(DocumentRequest docRequest); + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Routing/LookupWeightAttribute.cs b/src/Umbraco.Web/Routing/LookupWeightAttribute.cs deleted file mode 100644 index 8e120b4455..0000000000 --- a/src/Umbraco.Web/Routing/LookupWeightAttribute.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; - -namespace Umbraco.Web.Routing -{ - /// - /// Specifies the relative weight of an ILookup. - /// - [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] - internal class LookupWeightAttribute : Attribute - { - - /// - /// Gets the default part weight. - /// - public const int DefaultWeight = 100; - - /// - /// Initializes a new instance of the class with the weight. - /// - /// The weight of the part. - public LookupWeightAttribute(int weight) - : base() - { - this.Weight = weight; - } - - /// - /// Gets the weight of the part. - /// - public int Weight { get; private set; } - } -} \ No newline at end of file diff --git a/src/Umbraco.Web/Routing/RequestDocumentResolverWeightAttribute.cs b/src/Umbraco.Web/Routing/RequestDocumentResolverWeightAttribute.cs new file mode 100644 index 0000000000..c76d7d5cf6 --- /dev/null +++ b/src/Umbraco.Web/Routing/RequestDocumentResolverWeightAttribute.cs @@ -0,0 +1,31 @@ +using System; + +namespace Umbraco.Web.Routing +{ + /// + /// Specifies the relative weight of an IRequestDocumentResolver implementation. + /// + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] + internal class RequestDocumentResolverWeightAttribute : Attribute + { + /// + /// Gets the default weight. + /// + public const int DefaultWeight = 100; + + /// + /// Initializes a new instance of the class with the weight. + /// + /// The weight. + public RequestDocumentResolverWeightAttribute(int weight) + : base() + { + this.Weight = weight; + } + + /// + /// Gets the weight. + /// + public int Weight { get; private set; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Routing/LookupByAlias.cs b/src/Umbraco.Web/Routing/ResolveByAlias.cs similarity index 79% rename from src/Umbraco.Web/Routing/LookupByAlias.cs rename to src/Umbraco.Web/Routing/ResolveByAlias.cs index 1580273f14..3f41375309 100644 --- a/src/Umbraco.Web/Routing/LookupByAlias.cs +++ b/src/Umbraco.Web/Routing/ResolveByAlias.cs @@ -10,14 +10,13 @@ namespace Umbraco.Web.Routing // at the moment aliases are not cleaned up into nice urls // - [LookupWeight(50)] - internal class LookupByAlias : ILookup + [RequestDocumentResolverWeight(50)] + internal class ResolveByAlias : IRequestDocumentResolver { - - static readonly TraceSource Trace = new TraceSource("LookupByAlias"); + static readonly TraceSource Trace = new TraceSource("ResolveByAlias"); - public bool LookupDocument(DocumentRequest docreq) + public bool TrySetDocument(DocumentRequest docreq) { XmlNode node = null; diff --git a/src/Umbraco.Web/Routing/LookupById.cs b/src/Umbraco.Web/Routing/ResolveById.cs similarity index 80% rename from src/Umbraco.Web/Routing/LookupById.cs rename to src/Umbraco.Web/Routing/ResolveById.cs index f9d8ec46aa..954152a7e2 100644 --- a/src/Umbraco.Web/Routing/LookupById.cs +++ b/src/Umbraco.Web/Routing/ResolveById.cs @@ -7,13 +7,12 @@ namespace Umbraco.Web.Routing // handles /1234 where 1234 is the id of a document // - [LookupWeight(20)] - internal class LookupById : ILookup + [RequestDocumentResolverWeight(20)] + internal class ResolveById : IRequestDocumentResolver { - - static readonly TraceSource Trace = new TraceSource("LookupById"); + static readonly TraceSource Trace = new TraceSource("ResolveById"); - public bool LookupDocument(DocumentRequest docreq) + public bool TrySetDocument(DocumentRequest docreq) { XmlNode node = null; diff --git a/src/Umbraco.Web/Routing/LookupByPath.cs b/src/Umbraco.Web/Routing/ResolveByNiceUrl.cs similarity index 86% rename from src/Umbraco.Web/Routing/LookupByPath.cs rename to src/Umbraco.Web/Routing/ResolveByNiceUrl.cs index 4acb683a68..d5d7cb1b8f 100644 --- a/src/Umbraco.Web/Routing/LookupByPath.cs +++ b/src/Umbraco.Web/Routing/ResolveByNiceUrl.cs @@ -6,13 +6,12 @@ namespace Umbraco.Web.Routing // handles "/foo/bar" where "/foo/bar" is the path to a document // - [LookupWeight(10)] - internal class LookupByPath : ILookup + [RequestDocumentResolverWeight(10)] + internal class ResolveByNiceUrl : IRequestDocumentResolver { - - static readonly TraceSource Trace = new TraceSource("LookupByPath"); + static readonly TraceSource Trace = new TraceSource("ResolveByNiceUrl"); - public virtual bool LookupDocument(DocumentRequest docreq) + public virtual bool TrySetDocument(DocumentRequest docreq) { string route; if (docreq.HasDomain) diff --git a/src/Umbraco.Web/Routing/LookupByPathWithTemplate.cs b/src/Umbraco.Web/Routing/ResolveByNiceUrlAndTemplate.cs similarity index 81% rename from src/Umbraco.Web/Routing/LookupByPathWithTemplate.cs rename to src/Umbraco.Web/Routing/ResolveByNiceUrlAndTemplate.cs index a14de76b6e..1ccb043af6 100644 --- a/src/Umbraco.Web/Routing/LookupByPathWithTemplate.cs +++ b/src/Umbraco.Web/Routing/ResolveByNiceUrlAndTemplate.cs @@ -8,12 +8,12 @@ namespace Umbraco.Web.Routing // handles /foo/bar/