All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.yoojia.web.BeforeLoggerHandler.kt Maven / Gradle / Ivy

There is a newer version: 3.0.5-alpha
Show newest version
package com.github.yoojia.web

import com.github.yoojia.web.core.Config
import com.github.yoojia.web.core.Context
import com.github.yoojia.web.core.DispatchChain
import com.github.yoojia.web.supports.InternalPriority
import com.github.yoojia.web.supports.Comparator
import org.slf4j.LoggerFactory
import java.util.*

/**
 * @author Yoojia Chen ([email protected])
 * @since 2.a.13
 */
class BeforeLoggerHandler : LoggerModule() {

    companion object {

        private val Logger = LoggerFactory.getLogger(BeforeLoggerHandler::class.java)

        private fun prepareRequestLog(request: Request): String {
            val buff = StringBuilder("\r\n <=== START ===>").append("\r\n")
            addLine("Request-At", FORMATTER.format(Date(request.createTime)), buff)
            addLine("Request-Uri", request.path, buff)
            addLine("Request-Method", request.method, buff)
            // headers
            val headers = request.headers()
            if (headers.isNotEmpty()) {
                buff.append("Headers:").append("\r\n")
                for((name, value) in request.headers()) { addSubLine(name, value, buff) }
            }
            // Cookies
            val cookies = request.cookies()
            if(cookies.isNotEmpty()) {
                buff.append("Cookies:").append("\r\n")
                request.cookies().forEach { cookie -> addSubLine(cookie.name, cookie.value, buff) }
            }
            // params
            val params = request.params()
            if(params.isNotEmpty()) {
                buff.append("Query-Parameters:").append("\r\n")
                for((name, value) in request.params()) { addSubLine(name, value, buff) }
            }
            return buff.toString()
        }

        @JvmStatic val DEFAULT_PRIORITY = InternalPriority.LOGGER_BEFORE

    }

    private val ignores = ArrayList()

    override fun onCreated(context: Context, config: Config) {
        config.getTypedList("uri-ignores").forEach { uri ->
            val path = if(uri.endsWith("/")) "$uri/*" else uri
            Logger.debug("BeforeLogger-URI-Ignore: $path")
            ignores.add(Comparator.createDefine("ALL", path))
        }
    }

    override fun onDestroy() {

    }

    override fun process(request: Request, response: Response, dispatch: DispatchChain) {
        var enabled = true
        ignores.forEach { define ->
            if(request.comparator.isMatchDefine(define)) {
                enabled = false
                return@forEach
            }
        }
        if(enabled) {
            request.putParam(LOGGING_TEXT_NAME, prepareRequestLog(request))
            request.putParam(LOGGING_ENABLED_NAME, true)
        }
        super.process(request, response, dispatch)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy