/// <summary>
        /// Requests the remote HTML.
        /// </summary>
        /// <param name="cacheProvider">Strategy for caching the remote HTML.</param>
        /// <param name="selectedSection">The selected section.</param>
        private void RequestRemoteHtml(RemoteMasterPageCacheProviderBase cacheProvider, string selectedSection)
        {
            try
            {
                // Get the URL to request the cached control from.
                // Include text size so that header knows which links to apply
                var textSize        = new TextSize(HttpContext.Current.Request.Cookies, HttpContext.Current.Request.QueryString);
                Uri urlToRequest    = new Uri(HttpContext.Current.Request.Url, String.Format(CultureInfo.CurrentCulture, config["MasterPageControlUrl"], this.Control));
                var applicationPath = HttpUtility.UrlEncode(HttpRuntime.AppDomainAppVirtualPath.ToLower(CultureInfo.CurrentCulture).TrimEnd('/'));
                var query           = HttpUtility.ParseQueryString(urlToRequest.Query);
                query.Add("section", selectedSection);
                query.Add("host", Page.Request.Url.Host);
                query.Add("textsize", textSize.CurrentTextSize().ToString(CultureInfo.InvariantCulture));
                query.Add("path", applicationPath);
                urlToRequest = new Uri(urlToRequest.Scheme + "://" + urlToRequest.Authority + urlToRequest.AbsolutePath + "?" + query);

                // Create the request. Pass current user-agent so that library catalogue PCs can be detected by the remote script.
                var webRequest = (HttpWebRequest)WebRequest.Create(urlToRequest);
                webRequest.UseDefaultCredentials = true;
                webRequest.UserAgent             = Page.Request.UserAgent;
                webRequest.Proxy   = new ConfigurationProxyProvider().CreateProxy();
                webRequest.Timeout = 4000;
                if (!String.IsNullOrEmpty(this.config["Timeout"]))
                {
                    int timeout;
                    if (Int32.TryParse(this.config["Timeout"], out timeout))
                    {
                        webRequest.Timeout = timeout;
                    }
                }
#if DEBUG
                // Turn off SSL check in debug mode as it will always fail against a self-signed certificate used for development
                webRequest.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
#endif

                // Prepare the information we'll need when the response comes back
                var state = new RequestState();
                state.Request       = webRequest;
                state.CacheProvider = cacheProvider;

                // Kick off the request and, only if there's nothing already cached which we can use, wait for it to come back
                if (cacheProvider.SupportsAsync)
                {
                    RequestRemoteHtmlAsynchronous(cacheProvider, webRequest, state);
                }
                else
                {
                    RequestRemoteHtmlSynchronous(cacheProvider, webRequest);
                }
            }
            catch (UriFormatException ex)
            {
                throw new ConfigurationErrorsException(String.Format(CultureInfo.CurrentCulture, config["MasterPageControlUrl"], this.Control) + " is not a valid absolute URL", ex);
            }
        }
Пример #2
0
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected new void Page_Load(object sender, EventArgs e)
        {
            // Apply selected text size to page
            var textSize     = new TextSize(Request.Cookies, Request.QueryString);
            int baseTextSize = textSize.CurrentTextSize();

            if (baseTextSize > 1)
            {
                // Add a space if there are other classes, then add to body tag
                this.bodyclass.Controls.Add(new LiteralControl(" size" + baseTextSize.ToString(CultureInfo.InvariantCulture)));
            }

            // Support web fonts required by the current skin
            if (Skin != null)
            {
                var fontsHtml = new StringBuilder();
                foreach (var font in Skin.RequiresGoogleFonts())
                {
                    fontsHtml.Append("<link href=\"").Append(font.FontUrl).Append("\" rel=\"stylesheet\" type=\"text/css\" />");
                }

                if (Skin.RequiresTypekitFonts().Any())
                {
                    foreach (var font in Skin.RequiresTypekitFonts())
                    {
                        fontsHtml.Append("<script src=\"").Append(font.TypekitUrl).Append("\"></script>");
                    }
                    this.Typekit.Visible = true;
                }
                this.fonts.Text = fontsHtml.ToString();

                AddClientDependencies(Skin);
            }

            // Support web chat
            var context = new HostingEnvironmentContext();

            if (context.WebChatSettingsUrl != null)
            {
                var webChat = new WebChat();
                webChat.WebChatSettings         = new WebChatSettingsFromApi(context.WebChatSettingsUrl, new ConfigurationProxyProvider(), new ApplicationCacheStrategy <WebChatSettings>(TimeSpan.FromMinutes(context.WebChatSettingsCacheDuration))).ReadWebChatSettings();
                webChat.WebChatSettings.PageUrl = new Uri(Request.Url.AbsolutePath, UriKind.Relative);
                if (webChat.IsRequired())
                {
                    AddClientDependencies(webChat);
                }
            }

            // Run the base method as well
            base.Page_Load(sender, e);
        }
Пример #3
0
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected new void Page_Load(object sender, EventArgs e)
        {
            // Configure remote master page
            var httpClientProvider     = new HttpClientProvider(new ConfigurationProxyProvider());
            var masterPageSettings     = new RemoteMasterPageSettingsFromConfig();
            var forceCacheRefresh      = (Page.Request.QueryString["ForceCacheRefresh"] == "1"); // Provide a way to force an immediate update of the cache
            var remoteMasterPageClient = new RemoteMasterPageHtmlProvider(masterPageSettings.MasterPageControlUrl(), httpClientProvider, Request.UserAgent, new RemoteMasterPageMemoryCacheProvider {
                CacheDuration = TimeSpan.FromMinutes(masterPageSettings.CacheTimeout())
            }, forceCacheRefresh);

            this.htmlTag.HtmlControlProvider            = remoteMasterPageClient;
            this.metadataDesktop.HtmlControlProvider    = remoteMasterPageClient;
            this.aboveHeaderDesktop.HtmlControlProvider = remoteMasterPageClient;
            if (this.headerDesktop != null)
            {
                this.headerDesktop.HtmlControlProvider = remoteMasterPageClient;
            }
            if (this.footerDesktop != null)
            {
                this.footerDesktop.HtmlControlProvider = remoteMasterPageClient;
            }
            this.scriptsDesktop.HtmlControlProvider = remoteMasterPageClient;

            // Apply selected text size to page
            var textSize     = new TextSize(Request.Cookies?["textsize"]?.Value, Request.QueryString);
            int baseTextSize = textSize.CurrentTextSize();

            if (baseTextSize > 1)
            {
                // Add a space if there are other classes, then add to body tag
                this.bodyclass.Controls.Add(new LiteralControl(" size" + baseTextSize.ToString(CultureInfo.InvariantCulture)));
            }

            // Support web fonts required by the current skin
            if (Skin != null)
            {
                var fontsHtml = new StringBuilder();
                foreach (var font in Skin.RequiresGoogleFonts())
                {
                    fontsHtml.Append("<link href=\"").Append(font.FontUrl).Append("\" rel=\"stylesheet\" type=\"text/css\" />");
                }

                this.fonts.Text = fontsHtml.ToString();

                AddClientDependencies(Skin);
            }

            // Support web chat
            var context = new HostingEnvironmentContext(HttpContext.Current.Request.Url);

            if (context.WebChatSettingsUrl != null)
            {
                var webChat            = new WebChat();
                var webChatApiSettings = Options.Create(new WebChatApiSettings {
                    WebChatSettingsUrl = context.WebChatSettingsUrl, CacheMinutes = context.WebChatSettingsCacheDuration
                });
                webChat.WebChatSettings         = new WebChatSettingsFromApi(webChatApiSettings, httpClientProvider, new ApplicationCacheStrategy <WebChatSettings>()).ReadWebChatSettings().Result;
                webChat.WebChatSettings.PageUrl = new Uri(Request.Url.AbsolutePath, UriKind.Relative);
                if (webChat.IsRequired())
                {
                    AddClientDependencies(webChat);
                }
            }

            // Run the base method as well
            base.Page_Load(sender, e);
        }