Sun, 10 May 2020 10:58:31 +0200
adds data model for projects and versions
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 | |
33 | 31 | import org.slf4j.Logger; |
32 | import org.slf4j.LoggerFactory; | |
33 | ||
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | 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
|
35 | 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
|
36 | 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
|
37 | 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
|
38 | import javax.servlet.http.HttpSession; |
33 | 39 | import java.io.IOException; |
40 | import java.lang.reflect.Method; | |
41 | import java.lang.reflect.Modifier; | |
42 | import java.util.*; | |
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | /** |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | * 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
|
46 | * 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
|
47 | */ |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
48 | 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
|
49 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
50 | 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
|
51 | |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
52 | private static final String HTML_FULL_DISPATCHER = Functions.jspPath("html_full"); |
33 | 53 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
54 | /** |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
55 | * 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
|
56 | */ |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
57 | private LightPITModule.ELProxy moduleInfo = null; |
33 | 58 | |
59 | ||
12 | 60 | @FunctionalInterface |
33 | 61 | private interface HandlerMethod { |
62 | ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException; | |
12 | 63 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
64 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
65 | /** |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
66 | * 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
|
67 | * <p> |
18
a94b172c3a93
user friendly error pages for codes 404, 403 and 500
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
68 | * 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
|
69 | * 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
|
70 | * <p> |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
71 | * 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
|
72 | * {@link HttpServletRequest#getPathInfo()}. |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
73 | */ |
12 | 74 | private final Map<HttpMethod, Map<String, HandlerMethod>> mappings = new HashMap<>(); |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
75 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
76 | /** |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
77 | * Gives implementing modules access to the {@link ModuleManager}. |
33 | 78 | * |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
79 | * @return the module manager |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
80 | */ |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
81 | protected final ModuleManager getModuleManager() { |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
82 | 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
|
83 | } |
33 | 84 | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
85 | /** |
17
d1036b776eee
adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
86 | * Gives implementing modules access to the {@link DatabaseFacade}. |
33 | 87 | * |
17
d1036b776eee
adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
88 | * @return the database facade |
d1036b776eee
adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
89 | */ |
d1036b776eee
adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
90 | protected final DatabaseFacade getDatabaseFacade() { |
d1036b776eee
adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
91 | return (DatabaseFacade) getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME); |
d1036b776eee
adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
92 | } |
33 | 93 | |
94 | private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
95 | try { |
14
2b270c714678
changed logger implementation to log4j2
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
96 | LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName()); |
12 | 97 | return (ResponseType) method.invoke(this, req, resp); |
98 | } catch (ReflectiveOperationException | ClassCastException ex) { | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
99 | LOG.error(String.format("invocation of method %s failed", method.getName()), ex); |
12 | 100 | resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
101 | return ResponseType.NONE; | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
102 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
103 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
104 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
105 | @Override |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
106 | 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
|
107 | 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
|
108 | .map(LightPITModule.ELProxy::new).orElse(null); |
33 | 109 | |
110 | if (moduleInfo != null) { | |
12 | 111 | scanForRequestMappings(); |
112 | } | |
33 | 113 | |
12 | 114 | LOG.trace("{} initialized", getServletName()); |
115 | } | |
116 | ||
117 | private void scanForRequestMappings() { | |
118 | try { | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
119 | Method[] methods = getClass().getDeclaredMethods(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
120 | for (Method method : methods) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
121 | Optional<RequestMapping> mapping = Optional.ofNullable(method.getAnnotation(RequestMapping.class)); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
122 | if (mapping.isPresent()) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
123 | if (!Modifier.isPublic(method.getModifiers())) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
124 | LOG.warn("{} is annotated with {} but is not public", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
125 | method.getName(), RequestMapping.class.getSimpleName() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
126 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
127 | continue; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
128 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
129 | if (Modifier.isAbstract(method.getModifiers())) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
130 | LOG.warn("{} is annotated with {} but is abstract", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
131 | method.getName(), RequestMapping.class.getSimpleName() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
132 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
133 | continue; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
134 | } |
12 | 135 | if (!ResponseType.class.isAssignableFrom(method.getReturnType())) { |
136 | LOG.warn("{} is annotated with {} but has the wrong return type - 'ResponseType' required", | |
137 | method.getName(), RequestMapping.class.getSimpleName() | |
138 | ); | |
139 | continue; | |
140 | } | |
141 | ||
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
142 | Class<?>[] params = method.getParameterTypes(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
143 | if (params.length == 2 |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
144 | && HttpServletRequest.class.isAssignableFrom(params[0]) |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
145 | && HttpServletResponse.class.isAssignableFrom(params[1])) { |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
146 | |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
147 | final String requestPath = "/" + mapping.get().requestPath(); |
12 | 148 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
149 | if (mappings.computeIfAbsent(mapping.get().method(), k -> new HashMap<>()). |
18
a94b172c3a93
user friendly error pages for codes 404, 403 and 500
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
150 | putIfAbsent(requestPath, |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
151 | (req, resp) -> invokeMapping(method, req, resp)) != null) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
152 | LOG.warn("{} {} has multiple mappings", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
153 | mapping.get().method(), |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
154 | mapping.get().requestPath() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
155 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
156 | } |
12 | 157 | |
22
5a91fb7067af
minor changes to logging output
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
158 | LOG.debug("{} {} maps to {}::{}", |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
159 | 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
|
160 | requestPath, |
22
5a91fb7067af
minor changes to logging output
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
161 | getClass().getSimpleName(), |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
162 | method.getName() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
163 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
164 | } else { |
12 | 165 | LOG.warn("{} is annotated with {} but has the wrong parameters - (HttpServletRequest,HttpServletResponse) required", |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
166 | method.getName(), RequestMapping.class.getSimpleName() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
167 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
168 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
169 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
170 | } |
12 | 171 | } catch (SecurityException ex) { |
172 | 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
|
173 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
174 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
175 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
176 | @Override |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
177 | public void destroy() { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
178 | mappings.clear(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
179 | LOG.trace("{} destroyed", getServletName()); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
180 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
181 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
182 | /** |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
183 | * 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
|
184 | * <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
|
185 | * 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
|
186 | * 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
|
187 | * <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
|
188 | * 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
|
189 | * |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
190 | * @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
|
191 | * @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
|
192 | * @see Constants#DYN_FRAGMENT_PATH_PREFIX |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
193 | */ |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
194 | public void setDynamicFragment(HttpServletRequest req, String fragmentName) { |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
195 | 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
|
196 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
197 | |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
198 | /** |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
199 | * 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
|
200 | * <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
|
201 | * 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
|
202 | * output. |
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 | * |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
207 | * @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
|
208 | * @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
|
209 | */ |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
210 | 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
|
211 | 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
|
212 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
213 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
214 | 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
|
215 | throws IOException, ServletException { |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
216 | |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
217 | req.setAttribute(Constants.REQ_ATTR_MENU, getModuleManager().getMainMenu()); |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
218 | req.getRequestDispatcher(HTML_FULL_DISPATCHER).forward(req, resp); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
219 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
220 | |
12 | 221 | private Optional<HandlerMethod> findMapping(HttpMethod method, HttpServletRequest req) { |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
222 | return Optional.ofNullable(mappings.get(method)).map( |
18
a94b172c3a93
user friendly error pages for codes 404, 403 and 500
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
223 | (rm) -> rm.get(Optional.ofNullable(req.getPathInfo()).orElse("/")) |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
224 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
225 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
226 | |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
227 | private void forwardAsSpecified(ResponseType type, HttpServletRequest req, HttpServletResponse resp) |
12 | 228 | throws ServletException, IOException { |
229 | switch (type) { | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
230 | case NONE: |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
231 | return; |
12 | 232 | case HTML_FULL: |
233 | forwardToFullView(req, resp); | |
234 | return; | |
235 | // TODO: implement remaining response types | |
236 | default: | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
237 | throw new AssertionError("ResponseType switch is not exhaustive - this is a bug!"); |
12 | 238 | } |
239 | } | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
240 | |
12 | 241 | private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp) |
242 | throws ServletException, IOException { | |
27
1f2a96efa69f
removes caching of main menu
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
243 | |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
244 | // 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
|
245 | 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
|
246 | 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
|
247 | 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
|
248 | 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
|
249 | 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
|
250 | 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
|
251 | 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
|
252 | } else { |
15
bb594abac796
language selector completed
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
253 | Locale sessionLocale = (Locale) session.getAttribute(Constants.SESSION_ATTR_LANGUAGE); |
bb594abac796
language selector completed
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
254 | resp.setLocale(sessionLocale); |
bb594abac796
language selector completed
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
255 | 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
|
256 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
257 | |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
258 | // set some internal request attributes |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
259 | req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req)); |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
260 | req.setAttribute(Constants.REQ_ATTR_MODULE_CLASSNAME, this.getClass().getName()); |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
261 | 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
|
262 | |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
263 | |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
264 | // call the handler, if available, or send an HTTP 404 error |
12 | 265 | Optional<HandlerMethod> mapping = findMapping(method, req); |
266 | if (mapping.isPresent()) { | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
267 | forwardAsSpecified(mapping.get().apply(req, resp), req, resp); |
12 | 268 | } else { |
269 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); | |
270 | } | |
271 | } | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
272 | |
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
273 | @Override |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
274 | 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
|
275 | throws ServletException, IOException { |
12 | 276 | 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
|
277 | } |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
278 | |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
279 | @Override |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
280 | 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
|
281 | throws ServletException, IOException { |
12 | 282 | 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
|
283 | } |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
284 | } |