Imports Microsoft.VisualBasic
Imports System.Xml

Public Class RedirectHandler
    Implements IHttpHandler

    Const REDIRECTS As String = "Redirects"
    Const CHARTERHOST As String = "www.charter.com"
    Const CHARTERHOST2 As String = "charter.com"

    Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

    Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest

        If Not context Is Nothing Then
            Dim currentURL As String
            Dim newUrl As String

            Dim url As String = context.Request.Url.ToString
            'If context.Request.Url.Host.ToLower.Equals(CHARTERHOST) OrElse context.Request.Url.Host.ToLower.Equals(CHARTERHOST2) Then
            currentURL = context.Request.Url.LocalPath.ToString().ToLower


            '######################################################
            'PUT THIS LINE IN TO BE ABLE TO RUN REDIRECTS LOCALLY.
            '######################################################
            currentURL = currentURL.Replace("/ui/", "/")
            '######################################################


            newUrl = GetRedirectURL(currentURL, context)
            'Else
            ' newUrl = GetRedirectURLFromHost(context.Request.Url.Host.ToLower)
            'End If
            Redirect301(newUrl, True)
        End If

    End Sub

    Private Function GetRedirectURL(ByVal currentURL As String, ByVal context_http As System.Web.HttpContext) As String

        '#########################################################
        'MODIFICATION   ROD COPELAND    12/22/2009  ISSUE ID 7865
        '#########################################################
        If currentURL.ToLower().Contains("/order/PayYourBill.aspx".ToLower()) Then
            Return "https://www.charter.com/order/login.aspx"
        End If


        If currentURL.Contains("/visitors/") Then
            Select Case currentURL
                Case "/visitors/contactus.aspx"
                    Dim x As String = context_http.Request.Url.ToString().ToLower().Replace("/visitors/", "/customers/")
                    Return x
                Case "/visitors/printarticle.aspx"
                    Dim x As String = context_http.Request.Url.ToString().ToLower().Replace("/visitors/", "/customers/")
                    Return x
                Case "/visitors/searchsupport.aspx"
                    Dim x As String = context_http.Request.Url.ToString().ToLower().Replace("/visitors/", "/customers/")
                    Return x
                Case "/visitors/support.aspx"
                    Dim x As String = context_http.Request.Url.ToString().ToLower().Replace("/visitors/", "/customers/")
                    Return x
            End Select

            '#####################################################################################################
            'ROD COPELAND   MODIFICATION    1-21-2009   CL PROJECT - GRANDFATHER OLD URLS
            '#####################################################################################################
            'FOR REDIRECTS FROM AND TO THE SAME PAGE AS OPPOSED TO REDIRECTING TO DIFFERENT PAGES, 
            'LOOK AT \APP_CODE\CharterCMSPage.vb
            '#####################################################################################################
            Select Case True
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/visitors/nonproducts.aspx?nonproductitem=81".ToLower())
                    Return "../Visitors/Products.aspx?MenuItem=22"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/FrequentQuestions.aspx?ProductItem=1".ToLower())
                    Return "../Customers/Support.aspx"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/NonProducts.aspx?NonProductItem=66".ToLower())
                    Return "../customers/support.aspx?supportarticleid=1660#Cable%20Internet%20Connection%20Support"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/NonProducts.aspx?NonProductItem=68".ToLower())
                    Return "../Visitors/Products.aspx?MenuItem=31"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/FrequentQuestions.aspx?ProductItem=2".ToLower())
                    Return "../Customers/Support.aspx?MenuItem=3"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/Products.aspx?MenuItem=4".ToLower())
                    Return "../Visitors/Products.aspx?MenuItem=4"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/NonProducts.aspx?NonProductItem=81".ToLower())
                    Return "../Visitors/Products.aspx?MenuItem=22"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/NonProducts.aspx?NonProductItem=83&Section=2".ToLower())
                    Return "../Visitors/Products.aspx?MenuItem=24"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/NonProducts.aspx?NonProductItem=83".ToLower())
                    Return "../Visitors/Products.aspx?MenuItem=23"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/FrequentQuestions.aspx?ProductItem=3".ToLower())
                    Return "../Customers/Support.aspx?MenuItem=4"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/NonProducts.aspx?NonProductItem=61".ToLower())
                    Return "../Visitors/Products.aspx?MenuItem=20"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/NonProducts.aspx?NonProductItem=62".ToLower())
                    Return "../Visitors/Products.aspx?MenuItem=30"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/NonProducts.aspx?NonProductItem=63".ToLower())
                    Return "../Visitors/Products.aspx?MenuItem=30"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/NonProducts.aspx?NonProductItem=64".ToLower())
                    Return "../Visitors/Products.aspx?MenuItem=30"
                Case context_http.Request.RawUrl.ToString.ToLower().Contains("/Visitors/NonProducts.aspx?NonProductItem=65".ToLower())
                    Return "../Visitors/Products.aspx?MenuItem=30"
            End Select
            '#####################################################################################################


        End If


    End Function

    Private Function GetRedirectURLFromHost(ByVal host As String)
        Select Case host

            'Case "charterzone.charter.com", "www.charterzone.charter.com"
            '    Return "http://connect.charter.com/Portal/content/Newsletters/2006/November06/WebLink.aspx?action=Preview&user=Anonymous"
            Case "unsubscribe.charter.com", "www.unsubscribe.charter.com"
                Return "http://connect.charter.com/Portal/content/Newsletters/2006/November06/WebLink.aspx?action=Preview&user=Anonymous"
            Case "www.chartersafeschools.com", "chartersafeschools.com"
                Return "visitors/support.aspx?nonproductitem=303&section=4"
            Case "security.charter.com", "www.security.charter.com"
                ' issue 16087
                Return "http://www.charter.com/customers/support.aspx?menuitem=3&tab=tabols"
                'Return "visitors/support.aspx?nonproductitem=303"
            Case "jobs.charter.com", "www.jobs.charter.com"
                Return "http://www.charter.com/Visitors/Careers.aspx?NonProductItem=600"
            Case "www.charterphone.com", "charterphone.com"
                Return "/visitors/products.aspx?menuitem=4"
            Case "support2.charter.com", "www.support2.charter.com"
                Return "/customers/support.aspx"
            Case "charter-al.com", "www.charter-al.com"
                Return "/visitors/products.aspx?menuitem=3"
            Case "support.charter.com", "www.support.charter.com"
                Return "/customers/support.aspx"
                'Case "charterzone.charter.com", "www.charterzone.charter.com"
                '    Return "http://connect.charter.com/Portal/content/Newsletters/2006/November06/WebLink.aspx?action=Preview&user=Anonymous"
            Case "charter.com/ppv"
                Return "http://www.charter.com/Visitors/Products.aspx?ProductItem=8"
            Case "charter.com/telephone"
                Return "http://www.charter.com/Visitors/Products.aspx?MenuItem=4"
            Case "charter.com/ineedcable"
                Return "http://connect.charter.com/landing/Movingineedcable.aspx"
            Case "charter.com/ondemand"
                Return "http://www.charter.com/Visitors/Products.aspx?ProductItem=9"
            Case Else
                Return "/Visitors/Home.aspx"
        End Select
    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
