Sun, 17 Dec 2017 01:45:28 +0100
implements simple request mapper
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; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
34 | import java.util.HashMap; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
35 | import java.util.Map; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
36 | import java.util.Optional; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
37 | import java.util.function.BiConsumer; |
7
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.ServletException; |
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.HttpServlet; |
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.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
|
41 | import javax.servlet.http.HttpServletResponse; |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
42 | import org.slf4j.Logger; |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
43 | 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
|
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 | /** |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | * 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
|
47 | * 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
|
48 | */ |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
49 | 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
|
50 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
51 | 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
|
52 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
53 | /** |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
54 | * Store a reference to the annotation for quicker access. |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
55 | */ |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
56 | 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
|
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 | * 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
|
60 | */ |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
61 | 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
|
62 | |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
63 | /** |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
64 | * Invocation mapping gathered from the {@link RequestMapping} annotations. |
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 final Map<HttpMethod, Map<String, BiConsumer<HttpServletRequest, HttpServletResponse>>> mappings = new HashMap<>(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
67 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
68 | /** |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
69 | * 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
|
70 | * @return the module manager |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
71 | */ |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
72 | protected final ModuleManager getModuleManager() { |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
73 | 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
|
74 | } |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
75 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
76 | private void invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
77 | try { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
78 | LOG.debug("invoke {}", method.getName()); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
79 | method.invoke(this, req, resp); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
80 | } catch (ReflectiveOperationException ex) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
81 | LOG.error(String.format("invocation of method %s failed", method.getName()), ex); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
82 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
83 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
84 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
85 | @Override |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
86 | public void init() throws ServletException { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
87 | moduleInfo = Optional.ofNullable(this.getClass().getAnnotation(LightPITModule.class)); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
88 | moduleInfoELProxy = moduleInfo.map(LightPITModule.ELProxy::convert); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
89 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
90 | if (moduleInfo.isPresent()) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
91 | Method[] methods = getClass().getDeclaredMethods(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
92 | for (Method method : methods) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
93 | Optional<RequestMapping> mapping = Optional.ofNullable(method.getAnnotation(RequestMapping.class)); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
94 | if (mapping.isPresent()) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
95 | if (!Modifier.isPublic(method.getModifiers())) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
96 | LOG.warn("{} is annotated with {} but is not public", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
97 | method.getName(), RequestMapping.class.getSimpleName() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
98 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
99 | continue; |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
100 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
101 | if (Modifier.isAbstract(method.getModifiers())) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
102 | LOG.warn("{} is annotated with {} but is abstract", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
103 | method.getName(), RequestMapping.class.getSimpleName() |
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 | continue; |
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 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
108 | Class<?>[] params = method.getParameterTypes(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
109 | if (params.length == 2 |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
110 | && HttpServletRequest.class.isAssignableFrom(params[0]) |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
111 | && HttpServletResponse.class.isAssignableFrom(params[1])) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
112 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
113 | if (mappings.computeIfAbsent(mapping.get().method(), k -> new HashMap<>()). |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
114 | putIfAbsent(mapping.get().requestPath(), |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
115 | (req, resp) -> invokeMapping(method, req, resp)) != null) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
116 | LOG.warn("{} {} has multiple mappings", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
117 | mapping.get().method(), |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
118 | mapping.get().requestPath() |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
119 | ); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
120 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
121 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
122 | LOG.info("{} {} maps to {}", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
123 | mapping.get().method(), |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
124 | mapping.get().requestPath(), |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
125 | method.getName() |
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 | } else { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
128 | LOG.warn("{} is annotated with {} but has the wrong signature - (HttpServletRequest,HttpServletResponse) required", |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
129 | method.getName(), RequestMapping.class.getSimpleName() |
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 | } |
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 | } |
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 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
136 | LOG.trace("{} initialized", getServletName()); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
137 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
138 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
139 | @Override |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
140 | public void destroy() { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
141 | mappings.clear(); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
142 | LOG.trace("{} destroyed", getServletName()); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
143 | } |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
144 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
145 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
146 | /** |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
147 | * Sets several requests attributes, that can be used by the JSP. |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
148 | * |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
149 | * @param req the servlet request object |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
150 | * @see Constants#REQ_ATTR_PATH |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
151 | * @see Constants#REQ_ATTR_MODULE_CLASSNAME |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
152 | * @see Constants#REQ_ATTR_MODULE_INFO |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
153 | */ |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
154 | private void setGenericRequestAttributes(HttpServletRequest req) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
155 | req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req)); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
156 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
157 | req.setAttribute(Constants.REQ_ATTR_MODULE_CLASSNAME, this.getClass().getName()); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
158 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
159 | moduleInfoELProxy.ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy)); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
160 | } |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
161 | |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
162 | 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
|
163 | throws IOException, ServletException { |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
164 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
165 | setGenericRequestAttributes(req); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
166 | req.setAttribute(Constants.REQ_ATTR_MENU, getModuleManager().getMainMenu()); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
167 | req.getRequestDispatcher(Functions.jspPath("full.jsp")).forward(req, resp); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
168 | } |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
169 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
170 | private Optional<BiConsumer<HttpServletRequest, HttpServletResponse>> findMapping(HttpMethod method, HttpServletRequest req) { |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
171 | return Optional.ofNullable(mappings.get(method)).map( |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
172 | (rm) -> rm.get(Optional.ofNullable(req.getPathInfo()).orElse("")) |
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 | |
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
176 | @Override |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
177 | 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
|
178 | throws ServletException, IOException { |
11
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 | findMapping(HttpMethod.GET, req).ifPresent((consumer) -> consumer.accept(req, resp)); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
181 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
182 | // TODO: let the invoked handler decide (signature must be changed from a BiConsumer to a BiFunction) |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
183 | // TODO: we should call a default handler, if no specific mapping could be found |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
184 | forwardToFullView(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
|
185 | } |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
186 | |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
187 | @Override |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
188 | 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
|
189 | throws ServletException, IOException { |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
190 | |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
191 | findMapping(HttpMethod.POST, req).ifPresent((consumer) -> consumer.accept(req, resp)); |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
192 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
193 | forwardToFullView(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
|
194 | } |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
195 | } |