U4-1441 - refactor (fix) legacy NotFoundHandler support

This commit is contained in:
Stephan
2013-02-03 14:34:04 -01:00
parent 3ebb213916
commit da7fea47c6
8 changed files with 353 additions and 237 deletions

View File

@@ -1,228 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Web;
using System.Xml;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using umbraco.IO;
using umbraco.interfaces;
namespace Umbraco.Web.Routing
{
/// <summary>
/// Provides an implementation of <see cref="IDocumentLastChanceLookup"/> that handles backward compatilibty with legacy <c>INotFoundHandler</c>.
/// </summary>
internal class DefaultLastChanceLookup : IDocumentLastChanceLookup
{
// notes
//
// at the moment we load the legacy INotFoundHandler
// excluding those that have been replaced by proper lookups,
// and run them.
//
// when we finaly obsolete INotFoundHandler, we'll have to move
// over here code from legacy requestHandler.hande404, which
// basically uses umbraco.library.GetCurrentNotFoundPageId();
// which also would need to be refactored / migrated here.
//
// the best way to do this would be to create a DefaultLastChanceLookup2
// that would do everything by itself, and let ppl use it if they
// want, then make it the default one, then remove this one.
/// <summary>
/// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
/// </summary>
/// <param name="docRequest">The <c>PublishedContentRequest</c>.</param>
/// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
public bool TrySetDocument(PublishedContentRequest docRequest)
{
docRequest.PublishedContent = HandlePageNotFound(docRequest);
return docRequest.HasNode;
}
#region Copied over from presentation.requestHandler
//FIXME: this is temporary and should be obsoleted
string GetLegacyUrlForNotFoundHandlers(PublishedContentRequest docRequest)
{
// that's not backward-compatible because when requesting "/foo.aspx"
// 4.9 : url = "foo.aspx"
// 4.10 : url = "/foo"
//return docRequest.Uri.AbsolutePath;
// so we have to run the legacy code for url preparation :-(
// code from requestModule.UmbracoRewrite
string tmp = HttpContext.Current.Request.Path.ToLower();
// note: requestModule.UmbracoRewrite also does some confusing stuff
// with stripping &umbPage from the querystring?! ignored.
// code from requestHandler.cleanUrl
string root = Umbraco.Core.IO.SystemDirectories.Root.ToLower();
if (!string.IsNullOrEmpty(root) && tmp.StartsWith(root))
tmp = tmp.Substring(root.Length);
tmp = tmp.TrimEnd('/');
if (tmp == "/default.aspx")
tmp = string.Empty;
else if (tmp == root)
tmp = string.Empty;
// code from UmbracoDefault.Page_PreInit
if (tmp != "" && HttpContext.Current.Request["umbPageID"] == null)
{
string tryIntParse = tmp.Replace("/", "").Replace(".aspx", string.Empty);
int result;
if (int.TryParse(tryIntParse, out result))
tmp = tmp.Replace(".aspx", string.Empty);
}
else if (!string.IsNullOrEmpty(HttpContext.Current.Request["umbPageID"]))
{
int result;
if (int.TryParse(HttpContext.Current.Request["umbPageID"], out result))
{
tmp = HttpContext.Current.Request["umbPageID"];
}
}
// code from requestHandler.ctor
if (tmp != "")
tmp = tmp.Substring(1);
return tmp;
}
IPublishedContent HandlePageNotFound(PublishedContentRequest docRequest)
{
LogHelper.Debug<DefaultLastChanceLookup>("Running for url='{0}'.", () => docRequest.Uri.AbsolutePath);
//XmlNode currentPage = null;
IPublishedContent currentPage = null;
var url = GetLegacyUrlForNotFoundHandlers(docRequest);
foreach (var handler in GetNotFoundHandlers())
{
if (handler.Execute(url) && handler.redirectID > 0)
{
//currentPage = umbracoContent.GetElementById(handler.redirectID.ToString());
currentPage = docRequest.RoutingContext.PublishedContentStore.GetDocumentById(
docRequest.RoutingContext.UmbracoContext,
handler.redirectID);
// FIXME - could it be null?
LogHelper.Debug<DefaultLastChanceLookup>("Handler '{0}' found node with id={1}.", () => handler.GetType().FullName, () => handler.redirectID);
//// check for caching
//if (handler.CacheUrl)
//{
// if (url.StartsWith("/"))
// url = "/" + url;
// var cacheKey = (currentDomain == null ? "" : currentDomain.Name) + url;
// var culture = currentDomain == null ? null : currentDomain.Language.CultureAlias;
// SetCache(cacheKey, new CacheEntry(handler.redirectID.ToString(), culture));
// HttpContext.Current.Trace.Write("NotFoundHandler",
// string.Format("Added to cache '{0}', {1}.", url, handler.redirectID));
//}
break;
}
}
return currentPage;
}
static IEnumerable<Type> _customHandlerTypes = null;
static readonly object CustomHandlerTypesLock = new object();
IEnumerable<Type> InitializeNotFoundHandlers()
{
// initialize handlers
// create the definition cache
LogHelper.Debug<DefaultLastChanceLookup>("Registering custom handlers.");
var customHandlerTypes = new List<Type>();
var customHandlers = new XmlDocument();
customHandlers.Load(Umbraco.Core.IO.IOHelper.MapPath(Umbraco.Core.IO.SystemFiles.NotFoundhandlersConfig));
foreach (XmlNode n in customHandlers.DocumentElement.SelectNodes("notFound"))
{
var assemblyName = n.Attributes.GetNamedItem("assembly").Value;
var typeName = n.Attributes.GetNamedItem("type").Value;
string ns = assemblyName;
var nsAttr = n.Attributes.GetNamedItem("namespace");
if (nsAttr != null && !string.IsNullOrWhiteSpace(nsAttr.Value))
ns = nsAttr.Value;
if (assemblyName == "umbraco" && (ns + "." + typeName) != "umbraco.handle404")
{
// skip those that are in umbraco.dll because we have replaced them with IDocumentLookups
// but do not skip "handle404" as that's the built-in legacy final handler, and for the time
// being people will have it in their config.
continue;
}
LogHelper.Debug<DefaultLastChanceLookup>("Registering '{0}.{1},{2}'.", () => ns, () => typeName, () => assemblyName);
Type type = null;
try
{
//TODO: This isn't a good way to load the assembly, its already in the Domain so we should be getting the type
// this loads the assembly into the wrong assembly load context!!
var assembly = Assembly.LoadFrom(Umbraco.Core.IO.IOHelper.MapPath(Umbraco.Core.IO.SystemDirectories.Bin + "/" + assemblyName + ".dll"));
type = assembly.GetType(ns + "." + typeName);
}
catch (Exception e)
{
LogHelper.Error<DefaultLastChanceLookup>("Error registering handler, ignoring.", e);
}
if (type != null)
customHandlerTypes.Add(type);
}
return customHandlerTypes;
}
IEnumerable<INotFoundHandler> GetNotFoundHandlers()
{
// instanciate new handlers
// using definition cache
lock (CustomHandlerTypesLock)
{
if (_customHandlerTypes == null)
_customHandlerTypes = InitializeNotFoundHandlers();
}
var handlers = new List<INotFoundHandler>();
foreach (var type in _customHandlerTypes)
{
try
{
var handler = Activator.CreateInstance(type) as INotFoundHandler;
if (handler != null)
handlers.Add(handler);
}
catch (Exception e)
{
LogHelper.Error<DefaultLastChanceLookup>(string.Format("Error instanciating handler {0}, ignoring.", type.FullName), e);
}
}
return handlers;
}
#endregion
}
}

View File

@@ -8,9 +8,13 @@ namespace Umbraco.Web.Routing
/// </summary>
internal sealed class LastChanceLookupResolver : SingleObjectResolverBase<LastChanceLookupResolver, IDocumentLastChanceLookup>
{
internal LastChanceLookupResolver(IDocumentLastChanceLookup lastChanceLookup)
: base(lastChanceLookup)
internal LastChanceLookupResolver()
: base(true)
{
}
internal LastChanceLookupResolver(IDocumentLastChanceLookup lastChanceLookup)
: base(lastChanceLookup, true)
{
}

View File

@@ -24,7 +24,6 @@ namespace Umbraco.Web.Routing
/// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
public bool TrySetDocument(PublishedContentRequest docRequest)
{
IPublishedContent node = null;
if (docRequest.Uri.AbsolutePath != "/") // no alias if "/"

View File

@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
namespace Umbraco.Web.Routing
{
internal class LookupByLegacy404 : IPublishedContentLookup
{
/// <summary>
/// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
/// </summary>
/// <param name="pcr">The <c>PublishedContentRequest</c>.</param>
/// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
public bool TrySetDocument(PublishedContentRequest pcr)
{
LogHelper.Debug<LookupByLegacy404>("Looking for a page to handle 404.");
// TODO - replace the whole logic and stop calling into library!
var error404 = global::umbraco.library.GetCurrentNotFoundPageId();
var id = int.Parse(error404);
IPublishedContent content = null;
if (id > 0)
{
LogHelper.Debug<LookupByLegacy404>("Got id={0}.", () => id);
content = pcr.RoutingContext.PublishedContentStore.GetDocumentById(
pcr.RoutingContext.UmbracoContext,
id);
if (content == null)
LogHelper.Debug<LookupByLegacy404>("Could not find content with that id.");
else
LogHelper.Debug<LookupByLegacy404>("Found corresponding content.");
}
else
{
LogHelper.Debug<LookupByLegacy404>("Got nothing.");
}
pcr.PublishedContent = content;
pcr.Is404 = true;
return content != null;
}
}
}

View File

@@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Web;
using System.Xml;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using umbraco.IO;
using umbraco.interfaces;
namespace Umbraco.Web.Routing
{
/// <summary>
/// Provides an implementation of <see cref="IPublishedContentLookup"/> that handles backward compatilibty with legacy <c>INotFoundHandler</c>.
/// </summary>
internal class LookupByNotFoundHandlers : IPublishedContentLookup
{
// notes
//
// at the moment we load the legacy INotFoundHandler
// excluding those that have been replaced by proper finders,
// and run them.
/// <summary>
/// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
/// </summary>
/// <param name="docRequest">The <c>PublishedContentRequest</c>.</param>
/// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
public bool TrySetDocument(PublishedContentRequest docRequest)
{
HandlePageNotFound(docRequest);
return docRequest.HasNode;
}
#region Copied over and adapted from presentation.requestHandler
//FIXME: this is temporary and should be obsoleted
void HandlePageNotFound(PublishedContentRequest docRequest)
{
LogHelper.Debug<LookupByNotFoundHandlers>("Running for url='{0}'.", () => docRequest.Uri.AbsolutePath);
var url = NotFoundHandlerHelper.GetLegacyUrlForNotFoundHandlers();
foreach (var handler in GetNotFoundHandlers())
{
IPublishedContentLookup lookup = null;
LogHelper.Debug<LookupByNotFoundHandlers>("Handler '{0}'.", () => handler.GetType().FullName);
// replace with our own implementation
if (handler is global::umbraco.SearchForAlias)
lookup = new LookupByAlias();
else if (handler is global::umbraco.SearchForProfile)
lookup = new LookupByProfile();
else if (handler is global::umbraco.SearchForTemplate)
lookup = new LookupByNiceUrlAndTemplate();
else if (handler is global::umbraco.handle404)
lookup = new LookupByLegacy404();
if (lookup != null)
{
LogHelper.Debug<LookupByNotFoundHandlers>("Replace handler '{0}' by new lookup '{1}'.", () => handler.GetType().FullName, () => lookup.GetType().FullName);
if (lookup.TrySetDocument(docRequest))
{
// do NOT set docRequest.PublishedContent again here as
// it would clear any template that the finder might have set
LogHelper.Debug<LookupByNotFoundHandlers>("Lookup '{0}' found node with id={1}.", () => lookup.GetType().FullName, () => docRequest.PublishedContent.Id);
if (docRequest.Is404)
LogHelper.Debug<LookupByNotFoundHandlers>("Lookup '{0}' set status to 404.", () => lookup.GetType().FullName);
return;
}
}
// else it's a legacy handler, run
if (handler.Execute(url) && handler.redirectID > 0)
{
docRequest.PublishedContent = docRequest.RoutingContext.PublishedContentStore.GetDocumentById(
docRequest.RoutingContext.UmbracoContext,
handler.redirectID);
if (!docRequest.HasNode)
{
LogHelper.Debug<LookupByNotFoundHandlers>("Handler '{0}' found node with id={1} which is not valid.", () => handler.GetType().FullName, () => handler.redirectID);
break;
}
LogHelper.Debug<LookupByNotFoundHandlers>("Handler '{0}' found valid node with id={1}.", () => handler.GetType().FullName, () => handler.redirectID);
if (docRequest.RoutingContext.UmbracoContext.HttpContext.Response.StatusCode == 404)
{
LogHelper.Debug<LookupByNotFoundHandlers>("Handler '{0}' set status code to 404.", () => handler.GetType().FullName);
docRequest.Is404 = true;
}
//// check for caching
//if (handler.CacheUrl)
//{
// if (url.StartsWith("/"))
// url = "/" + url;
// var cacheKey = (currentDomain == null ? "" : currentDomain.Name) + url;
// var culture = currentDomain == null ? null : currentDomain.Language.CultureAlias;
// SetCache(cacheKey, new CacheEntry(handler.redirectID.ToString(), culture));
// HttpContext.Current.Trace.Write("NotFoundHandler",
// string.Format("Added to cache '{0}', {1}.", url, handler.redirectID));
//}
break;
}
}
}
IEnumerable<INotFoundHandler> GetNotFoundHandlers()
{
// instanciate new handlers
// using definition cache
var handlers = new List<INotFoundHandler>();
foreach (var type in NotFoundHandlerHelper.CustomHandlerTypes)
{
try
{
var handler = Activator.CreateInstance(type) as INotFoundHandler;
if (handler != null)
handlers.Add(handler);
}
catch (Exception e)
{
LogHelper.Error<LookupByNotFoundHandlers>(string.Format("Error instanciating handler {0}, ignoring.", type.FullName), e);
}
}
return handlers;
}
#endregion
}
}

View File

@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Reflection;
using Umbraco.Core;
using Umbraco.Core.Logging;
namespace Umbraco.Web.Routing
{
// provides internal access to legacy url -- should get rid of it eventually
internal static class NotFoundHandlerHelper
{
const string ContextKey = "Umbraco.Web.Routing.NotFoundHandlerHelper.Url";
static NotFoundHandlerHelper()
{
InitializeNotFoundHandlers();
}
public static string GetLegacyUrlForNotFoundHandlers()
{
// that's not backward-compatible because when requesting "/foo.aspx"
// 4.9 : url = "foo.aspx"
// 4.10 : url = "/foo"
//return pcr.Uri.AbsolutePath;
// so we have to run the legacy code for url preparation :-(
var httpContext = HttpContext.Current;
if (httpContext == null)
return "";
var url = httpContext.Items[ContextKey] as string;
if (url != null)
return url;
// code from requestModule.UmbracoRewrite
string tmp = httpContext.Request.Path.ToLower();
// note: requestModule.UmbracoRewrite also did some stripping of &umbPage
// from the querystring... that was in v3.x to fix some issues with pre-forms
// auth. Paul Sterling confirmed in jan. 2013 that we can get rid of it.
// code from requestHandler.cleanUrl
string root = Umbraco.Core.IO.SystemDirectories.Root.ToLower();
if (!string.IsNullOrEmpty(root) && tmp.StartsWith(root))
tmp = tmp.Substring(root.Length);
tmp = tmp.TrimEnd('/');
if (tmp == "/default.aspx")
tmp = string.Empty;
else if (tmp == root)
tmp = string.Empty;
// code from UmbracoDefault.Page_PreInit
if (tmp != "" && httpContext.Request["umbPageID"] == null)
{
string tryIntParse = tmp.Replace("/", "").Replace(".aspx", string.Empty);
int result;
if (int.TryParse(tryIntParse, out result))
tmp = tmp.Replace(".aspx", string.Empty);
}
else if (!string.IsNullOrEmpty(httpContext.Request["umbPageID"]))
{
int result;
if (int.TryParse(httpContext.Request["umbPageID"], out result))
{
tmp = httpContext.Request["umbPageID"];
}
}
// code from requestHandler.ctor
if (tmp != "")
tmp = tmp.Substring(1);
httpContext.Items[ContextKey] = tmp;
return tmp;
}
static IEnumerable<Type> _customHandlerTypes = null;
static void InitializeNotFoundHandlers()
{
// initialize handlers
// create the definition cache
LogHelper.Debug<LookupByNotFoundHandlers>("Registering custom handlers.");
var customHandlerTypes = new List<Type>();
var customHandlers = new XmlDocument();
customHandlers.Load(Umbraco.Core.IO.IOHelper.MapPath(Umbraco.Core.IO.SystemFiles.NotFoundhandlersConfig));
foreach (XmlNode n in customHandlers.DocumentElement.SelectNodes("notFound"))
{
var assemblyName = n.Attributes.GetNamedItem("assembly").Value;
var typeName = n.Attributes.GetNamedItem("type").Value;
string ns = assemblyName;
var nsAttr = n.Attributes.GetNamedItem("namespace");
if (nsAttr != null && !string.IsNullOrWhiteSpace(nsAttr.Value))
ns = nsAttr.Value;
LogHelper.Debug<LookupByNotFoundHandlers>("Registering '{0}.{1},{2}'.", () => ns, () => typeName, () => assemblyName);
Type type = null;
try
{
//TODO: This isn't a good way to load the assembly, its already in the Domain so we should be getting the type
// this loads the assembly into the wrong assembly load context!!
var assembly = Assembly.LoadFrom(Umbraco.Core.IO.IOHelper.MapPath(Umbraco.Core.IO.SystemDirectories.Bin + "/" + assemblyName + ".dll"));
type = assembly.GetType(ns + "." + typeName);
}
catch (Exception e)
{
LogHelper.Error<LookupByNotFoundHandlers>("Error registering handler, ignoring.", e);
}
if (type != null)
customHandlerTypes.Add(type);
}
_customHandlerTypes = customHandlerTypes;
}
public static IEnumerable<Type> CustomHandlerTypes
{
get
{
return _customHandlerTypes;
}
}
}
}

View File

@@ -315,8 +315,10 @@
<Compile Include="RenderFieldCaseType.cs" />
<Compile Include="RenderFieldEncodingType.cs" />
<Compile Include="RouteCollectionExtensions.cs" />
<Compile Include="Routing\LookupByLegacy404.cs" />
<Compile Include="Routing\LookupByPageIdQuery.cs" />
<Compile Include="Mvc\SurfaceControllerResolver.cs" />
<Compile Include="Routing\NotFoundHandlerHelper.cs" />
<Compile Include="Templates\TemplateRenderer.cs" />
<Compile Include="Templates\TemplateUtilities.cs" />
<Compile Include="umbraco.presentation\Default.aspx.cs">
@@ -398,7 +400,7 @@
<Compile Include="WebBootManager.cs" />
<Compile Include="Routing\LegacyRequestInitializer.cs" />
<Compile Include="Mvc\ControllerExtensions.cs" />
<Compile Include="Routing\DefaultLastChanceLookup.cs" />
<Compile Include="Routing\LookupByNotFoundHandlers.cs" />
<Compile Include="Routing\IDocumentLastChanceLookup.cs" />
<Compile Include="Routing\LastChanceLookupResolver.cs" />
<Compile Include="Routing\LookupByAlias.cs" />

View File

@@ -221,7 +221,9 @@ namespace Umbraco.Web
typeof (RenderControllerFactory)
});
LastChanceLookupResolver.Current = new LastChanceLookupResolver(new DefaultLastChanceLookup());
// the legacy 404 will run from within LookupByNotFoundHandlers below
// so for the time being there is no last chance lookup
LastChanceLookupResolver.Current = new LastChanceLookupResolver();
DocumentLookupsResolver.Current = new DocumentLookupsResolver(
//add all known resolvers in the correct order, devs can then modify this list on application startup either by binding to events
@@ -231,9 +233,12 @@ namespace Umbraco.Web
typeof (LookupByPageIdQuery),
typeof (LookupByNiceUrl),
typeof (LookupByIdPath),
typeof (LookupByNiceUrlAndTemplate),
typeof (LookupByProfile),
typeof (LookupByAlias)
// these will be handled by LookupByNotFoundHandlers
// so they can be enabled/disabled even though resolvers are not public yet
//typeof (LookupByNiceUrlAndTemplate),
//typeof (LookupByProfile),
//typeof (LookupByAlias),
typeof (LookupByNotFoundHandlers)
});
RoutesCacheResolver.Current = new RoutesCacheResolver(new DefaultRoutesCache(_isForTesting == false));