Fri, 22 May 2020 16:21:59 +0200
some reformatting
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 | * |
24 | 4 | * Copyright 2018 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 | |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
31 | import de.uapcore.lightpit.dao.DataAccessObjects; |
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
32 | import de.uapcore.lightpit.dao.postgres.PGDataAccessObjects; |
33 | 33 | import org.slf4j.Logger; |
34 | import org.slf4j.LoggerFactory; | |
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 | 41 | import java.io.IOException; |
47
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
42 | import java.lang.reflect.Constructor; |
33 | 43 | import java.lang.reflect.Method; |
44 | import java.lang.reflect.Modifier; | |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
45 | import java.sql.Connection; |
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
46 | import java.sql.SQLException; |
33 | 47 | 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
|
48 | import java.util.function.Function; |
7
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 | /** |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | * A special implementation of a HTTPServlet which is focused on implementing |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | * the necessary functionality for {@link LightPITModule}s. |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | */ |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
54 | public abstract class AbstractLightPITServlet extends HttpServlet { |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
55 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
56 | private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class); |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
57 | |
43
9abf0bf44c7b
renames some crappy constants
Mike Becker <universe@uap-core.de>
parents:
42
diff
changeset
|
58 | private static final String SITE_JSP = Functions.jspPath("site"); |
33 | 59 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
60 | /** |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
61 | * The EL proxy is necessary, because the EL resolver cannot handle annotation properties. |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
62 | */ |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
63 | private LightPITModule.ELProxy moduleInfo = null; |
33 | 64 | |
63
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
65 | @FunctionalInterface |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
66 | protected interface SQLFindFunction<K, T> { |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
67 | T apply(K key) throws SQLException; |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
68 | |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
69 | default <V> SQLFindFunction<V, T> compose(Function<? super V, ? extends K> before) throws SQLException { |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
70 | Objects.requireNonNull(before); |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
71 | return (v) -> this.apply(before.apply(v)); |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
72 | } |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
73 | |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
74 | default <V> SQLFindFunction<K, V> andThen(Function<? super T, ? extends V> after) throws SQLException { |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
75 | Objects.requireNonNull(after); |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
76 | return (t) -> after.apply(this.apply(t)); |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
77 | } |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
78 | |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
79 | static <K> Function<K, K> identity() { |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
80 | return (t) -> t; |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
81 | } |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
82 | } |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
83 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
84 | /** |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
85 | * 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
|
86 | * <p> |
18
a94b172c3a93
user friendly error pages for codes 404, 403 and 500
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
87 | * 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
|
88 | * 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
|
89 | * <p> |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
90 | * 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
|
91 | * {@link HttpServletRequest#getPathInfo()}. |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
92 | */ |
39
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
93 | private final Map<HttpMethod, Map<String, Method>> mappings = new HashMap<>(); |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
94 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
95 | /** |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
96 | * Gives implementing modules access to the {@link ModuleManager}. |
33 | 97 | * |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
98 | * @return the module manager |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
99 | */ |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
100 | protected final ModuleManager getModuleManager() { |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
101 | return (ModuleManager) getServletContext().getAttribute(ModuleManager.SC_ATTR_NAME); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
102 | } |
33 | 103 | |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
104 | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
105 | /** |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
106 | * Creates a set of data access objects for the specified connection. |
33 | 107 | * |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
108 | * @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
|
109 | * @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
|
110 | */ |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
111 | private DataAccessObjects createDataAccessObjects(Connection connection) throws SQLException { |
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
112 | final var df = (DatabaseFacade) getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME); |
39
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
113 | if (df.getSQLDialect() == DatabaseFacade.Dialect.Postgres) { |
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
114 | return new PGDataAccessObjects(connection); |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
115 | } |
39
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
116 | throw new AssertionError("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
|
117 | } |
33 | 118 | |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
119 | private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException { |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
120 | try { |
14
2b270c714678
changed logger implementation to log4j2
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
121 | 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
|
122 | final var paramTypes = method.getParameterTypes(); |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
123 | 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
|
124 | 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
|
125 | 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
|
126 | paramValues[i] = req; |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
127 | } 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
|
128 | paramValues[i] = resp; |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
129 | } |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
130 | if (paramTypes[i].isAssignableFrom(DataAccessObjects.class)) { |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
131 | paramValues[i] = dao; |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
132 | } |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
133 | } |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
134 | return (ResponseType) method.invoke(this, paramValues); |
12 | 135 | } catch (ReflectiveOperationException | ClassCastException ex) { |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
136 | LOG.error("invocation of method {} failed: {}", method.getName(), ex.getMessage()); |
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
137 | LOG.debug("Details: ", ex); |
12 | 138 | resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
139 | return ResponseType.NONE; | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
140 | } |
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 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
143 | @Override |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
144 | public void init() throws ServletException { |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
145 | moduleInfo = Optional.ofNullable(this.getClass().getAnnotation(LightPITModule.class)) |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
146 | .map(LightPITModule.ELProxy::new).orElse(null); |
33 | 147 | |
148 | if (moduleInfo != null) { | |
12 | 149 | scanForRequestMappings(); |
150 | } | |
33 | 151 | |
12 | 152 | LOG.trace("{} initialized", getServletName()); |
153 | } | |
154 | ||
155 | private void scanForRequestMappings() { | |
156 | try { | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
157 | Method[] methods = getClass().getDeclaredMethods(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
158 | for (Method method : methods) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
159 | Optional<RequestMapping> mapping = Optional.ofNullable(method.getAnnotation(RequestMapping.class)); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
160 | if (mapping.isPresent()) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
161 | if (!Modifier.isPublic(method.getModifiers())) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
162 | LOG.warn("{} is annotated with {} but is not public", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
163 | method.getName(), RequestMapping.class.getSimpleName() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
164 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
165 | continue; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
166 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
167 | if (Modifier.isAbstract(method.getModifiers())) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
168 | LOG.warn("{} is annotated with {} but is abstract", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
169 | method.getName(), RequestMapping.class.getSimpleName() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
170 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
171 | continue; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
172 | } |
12 | 173 | if (!ResponseType.class.isAssignableFrom(method.getReturnType())) { |
174 | LOG.warn("{} is annotated with {} but has the wrong return type - 'ResponseType' required", | |
175 | method.getName(), RequestMapping.class.getSimpleName() | |
176 | ); | |
177 | continue; | |
178 | } | |
179 | ||
42
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
180 | boolean paramsInjectible = true; |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
181 | for (var param : method.getParameterTypes()) { |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
182 | paramsInjectible &= HttpServletRequest.class.isAssignableFrom(param) |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
183 | || HttpServletResponse.class.isAssignableFrom(param) |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
184 | || DataAccessObjects.class.isAssignableFrom(param); |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
185 | } |
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
186 | if (paramsInjectible) { |
58
8d3047f78190
fixes duplicated trailing slash if index path has a menu entry
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
187 | String requestPath = "/" + mapping.get().requestPath(); |
12 | 188 | |
39
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
189 | if (mappings |
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
190 | .computeIfAbsent(mapping.get().method(), k -> new HashMap<>()) |
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
191 | .putIfAbsent(requestPath, method) != null) { |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
192 | LOG.warn("{} {} has multiple mappings", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
193 | mapping.get().method(), |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
194 | mapping.get().requestPath() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
195 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
196 | } |
12 | 197 | |
22
5a91fb7067af
minor changes to logging output
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
198 | LOG.debug("{} {} maps to {}::{}", |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
199 | mapping.get().method(), |
18
a94b172c3a93
user friendly error pages for codes 404, 403 and 500
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
200 | requestPath, |
22
5a91fb7067af
minor changes to logging output
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
201 | getClass().getSimpleName(), |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
202 | method.getName() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
203 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
204 | } else { |
42
f962ff9dd44e
makes every parameter in a handler method optional
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
205 | LOG.warn("{} is annotated with {} but has the wrong parameters - only HttpServletRequest. HttpServletResponse, and DataAccessObjects are allowed", |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
206 | method.getName(), RequestMapping.class.getSimpleName() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
207 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
208 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
209 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
210 | } |
12 | 211 | } catch (SecurityException ex) { |
212 | 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
|
213 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
214 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
215 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
216 | @Override |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
217 | public void destroy() { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
218 | mappings.clear(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
219 | LOG.trace("{} destroyed", getServletName()); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
220 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
221 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
222 | /** |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
223 | * Sets the name of the dynamic fragment. |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
224 | * <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
|
225 | * 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
|
226 | * 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
|
227 | * <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
|
228 | * The fragment must be located in the dynamic fragments folder. |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
229 | * |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
230 | * @param req the servlet request object |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
231 | * @param fragmentName the name of the fragment |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
232 | * @see Constants#DYN_FRAGMENT_PATH_PREFIX |
71 | 233 | * @see Constants#REQ_ATTR_FRAGMENT |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
234 | */ |
63
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
235 | protected void setDynamicFragment(HttpServletRequest req, String fragmentName) { |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
236 | req.setAttribute(Constants.REQ_ATTR_FRAGMENT, Functions.dynFragmentPath(fragmentName)); |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
237 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
238 | |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
239 | /** |
71 | 240 | * Sets the breadcrumbs menu. |
241 | * | |
242 | * @param req the servlet request object | |
243 | * @param breadcrumbs the menu entries for the breadcrumbs menu | |
244 | * @see Constants#REQ_ATTR_BREADCRUMBS | |
245 | */ | |
246 | protected void setBreadcrumbs(HttpServletRequest req, List<MenuEntry> breadcrumbs) { | |
247 | req.setAttribute(Constants.REQ_ATTR_BREADCRUMBS, breadcrumbs); | |
248 | } | |
249 | ||
250 | /** | |
47
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
251 | * @param req the servlet request object |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
252 | * @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
|
253 | * @see Constants#REQ_ATTR_REDIRECT_LOCATION |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
254 | */ |
63
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
255 | 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
|
256 | if (location.startsWith("./")) { |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
257 | location = location.replaceFirst("\\./", Functions.baseHref(req)); |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
258 | } |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
259 | 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
|
260 | } |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
261 | |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
262 | /** |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
263 | * Specifies the name of an additional stylesheet used by the module. |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
264 | * <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
|
265 | * Setting an additional stylesheet is optional, but quite common for HTML |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
266 | * output. |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
267 | * <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
|
268 | * 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
|
269 | * 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
|
270 | * |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
271 | * @param req the servlet request object |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
272 | * @param stylesheet the name of the stylesheet |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
273 | */ |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
274 | public void setStylesheet(HttpServletRequest req, String stylesheet) { |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
275 | req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Functions.enforceExt(stylesheet, ".css")); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
276 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
277 | |
47
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
278 | /** |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
279 | * 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
|
280 | * 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
|
281 | * 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
|
282 | * |
71 | 283 | * @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
|
284 | * @param clazz the class object of the expected type |
71 | 285 | * @param name the name of the parameter |
286 | * @param <T> the expected type | |
47
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
287 | * @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
|
288 | */ |
71 | 289 | protected <T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) { |
47
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
290 | final String paramValue = req.getParameter(name); |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
291 | if (paramValue == null) return Optional.empty(); |
71 | 292 | if (clazz.equals(String.class)) return Optional.of((T) paramValue); |
47
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
293 | try { |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
294 | final Constructor<T> ctor = clazz.getConstructor(String.class); |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
295 | return Optional.of(ctor.newInstance(paramValue)); |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
296 | } catch (ReflectiveOperationException e) { |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
297 | throw new RuntimeException(e); |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
298 | } |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
299 | |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
300 | } |
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
301 | |
63
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
302 | /** |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
303 | * 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
|
304 | * |
71 | 305 | * @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
|
306 | * @param clazz the class representing the type of the request parameter |
71 | 307 | * @param name the name of the request parameter |
308 | * @param find the find function (typically a DAO function) | |
309 | * @param <T> the type of the request parameter | |
310 | * @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
|
311 | * @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
|
312 | * @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
|
313 | */ |
71 | 314 | protected <T, R> Optional<R> findByParameter(HttpServletRequest req, Class<T> clazz, String name, SQLFindFunction<? super T, ? extends R> find) throws SQLException { |
63
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
315 | 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
|
316 | 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
|
317 | 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
|
318 | } else { |
51aa5e267c7f
adds utility function to find an entity by ID (reduces code duplication)
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
319 | 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
|
320 | } |
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 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
323 | private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp) |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
324 | throws IOException, ServletException { |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
325 | |
71 | 326 | final var mainMenu = new ArrayList<MenuEntry>(getModuleManager().getMainMenu()); |
327 | for (var entry : mainMenu) { | |
328 | if (Functions.fullPath(req).startsWith("/" + entry.getPathName())) { | |
329 | entry.setActive(true); | |
330 | } | |
331 | } | |
332 | req.setAttribute(Constants.REQ_ATTR_MENU, mainMenu); | |
43
9abf0bf44c7b
renames some crappy constants
Mike Becker <universe@uap-core.de>
parents:
42
diff
changeset
|
333 | req.getRequestDispatcher(SITE_JSP).forward(req, resp); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
334 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
335 | |
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
|
336 | 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
|
337 | 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
|
338 | } |
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
|
339 | |
39
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
340 | private Optional<Method> findMapping(HttpMethod method, HttpServletRequest req) { |
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
|
341 | return Optional.ofNullable(mappings.get(method)).map(rm -> rm.get(sanitizeRequestPath(req))); |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
342 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
343 | |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
344 | private void forwardAsSpecified(ResponseType type, HttpServletRequest req, HttpServletResponse resp) |
12 | 345 | throws ServletException, IOException { |
346 | switch (type) { | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
347 | case NONE: |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
348 | return; |
43
9abf0bf44c7b
renames some crappy constants
Mike Becker <universe@uap-core.de>
parents:
42
diff
changeset
|
349 | case HTML: |
12 | 350 | forwardToFullView(req, resp); |
351 | return; | |
352 | // TODO: implement remaining response types | |
353 | default: | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
354 | throw new AssertionError("ResponseType switch is not exhaustive - this is a bug!"); |
12 | 355 | } |
356 | } | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
357 | |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
358 | private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
27
1f2a96efa69f
removes caching of main menu
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
359 | |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
360 | // 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
|
361 | 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
|
362 | if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) { |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
363 | Optional<List<String>> availableLanguages = Functions.availableLanguages(getServletContext()).map(Arrays::asList); |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
364 | 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
|
365 | 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
|
366 | 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
|
367 | 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
|
368 | } else { |
15
bb594abac796
language selector completed
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
369 | Locale sessionLocale = (Locale) session.getAttribute(Constants.SESSION_ATTR_LANGUAGE); |
bb594abac796
language selector completed
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
370 | resp.setLocale(sessionLocale); |
bb594abac796
language selector completed
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
371 | 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
|
372 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
373 | |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
374 | // set some internal request attributes |
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
|
375 | final String fullPath = Functions.fullPath(req); |
47
57cfb94ab99f
projects can now be added and updated
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
376 | req.setAttribute(Constants.REQ_ATTR_BASE_HREF, Functions.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
|
377 | req.setAttribute(Constants.REQ_ATTR_PATH, fullPath); |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
378 | Optional.ofNullable(moduleInfo).ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy)); |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
379 | |
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
|
380 | // 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
|
381 | 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
|
382 | 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
|
383 | if (mapping.isPresent()) { |
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
|
384 | forwardAsSpecified(invokeMapping(mapping.get(), req, resp, null), req, resp); |
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
|
385 | } |
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
|
386 | 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
|
387 | } |
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
|
388 | |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
389 | // obtain a connection and create the data access objects |
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
390 | final var db = (DatabaseFacade) req.getServletContext().getAttribute(DatabaseFacade.SC_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
|
391 | 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
|
392 | 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
|
393 | 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
|
394 | 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
|
395 | } |
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
|
396 | 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
|
397 | 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
|
398 | try { |
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
399 | connection.setAutoCommit(false); |
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
400 | // 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
|
401 | 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
|
402 | if (mapping.isPresent()) { |
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
403 | forwardAsSpecified(invokeMapping(mapping.get(), req, resp, dao), req, resp); |
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
404 | } else { |
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
405 | 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
|
406 | } |
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
407 | connection.commit(); |
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
408 | } catch (SQLException ex) { |
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
409 | 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
|
410 | LOG.debug("Details: ", ex); |
54
77e01cda5a40
adds missing spaces in error messages
Mike Becker <universe@uap-core.de>
parents:
53
diff
changeset
|
411 | 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
|
412 | connection.rollback(); |
38
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
413 | } |
cf85ef18f231
adds DAO for Project entity and save/update methods
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
414 | } catch (SQLException ex) { |
39
e722861558bb
fixes minor issues that were reported by default inspection
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
415 | 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
|
416 | LOG.debug("Details: ", ex); |
54
77e01cda5a40
adds missing spaces in error messages
Mike Becker <universe@uap-core.de>
parents:
53
diff
changeset
|
417 | resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Database Error - Code: " + ex.getErrorCode()); |
12 | 418 | } |
419 | } | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
420 | |
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
421 | @Override |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
422 | 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
|
423 | throws ServletException, IOException { |
12 | 424 | 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
|
425 | } |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
426 | |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
427 | @Override |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
428 | 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
|
429 | throws ServletException, IOException { |
12 | 430 | 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
|
431 | } |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
432 | } |