﻿<%@ WebHandler Language="VB" Class="handler" %>

Imports System
Imports System.Web
Imports DeprogToolKit.PartsManager

Public Class handler : Implements IHttpHandler, IRequiresSessionState


    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim res As System.Web.HttpResponse = context.Response
        Dim req As System.Web.HttpRequest = context.Request

        ''response value
        Dim ret As New Hashtable

        ''response header
        res.ContentType = "text/javascript"
        res.AppendHeader("Access-Control-Allow-Origin", "*")
        res.AppendHeader("Access-Control-Allow-Headers", "x-requested-with")

        Try
            Dim pRoot As String = DeprogToolKit.MapPath("~/_deprog")
            Dim pAjax As String = IO.Path.Combine(pRoot, "ajax")
            Dim pLib As String = IO.Path.Combine(pRoot, "lib")

            Dim sb As New Text.StringBuilder
            sb.Append(Me.GetFileText(IO.Path.Combine(pLib, "common.js")) + Me.SetLF(5))
            sb.Append(Me.GetFileText(IO.Path.Combine(pAjax, "common.js")) + Me.SetLF(5))
            sb.Append("ajax_top = " + Chr(34) + DeprogToolKit.ResolveUrl("~/_deprog/ajax") + Chr(34) + ";   // REWRITE" + Me.SetLF(5))
            sb.Append(Me.GetFileText(IO.Path.Combine(pAjax, "common.db.js")) + Me.SetLF(5))
            sb.Append(Me.GetFileText(IO.Path.Combine(pAjax, "common.db.cl.js")) + Me.SetLF(5))
            If (req.UrlReferrer IsNot Nothing) Then
                Dim ref As String = req.UrlReferrer.LocalPath
                ref = ref.Replace(".html", ".js")
                ref = ref.Replace(".aspx", ".js")
                ref = ref.Replace(".php", ".js")
                ref = DeprogToolKit.MapPath(ref)
                sb.Append(Me.GetFileText(ref))
            End If

            ''return 
            res.Write(sb.ToString())

        Catch ex As Exception
            ''make log
            DeprogToolKit.LogWrite(ex)
            res.Write(ex.ToString())
        End Try

        ''exit
        res.End()

    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

    Private Function GetFileText(ByVal path As String) As String
        Dim ret As New Text.StringBuilder
        If (IO.File.Exists(path)) Then
            Dim fi As New IO.FileInfo(path)
            ret.Append("/************************" + vbLf)
            ret.Append("** " + fi.Name + "  [Write file text]" + vbLf)
            ret.Append("************************/" + vbLf)
            Using fs As IO.FileStream = New IO.FileStream(path, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite)
                Using sr As IO.StreamReader = New IO.StreamReader(fs)
                    ret.Append(sr.ReadToEnd())
                End Using
            End Using
        End If
        Return ret.ToString()
    End Function

    Private Function SetLF(Optional cnt As Integer = 1) As String
        Dim ret As New Text.StringBuilder
        For i As Integer = 1 To cnt
            ret.Append(vbLf)
        Next
        Return ret.ToString()
    End Function


End Class