Exemple #1
0
        /// <summary>
        /// Gets the content URL.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        /// <remarks>
        /// This takes into account the current host name in the request. If the current host name matches a host name
        /// defined in the domain list for the entity being looked up, then the hostname of the current request will
        /// be used, otherwise the primary (first ordinal) domain will be used for the url.
        /// </remarks>
        protected UrlResolutionResult GetContentUrl(TypedEntity entity)
        {
            Mandate.ParameterNotNull(entity, "entity");

            //the cache key must include the host since it must be unique depending on what the current host/port is
            var cacheKey = ContentUrlKey + entity.Id + _httpContext.Request.Url.Authority;

            //return from cache if its there, otherwise go get it
            return(ApplicationContext.FrameworkContext.ApplicationCache
                   .GetOrCreate(cacheKey, () =>
            {
                using (var uow = ApplicationContext.Hive.OpenReader <IContentStore>())
                {
                    //need to check if this node is in a branch with an assigned domain

                    // get the ancestor ids except system ones
                    var allAncestorIds = uow.Repositories.GetAncestorIds(entity.Id, FixedRelationTypes.DefaultRelationType).ToArray();

                    var ancestorIds = allAncestorIds
                                      .Where(x => !x.IsSystem())
                                      .ToArray();

                    // load the published ancestors
                    var ancestors = uow.Repositories
                                    .OfRevisionType(FixedStatusTypes.Published)
                                    .InIds(ancestorIds)
                                    .ToArray();

                    // add current one to mimic "ancestors or self" but without reloading it
                    ancestors = entity.AsEnumerableOfOne().Concat(ancestors).ToArray();

                    // (APN Oct 2011) In latest codebase ancestors are returned in reverse document order, same as Xml
                    var reverse = ancestors.Reverse();

                    var nonDomainUrl = GetNonDomainUrl(reverse);
                    if (nonDomainUrl != null && nonDomainUrl.Status != UrlResolutionStatus.OnlyDomainUrlsValid)
                    {
                        return new HttpRuntimeCacheParameters <UrlResolutionResult>(nonDomainUrl);
                    }

                    //this is a branch that has a hostname assigned, so need to route by that
                    var domainUrls = GetDomainUrls(reverse);
                    if (domainUrls == null || !domainUrls.Any())
                    {
                        return new HttpRuntimeCacheParameters <UrlResolutionResult>(new UrlResolutionResult(string.Empty, UrlResolutionStatus.FailedRequiresHostname));
                    }

                    //now we need to determine what the current authority is, and if it matches one of the domain URLs, then we can remove the authority since
                    //we'll be returning a relative URL to the current request

                    var relativeHostUrl = domainUrls.Where(x => x.Url.StartsWith(_httpContext.Request.Url.Authority)).FirstOrDefault();
                    if (relativeHostUrl != null)
                    {
                        //return a relative URL for the current request authority
                        //var url = relativeHostUrl.Url.Substring(0, _httpContext.Request.Url.Authority.Length);
                        var url = relativeHostUrl.Url.Substring(_httpContext.Request.Url.Authority.Length, relativeHostUrl.Url.Length - _httpContext.Request.Url.Authority.Length);
                        return new HttpRuntimeCacheParameters <UrlResolutionResult>(new UrlResolutionResult(url, UrlResolutionStatus.SuccessWithoutHostname));
                    }

                    //if the request has come in on a domain that isn't in this entity's domain urls, then return the first domain url assigned to it
                    return new HttpRuntimeCacheParameters <UrlResolutionResult>(domainUrls.First());
                }
            }));
        }
        /// <summary>
        /// Gets the content URL.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        /// <remarks>
        /// This takes into account the current host name in the request. If the current host name matches a host name
        /// defined in the domain list for the entity being looked up, then the hostname of the current request will 
        /// be used, otherwise the primary (first ordinal) domain will be used for the url.
        /// </remarks>
        protected UrlResolutionResult GetContentUrl(TypedEntity entity)
        {
            Mandate.ParameterNotNull(entity, "entity");

            //the cache key must include the host since it must be unique depending on what the current host/port is
            var cacheKey = ContentUrlKey + entity.Id + _httpContext.Request.Url.Authority;

            //return from cache if its there, otherwise go get it
            return ApplicationContext.FrameworkContext.ApplicationCache
                .GetOrCreate(cacheKey, () =>
                    {
                        using (var uow = ApplicationContext.Hive.OpenReader<IContentStore>())
                        {
                            //need to check if this node is in a branch with an assigned domain       

                            // get the ancestor ids except system ones
                            var allAncestorIds = uow.Repositories.GetAncestorIds(entity.Id, FixedRelationTypes.DefaultRelationType).ToArray();

                            var ancestorIds = allAncestorIds
                                .Where(x => !x.IsSystem())
                                .ToArray();

                            // load the published ancestors
                            var ancestors = uow.Repositories
                                .OfRevisionType(FixedStatusTypes.Published)
                                .InIds(ancestorIds)
                                .ToArray();

                            // add current one to mimic "ancestors or self" but without reloading it
                            ancestors = entity.AsEnumerableOfOne().Concat(ancestors).ToArray();

                            // (APN Oct 2011) In latest codebase ancestors are returned in reverse document order, same as Xml
                            var reverse = ancestors.Reverse();

                            var nonDomainUrl = GetNonDomainUrl(reverse);
                            if (nonDomainUrl != null && nonDomainUrl.Status != UrlResolutionStatus.OnlyDomainUrlsValid)
                            {
                                return new HttpRuntimeCacheParameters<UrlResolutionResult>(nonDomainUrl);
                            }

                            //this is a branch that has a hostname assigned, so need to route by that
                            var domainUrls = GetDomainUrls(reverse);
                            if (domainUrls == null || !domainUrls.Any())
                            {
                                return new HttpRuntimeCacheParameters<UrlResolutionResult>(new UrlResolutionResult(string.Empty, UrlResolutionStatus.FailedRequiresHostname));
                            }

                            //now we need to determine what the current authority is, and if it matches one of the domain URLs, then we can remove the authority since
                            //we'll be returning a relative URL to the current request

                            var relativeHostUrl = domainUrls.Where(x => x.Url.StartsWith(_httpContext.Request.Url.Authority)).FirstOrDefault();
                            if (relativeHostUrl != null)
                            {
                                //return a relative URL for the current request authority
                                //var url = relativeHostUrl.Url.Substring(0, _httpContext.Request.Url.Authority.Length);
                                var url = relativeHostUrl.Url.Substring(_httpContext.Request.Url.Authority.Length, relativeHostUrl.Url.Length - _httpContext.Request.Url.Authority.Length);
                                return new HttpRuntimeCacheParameters<UrlResolutionResult>(new UrlResolutionResult(url, UrlResolutionStatus.SuccessWithoutHostname));
                            }

                            //if the request has come in on a domain that isn't in this entity's domain urls, then return the first domain url assigned to it
                            return new HttpRuntimeCacheParameters<UrlResolutionResult>(domainUrls.First());

                        }
                    });
        }