From 6dea69c5a1c4e0278c8059d38e19ad2e07b279bd Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 27 Apr 2016 19:09:49 +0200 Subject: [PATCH] U4-2508 - improve culture detection in 404 situation --- .../Routing/ContentFinderByLegacy404.cs | 34 ++++++++++++-- .../Routing/NotFoundHandlerHelper.cs | 47 ++++++------------- 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs b/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs index 22c4364891..6922510de8 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs @@ -1,5 +1,7 @@ -using System.Linq; +using System.Globalization; +using System.Linq; using System.Web; +using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Models; @@ -20,15 +22,39 @@ namespace Umbraco.Web.Routing { LogHelper.Debug("Looking for a page to handle 404."); + // try to find a culture as best as we can + var errorCulture = CultureInfo.CurrentUICulture; + if (pcr.HasDomain) + { + errorCulture = CultureInfo.GetCultureInfo(pcr.UmbracoDomain.LanguageIsoCode); + } + else + { + var route = pcr.Uri.GetAbsolutePathDecoded(); + var pos = route.LastIndexOf('/'); + IPublishedContent node = null; + while (pos > 1) + { + route = route.Substring(0, pos); + node = pcr.RoutingContext.UmbracoContext.ContentCache.GetByRoute(route); + if (node != null) break; + pos = route.LastIndexOf('/'); + } + if (node != null) + { + var d = DomainHelper.FindWildcardDomainInPath(pcr.RoutingContext.UmbracoContext.Application.Services.DomainService.GetAll(true), node.Path, null); + if (d != null && string.IsNullOrWhiteSpace(d.LanguageIsoCode) == false) + errorCulture = CultureInfo.GetCultureInfo(d.LanguageIsoCode); + } + } + // TODO - replace the whole logic var error404 = NotFoundHandlerHelper.GetCurrentNotFoundPageId( //TODO: The IContentSection should be ctor injected into this class in v8! UmbracoConfig.For.UmbracoSettings().Content.Error404Collection.ToArray(), - //TODO: Is there a better way to extract this value? at least we're not relying on singletons here though - pcr.RoutingContext.UmbracoContext.HttpContext.Request.ServerVariables["SERVER_NAME"], pcr.RoutingContext.UmbracoContext.Application.Services.EntityService, new PublishedContentQuery(pcr.RoutingContext.UmbracoContext.ContentCache, pcr.RoutingContext.UmbracoContext.MediaCache), - pcr.RoutingContext.UmbracoContext.Application.Services.DomainService); + errorCulture); IPublishedContent content = null; diff --git a/src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs b/src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs index 50497ba032..94da38e210 100644 --- a/src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs +++ b/src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Web; using System.Xml; @@ -232,47 +233,29 @@ namespace Umbraco.Web.Routing /// /// internal static int? GetCurrentNotFoundPageId( - IContentErrorPage[] error404Collection, - string requestServerName, + IContentErrorPage[] error404Collection, + string requestServerName, IEntityService entityService, ITypedPublishedContentQuery publishedContentQuery, IDomainService domainService) { - if (error404Collection.Count() > 1) + throw new NotImplementedException(); + } + + internal static int? GetCurrentNotFoundPageId( + IContentErrorPage[] error404Collection, + IEntityService entityService, + ITypedPublishedContentQuery publishedContentQuery, + CultureInfo errorCulture) + { + if (error404Collection.Length > 1) { - // try to get the 404 based on current culture (via domain) - IContentErrorPage cultureErr; - - var d = domainService.GetByName(requestServerName); - - if (d != null && d.LanguageId.HasValue) - { - // test if a 404 page exists with current culture - cultureErr = error404Collection - .FirstOrDefault(x => x.Culture == d.LanguageIsoCode); - - if (cultureErr != null) - { - return GetContentIdFromErrorPageConfig(cultureErr, entityService, publishedContentQuery); - } - } - // test if a 404 page exists with current culture thread - cultureErr = error404Collection - .FirstOrDefault(x => x.Culture == System.Threading.Thread.CurrentThread.CurrentUICulture.Name); - if (cultureErr != null) - { - return GetContentIdFromErrorPageConfig(cultureErr, entityService, publishedContentQuery); - } - - // there should be a default one! - cultureErr = error404Collection - .FirstOrDefault(x => x.Culture == "default"); + var cultureErr = error404Collection.FirstOrDefault(x => x.Culture == errorCulture.Name) + ?? error404Collection.FirstOrDefault(x => x.Culture == "default"); // there should be a default one! if (cultureErr != null) - { return GetContentIdFromErrorPageConfig(cultureErr, entityService, publishedContentQuery); - } } else {