1.1 --- a/src/java/de/uapcore/lightpit/AbstractLightPITServlet.java Sat Dec 23 17:28:19 2017 +0100 1.2 +++ b/src/java/de/uapcore/lightpit/AbstractLightPITServlet.java Tue Dec 26 17:36:47 2017 +0100 1.3 @@ -31,13 +31,17 @@ 1.4 import java.io.IOException; 1.5 import java.lang.reflect.Method; 1.6 import java.lang.reflect.Modifier; 1.7 +import java.util.Arrays; 1.8 import java.util.HashMap; 1.9 +import java.util.List; 1.10 +import java.util.Locale; 1.11 import java.util.Map; 1.12 import java.util.Optional; 1.13 import javax.servlet.ServletException; 1.14 import javax.servlet.http.HttpServlet; 1.15 import javax.servlet.http.HttpServletRequest; 1.16 import javax.servlet.http.HttpServletResponse; 1.17 +import javax.servlet.http.HttpSession; 1.18 import org.slf4j.Logger; 1.19 import org.slf4j.LoggerFactory; 1.20 1.21 @@ -49,6 +53,8 @@ 1.22 1.23 private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class); 1.24 1.25 + private static final String HTML_FULL_DISPATCHER = Functions.jspPath("html_full"); 1.26 + 1.27 /** 1.28 * Store a reference to the annotation for quicker access. 1.29 */ 1.30 @@ -164,29 +170,43 @@ 1.31 LOG.trace("{} destroyed", getServletName()); 1.32 } 1.33 1.34 + /** 1.35 + * Sets the name of the dynamic fragment. 1.36 + * 1.37 + * It is sufficient to specify the name without any extension. The extension 1.38 + * is added automatically if not specified. 1.39 + * 1.40 + * The fragment must be located in the dynamic fragments folder. 1.41 + * 1.42 + * @param req the servlet request object 1.43 + * @param fragmentName the name of the fragment 1.44 + * @see Constants#DYN_FRAGMENT_PATH_PREFIX 1.45 + */ 1.46 + public void setDynamicFragment(HttpServletRequest req, String fragmentName) { 1.47 + req.setAttribute(Constants.REQ_ATTR_FRAGMENT, Functions.dynFragmentPath(fragmentName)); 1.48 + } 1.49 1.50 /** 1.51 - * Sets several requests attributes, that can be used by the JSP. 1.52 + * Specifies the name of an additional stylesheet used by the module. 1.53 + * 1.54 + * Setting an additional stylesheet is optional, but quite common for HTML 1.55 + * output. 1.56 + * 1.57 + * It is sufficient to specify the name without any extension. The extension 1.58 + * is added automatically if not specified. 1.59 * 1.60 * @param req the servlet request object 1.61 - * @see Constants#REQ_ATTR_PATH 1.62 - * @see Constants#REQ_ATTR_MODULE_CLASSNAME 1.63 - * @see Constants#REQ_ATTR_MODULE_INFO 1.64 + * @param stylesheet the name of the stylesheet 1.65 */ 1.66 - private void setGenericRequestAttributes(HttpServletRequest req) { 1.67 - req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req)); 1.68 - 1.69 - req.setAttribute(Constants.REQ_ATTR_MODULE_CLASSNAME, this.getClass().getName()); 1.70 - 1.71 - moduleInfoELProxy.ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy)); 1.72 + public void setStylesheet(HttpServletRequest req, String stylesheet) { 1.73 + req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Functions.enforceExt(stylesheet, ".css")); 1.74 } 1.75 1.76 private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp) 1.77 throws IOException, ServletException { 1.78 1.79 - setGenericRequestAttributes(req); 1.80 req.setAttribute(Constants.REQ_ATTR_MENU, getModuleManager().getMainMenu()); 1.81 - req.getRequestDispatcher(Functions.jspPath("full.jsp")).forward(req, resp); 1.82 + req.getRequestDispatcher(HTML_FULL_DISPATCHER).forward(req, resp); 1.83 } 1.84 1.85 private Optional<HandlerMethod> findMapping(HttpMethod method, HttpServletRequest req) { 1.86 @@ -212,6 +232,22 @@ 1.87 1.88 private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp) 1.89 throws ServletException, IOException { 1.90 + 1.91 + HttpSession session = req.getSession(); 1.92 + 1.93 + // choose the requested language as session language (if available) or fall back to english, otherwise 1.94 + if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) { 1.95 + Optional<List<String>> availableLanguages = Functions.availableLanguages(getServletContext()).map(Arrays::asList); 1.96 + Optional<Locale> reqLocale = Optional.of(req.getLocale()); 1.97 + Locale sessionLocale = reqLocale.filter((rl) -> availableLanguages.map((al) -> al.contains(rl.getLanguage())).orElse(false)).orElse(Locale.ENGLISH); 1.98 + session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, sessionLocale); 1.99 + LOG.debug("Settng language for new session {}: {}", session.getId(), sessionLocale.getDisplayLanguage()); 1.100 + } 1.101 + 1.102 + req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req)); 1.103 + req.setAttribute(Constants.REQ_ATTR_MODULE_CLASSNAME, this.getClass().getName()); 1.104 + moduleInfoELProxy.ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy)); 1.105 + 1.106 Optional<HandlerMethod> mapping = findMapping(method, req); 1.107 if (mapping.isPresent()) { 1.108 forwardAsSepcified(mapping.get().apply(req, resp), req, resp);