Stephen2

CustomPageRouteHandler

Feb 22nd, 2014
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Web;
  4. using System.Web.Routing;
  5. using System.Web.UI;
  6. using Telerik.Microsoft.Practices.Unity;
  7. using Telerik.Sitefinity.Abstractions;
  8. using Telerik.Sitefinity.Configuration;
  9. using Telerik.Sitefinity.Modules.Pages.Configuration;
  10. using Telerik.Sitefinity.Services;
  11. using Telerik.Sitefinity.Web;
  12.  
  13. namespace SitefinityWebApp.Websilk.Code
  14. {
  15.     public class CustomPageRouteHandler : Telerik.Sitefinity.Web.PageRouteHandler
  16.     {
  17.         private static bool IsPageViewPermitted(HttpContext context, PageSiteNode siteNode)
  18.         {
  19.             SiteMapBase provider = siteNode.Provider as SiteMapBase;
  20.  
  21.             if (provider == null)
  22.             {
  23.                 return true;
  24.             }
  25.  
  26.             return provider.IsAccessibleToUser(context, siteNode);
  27.         }
  28.  
  29.         private static bool IgnoreOutputCache(HttpContext context, PageSiteNode siteNode)
  30.         {
  31.             //Caching POST requests is a scary thing - don't do it!
  32.             if (context.Request.RequestType == "POST")
  33.             {
  34.                 return true;
  35.             }
  36.  
  37.             //I also don't want to cache anything that has any kind of query string
  38.             if (context.Request.QueryString.HasKeys())
  39.             {
  40.                 return true;
  41.             }
  42.  
  43.             if (!IsPageViewPermitted(context, siteNode))
  44.             {
  45.                 return true;
  46.             }
  47.  
  48.             if (siteNode.IsBackend || ControlExtensions.BrowseAndEditIsEnabled())
  49.             {
  50.                 return true;
  51.             }
  52.  
  53.             if (!SystemManager.CurrentHttpContext.Request.IsAuthenticated)
  54.             {
  55.                 return false;
  56.             }
  57.  
  58.             return CustomPageRouteHandler.SecuredControlsCache.ContainsKey(siteNode.Key.GetHashCode());
  59.         }
  60.  
  61.         protected override void InitOutputCache(RequestContext requestContext, PageSiteNode siteNode)
  62.         {
  63.             OutputCacheProfileElement outputCacheProfileElement;
  64.  
  65.             if (IgnoreOutputCache(HttpContext.Current, siteNode))
  66.             {
  67.                 requestContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
  68.                 return;
  69.             }
  70.  
  71.             SystemConfig systemConfig = Config.Get<SystemConfig>();
  72.  
  73.             if (!systemConfig.CacheSettings.EnableOutputCache)
  74.             {
  75.                 requestContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
  76.             }
  77.             else
  78.             {
  79.                 string outputCacheProfile = siteNode.OutputCacheProfile;
  80.  
  81.                 if (string.IsNullOrEmpty(outputCacheProfile))
  82.                 {
  83.                     outputCacheProfile = systemConfig.CacheSettings.DefaultProfile;
  84.                 }
  85.  
  86.                 if (!systemConfig.CacheSettings.Profiles.TryGetValue(outputCacheProfile, out outputCacheProfileElement))
  87.                 {
  88.                     object[] objArray = new object[] { outputCacheProfile };
  89.                     throw new ArgumentException("Invalid output cache profile specified: \"{0}\".".Arrange(objArray));
  90.                 }
  91.  
  92.                 if (!outputCacheProfileElement.Enabled)
  93.                 {
  94.                     return;
  95.                 }
  96.  
  97.                 int duration = outputCacheProfileElement.Duration;
  98.                 bool slidingExpiration = outputCacheProfileElement.SlidingExpiration;
  99.  
  100.                 HttpCachePolicyBase cache = requestContext.HttpContext.Response.Cache;
  101.                 cache.AddValidationCallback(new HttpCacheValidateHandler(CustomPageRouteHandler.ValidateCacheOutput), siteNode);
  102.                 cache.SetCacheability(HttpCacheability.Server);
  103.                 cache.SetExpires(DateTime.Now.AddSeconds((double)duration));
  104.                 cache.SetMaxAge(new TimeSpan(0, 0, duration));
  105.                 cache.SetValidUntilExpires(true);
  106.  
  107.                 if (slidingExpiration)
  108.                 {
  109.                     cache.SetSlidingExpiration(true);
  110.                     cache.SetETagFromFileDependencies();
  111.                 }
  112.                
  113.                 //This is way too old school.... Removing it
  114.                 //cache.VaryByHeaders.UserAgent = true;
  115.                
  116.                 //This is handled in IgnoreOutputCache, never caching any request with a query string
  117.                 //cache.VaryByParams.IgnoreParams = false;
  118.                 //cache.VaryByParams["*"] = true;
  119.                
  120.                 cache.VaryByHeaders["host"] = true;
  121.                 requestContext.HttpContext.Items[CustomPageRouteHandler.AddCacheDependencies] = true;
  122.             }
  123.  
  124.             Guid pageId = siteNode.PageId;
  125.             string str = string.Format("Page with id {0} was added to cache.", pageId.ToString().ToUpperInvariant());
  126.             Log.Write(str, ConfigurationPolicy.TestTracing);
  127.         }
  128.  
  129.         private static void ValidateCacheOutput(HttpContext context, object data, ref HttpValidationStatus status)
  130.         {
  131.             PageSiteNode pageSiteNode = (PageSiteNode)data;
  132.             RouteHelper.SslRedirectIfNeeded(new HttpContextWrapper(context), pageSiteNode);
  133.  
  134.             if (!IgnoreOutputCache(context, pageSiteNode))
  135.             {
  136.                 status = HttpValidationStatus.Valid;
  137.                 return;
  138.             }
  139.  
  140.             status = HttpValidationStatus.IgnoreThisRequest;
  141.         }
  142.  
  143.         public static void RegisterType()
  144.         {
  145.             ObjectFactory.Container.RegisterType(typeof(Telerik.Sitefinity.Web.PageRouteHandler), typeof(CustomPageRouteHandler));
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment