com.hexagonkt.http.server.servlet.ServletRequestAdapter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http_server_servlet Show documentation
Show all versions of http_server_servlet Show documentation
HTTP server adapter to be used on JEE Web Applications (deployed inside a server).
package com.hexagonkt.http.server.servlet
import com.hexagonkt.http.model.*
import com.hexagonkt.http.parseContentType
import com.hexagonkt.http.parseQueryString
import java.security.cert.X509Certificate
import jakarta.servlet.http.HttpServletRequest
internal abstract class ServletRequestAdapter(req: HttpServletRequest) : HttpRequestPort {
private companion object {
const val CERTIFICATE_ATTRIBUTE = "jakarta.servlet.request.X509Certificate"
}
@Suppress("UNCHECKED_CAST")
override val certificateChain: List by lazy {
(req.getAttribute(CERTIFICATE_ATTRIBUTE) as? Array)
?.toList()
?: emptyList()
}
override val accept: List by lazy {
req.getHeaders("accept")
?.toList()
?.flatMap { it.split(",") }
?.map { parseContentType(it) }
?: emptyList()
}
override val contentLength: Long by lazy { req.contentLength.toLong() }
override val queryParameters: QueryParameters by lazy {
parseQueryString(req.queryString ?: "")
}
override val method: HttpMethod by lazy {
HttpMethod.valueOf(req.method)
}
override val protocol: HttpProtocol by lazy { HttpProtocol.valueOf(req.scheme.uppercase()) }
override val host: String by lazy { req.remoteHost }
override val port: Int by lazy { req.serverPort }
override val path: String by lazy { req.servletPath.ifEmpty { req.pathInfo } }
override val authorization: Authorization? by lazy { authorization() }
override val cookies: List by lazy {
req.cookies
?.map {
Cookie(
it.name,
it.value,
it.maxAge.toLong(),
it.secure,
it.path ?: "/",
it.isHttpOnly,
it.domain,
)
}
?: emptyList()
}
override val headers: Headers by lazy {
Headers(
req.headerNames
.toList()
.map { it.lowercase() }
.map { Header(it, req.getHeaders(it).toList()) }
)
}
override val contentType: ContentType? by lazy {
req.contentType?.let { parseContentType(it) }
}
}