Sat, 30 Dec 2017 20:41:55 +0100
adds getter for the database facade to the abstract servlet
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. |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2017 Mike Becker. All rights reserved. |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
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. |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | * |
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 | |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | import java.io.IOException; |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
32 | import java.lang.reflect.Method; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
33 | import java.lang.reflect.Modifier; |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
34 | import java.util.Arrays; |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
35 | import java.util.HashMap; |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
36 | import java.util.List; |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
37 | import java.util.Locale; |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
38 | import java.util.Map; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
39 | import java.util.Optional; |
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | 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
|
41 | 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
|
42 | 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
|
43 | 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
|
44 | import javax.servlet.http.HttpSession; |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
45 | import org.slf4j.Logger; |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
46 | import org.slf4j.LoggerFactory; |
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | /** |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | * 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
|
50 | * 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
|
51 | */ |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
52 | public abstract class AbstractLightPITServlet extends HttpServlet { |
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
54 | private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class); |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
55 | |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
56 | private static final String HTML_FULL_DISPATCHER = Functions.jspPath("html_full"); |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
57 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
58 | /** |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
59 | * Store a reference to the annotation for quicker access. |
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 | private Optional<LightPITModule> moduleInfo = Optional.empty(); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
62 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
63 | /** |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
64 | * 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
|
65 | */ |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
66 | private Optional<LightPITModule.ELProxy> moduleInfoELProxy = Optional.empty(); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
67 | |
12 | 68 | |
69 | @FunctionalInterface | |
70 | private static interface HandlerMethod { | |
71 | ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException, ServletException; | |
72 | } | |
73 | ||
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
74 | /** |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
75 | * Invocation mapping gathered from the {@link RequestMapping} annotations. |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
76 | */ |
12 | 77 | 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
|
78 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
79 | /** |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
80 | * Gives implementing modules access to the {@link ModuleManager}. |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
81 | * @return the module manager |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
82 | */ |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
83 | protected final ModuleManager getModuleManager() { |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
84 | 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
|
85 | } |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
86 | |
17
d1036b776eee
adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
87 | /** |
d1036b776eee
adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
88 | * Gives implementing modules access to the {@link DatabaseFacade}. |
d1036b776eee
adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
89 | * @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
|
90 | */ |
d1036b776eee
adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
91 | 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
|
92 | 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
|
93 | } |
d1036b776eee
adds getter for the database facade to the abstract servlet
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
94 | |
12 | 95 | private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp) |
96 | throws IOException, ServletException { | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
97 | try { |
14
2b270c714678
changed logger implementation to log4j2
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
98 | LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName()); |
12 | 99 | return (ResponseType) method.invoke(this, req, resp); |
100 | } catch (ReflectiveOperationException | ClassCastException ex) { | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
101 | LOG.error(String.format("invocation of method %s failed", method.getName()), ex); |
12 | 102 | resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
103 | return ResponseType.NONE; | |
11
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 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
106 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
107 | @Override |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
108 | public void init() throws ServletException { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
109 | moduleInfo = Optional.ofNullable(this.getClass().getAnnotation(LightPITModule.class)); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
110 | moduleInfoELProxy = moduleInfo.map(LightPITModule.ELProxy::convert); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
111 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
112 | if (moduleInfo.isPresent()) { |
12 | 113 | scanForRequestMappings(); |
114 | } | |
115 | ||
116 | LOG.trace("{} initialized", getServletName()); | |
117 | } | |
118 | ||
119 | private void scanForRequestMappings() { | |
120 | try { | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
121 | Method[] methods = getClass().getDeclaredMethods(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
122 | for (Method method : methods) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
123 | Optional<RequestMapping> mapping = Optional.ofNullable(method.getAnnotation(RequestMapping.class)); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
124 | if (mapping.isPresent()) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
125 | if (!Modifier.isPublic(method.getModifiers())) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
126 | LOG.warn("{} is annotated with {} but is not public", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
127 | method.getName(), RequestMapping.class.getSimpleName() |
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 | continue; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
130 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
131 | if (Modifier.isAbstract(method.getModifiers())) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
132 | LOG.warn("{} is annotated with {} but is abstract", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
133 | method.getName(), RequestMapping.class.getSimpleName() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
134 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
135 | continue; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
136 | } |
12 | 137 | if (!ResponseType.class.isAssignableFrom(method.getReturnType())) { |
138 | LOG.warn("{} is annotated with {} but has the wrong return type - 'ResponseType' required", | |
139 | method.getName(), RequestMapping.class.getSimpleName() | |
140 | ); | |
141 | continue; | |
142 | } | |
143 | ||
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
144 | Class<?>[] params = method.getParameterTypes(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
145 | if (params.length == 2 |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
146 | && HttpServletRequest.class.isAssignableFrom(params[0]) |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
147 | && HttpServletResponse.class.isAssignableFrom(params[1])) { |
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<>()). |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
150 | putIfAbsent(mapping.get().requestPath(), |
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 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
158 | LOG.info("{} {} maps to {}", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
159 | mapping.get().method(), |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
160 | mapping.get().requestPath(), |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
161 | method.getName() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
162 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
163 | } else { |
12 | 164 | 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
|
165 | method.getName(), RequestMapping.class.getSimpleName() |
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 | } |
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 | } |
12 | 170 | } catch (SecurityException ex) { |
171 | 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
|
172 | } |
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 | @Override |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
176 | public void destroy() { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
177 | mappings.clear(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
178 | LOG.trace("{} destroyed", getServletName()); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
179 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
180 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
181 | /** |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
182 | * Sets the name of the dynamic fragment. |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
183 | * |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
184 | * 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
|
185 | * is added automatically if not specified. |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
186 | * |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
187 | * The fragment must be located in the dynamic fragments folder. |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
188 | * |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
189 | * @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
|
190 | * @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
|
191 | * @see Constants#DYN_FRAGMENT_PATH_PREFIX |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
192 | */ |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
193 | 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
|
194 | 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
|
195 | } |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
196 | |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
197 | /** |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
198 | * Specifies the name of an additional stylesheet used by the module. |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
199 | * |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
200 | * 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
|
201 | * output. |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
202 | * |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
203 | * 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
|
204 | * is added automatically if not specified. |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
205 | * |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
206 | * @param req the servlet request object |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
207 | * @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
|
208 | */ |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
209 | 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
|
210 | 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
|
211 | } |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
212 | |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
213 | 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
|
214 | throws IOException, ServletException { |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
215 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
216 | 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
|
217 | 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
|
218 | } |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
219 | |
12 | 220 | private Optional<HandlerMethod> findMapping(HttpMethod method, HttpServletRequest req) { |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
221 | return Optional.ofNullable(mappings.get(method)).map( |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
222 | (rm) -> rm.get(Optional.ofNullable(req.getPathInfo()).orElse("")) |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
223 | ); |
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 | |
12 | 226 | private void forwardAsSepcified(ResponseType type, HttpServletRequest req, HttpServletResponse resp) |
227 | throws ServletException, IOException { | |
228 | switch (type) { | |
229 | case NONE: return; | |
230 | case HTML_FULL: | |
231 | forwardToFullView(req, resp); | |
232 | return; | |
233 | // TODO: implement remaining response types | |
234 | default: | |
235 | // this code should be unreachable | |
236 | LOG.error("ResponseType switch is not exhaustive - this is a bug!"); | |
237 | throw new UnsupportedOperationException(); | |
238 | } | |
239 | } | |
240 | ||
241 | private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp) | |
242 | throws ServletException, IOException { | |
13
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
243 | |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
244 | HttpSession session = req.getSession(); |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
245 | |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
246 | // choose the requested language as session language (if available) or fall back to english, otherwise |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
247 | 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
|
248 | 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
|
249 | 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
|
250 | 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
|
251 | session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, sessionLocale); |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
252 | LOG.debug("Settng 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
|
253 | } else { |
15
bb594abac796
language selector completed
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
254 | Locale sessionLocale = (Locale) session.getAttribute(Constants.SESSION_ATTR_LANGUAGE); |
bb594abac796
language selector completed
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
255 | resp.setLocale(sessionLocale); |
bb594abac796
language selector completed
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
256 | 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
|
257 | } |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
258 | |
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()); |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
261 | moduleInfoELProxy.ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy)); |
f4608ad6c947
adds dynamic fragments to LightPIT request handling framework + basic language recognition code
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
262 | |
12 | 263 | Optional<HandlerMethod> mapping = findMapping(method, req); |
264 | if (mapping.isPresent()) { | |
265 | forwardAsSepcified(mapping.get().apply(req, resp), req, resp); | |
266 | } else { | |
267 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); | |
268 | } | |
269 | } | |
270 | ||
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
271 | @Override |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
272 | 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
|
273 | throws ServletException, IOException { |
12 | 274 | 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
|
275 | } |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
276 | |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
277 | @Override |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
278 | 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
|
279 | throws ServletException, IOException { |
12 | 280 | 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
|
281 | } |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
282 | } |