Skip to content

Instantly share code, notes, and snippets.

@abhi911kumar
Created November 5, 2019 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhi911kumar/5afa0d945dcb23e65e115b5f8c271f6d to your computer and use it in GitHub Desktop.
Save abhi911kumar/5afa0d945dcb23e65e115b5f8c271f6d to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Xml;
using System.Xml.Linq;
namespace TestTemplate.Helper.Filter
{
public class IncompatibleBrowserCheckFilter : ActionFilterAttribute
{
/// <summary>
/// Method to Check if Client Browser is Supported for every request made by every client browser
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
try
{
//Check if we have already detected incompatible browser
if (!CheckIfIncompatibleBorwserAlreadyDetected(filterContext))
{
//Declaring the Compatible Browsers List
List<string> compatibleBrowsers = new List<string>();
//Initialize the Data Structure to know the Browsers and the versions
Dictionary<string, double> browserWithVersionDictionary = new Dictionary<string, double>();
//Get the Browsers Supported by our Platform
compatibleBrowsers = GetCompatibleBrowsers(browserWithVersionDictionary);
//Check the Browser of the Client
var clientBrowser = GetClientBrowser(filterContext);
//Validate the Client Browser from the List of Browsers Supported
if (!CheckIfClientBrowserIsSupported(clientBrowser, compatibleBrowsers, browserWithVersionDictionary))
{
//Redirecting to URL to Error Page of Browser Compatibility
filterContext.Result = RedirectToDefaultErrorPageForBrowserCompatibility(filterContext);
}
}
}
catch (UnableToGetBrowsersSupportedException)
{
//Redirecting to Server Error Page
filterContext.Result = RedirectToDefaultErrorPageForServerError(filterContext);
}
catch (UnableToGetClientBrowserException)
{
//Redirecting to Server Error Page
filterContext.Result = RedirectToDefaultErrorPageForServerError(filterContext);
}
catch (UnableToLoadBrowserConfigException)
{
//Redirecting to Server Error Page
filterContext.Result = RedirectToDefaultErrorPageForServerError(filterContext);
}
catch (UnableToReadBrowserConfigFileException)
{
//Redirecting to Server Error Page
filterContext.Result = RedirectToDefaultErrorPageForServerError(filterContext);
}
catch (Exception)
{
//Redirecting to Server Error Page
filterContext.Result = RedirectToDefaultErrorPageForServerError(filterContext);
}
finally
{
base.OnActionExecuting(filterContext);
}
}
/// <summary>
/// /// Method to Change the ActionResult and redirect the client to Server Error page
/// </summary>
/// <param name="filterContext"></param>
/// <returns></returns>
private ActionResult RedirectToDefaultErrorPageForServerError(ActionExecutingContext filterContext)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "ErrorPage" },
{ "action", "Error500"}
});
return filterContext.Result;
}
/// <summary>
/// Method to Change the ActionResult and redirect the client to BrowserNotSupported Error page
/// </summary>
private ActionResult RedirectToDefaultErrorPageForBrowserCompatibility(ActionExecutingContext filterContext)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "ErrorPage" },
{ "action", "IncompatibleBrowser"}
});
return filterContext.Result;
}
/// <summary>
/// Method to Check if Client Browser is Supported from the List of Browsers we have defined
/// </summary>
/// <returns></returns>
protected bool CheckIfClientBrowserIsSupported(HttpBrowserCapabilitiesBase clientBrowser, List<string> compatibleBrowsers, Dictionary<string, double> browserWithVersionDictionary)
{
var isClientBrowserSupported = false;
//Check if Browser Family and Version are supported
if (CheckIfClientBrowserFamilyIsSupported(clientBrowser, compatibleBrowsers))
{
if (CheckIfClientBrowserVersionIsSupported(clientBrowser, compatibleBrowsers, browserWithVersionDictionary))
{
isClientBrowserSupported = true;
}
}
return isClientBrowserSupported;
}
/// <summary>
/// Method to check if the Client Browser Family Version is Supported by our Platform
/// </summary>
/// <param name="clientBrowser"></param>
/// <param name="compatibleBrowsers"></param>
/// <returns></returns>
private bool CheckIfClientBrowserVersionIsSupported(HttpBrowserCapabilitiesBase clientBrowser, List<string> compatibleBrowsers, Dictionary<string, double> browserWithVersionDictionary)
{
var isClientBrowserFamilyVersionSupported = true;
//Write the Logic to check if the Bowser Version is Supported
if (Convert.ToDouble(clientBrowser.Version) < browserWithVersionDictionary[clientBrowser.Browser.Trim().ToString()])
{
isClientBrowserFamilyVersionSupported = false;
}
return isClientBrowserFamilyVersionSupported;
}
/// <summary>
/// Method to check if the Client Browser Family is Supported by our Platform
/// </summary>
/// <param name="clientBrowser"></param>
/// <param name="compatibleBrowsers"></param>
/// <returns></returns>
private bool CheckIfClientBrowserFamilyIsSupported(HttpBrowserCapabilitiesBase clientBrowser, List<string> compatibleBrowsers)
{
var isClientBrowserFamilySupported = false;
if (compatibleBrowsers.Contains(clientBrowser.Browser.Trim()))
{
isClientBrowserFamilySupported = true;
}
return isClientBrowserFamilySupported;
}
/// <summary>
/// Method to check if Browser Compatibility has already been detected
/// </summary>
/// <param name="filterContext"></param>
/// <returns></returns>
protected bool CheckIfIncompatibleBorwserAlreadyDetected(ActionExecutingContext filterContext)
{
var incompatibleBrowserAlreadyDetected = true;
if (filterContext.ActionDescriptor.ActionName.ToLower() != "incompatiblebrowser")
{
incompatibleBrowserAlreadyDetected = false;
}
return incompatibleBrowserAlreadyDetected;
}
/// <summary>
/// Method to get Client's Browser from the Request
/// </summary>
/// <param name="filterContext"></param>
/// <returns></returns>
protected HttpBrowserCapabilitiesBase GetClientBrowser(ActionExecutingContext filterContext)
{
HttpBrowserCapabilitiesBase clientBrowser = null;
try
{
clientBrowser = filterContext.RequestContext.HttpContext.Request.Browser;
return clientBrowser;
}
catch (Exception)
{
throw new UnableToGetClientBrowserException();
}
}
/// <summary>
/// Method to get the List of Compatible Browsers
/// </summary>
/// <returns></returns>
protected List<string> GetCompatibleBrowsers(Dictionary<string, double> browserWithVersionDictionary)
{
//Declaring an XML Document Object to get all the Browsers Compatible for our Application
var xml = new XmlDocument();
List<string> compatibleBrowsers = new List<string>();
try
{
//Read the Configuration File for Browsers Supported
xml = GetXMLDocument();
//Read the nodes for Clients supported
LoadBrowsersListFromXML(xml, compatibleBrowsers, browserWithVersionDictionary);
return compatibleBrowsers;
}
catch (Exception)
{
throw new UnableToGetClientBrowserException();
}
}
/// <summary>
/// Method to load the XML Document from the Configuration File for the Supported Browsers
/// </summary>
/// <returns></returns>
protected XmlDocument GetXMLDocument()
{
var xmlDocument = new XmlDocument();
try
{
//Read from the XML Config File for the list of Supported Browsers
xmlDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/App_Config/ClientsDetail.xml"));
return xmlDocument;
}
catch (Exception)
{
throw new UnableToLoadBrowserConfigException("Sorry Unable to Load Browser Compatibility Configuration File");
}
}
/// <summary>
/// Method to Load Browsers List from the XML Configuration File
/// </summary>
/// <param name="xmlDocument"></param>
/// <param name="compatibleBrowsers"></param>
protected void LoadBrowsersListFromXML(XmlDocument xmlDocument, List<string> compatibleBrowsers, Dictionary<string, double> browserWithVersionDictionary)
{
try
{
//Select the nodes from where you have to get the Browsers List
XmlNodeList resources = xmlDocument.SelectNodes("data/client");
foreach (XmlNode node in resources)
{
compatibleBrowsers.Add(node.Attributes["clientName"].Value);
browserWithVersionDictionary.Add(node.Attributes["clientName"].Value, Double.Parse(node.InnerText));
}
}
catch (Exception)
{
throw new UnableToReadBrowserConfigFileException("Sorry Unable to Read Browser Compatibility Configuration File");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment