Exemplo n.º 1
0
        internal Chart(HttpContextBase httpContext, VirtualPathProvider virtualPathProvider, int width, int height,
                       string theme = null, string themePath = null)
        {
            Debug.Assert(httpContext != null);

            if (width < 0)
            {
                throw new ArgumentOutOfRangeException("width", String.Format(CultureInfo.CurrentCulture,
                                                                             CommonResources.Argument_Must_Be_GreaterThanOrEqualTo, 0));
            }
            if (height < 0)
            {
                throw new ArgumentOutOfRangeException("height", String.Format(CultureInfo.CurrentCulture,
                                                                              CommonResources.Argument_Must_Be_GreaterThanOrEqualTo, 0));
            }

            _httpContext         = httpContext;
            _virtualPathProvider = virtualPathProvider;
            _width  = width;
            _height = height;
            _theme  = theme;

            // path must be app-relative in case chart is rendered from handler in different directory
            if (!String.IsNullOrEmpty(themePath))
            {
                _themePath = VirtualPathUtil.ResolvePath(TemplateStack.GetCurrentTemplate(httpContext), httpContext, themePath);
                if (!_virtualPathProvider.FileExists(_themePath))
                {
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, HelpersResources.Chart_ThemeFileNotFound, _themePath), "themePath");
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Resolves and maps a path (physical or virtual) to a physical path on the server.
        /// </summary>
        /// <param name="httpContext">The <see cref="HttpContextBase"/>.</param>
        /// <param name="path">Either a physical rooted path or a virtual path to be mapped.
        /// Physical paths are returned without modifications. Virtual paths are resolved relative to the current executing page.
        /// </param>
        /// <remarks>Result of this call should not be shown to the user (e.g. in an exception message) since
        /// it could be security sensitive. But we need to pass this result to the file APIs like File.WriteAllBytes
        /// which will show it if exceptions are raised from them. Unfortunately VirtualPathProvider doesn't have
        /// APIs for writing so we can't use that.</remarks>
        public static string MapPath(HttpContextBase httpContext, string path)
        {
            Debug.Assert(!String.IsNullOrEmpty(path));

            if (Path.IsPathRooted(path))
            {
                return(path);
            }

            // There is no TryMapPath API so we have to catch HttpException if we want to
            // throw ArgumentException instead.
            try
            {
                return(httpContext.Request.MapPath(
                           ResolvePath(TemplateStack.GetCurrentTemplate(httpContext), httpContext, path)
                           ));
            }
            catch (HttpException)
            {
                throw new ArgumentException(
                          String.Format(
                              CultureInfo.InvariantCulture,
                              HelpersResources.PathUtils_IncorrectPath,
                              path
                              ),
                          "path"
                          );
            }
        }
        public void GetCurrentContextReturnsNullWhenStackIsEmpty()
        {
            // Arrange
            var httpContext = GetHttpContext();

            // Act
            var template = TemplateStack.GetCurrentTemplate(httpContext);

            // Assert
            Assert.Equal(1, httpContext.Items.Count);
            Assert.Null(template);
        }
        public void GetCurrentContextReturnsNullAfterPop()
        {
            // Arrange
            var httpContext = GetHttpContext();
            var template    = GetTemplateFile();

            // Act
            TemplateStack.Push(httpContext, template);
            TemplateStack.Pop(httpContext);

            // Assert
            Assert.Null(TemplateStack.GetCurrentTemplate(httpContext));
        }
        public void GetCurrentContextReturnsCurrentContext()
        {
            // Arrange
            var template    = GetTemplateFile();
            var httpContext = GetHttpContext();

            // Act
            TemplateStack.Push(httpContext, template);

            // Assert
            var currentTemplate = TemplateStack.GetCurrentTemplate(httpContext);

            Assert.Equal(template, currentTemplate);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Resolves path relative to the current executing page
        /// </summary>
        public static string ResolvePath(string virtualPath)
        {
            if (String.IsNullOrEmpty(virtualPath))
            {
                return(virtualPath);
            }

            if (HttpContext.Current == null)
            {
                return(virtualPath);
            }
            var httpContext = new HttpContextWrapper(HttpContext.Current);

            return(ResolvePath(TemplateStack.GetCurrentTemplate(httpContext), httpContext, virtualPath));
        }
        public void GetCurrentContextReturnsLastPushedContext()
        {
            // Arrange
            var httpContext = GetHttpContext();
            var template1   = GetTemplateFile("page1");
            var template2   = GetTemplateFile("page2");

            // Act
            TemplateStack.Push(httpContext, template1);
            TemplateStack.Push(httpContext, template2);

            // Assert
            var currentTemplate = TemplateStack.GetCurrentTemplate(httpContext);

            Assert.Equal(template2, currentTemplate);
        }
Exemplo n.º 8
0
        private static HttpContextBase GetContext(params string[] virtualPaths)
        {
            var httpContext = new Mock <HttpContextBase>();
            var table       = new Hashtable();

            httpContext.SetupGet(c => c.Items).Returns(table);

            foreach (var item in virtualPaths)
            {
                var page = new Mock <ITemplateFile>();
                page.SetupGet(p => p.TemplateInfo).Returns(new TemplateFileInfo(item));
                TemplateStack.Push(httpContext.Object, page.Object);
            }

            return(httpContext.Object);
        }
Exemplo n.º 9
0
        internal Chart(HttpContextBase httpContext, Func <VirtualPathProvider> virtualPathProviderFunc,
                       int width, int height, string theme = null, string themePath = null)
        {
            Contract.Assert(httpContext != null);
            Contract.Assert(virtualPathProviderFunc != null);

            // HostingEnvironment.VirtualPathProvider never null in running host but may change at any time.
            Contract.Assert(virtualPathProviderFunc() != null);

            if (width < 0)
            {
                throw new ArgumentOutOfRangeException("width", String.Format(
                                                          CultureInfo.CurrentCulture,
                                                          CommonResources.Argument_Must_Be_GreaterThanOrEqualTo,
                                                          0));
            }
            if (height < 0)
            {
                throw new ArgumentOutOfRangeException("height", String.Format(
                                                          CultureInfo.CurrentCulture,
                                                          CommonResources.Argument_Must_Be_GreaterThanOrEqualTo,
                                                          0));
            }

            _httpContext             = httpContext;
            _virtualPathProviderFunc = virtualPathProviderFunc;
            _width  = width;
            _height = height;
            _theme  = theme;

            // path must be app-relative in case chart is rendered from handler in different directory
            if (!String.IsNullOrEmpty(themePath))
            {
                _themePath = VirtualPathUtil.ResolvePath(TemplateStack.GetCurrentTemplate(httpContext), httpContext, themePath);
                if (!virtualPathProviderFunc().FileExists(_themePath))
                {
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, HelpersResources.Chart_ThemeFileNotFound, _themePath), "themePath");
                }
            }
        }
        private string GetPageRelativePath(HttpContextBase httpContext, string path)
        {
            if (httpContext == null)
            {
                return(path);
            }
            var templateFile = TemplateStack.GetCurrentTemplate(httpContext);

            if (templateFile != null)
            {
                var templateVirtualPath = templateFile.TemplateInfo.VirtualPath;
                if (path.IsEmpty())
                {
                    path = templateVirtualPath;
                }
                else
                {
                    path = VirtualPathUtility.Combine(templateVirtualPath, path);
                }
            }
            return(VirtualPathUtility.ToAbsolute(path ?? "~/"));
        }