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

Wed, 15 Dec 2021 19:56:05 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 15 Dec 2021 19:56:05 +0100
changeset 247
e71ae69c68c0
parent 208
785820da6485
child 254
55ca6cafc3dd
permissions
-rw-r--r--

remove log4j entirely

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@184 31 import java.sql.SQLException
universe@184 32 import java.util.*
universe@184 33 import javax.servlet.http.HttpServlet
universe@184 34 import javax.servlet.http.HttpServletRequest
universe@184 35 import javax.servlet.http.HttpServletResponse
universe@184 36
universe@247 37 abstract class AbstractServlet : HttpServlet() {
universe@247 38
universe@247 39 protected val logger = MyLogger()
universe@184 40
universe@184 41 /**
universe@184 42 * Contains the GET request mappings.
universe@184 43 */
universe@184 44 private val getMappings = mutableMapOf<PathPattern, MappingMethod>()
universe@184 45
universe@184 46 /**
universe@184 47 * Contains the POST request mappings.
universe@184 48 */
universe@184 49 private val postMappings = mutableMapOf<PathPattern, MappingMethod>()
universe@184 50
universe@184 51 protected fun get(pattern: String, method: MappingMethod) {
universe@184 52 getMappings[PathPattern(pattern)] = method
universe@184 53 }
universe@184 54
universe@184 55 protected fun post(pattern: String, method: MappingMethod) {
universe@184 56 postMappings[PathPattern(pattern)] = method
universe@184 57 }
universe@184 58
universe@184 59 private fun notFound(http: HttpRequest, dao: DataAccessObject) {
universe@184 60 http.response.sendError(HttpServletResponse.SC_NOT_FOUND)
universe@184 61 }
universe@184 62
universe@184 63 private fun findMapping(
universe@184 64 mappings: Map<PathPattern, MappingMethod>,
universe@184 65 req: HttpServletRequest
universe@184 66 ): Pair<PathPattern, MappingMethod> {
universe@184 67 val requestPath = sanitizedRequestPath(req)
universe@184 68 val candidates = mappings.filter { it.key.matches(requestPath) }
universe@184 69 return if (candidates.isEmpty()) {
universe@184 70 Pair(PathPattern(requestPath), ::notFound)
universe@184 71 } else {
universe@184 72 if (candidates.size > 1) {
universe@247 73 logger.warn("Ambiguous mapping for request path '{0}'", requestPath)
universe@184 74 }
universe@184 75 candidates.entries.first().toPair()
universe@184 76 }
universe@184 77 }
universe@184 78
universe@184 79 private fun invokeMapping(
universe@184 80 mapping: Pair<PathPattern, MappingMethod>,
universe@184 81 req: HttpServletRequest,
universe@184 82 resp: HttpServletResponse,
universe@184 83 dao: DataAccessObject
universe@184 84 ) {
universe@184 85 val params = mapping.first.obtainPathParameters(sanitizedRequestPath(req))
universe@184 86 val method = mapping.second
universe@247 87 logger.trace("invoke {0}", method)
universe@184 88 method(HttpRequest(req, resp, params), dao)
universe@184 89 }
universe@184 90
universe@184 91 private fun sanitizedRequestPath(req: HttpServletRequest) = req.pathInfo ?: "/"
universe@184 92
universe@184 93 private fun doProcess(
universe@184 94 req: HttpServletRequest,
universe@184 95 resp: HttpServletResponse,
universe@184 96 mappings: Map<PathPattern, MappingMethod>
universe@184 97 ) {
universe@184 98 val session = req.session
universe@184 99
universe@184 100 // the very first thing to do is to force UTF-8
universe@184 101 req.characterEncoding = "UTF-8"
universe@184 102
universe@184 103 // choose the requested language as session language (if available) or fall back to english, otherwise
universe@184 104 if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) {
universe@184 105 val availableLanguages = availableLanguages()
universe@184 106 val reqLocale = req.locale
universe@184 107 val sessionLocale = if (availableLanguages.contains(reqLocale)) reqLocale else availableLanguages.first()
universe@184 108 session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, sessionLocale)
universe@208 109 resp.locale = sessionLocale
universe@247 110 logger.debug(
universe@247 111 "Setting language for new session {0}: {1}", session.id, sessionLocale.displayLanguage
universe@184 112 )
universe@184 113 } else {
universe@184 114 val sessionLocale = session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) as Locale
universe@184 115 resp.locale = sessionLocale
universe@247 116 logger.trace("Continuing session {0} with language {1}", session.id, sessionLocale)
universe@184 117 }
universe@184 118
universe@184 119 // set some internal request attributes
universe@184 120 val http = HttpRequest(req, resp)
universe@184 121 val fullPath = req.servletPath + Optional.ofNullable(req.pathInfo).orElse("")
universe@184 122 req.setAttribute(Constants.REQ_ATTR_BASE_HREF, http.baseHref)
universe@184 123 req.setAttribute(Constants.REQ_ATTR_PATH, fullPath)
universe@184 124 req.getHeader("Referer")?.let {
universe@184 125 // TODO: add a sanity check to avoid link injection
universe@184 126 req.setAttribute(Constants.REQ_ATTR_REFERER, it)
universe@184 127 }
universe@184 128
universe@184 129 // if this is an error path, bypass the normal flow
universe@184 130 if (fullPath.startsWith("/error/")) {
universe@184 131 http.styleSheets = listOf("error")
universe@184 132 http.render("error")
universe@184 133 return
universe@184 134 }
universe@184 135
universe@184 136 // obtain a connection and create the data access objects
universe@184 137 val dsp = req.servletContext.getAttribute(SC_ATTR_NAME) as DataSourceProvider
universe@184 138 val dialect = dsp.dialect
universe@184 139 val ds = dsp.dataSource
universe@184 140 if (ds == null) {
universe@184 141 resp.sendError(
universe@184 142 HttpServletResponse.SC_SERVICE_UNAVAILABLE,
universe@184 143 "JNDI DataSource lookup failed. See log for details."
universe@184 144 )
universe@184 145 return
universe@184 146 }
universe@184 147 try {
universe@184 148 ds.connection.use { connection ->
universe@184 149 val dao = createDataAccessObject(dialect, connection)
universe@184 150 try {
universe@184 151 connection.autoCommit = false
universe@184 152 invokeMapping(findMapping(mappings, req), req, resp, dao)
universe@184 153 connection.commit()
universe@184 154 } catch (ex: SQLException) {
universe@247 155 logger.warn("Database transaction failed (Code {0}): {1}", ex.errorCode, ex.message)
universe@247 156 logger.debug("Details: ", ex)
universe@184 157 resp.sendError(
universe@184 158 HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
universe@184 159 "Unhandled Transaction Error - Code: " + ex.errorCode
universe@184 160 )
universe@184 161 connection.rollback()
universe@184 162 }
universe@184 163 }
universe@184 164 } catch (ex: SQLException) {
universe@247 165 logger.error("Severe Database Exception (Code {0}): {1}", ex.errorCode, ex.message)
universe@247 166 logger.debug("Details: ", ex)
universe@184 167 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Database Error - Code: " + ex.errorCode)
universe@184 168 }
universe@184 169 }
universe@184 170
universe@184 171 override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
universe@184 172 doProcess(req, resp, getMappings)
universe@184 173 }
universe@184 174
universe@184 175 override fun doPost(req: HttpServletRequest, resp: HttpServletResponse) {
universe@184 176 doProcess(req, resp, postMappings)
universe@184 177 }
universe@184 178
universe@184 179 protected fun availableLanguages(): List<Locale> {
universe@184 180 val langTags = servletContext.getInitParameter(Constants.CTX_ATTR_LANGUAGES)?.split(",")?.map(String::trim) ?: emptyList()
universe@184 181 val locales = langTags.map(Locale::forLanguageTag).filter { it.language.isNotEmpty() }
universe@208 182 return locales.ifEmpty { listOf(Locale.ENGLISH) }
universe@184 183 }
universe@184 184
universe@184 185 }

mercurial