29 package de.uapcore.lightpit; |
29 package de.uapcore.lightpit; |
30 |
30 |
31 import java.io.IOException; |
31 import java.io.IOException; |
32 import java.lang.reflect.Method; |
32 import java.lang.reflect.Method; |
33 import java.lang.reflect.Modifier; |
33 import java.lang.reflect.Modifier; |
|
34 import java.util.Arrays; |
34 import java.util.HashMap; |
35 import java.util.HashMap; |
|
36 import java.util.List; |
|
37 import java.util.Locale; |
35 import java.util.Map; |
38 import java.util.Map; |
36 import java.util.Optional; |
39 import java.util.Optional; |
37 import javax.servlet.ServletException; |
40 import javax.servlet.ServletException; |
38 import javax.servlet.http.HttpServlet; |
41 import javax.servlet.http.HttpServlet; |
39 import javax.servlet.http.HttpServletRequest; |
42 import javax.servlet.http.HttpServletRequest; |
40 import javax.servlet.http.HttpServletResponse; |
43 import javax.servlet.http.HttpServletResponse; |
|
44 import javax.servlet.http.HttpSession; |
41 import org.slf4j.Logger; |
45 import org.slf4j.Logger; |
42 import org.slf4j.LoggerFactory; |
46 import org.slf4j.LoggerFactory; |
43 |
47 |
44 /** |
48 /** |
45 * A special implementation of a HTTPServlet which is focused on implementing |
49 * A special implementation of a HTTPServlet which is focused on implementing |
46 * the necessary functionality for {@link LightPITModule}s. |
50 * the necessary functionality for {@link LightPITModule}s. |
47 */ |
51 */ |
48 public abstract class AbstractLightPITServlet extends HttpServlet { |
52 public abstract class AbstractLightPITServlet extends HttpServlet { |
49 |
53 |
50 private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class); |
54 private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class); |
|
55 |
|
56 private static final String HTML_FULL_DISPATCHER = Functions.jspPath("html_full"); |
51 |
57 |
52 /** |
58 /** |
53 * Store a reference to the annotation for quicker access. |
59 * Store a reference to the annotation for quicker access. |
54 */ |
60 */ |
55 private Optional<LightPITModule> moduleInfo = Optional.empty(); |
61 private Optional<LightPITModule> moduleInfo = Optional.empty(); |
162 public void destroy() { |
168 public void destroy() { |
163 mappings.clear(); |
169 mappings.clear(); |
164 LOG.trace("{} destroyed", getServletName()); |
170 LOG.trace("{} destroyed", getServletName()); |
165 } |
171 } |
166 |
172 |
167 |
173 /** |
168 /** |
174 * Sets the name of the dynamic fragment. |
169 * Sets several requests attributes, that can be used by the JSP. |
175 * |
|
176 * It is sufficient to specify the name without any extension. The extension |
|
177 * is added automatically if not specified. |
|
178 * |
|
179 * The fragment must be located in the dynamic fragments folder. |
170 * |
180 * |
171 * @param req the servlet request object |
181 * @param req the servlet request object |
172 * @see Constants#REQ_ATTR_PATH |
182 * @param fragmentName the name of the fragment |
173 * @see Constants#REQ_ATTR_MODULE_CLASSNAME |
183 * @see Constants#DYN_FRAGMENT_PATH_PREFIX |
174 * @see Constants#REQ_ATTR_MODULE_INFO |
184 */ |
175 */ |
185 public void setDynamicFragment(HttpServletRequest req, String fragmentName) { |
176 private void setGenericRequestAttributes(HttpServletRequest req) { |
186 req.setAttribute(Constants.REQ_ATTR_FRAGMENT, Functions.dynFragmentPath(fragmentName)); |
177 req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req)); |
187 } |
178 |
188 |
179 req.setAttribute(Constants.REQ_ATTR_MODULE_CLASSNAME, this.getClass().getName()); |
189 /** |
180 |
190 * Specifies the name of an additional stylesheet used by the module. |
181 moduleInfoELProxy.ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy)); |
191 * |
|
192 * Setting an additional stylesheet is optional, but quite common for HTML |
|
193 * output. |
|
194 * |
|
195 * It is sufficient to specify the name without any extension. The extension |
|
196 * is added automatically if not specified. |
|
197 * |
|
198 * @param req the servlet request object |
|
199 * @param stylesheet the name of the stylesheet |
|
200 */ |
|
201 public void setStylesheet(HttpServletRequest req, String stylesheet) { |
|
202 req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Functions.enforceExt(stylesheet, ".css")); |
182 } |
203 } |
183 |
204 |
184 private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp) |
205 private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp) |
185 throws IOException, ServletException { |
206 throws IOException, ServletException { |
186 |
207 |
187 setGenericRequestAttributes(req); |
|
188 req.setAttribute(Constants.REQ_ATTR_MENU, getModuleManager().getMainMenu()); |
208 req.setAttribute(Constants.REQ_ATTR_MENU, getModuleManager().getMainMenu()); |
189 req.getRequestDispatcher(Functions.jspPath("full.jsp")).forward(req, resp); |
209 req.getRequestDispatcher(HTML_FULL_DISPATCHER).forward(req, resp); |
190 } |
210 } |
191 |
211 |
192 private Optional<HandlerMethod> findMapping(HttpMethod method, HttpServletRequest req) { |
212 private Optional<HandlerMethod> findMapping(HttpMethod method, HttpServletRequest req) { |
193 return Optional.ofNullable(mappings.get(method)).map( |
213 return Optional.ofNullable(mappings.get(method)).map( |
194 (rm) -> rm.get(Optional.ofNullable(req.getPathInfo()).orElse("")) |
214 (rm) -> rm.get(Optional.ofNullable(req.getPathInfo()).orElse("")) |
210 } |
230 } |
211 } |
231 } |
212 |
232 |
213 private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp) |
233 private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp) |
214 throws ServletException, IOException { |
234 throws ServletException, IOException { |
|
235 |
|
236 HttpSession session = req.getSession(); |
|
237 |
|
238 // choose the requested language as session language (if available) or fall back to english, otherwise |
|
239 if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) { |
|
240 Optional<List<String>> availableLanguages = Functions.availableLanguages(getServletContext()).map(Arrays::asList); |
|
241 Optional<Locale> reqLocale = Optional.of(req.getLocale()); |
|
242 Locale sessionLocale = reqLocale.filter((rl) -> availableLanguages.map((al) -> al.contains(rl.getLanguage())).orElse(false)).orElse(Locale.ENGLISH); |
|
243 session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, sessionLocale); |
|
244 LOG.debug("Settng language for new session {}: {}", session.getId(), sessionLocale.getDisplayLanguage()); |
|
245 } |
|
246 |
|
247 req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req)); |
|
248 req.setAttribute(Constants.REQ_ATTR_MODULE_CLASSNAME, this.getClass().getName()); |
|
249 moduleInfoELProxy.ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy)); |
|
250 |
215 Optional<HandlerMethod> mapping = findMapping(method, req); |
251 Optional<HandlerMethod> mapping = findMapping(method, req); |
216 if (mapping.isPresent()) { |
252 if (mapping.isPresent()) { |
217 forwardAsSepcified(mapping.get().apply(req, resp), req, resp); |
253 forwardAsSepcified(mapping.get().apply(req, resp), req, resp); |
218 } else { |
254 } else { |
219 resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
255 resp.sendError(HttpServletResponse.SC_NOT_FOUND); |