예제 #1
0
        public static string WidgetContent(this UrlHelper helper, string contentPath, string assemblyName)
        {
            if (contentPath.IsNullOrEmpty())
            {
                throw new ArgumentNullException("contentPath");
            }

            if (string.IsNullOrEmpty(assemblyName))
            {
                return(UrlHelpers.WidgetContent(helper, contentPath));
            }

            var packagesManager = new PackageManager();
            var packageName     = packagesManager.GetCurrentPackage();

            if (contentPath.StartsWith("~", StringComparison.Ordinal) || contentPath.StartsWith("/", StringComparison.Ordinal) || contentPath.Contains("://"))
            {
                var url = UrlTransformations.AppendParam(contentPath, PackageManager.PackageUrlParameterName, packageName);
                return(helper.Content(url));
            }

            var resourceUrl         = string.Format("~/{0}/{1}", FrontendManager.VirtualPathBuilder.GetVirtualPath(assemblyName), contentPath);
            var contentResolvedPath = UrlTransformations.AppendParam(resourceUrl, PackageManager.PackageUrlParameterName, packageName);

            return(helper.Content(contentResolvedPath));
        }
예제 #2
0
        public string GetPageContent(Guid pageId, bool authenticated, string suffixUrl)
        {
            PageManager pageManager = PageManager.GetManager();

            var page    = pageManager.GetPageNode(pageId);
            var pageUrl = page.GetFullUrl(SystemManager.CurrentContext.AppSettings.DefaultFrontendLanguage, true);

            pageUrl = RouteHelper.GetAbsoluteUrl(pageUrl);

            if (!string.IsNullOrWhiteSpace(suffixUrl))
            {
                pageUrl = Sitefinity.Web.Url.Combine(pageUrl, suffixUrl);
            }

            pageUrl = UrlTransformations.AppendParam(pageUrl, "t", Guid.NewGuid().ToString());

            string pageContent = WebRequestHelper.GetPageWebContent(
                pageUrl,
                null,
                (HttpClient client) =>
            {
                if (authenticated)
                {
                    client.DefaultHeaders["Authorization"] = HttpContext.Current.Request.Headers["Authorization"];
                    client.TransportSettings.Cookies       = new System.Net.CookieContainer();
                    client.TransportSettings.CachePolicy   = new System.Net.Cache.HttpRequestCachePolicy();
                }
            });

            return(pageContent);
        }
예제 #3
0
        /// <summary>
        /// Resolves URL based on the current widget.
        /// </summary>
        /// <param name="helper">The URL helper.</param>
        /// <param name="contentPath">The content path.</param>
        /// <returns>Resolved URL.</returns>
        /// <exception cref="System.ArgumentNullException">contentPath</exception>
        /// <exception cref="System.InvalidOperationException">
        /// Could not resolve the given URL because RouteData of the current context is null.
        /// or
        /// Could not resolve the given URL because RouteData does not contain \controller\ key.
        /// </exception>
        public static string WidgetContent(this UrlHelper helper, string contentPath)
        {
            if (contentPath.IsNullOrEmpty())
            {
                throw new ArgumentNullException("contentPath");
            }

            var packagesManager = new PackageManager();
            var packageName     = packagesManager.GetCurrentPackage();

            if (contentPath.StartsWith("~", StringComparison.Ordinal) || contentPath.StartsWith("/", StringComparison.Ordinal) || contentPath.Contains("://"))
            {
                var url = UrlTransformations.AppendParam(contentPath, PackageManager.PackageUrlParameterName, packageName);
                url = UrlHelpers.AppendVersion(url);
                return(helper.Content(url));
            }

            if (helper.RequestContext.RouteData == null)
            {
                throw new InvalidOperationException("Could not resolve the given URL because RouteData of the current context is null.");
            }

            var    contentResolvedPath = string.Empty;
            object controllerName;

            // "widgetName" is a parameter in the route of the Designer. It allows us to have a special fallback logic
            // where we first check for the requested resource in the widget assembly and then fallback to the current controller assembly.
            if (helper.RequestContext.RouteData.Values.TryGetValue("widgetName", out controllerName))
            {
                contentResolvedPath = UrlHelpers.GetResourcePath((string)controllerName, contentPath, PackageManager.PackageUrlParameterName, packageName);
            }

            if (string.IsNullOrEmpty(contentResolvedPath))
            {
                if (helper.RequestContext.RouteData.Values.TryGetValue("controller", out controllerName))
                {
                    contentResolvedPath = UrlHelpers.GetResourcePath((string)controllerName, contentPath, PackageManager.PackageUrlParameterName, packageName);
                }
                else
                {
                    throw new InvalidOperationException("Could not resolve the given URL because RouteData does not contain \"controller\" key.");
                }
            }

            if (string.IsNullOrEmpty(contentResolvedPath))
            {
                var url = "~/" + FrontendManager.VirtualPathBuilder.GetVirtualPath(typeof(UrlHelpers).Assembly) + contentPath;
                contentResolvedPath = UrlTransformations.AppendParam(url, PackageManager.PackageUrlParameterName, packageName);
            }

            contentResolvedPath = UrlHelpers.AppendVersion(contentResolvedPath);

            return(helper.Content(contentResolvedPath));
        }
예제 #4
0
        internal static string AppendVersion(string contentPath)
        {
            if (contentPath.EndsWith(".js"))
            {
                var bytes         = Encoding.UTF8.GetBytes(GetAssemblyVersion());
                var base64version = Convert.ToBase64String(bytes);
                contentPath = UrlTransformations.AppendParam(contentPath, VersionQueryParam, base64version);
            }

            return(contentPath);
        }
예제 #5
0
        /// <summary>
        /// Gets the public page content.
        /// </summary>
        /// <param name="pageId">The id of the page.</param>
        /// <returns>The page content.</returns>
        public string GetPageContent(Guid pageId)
        {
            PageManager pageManager = PageManager.GetManager();

            var page    = pageManager.GetPageNode(pageId);
            var pageUrl = page.GetFullUrl(SystemManager.CurrentContext.AppSettings.DefaultFrontendLanguage, true);

            pageUrl = RouteHelper.GetAbsoluteUrl(pageUrl);
            pageUrl = UrlTransformations.AppendParam(pageUrl, "t", Guid.NewGuid().ToString());

            string pageContent = WebRequestHelper.GetPageWebContent(pageUrl);

            return(pageContent);
        }
예제 #6
0
        /// <summary>
        /// Gets the resource path.
        /// </summary>
        /// <param name="controllerName">Name of the controller.</param>
        /// <param name="contentPath">The content path.</param>
        /// <param name="paramaterName">Name of the paramater.</param>
        /// <param name="packageName">Name of the package.</param>
        /// <returns></returns>
        private static string GetResourcePath(string controllerName, string contentPath, string paramaterName, string packageName)
        {
            var controllerType = FrontendManager.ControllerFactory.ResolveControllerType(controllerName);
            var alternatePath  = FrontendManager.VirtualPathBuilder.GetVirtualPath(controllerType);
            var baseUrl        = "~/" + alternatePath + contentPath;

            if (HostingEnvironment.VirtualPathProvider == null || HostingEnvironment.VirtualPathProvider.FileExists(baseUrl))
            {
                alternatePath = UrlTransformations.AppendParam(baseUrl, paramaterName, packageName);
                return(alternatePath);
            }

            return(string.Empty);
        }
예제 #7
0
        /// <summary>
        /// Gets the public page content.
        /// </summary>
        /// <param name="pageId">The id of the page.</param>
        /// <returns>The page content.</returns>
        public string GetPageContent(Guid pageId)
        {
            PageManager pageManager = PageManager.GetManager();

            var page    = pageManager.GetPageNode(pageId);
            var pageUrl = page.GetFullUrl();

            pageUrl = RouteHelper.GetAbsoluteUrl(pageUrl);
            pageUrl = UrlTransformations.AppendParam(pageUrl, "t", Guid.NewGuid().ToString());

            string pageContent = WebRequestHelper.GetPageWebContent(pageUrl);

            return(pageContent);
        }
예제 #8
0
        internal static string AppendVersion(string contentPath)
        {
            if (contentPath.EndsWith(".js") || contentPath.Contains(".js?"))
            {
                var hash = VirtualPathManager.GetFileHash(contentPath, null);
                if (hash != null)
                {
                    var bytes         = Encoding.UTF8.GetBytes(hash);
                    var base64version = Convert.ToBase64String(bytes);
                    contentPath = UrlTransformations.AppendParam(contentPath, VersionQueryParam, base64version);
                }
            }

            return(contentPath);
        }
예제 #9
0
        public void AppendPackageParam_GivenUrl_VerifyThePackageNameIsAppendedCorrectly()
        {
            // Arrange: Create variables holding the package name and fake URL to which the package will be appended
            var urlWithParamters   = @"http://fakedomain.org/homePage?fakeParam=0";
            var urlWithNoParamters = @"http://fakedomain.org/homePage";
            var packageName        = "fakePackageName";

            string appendedUrhWithParams;
            string appendedUrWithNoParams;
            string appendedUrlWithEmptyPackagename;

            // Act: append the package name
            appendedUrlWithEmptyPackagename = UrlTransformations.AppendParam(urlWithNoParamters, PackageManager.PackageUrlParameterName, null);
            appendedUrhWithParams           = UrlTransformations.AppendParam(urlWithParamters, PackageManager.PackageUrlParameterName, packageName);
            appendedUrWithNoParams          = UrlTransformations.AppendParam(urlWithNoParamters, PackageManager.PackageUrlParameterName, packageName);

            // Assert: Verify the package name is properly appended
            Assert.AreEqual(urlWithNoParamters, appendedUrlWithEmptyPackagename, "The URL must not be changed due to empty package name passed as parameter");
            Assert.AreEqual(string.Format(System.Globalization.CultureInfo.InvariantCulture, "http://fakedomain.org/homePage?fakeParam=0&package={0}", packageName), appendedUrhWithParams, "The package name was not appended correctly as a second parameter");
            Assert.AreEqual(string.Format(System.Globalization.CultureInfo.InvariantCulture, "http://fakedomain.org/homePage?package={0}", packageName), appendedUrWithNoParams, "The package name was not appended correctly as a parameter");
        }