src/main/kotlin/de/uapcore/lightpit/AbstractServlet.kt

Sat, 06 Jan 2024 20:31:14 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 06 Jan 2024 20:31:14 +0100
changeset 298
1275eb652008
parent 254
55ca6cafc3dd
permissions
-rw-r--r--

add language selection cookie - fixes #352

universe@184 1 /*
universe@184 2 * Copyright 2021 Mike Becker. All rights reserved.
universe@184 3 *
universe@184 4 * Redistribution and use in source and binary forms, with or without
universe@184 5 * modification, are permitted provided that the following conditions are met:
universe@184 6 *
universe@184 7 * 1. Redistributions of source code must retain the above copyright
universe@184 8 * notice, this list of conditions and the following disclaimer.
universe@184 9 *
universe@184 10 * 2. Redistributions in binary form must reproduce the above copyright
universe@184 11 * notice, this list of conditions and the following disclaimer in the
universe@184 12 * documentation and/or other materials provided with the distribution.
universe@184 13 *
universe@184 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@184 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@184 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
universe@184 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
universe@184 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
universe@184 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
universe@184 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
universe@184 21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
universe@184 22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
universe@184 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
universe@184 24 */
universe@184 25
universe@184 26 package de.uapcore.lightpit
universe@184 27
universe@184 28 import de.uapcore.lightpit.DataSourceProvider.Companion.SC_ATTR_NAME
universe@184 29 import de.uapcore.lightpit.dao.DataAccessObject
universe@184 30 import de.uapcore.lightpit.dao.createDataAccessObject
universe@298 31 import jakarta.servlet.http.Cookie
universe@254 32 import jakarta.servlet.http.HttpServlet
universe@254 33 import jakarta.servlet.http.HttpServletRequest
universe@254 34 import jakarta.servlet.http.HttpServletResponse
universe@184 35 import java.sql.SQLException
universe@184 36 import java.util.*
universe@184 37
universe@247 38 abstract class AbstractServlet : HttpServlet() {
universe@298 39
universe@298 40 companion object {
universe@298 41 const val LANGUAGE_COOKIE_NAME = "lpit_language"
universe@298 42 }
universe@247 43
universe@247 44 protected val logger = MyLogger()
universe@184 45
universe@184 46 /**
universe@184 47 * Contains the GET request mappings.
universe@184 48 */
universe@184 49 private val getMappings = mutableMapOf<PathPattern, MappingMethod>()
universe@184 50
universe@184 51 /**
universe@184 52 * Contains the POST request mappings.
universe@184 53 */
universe@184 54 private val postMappings = mutableMapOf<PathPattern, MappingMethod>()
universe@184 55
universe@184 56 protected fun get(pattern: String, method: MappingMethod) {
universe@184 57 getMappings[PathPattern(pattern)] = method
universe@184 58 }
universe@184 59
universe@184 60 protected fun post(pattern: String, method: MappingMethod) {
universe@184 61 postMappings[PathPattern(pattern)] = method
universe@184 62 }
universe@184 63
universe@184 64 private fun notFound(http: HttpRequest, dao: DataAccessObject) {
universe@184 65 http.response.sendError(HttpServletResponse.SC_NOT_FOUND)
universe@184 66 }
universe@184 67
universe@184 68 private fun findMapping(
universe@184 69 mappings: Map<PathPattern, MappingMethod>,
universe@184 70 req: HttpServletRequest
universe@184 71 ): Pair<PathPattern, MappingMethod> {
universe@184 72 val requestPath = sanitizedRequestPath(req)
universe@184 73 val candidates = mappings.filter { it.key.matches(requestPath) }
universe@184 74 return if (candidates.isEmpty()) {
universe@184 75 Pair(PathPattern(requestPath), ::notFound)
universe@184 76 } else {
universe@184 77 if (candidates.size > 1) {
universe@247 78 logger.warn("Ambiguous mapping for request path '{0}'", requestPath)
universe@184 79 }
universe@184 80 candidates.entries.first().toPair()
universe@184 81 }
universe@184 82 }
universe@184 83
universe@184 84 private fun invokeMapping(
universe@184 85 mapping: Pair<PathPattern, MappingMethod>,
universe@184 86 req: HttpServletRequest,
universe@184 87 resp: HttpServletResponse,
universe@184 88 dao: DataAccessObject
universe@184 89 ) {
universe@184 90 val params = mapping.first.obtainPathParameters(sanitizedRequestPath(req))
universe@184 91 val method = mapping.second
universe@247 92 logger.trace("invoke {0}", method)
universe@184 93 method(HttpRequest(req, resp, params), dao)
universe@184 94 }
universe@184 95
universe@184 96 private fun sanitizedRequestPath(req: HttpServletRequest) = req.pathInfo ?: "/"
universe@184 97
universe@184 98 private fun doProcess(
universe@184 99 req: HttpServletRequest,
universe@184 100 resp: HttpServletResponse,
universe@184 101 mappings: Map<PathPattern, MappingMethod>
universe@184 102 ) {
universe@184 103 val session = req.session
universe@184 104
universe@184 105 // the very first thing to do is to force UTF-8
universe@184 106 req.characterEncoding = "UTF-8"
universe@184 107
universe@184 108 // set some internal request attributes
universe@184 109 val http = HttpRequest(req, resp)
universe@184 110 val fullPath = req.servletPath + Optional.ofNullable(req.pathInfo).orElse("")
universe@184 111 req.setAttribute(Constants.REQ_ATTR_BASE_HREF, http.baseHref)
universe@184 112 req.setAttribute(Constants.REQ_ATTR_PATH, fullPath)
universe@184 113 req.getHeader("Referer")?.let {
universe@184 114 // TODO: add a sanity check to avoid link injection
universe@184 115 req.setAttribute(Constants.REQ_ATTR_REFERER, it)
universe@184 116 }
universe@184 117
universe@298 118 // choose the requested language as session language (if available)
universe@298 119 if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) {
universe@298 120 // language selection stored in cookie
universe@298 121 val cookieLocale = cookieLanguage(http)
universe@298 122
universe@298 123 // if no cookie, fall back to request locale a.k.a "Browser Language"
universe@298 124 val reqLocale = cookieLocale ?: req.locale
universe@298 125
universe@298 126 val availableLanguages = availableLanguages()
universe@298 127 val sessionLocale = if (availableLanguages.contains(reqLocale)) reqLocale else availableLanguages.first()
universe@298 128
universe@298 129 // select the language (this will also refresh the cookie max-age)
universe@298 130 selectLanguage(http, sessionLocale)
universe@298 131
universe@298 132 logger.debug(
universe@298 133 "Setting language for new session {0}: {1}", session.id, sessionLocale.displayLanguage
universe@298 134 )
universe@298 135 } else {
universe@298 136 val sessionLocale = session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) as Locale
universe@298 137 resp.locale = sessionLocale
universe@298 138 logger.trace("Continuing session {0} with language {1}", session.id, sessionLocale)
universe@298 139 }
universe@298 140
universe@184 141 // if this is an error path, bypass the normal flow
universe@184 142 if (fullPath.startsWith("/error/")) {
universe@184 143 http.styleSheets = listOf("error")
universe@184 144 http.render("error")
universe@184 145 return
universe@184 146 }
universe@184 147
universe@184 148 // obtain a connection and create the data access objects
universe@184 149 val dsp = req.servletContext.getAttribute(SC_ATTR_NAME) as DataSourceProvider
universe@184 150 val dialect = dsp.dialect
universe@184 151 val ds = dsp.dataSource
universe@184 152 if (ds == null) {
universe@184 153 resp.sendError(
universe@184 154 HttpServletResponse.SC_SERVICE_UNAVAILABLE,
universe@184 155 "JNDI DataSource lookup failed. See log for details."
universe@184 156 )
universe@184 157 return
universe@184 158 }
universe@184 159 try {
universe@184 160 ds.connection.use { connection ->
universe@184 161 val dao = createDataAccessObject(dialect, connection)
universe@184 162 try {
universe@184 163 connection.autoCommit = false
universe@184 164 invokeMapping(findMapping(mappings, req), req, resp, dao)
universe@184 165 connection.commit()
universe@184 166 } catch (ex: SQLException) {
universe@247 167 logger.warn("Database transaction failed (Code {0}): {1}", ex.errorCode, ex.message)
universe@247 168 logger.debug("Details: ", ex)
universe@184 169 resp.sendError(
universe@184 170 HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
universe@184 171 "Unhandled Transaction Error - Code: " + ex.errorCode
universe@184 172 )
universe@184 173 connection.rollback()
universe@184 174 }
universe@184 175 }
universe@184 176 } catch (ex: SQLException) {
universe@247 177 logger.error("Severe Database Exception (Code {0}): {1}", ex.errorCode, ex.message)
universe@247 178 logger.debug("Details: ", ex)
universe@184 179 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Database Error - Code: " + ex.errorCode)
universe@184 180 }
universe@184 181 }
universe@184 182
universe@184 183 override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
universe@184 184 doProcess(req, resp, getMappings)
universe@184 185 }
universe@184 186
universe@184 187 override fun doPost(req: HttpServletRequest, resp: HttpServletResponse) {
universe@184 188 doProcess(req, resp, postMappings)
universe@184 189 }
universe@184 190
universe@184 191 protected fun availableLanguages(): List<Locale> {
universe@184 192 val langTags = servletContext.getInitParameter(Constants.CTX_ATTR_LANGUAGES)?.split(",")?.map(String::trim) ?: emptyList()
universe@184 193 val locales = langTags.map(Locale::forLanguageTag).filter { it.language.isNotEmpty() }
universe@208 194 return locales.ifEmpty { listOf(Locale.ENGLISH) }
universe@184 195 }
universe@184 196
universe@298 197 private fun cookieLanguage(http: HttpRequest): Locale? =
universe@298 198 http.request.cookies?.firstOrNull { c -> c.name == LANGUAGE_COOKIE_NAME }
universe@298 199 ?.runCatching {Locale.forLanguageTag(this.value)}?.getOrNull()
universe@298 200
universe@298 201 protected fun selectLanguage(http: HttpRequest, locale: Locale) {
universe@298 202 http.response.locale = locale
universe@298 203 http.session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, locale)
universe@298 204 // delete cookie if language selection matches request locale, otherwise set cookie
universe@298 205 val cookie = Cookie(LANGUAGE_COOKIE_NAME, "")
universe@298 206 cookie.isHttpOnly = true
universe@298 207 cookie.path = http.request.contextPath
universe@298 208 if (http.request.locale.language == locale.language) {
universe@298 209 cookie.maxAge = 0
universe@298 210 } else {
universe@298 211 cookie.value = locale.language
universe@298 212 cookie.maxAge = 2592000 // 30 days
universe@298 213 }
universe@298 214 http.response.addCookie(cookie)
universe@298 215 }
universe@184 216 }

mercurial