Files
Umbraco-CMS/src/Umbraco.Infrastructure/Routing/NotFoundHandlerHelper.cs
Bjarke Berg 2baa1abfc4 Merge remote-tracking branch 'origin/v8/8.16' into v9/feature/merge_v8_11082021
# Conflicts:
#	.github/CONTRIBUTING.md
#	build/NuSpecs/UmbracoCms.Core.nuspec
#	build/NuSpecs/UmbracoCms.Web.nuspec
#	build/NuSpecs/UmbracoCms.nuspec
#	src/SolutionInfo.cs
#	src/Umbraco.Core/Cache/AppCaches.cs
#	src/Umbraco.Core/Cache/AppPolicedCacheDictionary.cs
#	src/Umbraco.Core/Cache/DeepCloneAppCache.cs
#	src/Umbraco.Core/Cache/WebCachingAppCache.cs
#	src/Umbraco.Core/CompositionExtensions.cs
#	src/Umbraco.Core/Models/Identity/BackOfficeIdentityUser.cs
#	src/Umbraco.Core/Models/PropertyGroupCollection.cs
#	src/Umbraco.Core/Models/PropertyTypeCollection.cs
#	src/Umbraco.Core/Persistence/Repositories/Implement/ExternalLoginRepository.cs
#	src/Umbraco.Core/ReadLock.cs
#	src/Umbraco.Core/Routing/SiteDomainMapper.cs
#	src/Umbraco.Core/UpgradeableReadLock.cs
#	src/Umbraco.Core/WriteLock.cs
#	src/Umbraco.Examine/ExamineExtensions.cs
#	src/Umbraco.Infrastructure/Examine/UmbracoFieldDefinitionCollection.cs
#	src/Umbraco.Infrastructure/Persistence/Dtos/ContentTypeDto.cs
#	src/Umbraco.Infrastructure/Persistence/Dtos/DictionaryDto.cs
#	src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberGroupRepository.cs
#	src/Umbraco.Infrastructure/Persistence/Repositories/Implement/TemplateRepository.cs
#	src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserRepository.cs
#	src/Umbraco.Infrastructure/Services/IdKeyMap.cs
#	src/Umbraco.Infrastructure/Services/Implement/ContentService.cs
#	src/Umbraco.ModelsBuilder.Embedded/PureLiveModelFactory.cs
#	src/Umbraco.Tests/App.config
#	src/Umbraco.Web.BackOffice/Controllers/EntityController.cs
#	src/Umbraco.Web.UI.Client/package.json
#	src/Umbraco.Web.UI.NetCore/umbraco/config/lang/da.xml
#	src/Umbraco.Web.UI.NetCore/umbraco/config/lang/en.xml
#	src/Umbraco.Web.UI.NetCore/umbraco/config/lang/en_us.xml
#	src/Umbraco.Web.UI/Umbraco.Web.UI.csproj
#	src/Umbraco.Web.UI/Umbraco/config/lang/cy.xml
#	src/Umbraco.Web.UI/web.Template.config
#	src/Umbraco.Web/CacheHelperExtensions.cs
#	src/Umbraco.Web/Editors/RelationTypeController.cs
#	src/Umbraco.Web/Logging/WebProfilerProvider.cs
#	src/Umbraco.Web/Models/Mapping/MemberMapDefinition.cs
#	src/Umbraco.Web/PublishedCache/NuCache/MemberCache.cs
#	src/Umbraco.Web/Routing/ContentFinderByConfigured404.cs
#	src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs
#	src/Umbraco.Web/Security/BackOfficeUserManager.cs
#	src/Umbraco.Web/Umbraco.Web.csproj
2021-08-11 19:11:35 +02:00

108 lines
4.1 KiB
C#

using System;
using System.Linq;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Xml;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Routing
{
/// <summary>
/// Used to determine the node to display when content is not found based on the configured error404 elements in umbracoSettings.config
/// </summary>
internal class NotFoundHandlerHelper
{
internal static int? GetCurrentNotFoundPageId(
ContentErrorPage[] error404Collection,
IEntityService entityService,
IPublishedContentQuery publishedContentQuery,
string errorCulture,
int? domainContentId)
{
if (error404Collection.Length > 1)
{
// test if a 404 page exists with current culture thread
ContentErrorPage cultureErr = error404Collection.FirstOrDefault(x => x.Culture.InvariantEquals(errorCulture))
?? error404Collection.FirstOrDefault(x => x.Culture == "default"); // there should be a default one!
if (cultureErr != null)
{
return GetContentIdFromErrorPageConfig(cultureErr, entityService, publishedContentQuery, domainContentId);
}
}
else if (error404Collection.Length == 1)
{
return GetContentIdFromErrorPageConfig(error404Collection.First(), entityService, publishedContentQuery, domainContentId);
}
return null;
}
/// <summary>
/// Returns the content id based on the configured ContentErrorPage section.
/// </summary>
internal static int? GetContentIdFromErrorPageConfig(
ContentErrorPage errorPage,
IEntityService entityService,
IPublishedContentQuery publishedContentQuery,
int? domainContentId)
{
if (errorPage.HasContentId)
{
return errorPage.ContentId;
}
if (errorPage.HasContentKey)
{
// need to get the Id for the GUID
// TODO: When we start storing GUIDs into the IPublishedContent, then we won't have to look this up
// but until then we need to look it up in the db. For now we've implemented a cached service for
// converting Int -> Guid and vice versa.
Attempt<int> found = entityService.GetId(errorPage.ContentKey, UmbracoObjectTypes.Document);
if (found)
{
return found.Result;
}
return null;
}
if (errorPage.ContentXPath.IsNullOrWhiteSpace() == false)
{
try
{
// we have an xpath statement to execute
var xpathResult = UmbracoXPathPathSyntaxParser.ParseXPathQuery(
xpathExpression: errorPage.ContentXPath,
nodeContextId: domainContentId,
getPath: nodeid =>
{
IEntitySlim ent = entityService.Get(nodeid);
return ent.Path.Split(',').Reverse();
},
publishedContentExists: i => publishedContentQuery.Content(i) != null);
// now we'll try to execute the expression
IPublishedContent nodeResult = publishedContentQuery.ContentSingleAtXPath(xpathResult);
if (nodeResult != null)
{
return nodeResult.Id;
}
}
catch (Exception ex)
{
StaticApplicationLogging.Logger.LogError(ex, "Could not parse xpath expression: {ContentXPath}", errorPage.ContentXPath);
return null;
}
}
return null;
}
}
}