src/main/java/de/uapcore/lightpit/AbstractServlet.java

Wed, 06 Jan 2021 16:41:09 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 06 Jan 2021 16:41:09 +0100
changeset 182
53f0e2685ad5
parent 180
009700915269
permissions
-rw-r--r--

single-use constant field can be inlined

7
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
3 *
180
009700915269 merge resource bundles
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
4 * Copyright 2021 Mike Becker. All rights reserved.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
5 *
7
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
27 *
7
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 package de.uapcore.lightpit;
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
167
3f30adba1c63 major refactoring of DAO architecture - also fixes #114
Mike Becker <universe@uap-core.de>
parents: 163
diff changeset
31 import de.uapcore.lightpit.dao.DataAccessObject;
3f30adba1c63 major refactoring of DAO architecture - also fixes #114
Mike Becker <universe@uap-core.de>
parents: 163
diff changeset
32 import de.uapcore.lightpit.dao.PostgresDataAccessObject;
33
fd8c40ff78c3 fixes several warnings
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
33 import org.slf4j.Logger;
fd8c40ff78c3 fixes several warnings
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
34 import org.slf4j.LoggerFactory;
fd8c40ff78c3 fixes several warnings
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
35
7
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 import javax.servlet.ServletException;
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 import javax.servlet.http.HttpServlet;
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 import javax.servlet.http.HttpServletRequest;
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 import javax.servlet.http.HttpServletResponse;
13
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
40 import javax.servlet.http.HttpSession;
33
fd8c40ff78c3 fixes several warnings
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
41 import java.io.IOException;
83
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
42 import java.lang.reflect.*;
38
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
43 import java.sql.Connection;
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
44 import java.sql.SQLException;
33
fd8c40ff78c3 fixes several warnings
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
45 import java.util.*;
63
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
46 import java.util.function.Function;
163
a5b9632729b6 adds possibility to specify multiple additional CSS files
Mike Becker <universe@uap-core.de>
parents: 160
diff changeset
47 import java.util.stream.Collectors;
7
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 /**
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 * A special implementation of a HTTPServlet which is focused on implementing
79
f64255a88d66 bloat removal 3/3 - LightPITModule annotation and ModuleManager
Mike Becker <universe@uap-core.de>
parents: 78
diff changeset
51 * the necessary functionality for LightPIT pages.
7
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 */
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents: 168
diff changeset
53 public abstract class AbstractServlet extends HttpServlet {
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
54
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents: 168
diff changeset
55 private static final Logger LOG = LoggerFactory.getLogger(AbstractServlet.class);
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
56
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
57 /**
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
58 * Invocation mapping gathered from the {@link RequestMapping} annotations.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
59 * <p>
18
a94b172c3a93 user friendly error pages for codes 404, 403 and 500
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
60 * Paths in this map must always start with a leading slash, although
a94b172c3a93 user friendly error pages for codes 404, 403 and 500
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
61 * the specification in the annotation must not start with a leading slash.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
62 * <p>
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
63 * The reason for this is the different handling of empty paths in
18
a94b172c3a93 user friendly error pages for codes 404, 403 and 500
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
64 * {@link HttpServletRequest#getPathInfo()}.
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
65 */
130
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
66 private final Map<HttpMethod, Map<PathPattern, Method>> mappings = new HashMap<>();
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
67
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
68 /**
38
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
69 * Creates a set of data access objects for the specified connection.
33
fd8c40ff78c3 fixes several warnings
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
70 *
38
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
71 * @param connection the SQL connection
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
72 * @return a set of data access objects
17
d1036b776eee adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents: 15
diff changeset
73 */
167
3f30adba1c63 major refactoring of DAO architecture - also fixes #114
Mike Becker <universe@uap-core.de>
parents: 163
diff changeset
74 private DataAccessObject createDataAccessObjects(Connection connection) {
151
b3f14cd4f3ab migrate DataSourceProvider
Mike Becker <universe@uap-core.de>
parents: 145
diff changeset
75 final var df = (DataSourceProvider) getServletContext().getAttribute(DataSourceProvider.Companion.getSC_ATTR_NAME());
158
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
76 if (df.getDialect() == DataSourceProvider.Dialect.Postgres) {
167
3f30adba1c63 major refactoring of DAO architecture - also fixes #114
Mike Becker <universe@uap-core.de>
parents: 163
diff changeset
77 return new PostgresDataAccessObject(connection);
38
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
78 }
151
b3f14cd4f3ab migrate DataSourceProvider
Mike Becker <universe@uap-core.de>
parents: 145
diff changeset
79 throw new UnsupportedOperationException("Non-exhaustive if-else - this is a bug.");
17
d1036b776eee adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents: 15
diff changeset
80 }
33
fd8c40ff78c3 fixes several warnings
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
81
167
3f30adba1c63 major refactoring of DAO architecture - also fixes #114
Mike Becker <universe@uap-core.de>
parents: 163
diff changeset
82 private void invokeMapping(Map.Entry<PathPattern, Method> mapping, HttpServletRequest req, HttpServletResponse resp, DataAccessObject dao) throws IOException {
130
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
83 final var pathPattern = mapping.getKey();
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
84 final var method = mapping.getValue();
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
85 try {
14
2b270c714678 changed logger implementation to log4j2
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
86 LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName());
42
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
87 final var paramTypes = method.getParameterTypes();
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
88 final var paramValues = new Object[paramTypes.length];
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
89 for (int i = 0; i < paramTypes.length; i++) {
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
90 if (paramTypes[i].isAssignableFrom(HttpServletRequest.class)) {
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
91 paramValues[i] = req;
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
92 } else if (paramTypes[i].isAssignableFrom(HttpServletResponse.class)) {
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
93 paramValues[i] = resp;
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
94 }
167
3f30adba1c63 major refactoring of DAO architecture - also fixes #114
Mike Becker <universe@uap-core.de>
parents: 163
diff changeset
95 if (paramTypes[i].isAssignableFrom(DataAccessObject.class)) {
42
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
96 paramValues[i] = dao;
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
97 }
130
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
98 if (paramTypes[i].isAssignableFrom(PathParameters.class)) {
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
99 paramValues[i] = pathPattern.obtainPathParameters(sanitizeRequestPath(req));
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
100 }
42
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
101 }
157
1e6f16fad3a5 removes ResponseType enum
Mike Becker <universe@uap-core.de>
parents: 151
diff changeset
102 method.invoke(this, paramValues);
73
672b5003cafe improves error message for InvocationTargetExceptions
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
103 } catch (InvocationTargetException ex) {
672b5003cafe improves error message for InvocationTargetExceptions
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
104 LOG.error("invocation of method {}::{} failed: {}",
672b5003cafe improves error message for InvocationTargetExceptions
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
105 method.getDeclaringClass().getName(), method.getName(), ex.getTargetException().getMessage());
672b5003cafe improves error message for InvocationTargetExceptions
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
106 LOG.debug("Details: ", ex.getTargetException());
672b5003cafe improves error message for InvocationTargetExceptions
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
107 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getTargetException().getMessage());
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
108 } catch (ReflectiveOperationException | ClassCastException ex) {
73
672b5003cafe improves error message for InvocationTargetExceptions
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
109 LOG.error("invocation of method {}::{} failed: {}",
672b5003cafe improves error message for InvocationTargetExceptions
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
110 method.getDeclaringClass().getName(), method.getName(), ex.getMessage());
38
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
111 LOG.debug("Details: ", ex);
73
672b5003cafe improves error message for InvocationTargetExceptions
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
112 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
113 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
114 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
115
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
116 @Override
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
117 public void init() throws ServletException {
78
bb4c52bf3439 bloat removal 2/3 - moduleInfo
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
118 scanForRequestMappings();
33
fd8c40ff78c3 fixes several warnings
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
119
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
120 LOG.trace("{} initialized", getServletName());
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
121 }
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
122
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
123 private void scanForRequestMappings() {
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
124 try {
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
125 Method[] methods = getClass().getDeclaredMethods();
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
126 for (Method method : methods) {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
127 Optional<RequestMapping> mapping = Optional.ofNullable(method.getAnnotation(RequestMapping.class));
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
128 if (mapping.isPresent()) {
130
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
129 if (mapping.get().requestPath().isBlank()) {
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
130 LOG.warn("{} is annotated with {} but request path is empty",
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
131 method.getName(), RequestMapping.class.getSimpleName()
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
132 );
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
133 continue;
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
134 }
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
135
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
136 if (!Modifier.isPublic(method.getModifiers())) {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
137 LOG.warn("{} is annotated with {} but is not public",
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
138 method.getName(), RequestMapping.class.getSimpleName()
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
139 );
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
140 continue;
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
141 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
142 if (Modifier.isAbstract(method.getModifiers())) {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
143 LOG.warn("{} is annotated with {} but is abstract",
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
144 method.getName(), RequestMapping.class.getSimpleName()
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
145 );
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
146 continue;
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
147 }
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
148
42
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
149 boolean paramsInjectible = true;
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
150 for (var param : method.getParameterTypes()) {
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
151 paramsInjectible &= HttpServletRequest.class.isAssignableFrom(param)
167
3f30adba1c63 major refactoring of DAO architecture - also fixes #114
Mike Becker <universe@uap-core.de>
parents: 163
diff changeset
152 || HttpServletResponse.class.isAssignableFrom(param)
3f30adba1c63 major refactoring of DAO architecture - also fixes #114
Mike Becker <universe@uap-core.de>
parents: 163
diff changeset
153 || PathParameters.class.isAssignableFrom(param)
3f30adba1c63 major refactoring of DAO architecture - also fixes #114
Mike Becker <universe@uap-core.de>
parents: 163
diff changeset
154 || DataAccessObject.class.isAssignableFrom(param);
42
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
155 }
f962ff9dd44e makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
156 if (paramsInjectible) {
130
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
157 try {
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
158 PathPattern pathPattern = new PathPattern(mapping.get().requestPath());
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
159
131
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
160 final var methodMappings = mappings.computeIfAbsent(mapping.get().method(), k -> new HashMap<>());
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
161 final var currentMapping = methodMappings.putIfAbsent(pathPattern, method);
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
162 if (currentMapping != null) {
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
163 LOG.warn("Cannot map {} {} to {} in class {} - this would override the mapping to {}",
130
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
164 mapping.get().method(),
131
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
165 mapping.get().requestPath(),
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
166 method.getName(),
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
167 getClass().getSimpleName(),
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
168 currentMapping.getName()
130
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
169 );
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
170 }
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
171
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
172 LOG.debug("{} {} maps to {}::{}",
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
173 mapping.get().method(),
130
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
174 mapping.get().requestPath(),
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
175 getClass().getSimpleName(),
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
176 method.getName()
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
177 );
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
178 } catch (IllegalArgumentException ex) {
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
179 LOG.warn("Request mapping for {} failed: path pattern '{}' is syntactically invalid",
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
180 method.getName(), mapping.get().requestPath()
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
181 );
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
182 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
183 } else {
130
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
184 LOG.warn("{} is annotated with {} but has the wrong parameters - only HttpServletRequest, HttpServletResponse, PathParameters, and DataAccessObjects are allowed",
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
185 method.getName(), RequestMapping.class.getSimpleName()
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
186 );
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
187 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
188 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
189 }
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
190 } catch (SecurityException ex) {
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
191 LOG.error("Scan for request mappings on declared methods failed.", ex);
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
192 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
193 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
194
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
195 @Override
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
196 public void destroy() {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
197 mappings.clear();
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
198 LOG.trace("{} destroyed", getServletName());
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
199 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
200
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
201 /**
74
91d1fc2a3a14 removes that dynamic_fragment bullshit
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
202 * Sets the name of the content page.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
203 * <p>
13
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
204 * It is sufficient to specify the name without any extension. The extension
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
205 * is added automatically if not specified.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
206 *
74
91d1fc2a3a14 removes that dynamic_fragment bullshit
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
207 * @param req the servlet request object
91d1fc2a3a14 removes that dynamic_fragment bullshit
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
208 * @param pageName the name of the content page
91d1fc2a3a14 removes that dynamic_fragment bullshit
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
209 * @see Constants#REQ_ATTR_CONTENT_PAGE
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
210 */
74
91d1fc2a3a14 removes that dynamic_fragment bullshit
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
211 protected void setContentPage(HttpServletRequest req, String pageName) {
158
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
212 req.setAttribute(Constants.REQ_ATTR_CONTENT_PAGE, jspPath(pageName));
13
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
213 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
214
13
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
215 /**
96
b7b685f31e39 breadcrumb menu is now a sidebar navigation menu
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
216 * Sets the navigation menu.
71
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
217 *
109
2e0669e814ff adds versions overview
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
218 * @param req the servlet request object
2e0669e814ff adds versions overview
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
219 * @param jspName the name of the menu's jsp file
96
b7b685f31e39 breadcrumb menu is now a sidebar navigation menu
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
220 * @see Constants#REQ_ATTR_NAVIGATION
71
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
221 */
109
2e0669e814ff adds versions overview
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
222 protected void setNavigationMenu(HttpServletRequest req, String jspName) {
158
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
223 req.setAttribute(Constants.REQ_ATTR_NAVIGATION, jspPath(jspName));
71
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
224 }
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
225
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
226 /**
47
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
227 * @param req the servlet request object
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
228 * @param location the location where to redirect
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
229 * @see Constants#REQ_ATTR_REDIRECT_LOCATION
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
230 */
63
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
231 protected void setRedirectLocation(HttpServletRequest req, String location) {
47
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
232 if (location.startsWith("./")) {
158
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
233 location = location.replaceFirst("\\./", baseHref(req));
47
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
234 }
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
235 req.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, location);
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
236 }
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
237
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
238 /**
163
a5b9632729b6 adds possibility to specify multiple additional CSS files
Mike Becker <universe@uap-core.de>
parents: 160
diff changeset
239 * Specifies the names of additional stylesheets used by this Servlet.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
240 * <p>
13
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
241 * It is sufficient to specify the name without any extension. The extension
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
242 * is added automatically if not specified.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
243 *
163
a5b9632729b6 adds possibility to specify multiple additional CSS files
Mike Becker <universe@uap-core.de>
parents: 160
diff changeset
244 * @param req the servlet request object
a5b9632729b6 adds possibility to specify multiple additional CSS files
Mike Becker <universe@uap-core.de>
parents: 160
diff changeset
245 * @param stylesheets the names of the stylesheets
13
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
246 */
163
a5b9632729b6 adds possibility to specify multiple additional CSS files
Mike Becker <universe@uap-core.de>
parents: 160
diff changeset
247 public void setStylesheet(HttpServletRequest req, String ... stylesheets) {
a5b9632729b6 adds possibility to specify multiple additional CSS files
Mike Becker <universe@uap-core.de>
parents: 160
diff changeset
248 req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Arrays
a5b9632729b6 adds possibility to specify multiple additional CSS files
Mike Becker <universe@uap-core.de>
parents: 160
diff changeset
249 .stream(stylesheets)
a5b9632729b6 adds possibility to specify multiple additional CSS files
Mike Becker <universe@uap-core.de>
parents: 160
diff changeset
250 .map(s -> enforceExt(s, ".css"))
a5b9632729b6 adds possibility to specify multiple additional CSS files
Mike Becker <universe@uap-core.de>
parents: 160
diff changeset
251 .collect(Collectors.toUnmodifiableList()));
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
252 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
253
47
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
254 /**
86
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
255 * Sets the view model object.
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
256 * The type must match the expected type in the JSP file.
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
257 *
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
258 * @param req the servlet request object
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
259 * @param viewModel the view model object
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
260 */
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
261 public void setViewModel(HttpServletRequest req, Object viewModel) {
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
262 req.setAttribute(Constants.REQ_ATTR_VIEWMODEL, viewModel);
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
263 }
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
264
131
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
265 private <T> Optional<T> parseParameter(String paramValue, Class<T> clazz) {
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
266 if (paramValue == null) return Optional.empty();
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
267 if (clazz.equals(Boolean.class)) {
168
1c3694ae224c adds "Create another" checkbox to issue form - fixes #110
Mike Becker <universe@uap-core.de>
parents: 167
diff changeset
268 if (paramValue.equalsIgnoreCase("false") || paramValue.equals("0")) {
131
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
269 return Optional.of((T) Boolean.FALSE);
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
270 } else {
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
271 return Optional.of((T) Boolean.TRUE);
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
272 }
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
273 }
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
274 if (clazz.equals(String.class)) return Optional.of((T) paramValue);
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
275 if (java.sql.Date.class.isAssignableFrom(clazz)) {
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
276 try {
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
277 return Optional.of((T) java.sql.Date.valueOf(paramValue));
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
278 } catch (IllegalArgumentException ex) {
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
279 return Optional.empty();
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
280 }
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
281 }
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
282 try {
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
283 final Constructor<T> ctor = clazz.getConstructor(String.class);
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
284 return Optional.of(ctor.newInstance(paramValue));
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
285 } catch (ReflectiveOperationException e) {
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
286 // does not type check and is not convertible - treat as if the parameter was never set
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
287 return Optional.empty();
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
288 }
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
289 }
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
290
86
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
291 /**
47
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
292 * Obtains a request parameter of the specified type.
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
293 * The specified type must have a single-argument constructor accepting a string to perform conversion.
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
294 * The constructor of the specified type may throw an exception on conversion failures.
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
295 *
71
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
296 * @param req the servlet request object
47
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
297 * @param clazz the class object of the expected type
71
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
298 * @param name the name of the parameter
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
299 * @param <T> the expected type
47
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
300 * @return the parameter value or an empty optional, if no parameter with the specified name was found
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
301 */
71
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
302 protected <T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) {
83
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
303 if (clazz.isArray()) {
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
304 final String[] paramValues = req.getParameterValues(name);
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
305 int len = paramValues == null ? 0 : paramValues.length;
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
306 final var array = (T) Array.newInstance(clazz.getComponentType(), len);
86
0a658e53177c improves issue overview and adds progress information
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
307 for (int i = 0; i < len; i++) {
83
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
308 try {
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
309 final Constructor<?> ctor = clazz.getComponentType().getConstructor(String.class);
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
310 Array.set(array, i, ctor.newInstance(paramValues[i]));
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
311 } catch (ReflectiveOperationException e) {
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
312 throw new RuntimeException(e);
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
313 }
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
314 }
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
315 return Optional.of(array);
24a3596b8f98 adds version selection in issue editor
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
316 } else {
131
67df332e3146 changes request mapping to contain project and version ID as path parameters (this removes the session storage)
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
317 return parseParameter(req.getParameter(name), clazz);
80
27a25f32048e adds project overview page
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
318 }
47
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
319 }
57cfb94ab99f projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
320
63
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
321 /**
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
322 * Tries to look up an entity with a key obtained from a request parameter.
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
323 *
71
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
324 * @param req the servlet request object
63
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
325 * @param clazz the class representing the type of the request parameter
71
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
326 * @param name the name of the request parameter
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
327 * @param find the find function (typically a DAO function)
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
328 * @param <T> the type of the request parameter
dca186d3911f adds breadcrumb menu
Mike Becker <universe@uap-core.de>
parents: 70
diff changeset
329 * @param <R> the type of the looked up entity
63
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
330 * @return the retrieved entity or an empty optional if there is no such entity or the request parameter was missing
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
331 * @throws SQLException if the find function throws an exception
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
332 */
167
3f30adba1c63 major refactoring of DAO architecture - also fixes #114
Mike Becker <universe@uap-core.de>
parents: 163
diff changeset
333 protected <T, R> Optional<R> findByParameter(HttpServletRequest req, Class<T> clazz, String name, Function<? super T, ? extends R> find) {
63
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
334 final var param = getParameter(req, clazz, name);
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
335 if (param.isPresent()) {
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
336 return Optional.ofNullable(find.apply(param.get()));
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
337 } else {
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
338 return Optional.empty();
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
339 }
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
340 }
51aa5e267c7f adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
341
168
1c3694ae224c adds "Create another" checkbox to issue form - fixes #110
Mike Becker <universe@uap-core.de>
parents: 167
diff changeset
342 protected void setAttributeFromParameter(HttpServletRequest req, String name) {
1c3694ae224c adds "Create another" checkbox to issue form - fixes #110
Mike Becker <universe@uap-core.de>
parents: 167
diff changeset
343 final var parm = req.getParameter(name);
1c3694ae224c adds "Create another" checkbox to issue form - fixes #110
Mike Becker <universe@uap-core.de>
parents: 167
diff changeset
344 if (parm != null) {
1c3694ae224c adds "Create another" checkbox to issue form - fixes #110
Mike Becker <universe@uap-core.de>
parents: 167
diff changeset
345 req.setAttribute(name, parm);
1c3694ae224c adds "Create another" checkbox to issue form - fixes #110
Mike Becker <universe@uap-core.de>
parents: 167
diff changeset
346 }
1c3694ae224c adds "Create another" checkbox to issue form - fixes #110
Mike Becker <universe@uap-core.de>
parents: 167
diff changeset
347 }
1c3694ae224c adds "Create another" checkbox to issue form - fixes #110
Mike Becker <universe@uap-core.de>
parents: 167
diff changeset
348
45
cc7f082c5ef3 simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
349 private String sanitizeRequestPath(HttpServletRequest req) {
cc7f082c5ef3 simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
350 return Optional.ofNullable(req.getPathInfo()).orElse("/");
cc7f082c5ef3 simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
351 }
cc7f082c5ef3 simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
352
130
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
353 private Optional<Map.Entry<PathPattern, Method>> findMapping(HttpMethod method, HttpServletRequest req) {
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
354 return Optional.ofNullable(mappings.get(method)).flatMap(rm ->
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
355 rm.entrySet().stream().filter(
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
356 kv -> kv.getKey().matches(sanitizeRequestPath(req))
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
357 ).findAny()
7ef369744fd1 adds the possibility to specify path parameters to RequestMapping
Mike Becker <universe@uap-core.de>
parents: 109
diff changeset
358 );
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
359 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
360
157
1e6f16fad3a5 removes ResponseType enum
Mike Becker <universe@uap-core.de>
parents: 151
diff changeset
361 protected void renderSite(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
182
53f0e2685ad5 single-use constant field can be inlined
Mike Becker <universe@uap-core.de>
parents: 180
diff changeset
362 req.getRequestDispatcher(jspPath("site")).forward(req, resp);
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
363 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
364
158
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
365 protected Optional<String[]> availableLanguages() {
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
366 return Optional.ofNullable(getServletContext().getInitParameter(Constants.CTX_ATTR_LANGUAGES)).map((x) -> x.split("\\s*,\\s*"));
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
367 }
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
368
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
369 private static String baseHref(HttpServletRequest req) {
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
370 return String.format("%s://%s:%d%s/",
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
371 req.getScheme(),
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
372 req.getServerName(),
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
373 req.getServerPort(),
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
374 req.getContextPath());
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
375 }
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
376
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
377 private static String enforceExt(String filename, String ext) {
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
378 return filename.endsWith(ext) ? filename : filename + ext;
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
379 }
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
380
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
381 private static String jspPath(String filename) {
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
382 return enforceExt(Constants.JSP_PATH_PREFIX + filename, ".jsp");
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
383 }
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
384
38
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
385 private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
160
e2d09cf3fb96 forces the use of utf-8 as request character encoding - fixes #108
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
386 // the very first thing to do is to force UTF-8
e2d09cf3fb96 forces the use of utf-8 as request character encoding - fixes #108
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
387 req.setCharacterEncoding("UTF-8");
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
388
13
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
389 // choose the requested language as session language (if available) or fall back to english, otherwise
20
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
390 HttpSession session = req.getSession();
13
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
391 if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) {
158
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
392 Optional<List<String>> availableLanguages = availableLanguages().map(Arrays::asList);
13
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
393 Optional<Locale> reqLocale = Optional.of(req.getLocale());
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
394 Locale sessionLocale = reqLocale.filter((rl) -> availableLanguages.map((al) -> al.contains(rl.getLanguage())).orElse(false)).orElse(Locale.ENGLISH);
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
395 session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, sessionLocale);
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
396 LOG.debug("Setting language for new session {}: {}", session.getId(), sessionLocale.getDisplayLanguage());
14
2b270c714678 changed logger implementation to log4j2
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
397 } else {
15
bb594abac796 language selector completed
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
398 Locale sessionLocale = (Locale) session.getAttribute(Constants.SESSION_ATTR_LANGUAGE);
bb594abac796 language selector completed
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
399 resp.setLocale(sessionLocale);
bb594abac796 language selector completed
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
400 LOG.trace("Continuing session {} with language {}", session.getId(), sessionLocale);
13
f4608ad6c947 adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
401 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
402
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
403 // set some internal request attributes
158
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
404 final String fullPath = req.getServletPath() + Optional.ofNullable(req.getPathInfo()).orElse("");
4f912cd42876 migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents: 157
diff changeset
405 req.setAttribute(Constants.REQ_ATTR_BASE_HREF, baseHref(req));
53
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
406 req.setAttribute(Constants.REQ_ATTR_PATH, fullPath);
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
407
53
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
408 // if this is an error path, bypass the normal flow
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
409 if (fullPath.startsWith("/error/")) {
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
410 final var mapping = findMapping(method, req);
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
411 if (mapping.isPresent()) {
157
1e6f16fad3a5 removes ResponseType enum
Mike Becker <universe@uap-core.de>
parents: 151
diff changeset
412 invokeMapping(mapping.get(), req, resp, null);
53
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
413 }
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
414 return;
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
415 }
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
416
38
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
417 // obtain a connection and create the data access objects
151
b3f14cd4f3ab migrate DataSourceProvider
Mike Becker <universe@uap-core.de>
parents: 145
diff changeset
418 final var db = (DataSourceProvider) req.getServletContext().getAttribute(DataSourceProvider.Companion.getSC_ATTR_NAME());
53
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
419 final var ds = db.getDataSource();
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
420 if (ds == null) {
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
421 resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "JNDI DataSource lookup failed. See log for details.");
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
422 return;
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
423 }
6a8498291606 fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
424 try (final var connection = ds.getConnection()) {
38
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
425 final var dao = createDataAccessObjects(connection);
39
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
426 try {
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
427 connection.setAutoCommit(false);
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
428 // call the handler, if available, or send an HTTP 404 error
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
429 final var mapping = findMapping(method, req);
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
430 if (mapping.isPresent()) {
157
1e6f16fad3a5 removes ResponseType enum
Mike Becker <universe@uap-core.de>
parents: 151
diff changeset
431 invokeMapping(mapping.get(), req, resp, dao);
39
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
432 } else {
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
433 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
434 }
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
435 connection.commit();
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
436 } catch (SQLException ex) {
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
437 LOG.warn("Database transaction failed (Code {}): {}", ex.getErrorCode(), ex.getMessage());
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
438 LOG.debug("Details: ", ex);
54
77e01cda5a40 adds missing spaces in error messages
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
439 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unhandled Transaction Error - Code: " + ex.getErrorCode());
39
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
440 connection.rollback();
38
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
441 }
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
442 } catch (SQLException ex) {
39
e722861558bb fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
443 LOG.error("Severe Database Exception (Code {}): {}", ex.getErrorCode(), ex.getMessage());
38
cf85ef18f231 adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
444 LOG.debug("Details: ", ex);
54
77e01cda5a40 adds missing spaces in error messages
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
445 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Database Error - Code: " + ex.getErrorCode());
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
446 }
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
447 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
448
7
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
449 @Override
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
450 protected final void doGet(HttpServletRequest req, HttpServletResponse resp)
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
451 throws ServletException, IOException {
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
452 doProcess(HttpMethod.GET, req, resp);
7
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
453 }
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
454
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
455 @Override
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
456 protected final void doPost(HttpServletRequest req, HttpServletResponse resp)
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
457 throws ServletException, IOException {
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
458 doProcess(HttpMethod.POST, req, resp);
7
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
459 }
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
460 }

mercurial