Exemple #1
0
        /// <summary>
        /// Looks for wildcard domains in the path and updates <c>Culture</c> accordingly.
        /// </summary>
        internal void HandleWildcardDomains()
        {
            const string tracePrefix = "HandleWildcardDomains: ";

            if (!_pcr.HasPublishedContent)
            {
                return;
            }

            var nodePath = _pcr.PublishedContent.Path;

            LogHelper.Debug <PublishedContentRequestEngine>("{0}Path=\"{1}\"", () => tracePrefix, () => nodePath);
            var rootNodeId = _pcr.HasDomain ? _pcr.Domain.RootNodeId : (int?)null;
            var domain     = DomainHelper.FindWildcardDomainInPath(DomainHelper.GetAllDomains(true), nodePath, rootNodeId);

            if (domain != null)
            {
                _pcr.Culture = new CultureInfo(domain.Language.CultureAlias);
                LogHelper.Debug <PublishedContentRequestEngine>("{0}Got domain on node {1}, set culture to \"{2}\".", () => tracePrefix,
                                                                () => domain.RootNodeId, () => _pcr.Culture.Name);
            }
            else
            {
                LogHelper.Debug <PublishedContentRequestEngine>("{0}No match.", () => tracePrefix);
            }
        }
        /// <summary>
        /// Looks for wildcard domains in the path and updates <c>Culture</c> accordingly.
        /// </summary>
        internal void HandleWildcardDomains(PublishedRequest request)
        {
            const string tracePrefix = "HandleWildcardDomains: ";

            if (request.HasPublishedContent == false)
            {
                return;
            }

            var nodePath = request.PublishedContent.Path;

            _logger.Debug <PublishedRouter>("{TracePrefix}Path={NodePath}", tracePrefix, nodePath);
            var rootNodeId = request.HasDomain ? request.Domain.ContentId : (int?)null;
            var domain     = DomainHelper.FindWildcardDomainInPath(request.UmbracoContext.PublishedSnapshot.Domains.GetAll(true), nodePath, rootNodeId);

            // always has a contentId and a culture
            if (domain != null)
            {
                request.Culture = domain.Culture;
                _logger.Debug <PublishedRouter>("{TracePrefix}Got domain on node {DomainContentId}, set culture to {CultureName}", tracePrefix, domain.ContentId, request.Culture.Name);
            }
            else
            {
                _logger.Debug <PublishedRouter>("{TracePrefix}No match.", tracePrefix);
            }
        }
        /// <summary>
        /// Looks for wildcard domains in the path and updates <c>Culture</c> accordingly.
        /// </summary>
        internal void HandleWildcardDomains()
        {
            const string tracePrefix = "HandleWildcardDomains: ";

            if (_pcr.HasPublishedContent == false)
            {
                return;
            }

            var nodePath = _pcr.PublishedContent.Path;

            ProfilingLogger.Logger.Debug <PublishedContentRequestEngine>("{0}Path=\"{1}\"", () => tracePrefix, () => nodePath);
            var rootNodeId = _pcr.HasDomain ? _pcr.UmbracoDomain.RootContentId : (int?)null;
            var domain     = DomainHelper.FindWildcardDomainInPath(Services.DomainService.GetAll(true), nodePath, rootNodeId);

            if (domain != null && domain.LanguageIsoCode.IsNullOrWhiteSpace() == false)
            {
                _pcr.Culture = new CultureInfo(domain.LanguageIsoCode);
                ProfilingLogger.Logger.Debug <PublishedContentRequestEngine>("{0}Got domain on node {1}, set culture to \"{2}\".", () => tracePrefix,
                                                                             () => domain.RootContentId, () => _pcr.Culture.Name);
            }
            else
            {
                ProfilingLogger.Logger.Debug <PublishedContentRequestEngine>("{0}No match.", () => tracePrefix);
            }
        }
        /// <summary>
        /// Tries to find and assign an Umbraco document to a <c>PublishedRequest</c>.
        /// </summary>
        /// <param name="frequest">The <c>PublishedRequest</c>.</param>
        /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
        public bool TryFindContent(PublishedRequest frequest)
        {
            _logger.Debug <ContentFinderByConfigured404>("Looking for a page to handle 404.");

            // try to find a culture as best as we can
            var errorCulture = CultureInfo.CurrentUICulture;

            if (frequest.HasDomain)
            {
                errorCulture = frequest.Domain.Culture;
            }
            else
            {
                var route = frequest.Uri.GetAbsolutePathDecoded();
                var pos   = route.LastIndexOf('/');
                IPublishedContent node = null;
                while (pos > 1)
                {
                    route = route.Substring(0, pos);
                    node  = frequest.UmbracoContext.ContentCache.GetByRoute(route, culture: frequest?.Culture?.Name);
                    if (node != null)
                    {
                        break;
                    }
                    pos = route.LastIndexOf('/');
                }
                if (node != null)
                {
                    var d = DomainHelper.FindWildcardDomainInPath(frequest.UmbracoContext.PublishedSnapshot.Domains.GetAll(true), node.Path, null);
                    if (d != null)
                    {
                        errorCulture = d.Culture;
                    }
                }
            }

            var error404 = NotFoundHandlerHelper.GetCurrentNotFoundPageId(
                _contentConfigSection.Error404Collection.ToArray(),
                _entityService,
                new PublishedContentQuery(frequest.UmbracoContext.PublishedSnapshot, frequest.UmbracoContext.VariationContextAccessor),
                errorCulture);

            IPublishedContent content = null;

            if (error404.HasValue)
            {
                _logger.Debug <ContentFinderByConfigured404>("Got id={ErrorNodeId}.", error404.Value);

                content = frequest.UmbracoContext.ContentCache.GetById(error404.Value);

                _logger.Debug <ContentFinderByConfigured404>(content == null
                    ? "Could not find content with that id."
                    : "Found corresponding content.");
            }
            else
            {
                _logger.Debug <ContentFinderByConfigured404>("Got nothing.");
            }

            frequest.PublishedContent = content;
            frequest.Is404            = true;
            return(content != null);
        }
        /// <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 TryFindContent(PublishedContentRequest pcr)
        {
            LogHelper.Debug <ContentFinderByLegacy404>("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(),
                pcr.RoutingContext.UmbracoContext.Application.Services.EntityService,
                new PublishedContentQuery(pcr.RoutingContext.UmbracoContext.ContentCache, pcr.RoutingContext.UmbracoContext.MediaCache),
                errorCulture);

            IPublishedContent content = null;

            if (error404.HasValue)
            {
                LogHelper.Debug <ContentFinderByLegacy404>("Got id={0}.", () => error404.Value);

                content = pcr.RoutingContext.UmbracoContext.ContentCache.GetById(error404.Value);

                LogHelper.Debug <ContentFinderByLegacy404>(content == null
                                ? "Could not find content with that id."
                                : "Found corresponding content.");
            }
            else
            {
                LogHelper.Debug <ContentFinderByLegacy404>("Got nothing.");
            }

            pcr.PublishedContent = content;
            pcr.SetIs404();
            return(content != null);
        }