35 import kotlin.math.min |
35 import kotlin.math.min |
36 |
36 |
37 typealias MappingMethod = (HttpRequest, DataAccessObject) -> Unit |
37 typealias MappingMethod = (HttpRequest, DataAccessObject) -> Unit |
38 typealias PathParameters = Map<String, String> |
38 typealias PathParameters = Map<String, String> |
39 |
39 |
|
40 sealed interface ValidationResult<T> |
|
41 class ValidationError<T>(val message: String): ValidationResult<T> |
|
42 class ValidatedValue<T>(val result: T): ValidationResult<T> |
|
43 |
40 class HttpRequest( |
44 class HttpRequest( |
41 val request: HttpServletRequest, |
45 val request: HttpServletRequest, |
42 val response: HttpServletResponse, |
46 val response: HttpServletResponse, |
43 val pathParams: PathParameters = emptyMap() |
47 val pathParams: PathParameters = emptyMap() |
44 ) { |
48 ) { |
152 private fun String.withExt(ext: String) = if (endsWith(ext)) this else plus(ext) |
156 private fun String.withExt(ext: String) = if (endsWith(ext)) this else plus(ext) |
153 private fun jspPath(name: String) = Constants.JSP_PATH_PREFIX.plus(name).withExt(".jsp") |
157 private fun jspPath(name: String) = Constants.JSP_PATH_PREFIX.plus(name).withExt(".jsp") |
154 |
158 |
155 fun param(name: String): String? = request.getParameter(name) |
159 fun param(name: String): String? = request.getParameter(name) |
156 fun paramArray(name: String): Array<String> = request.getParameterValues(name) ?: emptyArray() |
160 fun paramArray(name: String): Array<String> = request.getParameterValues(name) ?: emptyArray() |
|
161 |
|
162 fun <T> param(name: String, validator: (String?) -> (ValidationResult<T>), errorMessages: MutableList<String>): T? { |
|
163 return when (val result = validator(param(name))) { |
|
164 is ValidationError -> { |
|
165 errorMessages.add(i18n(result.message)) |
|
166 null |
|
167 } |
|
168 is ValidatedValue -> { |
|
169 result.result |
|
170 } |
|
171 } |
|
172 } |
157 |
173 |
158 private fun forward(jsp: String) { |
174 private fun forward(jsp: String) { |
159 request.getRequestDispatcher(jspPath(jsp)).forward(request, response) |
175 request.getRequestDispatcher(jspPath(jsp)).forward(request, response) |
160 } |
176 } |
161 |
177 |