Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Specialized;
- using System.Web;
- using System.Web.Routing;
- using System.Web.UI;
- using Telerik.Microsoft.Practices.Unity;
- using Telerik.Sitefinity.Abstractions;
- using Telerik.Sitefinity.Configuration;
- using Telerik.Sitefinity.Modules.Pages.Configuration;
- using Telerik.Sitefinity.Services;
- using Telerik.Sitefinity.Web;
- namespace SitefinityWebApp.Websilk.Code
- {
- public class CustomPageRouteHandler : Telerik.Sitefinity.Web.PageRouteHandler
- {
- private static bool IsPageViewPermitted(HttpContext context, PageSiteNode siteNode)
- {
- SiteMapBase provider = siteNode.Provider as SiteMapBase;
- if (provider == null)
- {
- return true;
- }
- return provider.IsAccessibleToUser(context, siteNode);
- }
- private static bool IgnoreOutputCache(HttpContext context, PageSiteNode siteNode)
- {
- //Caching POST requests is a scary thing - don't do it!
- if (context.Request.RequestType == "POST")
- {
- return true;
- }
- //I also don't want to cache anything that has any kind of query string
- if (context.Request.QueryString.HasKeys())
- {
- return true;
- }
- if (!IsPageViewPermitted(context, siteNode))
- {
- return true;
- }
- if (siteNode.IsBackend || ControlExtensions.BrowseAndEditIsEnabled())
- {
- return true;
- }
- if (!SystemManager.CurrentHttpContext.Request.IsAuthenticated)
- {
- return false;
- }
- return CustomPageRouteHandler.SecuredControlsCache.ContainsKey(siteNode.Key.GetHashCode());
- }
- protected override void InitOutputCache(RequestContext requestContext, PageSiteNode siteNode)
- {
- OutputCacheProfileElement outputCacheProfileElement;
- if (IgnoreOutputCache(HttpContext.Current, siteNode))
- {
- requestContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
- return;
- }
- SystemConfig systemConfig = Config.Get<SystemConfig>();
- if (!systemConfig.CacheSettings.EnableOutputCache)
- {
- requestContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
- }
- else
- {
- string outputCacheProfile = siteNode.OutputCacheProfile;
- if (string.IsNullOrEmpty(outputCacheProfile))
- {
- outputCacheProfile = systemConfig.CacheSettings.DefaultProfile;
- }
- if (!systemConfig.CacheSettings.Profiles.TryGetValue(outputCacheProfile, out outputCacheProfileElement))
- {
- object[] objArray = new object[] { outputCacheProfile };
- throw new ArgumentException("Invalid output cache profile specified: \"{0}\".".Arrange(objArray));
- }
- if (!outputCacheProfileElement.Enabled)
- {
- return;
- }
- int duration = outputCacheProfileElement.Duration;
- bool slidingExpiration = outputCacheProfileElement.SlidingExpiration;
- HttpCachePolicyBase cache = requestContext.HttpContext.Response.Cache;
- cache.AddValidationCallback(new HttpCacheValidateHandler(CustomPageRouteHandler.ValidateCacheOutput), siteNode);
- cache.SetCacheability(HttpCacheability.Server);
- cache.SetExpires(DateTime.Now.AddSeconds((double)duration));
- cache.SetMaxAge(new TimeSpan(0, 0, duration));
- cache.SetValidUntilExpires(true);
- if (slidingExpiration)
- {
- cache.SetSlidingExpiration(true);
- cache.SetETagFromFileDependencies();
- }
- //This is way too old school.... Removing it
- //cache.VaryByHeaders.UserAgent = true;
- //This is handled in IgnoreOutputCache, never caching any request with a query string
- //cache.VaryByParams.IgnoreParams = false;
- //cache.VaryByParams["*"] = true;
- cache.VaryByHeaders["host"] = true;
- requestContext.HttpContext.Items[CustomPageRouteHandler.AddCacheDependencies] = true;
- }
- Guid pageId = siteNode.PageId;
- string str = string.Format("Page with id {0} was added to cache.", pageId.ToString().ToUpperInvariant());
- Log.Write(str, ConfigurationPolicy.TestTracing);
- }
- private static void ValidateCacheOutput(HttpContext context, object data, ref HttpValidationStatus status)
- {
- PageSiteNode pageSiteNode = (PageSiteNode)data;
- RouteHelper.SslRedirectIfNeeded(new HttpContextWrapper(context), pageSiteNode);
- if (!IgnoreOutputCache(context, pageSiteNode))
- {
- status = HttpValidationStatus.Valid;
- return;
- }
- status = HttpValidationStatus.IgnoreThisRequest;
- }
- public static void RegisterType()
- {
- ObjectFactory.Container.RegisterType(typeof(Telerik.Sitefinity.Web.PageRouteHandler), typeof(CustomPageRouteHandler));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment