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

cn.bestwu.logging.TraceHttpServletRequestWrapper.kt Maven / Gradle / Ivy

There is a newer version: 2.0.11
Show newest version
package cn.bestwu.logging

import org.springframework.util.StreamUtils
import java.io.BufferedReader
import java.io.ByteArrayOutputStream
import java.io.TraceBufferedReader
import javax.servlet.ReadListener
import javax.servlet.ServletInputStream
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletRequestWrapper

/**
 * @author Peter Wu
 * @since 0.0.1
 */
internal class TraceHttpServletRequestWrapper

/**
 * Constructs a request object wrapping the given request.
 *
 * @param request The request to wrap
 * @throws IllegalArgumentException if the request is null
 */
constructor(request: HttpServletRequest) : HttpServletRequestWrapper(request) {

    private val byteArrayOutputStream = ByteArrayOutputStream()

    val requestBody: ByteArray
        get() = if (isFinished()) byteArrayOutputStream.toByteArray() else try {
            StreamUtils.copyToByteArray(request.inputStream)
        } catch (e: Exception) {
            ByteArray(0)
        }

    private fun isFinished(): Boolean {
        try {
            return request.inputStream.isFinished
        } catch (e: java.lang.AbstractMethodError) {
            return byteArrayOutputStream.size() != 0
        }
    }

    override fun getInputStream(): ServletInputStream {
        return TraceServletInputStream(super.getInputStream())
    }

    override fun getReader(): BufferedReader {
        return TraceBufferedReader(super.getReader(), byteArrayOutputStream)
    }

    override fun getCharacterEncoding(): String {
        return super.getCharacterEncoding() ?: "UTF-8"
    }

    internal inner class TraceServletInputStream(private val delegate: ServletInputStream) : ServletInputStream() {

        override fun equals(other: Any?): Boolean {
            return delegate.equals(other)
        }

        override fun hashCode(): Int {
            return delegate.hashCode()
        }

        override fun toString(): String {
            return delegate.toString()
        }

        override fun skip(n: Long): Long {
            return delegate.skip(n)
        }

        override fun readLine(b: ByteArray?, off: Int, len: Int): Int {
            val readLine = delegate.readLine(b, off, len)
            if (readLine != -1) {
                byteArrayOutputStream.write(b, off, readLine)
                byteArrayOutputStream.write("\n".toByteArray())
            }
            return readLine
        }

        override fun isReady(): Boolean {
            return delegate.isReady
        }

        override fun available(): Int {
            return delegate.available()
        }

        override fun isFinished(): Boolean {
            return delegate.isFinished
        }

        override fun reset() {
            delegate.reset()
        }

        override fun close() {
            delegate.close()
        }

        override fun mark(readlimit: Int) {
            delegate.mark(readlimit)
        }

        override fun markSupported(): Boolean {
            return delegate.markSupported()
        }

        override fun read(): Int {
            val read = delegate.read()
            byteArrayOutputStream.write(read)
            return read
        }

        override fun read(b: ByteArray?): Int {
            val read = delegate.read(b)
            byteArrayOutputStream.write(b)
            return read
        }

        override fun read(b: ByteArray?, off: Int, len: Int): Int {
            val read = delegate.read(b, off, len)
            if (read != -1)
                byteArrayOutputStream.write(b, off, read)
            return read
        }

        override fun setReadListener(listener: ReadListener?) {
            delegate.setReadListener(listener)
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy