PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/CompositeC1/Composite/Core/Routing/Pages/C1PageRouteHander.cs

#
C# | 120 lines | 80 code | 20 blank | 20 comment | 15 complexity | 4ac86c5b6aef6390cdf65a15ec529106 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * The contents of this web application are subject to the Mozilla Public License Version
  3. * 1.1 (the "License"); you may not use this web application except in compliance with
  4. * the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/.
  5. *
  6. * Software distributed under the License is distributed on an "AS IS" basis,
  7. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  8. * for the specific language governing rights and limitations under the License.
  9. *
  10. * The Original Code is owned by and the Initial Developer of the Original Code is
  11. * Composite A/S (Danish business reg.no. 21744409). All Rights Reserved
  12. *
  13. * Section 11 of the License is EXPRESSLY amended to include a provision stating
  14. * that any dispute, including but not limited to disputes related to the enforcement
  15. * of the License, to which Composite A/S as owner of the Original Code, as Initial
  16. * Developer or in any other role, becomes a part to shall be governed by Danish law
  17. * and be initiated before the Copenhagen City Court ("K�benhavns Byret")
  18. */
  19. using System;
  20. using System.Diagnostics.CodeAnalysis;
  21. using System.Linq;
  22. using System.Web;
  23. using System.Web.Compilation;
  24. using System.Web.Configuration;
  25. using System.Web.Hosting;
  26. using System.Web.Routing;
  27. using System.Web.UI;
  28. using System.Xml.Linq;
  29. using Composite.Core.Extensions;
  30. using Composite.Core.Linq;
  31. namespace Composite.Core.Routing.Pages
  32. {
  33. internal class C1PageRouteHandler : IRouteHandler
  34. {
  35. private readonly PageUrlData _pageUrlData;
  36. private static readonly Type _handlerType;
  37. [SuppressMessage("Composite.IO", "Composite.DoNotUseConfigurationClass:DoNotUseConfigurationClass")]
  38. static C1PageRouteHandler()
  39. {
  40. bool isIntegratedPipeline = HttpRuntime.UsingIntegratedPipeline;
  41. string sectionName = isIntegratedPipeline ? "system.webServer" : "system.web";
  42. var config = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath).GetSection(sectionName);
  43. if (config != null)
  44. {
  45. string handlersSectionName = isIntegratedPipeline ? "handlers" : "httpHandlers";
  46. var handlers = XElement.Parse(config.SectionInformation.GetRawXml()).Element(handlersSectionName);
  47. if(handlers == null)
  48. {
  49. return;
  50. }
  51. var handler = handlers
  52. .Elements("add")
  53. .Where(e => e.Attribute("path") != null
  54. && e.Attribute("path").Value.Equals("Renderers/Page.aspx", StringComparison.OrdinalIgnoreCase))
  55. .SingleOrDefaultOrException("Multiple handlers for 'Renderers/Page.aspx' were found'");
  56. if (handler != null)
  57. {
  58. var typeAttr = handler.Attribute("type");
  59. Verify.IsNotNull(typeAttr, "'type' attribute is missing");
  60. _handlerType = Type.GetType(typeAttr.Value);
  61. if(_handlerType == null)
  62. {
  63. Log.LogError(typeof(C1PageRouteHandler).Name, "Failed to load type '{0}'", typeAttr.Value);
  64. }
  65. }
  66. }
  67. }
  68. public C1PageRouteHandler(PageUrlData pageUrlData)
  69. {
  70. _pageUrlData = pageUrlData;
  71. }
  72. public IHttpHandler GetHttpHandler(RequestContext requestContext)
  73. {
  74. var context = requestContext.HttpContext;
  75. string localPath = context.Request.Url.LocalPath;
  76. string pathInfo = _pageUrlData.PathInfo;
  77. // Doing a url rewriting so ASP.NET will get correct FilePath/PathInfo properties
  78. if (!pathInfo.IsNullOrEmpty())
  79. {
  80. string filePath = localPath.Substring(0, localPath.Length - pathInfo.Length);
  81. string queryString = context.Request.Url.Query;
  82. if (queryString.StartsWith("?"))
  83. {
  84. queryString = queryString.Substring(1);
  85. }
  86. context.RewritePath(filePath, pathInfo, queryString);
  87. }
  88. // Disabling ASP.NET cache if there's a logged-in user
  89. if (Composite.C1Console.Security.UserValidationFacade.IsLoggedIn())
  90. {
  91. context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
  92. }
  93. if (_handlerType != null)
  94. {
  95. return (IHttpHandler)Activator.CreateInstance(_handlerType);
  96. }
  97. return (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath("~/Renderers/Page.aspx", typeof(Page));
  98. }
  99. }
  100. }