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

com.hexagonkt.http.server.servlet.ServletRequestAdapterSync.kt Maven / Gradle / Ivy

Go to download

HTTP server adapter to be used on JEE Web Applications (deployed inside a server).

The newest version!
package com.hexagonkt.http.server.servlet

import com.hexagonkt.http.model.*
import com.hexagonkt.http.parseContentType
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.Part
import java.security.cert.X509Certificate
import kotlin.UnsupportedOperationException

internal class ServletRequestAdapterSync(req: HttpServletRequest) : ServletRequestAdapter(req) {

    private val parameters: Map> by lazy {
        req.parameterMap.map { it.key as String to it.value.toList() }.toMap()
    }

    override val parts: List by lazy {
        req.parts.map { servletPartAdapter(it) }
    }

    override val formParameters: FormParameters by lazy {
        val fields = parameters
            .filter { it.key !in queryParameters.httpFields.keys }
            .map { (k, v) -> FormParameter(k, v) }

        FormParameters(fields)
    }

    override fun with(
        body: Any,
        headers: Headers,
        contentType: ContentType?,
        method: HttpMethod,
        protocol: HttpProtocol,
        host: String,
        port: Int,
        path: String,
        queryParameters: QueryParameters,
        parts: List,
        formParameters: FormParameters,
        cookies: List,
        accept: List,
        authorization: Authorization?,
        certificateChain: List,
    ): HttpRequestPort =
        throw UnsupportedOperationException()

    override val body: Any by lazy {
        req.inputStream.readAllBytes()
    }

    private fun servletPartAdapter(part: Part) : HttpPart {
        val headerNames = part.headerNames.filterNotNull()
        return HttpPart(
            name = part.name,
            body = part.inputStream.readAllBytes(),
            headers = Headers(headerNames.map { Header(it, part.getHeaders(it).toList()) }),
            contentType = part.contentType?.let { parseContentType(it) },
            size = part.size,
            submittedFileName = part.submittedFileName,
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy