/// <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.");

            // 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);

            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);
        }
        /// <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.");

            // 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 <ContentFinderByLegacy404>("Got id={0}.", () => id);

                content = pcr.RoutingContext.UmbracoContext.ContentCache.GetById(id);

                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);
        }
		/// <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.");

            // 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<ContentFinderByLegacy404>("Got id={0}.", () => id);

				content = pcr.RoutingContext.UmbracoContext.ContentCache.GetById(id);

			    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;
		}
        /// <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);
        }
        public override bool TryFindContent(PublishedContentRequest contentRequest)
        {
            // eg / or /path/to/whatever
            var url = contentRequest.Uri.GetAbsolutePathDecoded();

            var mdRoot = "/" + MarkdownLogic.BaseUrl;
            if (url.StartsWith("/projects/umbraco-pro/contour/documentation"))
                mdRoot = "/projects";

            // ensure it's a md url
            if (url.StartsWith(mdRoot) == false)
                return false; // not for us

            // find the root content
            var node = FindContent(contentRequest, mdRoot);
            if (node == null)
                return false;

            // kill those old urls
            foreach (var s in new []{ "master", "v480" })
                if (url.StartsWith(mdRoot + "/" + s))
                {
                    url = url.Replace(mdRoot + "/" + s, mdRoot);
                    contentRequest.SetRedirectPermanent(url);
                    return true;
                }

            // find the md file
            var mdFilepath = FindMarkdownFile(url);
            
            //return the broken link doc page
            var is404 = false;
            if (mdFilepath == null)
            {
                mdFilepath = FindMarkdownFile("/documentation/broken-link");
                is404 = true;
            }
            if (mdFilepath == null)
            {                
                // clear the published content (that was set by FindContent) to cause a 404, and in
                // both case return 'true' because there's no point other finders try to handle the request
                contentRequest.PublishedContent = null;
                return true;
            }
 
            if (is404) contentRequest.SetIs404();

            // set the context vars
            var httpContext = contentRequest.RoutingContext.UmbracoContext.HttpContext;
            httpContext.Items[MarkdownLogic.MarkdownPathKey] = mdFilepath;
            httpContext.Items["topicTitle"] = string.Join(" - ", httpContext.Request.RawUrl
                .Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries)
                .Skip(1)
                .Reverse());

            // override the template
            const string altTemplate = "DocumentationSubpage";
            var templateIsSet = contentRequest.TrySetTemplate(altTemplate);
            //httpContext.Trace.Write("Markdown Files Handler",
            //    string.Format("Template changed to: '{0}' is {1}", altTemplate, templateIsSet));
            
            // be happy
            return true;
        }