Imports system.web
Imports system.Xml
Imports System.Collections.Specialized
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Enumerations
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices
Imports CharterDotCom.CommonEntities.Library.EntityEBSServices
Imports CharterDotCom.CommonEntities.Library.EntitySupport
Imports CharterDotCom.CommonEntities.Library.EntitySearch
Imports CharterDotCom.CommonEntities.Library.EntityWebConfig
Imports CharterDotCom.CommonEntities.Library.EntityG2BServices

Public Class RedirectModule
    Implements IHttpModule

    Const OLDURL As String = "currentURL"
    Const NEWURL As String = "activeURL"
    Const PAGE As String = "page"
    Const REDIRECTS As String = "Redirects"
    Const CHARTERHOST As String = "www.Charter.com"

    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

    End Sub

    Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
        AddHandler context.BeginRequest, New EventHandler(AddressOf Me.OnBeginRequest)
        ' AddHandler context.EndRequest, New EventHandler(AddressOf Me.OnEndRequest)
    End Sub

    Public Sub OnBeginRequest(ByVal o As Object, ByVal ea As EventArgs)
        Dim httpApp As System.Web.HttpApplication = CType(o, HttpApplication)
        Dim context As HttpContext = HttpContext.Current
        Try
            If Not context Is Nothing Then
                Dim currentURL As String
                Dim url As String = context.Request.Url.ToString
                If url.ToLower.Contains(".aspx") OrElse url.ToLower.Contains(".asp") OrElse url.ToLower.Contains(".html") Then

                    Dim redirects As StringDictionary
                    If Not context.Application("redirects") Is Nothing Then

                        redirects = DirectCast(context.Application("redirects"), StringDictionary)
                        currentURL = context.Request.Url.LocalPath.ToString()
                        If redirects.ContainsKey(currentURL) Then
                            Redirect301(redirects(currentURL), True)
                        End If

                    End If
                End If
            End If
        Catch ex As Exception
            Common.LogException(ex, ExceptionSeverity.High)
        End Try
    End Sub

    Public Sub OnEndRequest(ByVal o As Object, ByVal ea As EventArgs)

    End Sub


    Private Function RedirectURL(ByVal currentURL As String, ByVal page As String) As String

        Dim doc As New XmlDocument
        doc.LoadXml(CType(HttpContext.Current.Application("Redirects"), String))
        Dim redirectsection As XmlNode = doc.DocumentElement
        If Not redirectsection Is Nothing Then

            For Each node As XmlNode In redirectsection.ChildNodes
                If String.Compare(node.Attributes(OLDURL).Value, currentURL, True) = 0 Then
                    page = node.Attributes(page).Value
                    Return node.Attributes(NEWURL).Value
                End If
            Next

        End If
        Return Nothing

    End Function

    Private Sub Redirect301(ByVal url As String, ByVal endResponse As Boolean)

        Dim context As System.Web.HttpContext = System.Web.HttpContext.Current

        If Not context Is Nothing Then

            If Not url.Equals(String.Empty) Then

                context.Response.Status = "301 Moved Permanently"
                context.Response.AddHeader("Location", url)

                If endResponse Then
                    context.Response.End()
                End If
            End If

        End If

    End Sub

End Class
