src/java/de/uapcore/lightpit/AbstractLightPITServlet.java

Sat, 23 Dec 2017 17:28:19 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 23 Dec 2017 17:28:19 +0100
changeset 12
005d27918b57
parent 11
737ab27e37b3
child 13
f4608ad6c947
permissions
-rw-r--r--

implements ResponseTypes

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;
7
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.ServletException;
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 import javax.servlet.http.HttpServlet;
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.HttpServletRequest;
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.HttpServletResponse;
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
41 import org.slf4j.Logger;
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
42 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
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 {
7
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49
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);
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
51
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 * Store a reference to the annotation for quicker access.
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 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
56
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
57 /**
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
58 * 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
59 */
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
60 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
61
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
62
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
63 @FunctionalInterface
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
64 private static interface HandlerMethod {
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
65 ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException, ServletException;
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
66 }
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
67
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
68 /**
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
69 * Invocation mapping gathered from the {@link RequestMapping} annotations.
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
70 */
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
71 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
72
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
73 /**
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
74 * 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
75 * @return the module manager
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
76 */
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
77 protected final ModuleManager getModuleManager() {
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
78 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
79 }
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
80
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
81 private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp)
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
82 throws IOException, ServletException {
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
83 try {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
84 LOG.debug("invoke {}", method.getName());
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
85 return (ResponseType) method.invoke(this, req, resp);
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
86 } catch (ReflectiveOperationException | ClassCastException ex) {
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
87 LOG.error(String.format("invocation of method %s failed", method.getName()), ex);
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
88 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
89 return ResponseType.NONE;
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
90 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
91 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
92
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
93 @Override
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
94 public void init() throws ServletException {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
95 moduleInfo = Optional.ofNullable(this.getClass().getAnnotation(LightPITModule.class));
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
96 moduleInfoELProxy = moduleInfo.map(LightPITModule.ELProxy::convert);
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
97
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
98 if (moduleInfo.isPresent()) {
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
99 scanForRequestMappings();
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
100 }
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
101
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
102 LOG.trace("{} initialized", getServletName());
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
103 }
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
104
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
105 private void scanForRequestMappings() {
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
106 try {
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
107 Method[] methods = getClass().getDeclaredMethods();
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
108 for (Method method : methods) {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
109 Optional<RequestMapping> mapping = Optional.ofNullable(method.getAnnotation(RequestMapping.class));
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
110 if (mapping.isPresent()) {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
111 if (!Modifier.isPublic(method.getModifiers())) {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
112 LOG.warn("{} is annotated with {} but is not public",
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
113 method.getName(), RequestMapping.class.getSimpleName()
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
114 );
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
115 continue;
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
116 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
117 if (Modifier.isAbstract(method.getModifiers())) {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
118 LOG.warn("{} is annotated with {} but is abstract",
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
119 method.getName(), RequestMapping.class.getSimpleName()
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 continue;
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
122 }
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
123 if (!ResponseType.class.isAssignableFrom(method.getReturnType())) {
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
124 LOG.warn("{} is annotated with {} but has the wrong return type - 'ResponseType' required",
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
125 method.getName(), RequestMapping.class.getSimpleName()
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
126 );
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
127 continue;
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
128 }
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
129
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
130 Class<?>[] params = method.getParameterTypes();
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
131 if (params.length == 2
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
132 && HttpServletRequest.class.isAssignableFrom(params[0])
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
133 && HttpServletResponse.class.isAssignableFrom(params[1])) {
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
134
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
135 if (mappings.computeIfAbsent(mapping.get().method(), k -> new HashMap<>()).
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
136 putIfAbsent(mapping.get().requestPath(),
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
137 (req, resp) -> invokeMapping(method, req, resp)) != null) {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
138 LOG.warn("{} {} has multiple mappings",
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
139 mapping.get().method(),
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
140 mapping.get().requestPath()
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
141 );
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
142 }
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
143
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
144 LOG.info("{} {} maps to {}",
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
145 mapping.get().method(),
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
146 mapping.get().requestPath(),
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
147 method.getName()
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 } else {
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
150 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
151 method.getName(), RequestMapping.class.getSimpleName()
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
152 );
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 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
155 }
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
156 } catch (SecurityException ex) {
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
157 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
158 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
159 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
160
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
161 @Override
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
162 public void destroy() {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
163 mappings.clear();
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
164 LOG.trace("{} destroyed", getServletName());
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
165 }
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 * 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
170 *
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
171 * @param req the servlet request object
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
172 * @see Constants#REQ_ATTR_PATH
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
173 * @see Constants#REQ_ATTR_MODULE_CLASSNAME
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
174 * @see Constants#REQ_ATTR_MODULE_INFO
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 private void setGenericRequestAttributes(HttpServletRequest req) {
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
177 req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req));
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
178
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
179 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
180
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
181 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
182 }
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
183
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
184 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
185 throws IOException, ServletException {
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
186
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
187 setGenericRequestAttributes(req);
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
188 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
189 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
190 }
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
191
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
192 private Optional<HandlerMethod> findMapping(HttpMethod method, HttpServletRequest req) {
11
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
193 return Optional.ofNullable(mappings.get(method)).map(
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
194 (rm) -> rm.get(Optional.ofNullable(req.getPathInfo()).orElse(""))
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
195 );
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
196 }
737ab27e37b3 implements simple request mapper
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
197
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
198 private void forwardAsSepcified(ResponseType type, HttpServletRequest req, HttpServletResponse resp)
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
199 throws ServletException, IOException {
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
200 switch (type) {
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
201 case NONE: return;
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
202 case HTML_FULL:
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
203 forwardToFullView(req, resp);
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
204 return;
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
205 // TODO: implement remaining response types
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
206 default:
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
207 // this code should be unreachable
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
208 LOG.error("ResponseType switch is not exhaustive - this is a bug!");
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
209 throw new UnsupportedOperationException();
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
210 }
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
211 }
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
212
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
213 private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp)
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
214 throws ServletException, IOException {
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
215 Optional<HandlerMethod> mapping = findMapping(method, req);
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
216 if (mapping.isPresent()) {
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
217 forwardAsSepcified(mapping.get().apply(req, resp), req, resp);
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
218 } else {
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
219 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
220 }
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
221 }
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
222
7
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 @Override
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 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
225 throws ServletException, IOException {
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
226 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
227 }
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 @Override
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 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
231 throws ServletException, IOException {
12
005d27918b57 implements ResponseTypes
Mike Becker <universe@uap-core.de>
parents: 11
diff changeset
232 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
233 }
598670d5b0b8 core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 }

mercurial