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

Mon, 05 Aug 2024 19:38:47 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 05 Aug 2024 19:38:47 +0200
changeset 313
7cfe88b8e219
parent 298
1275eb652008
child 335
1eed60b779da
permissions
-rw-r--r--

fix removing filter not working

fixes #407

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

mercurial