diff -r 1e6f16fad3a5 -r 4f912cd42876 src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java --- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java Thu Nov 05 13:37:48 2020 +0100 +++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java Fri Nov 06 10:50:32 2020 +0100 @@ -53,7 +53,7 @@ private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class); - private static final String SITE_JSP = Functions.jspPath("site"); + private static final String SITE_JSP = jspPath("site"); @FunctionalInterface @@ -102,7 +102,7 @@ */ private DataAccessObjects createDataAccessObjects(Connection connection) throws SQLException { final var df = (DataSourceProvider) getServletContext().getAttribute(DataSourceProvider.Companion.getSC_ATTR_NAME()); - if (df.getDialect() == DatabaseDialect.Postgres) { + if (df.getDialect() == DataSourceProvider.Dialect.Postgres) { return new PGDataAccessObjects(connection); } throw new UnsupportedOperationException("Non-exhaustive if-else - this is a bug."); @@ -238,7 +238,7 @@ * @see Constants#REQ_ATTR_CONTENT_PAGE */ protected void setContentPage(HttpServletRequest req, String pageName) { - req.setAttribute(Constants.REQ_ATTR_CONTENT_PAGE, Functions.jspPath(pageName)); + req.setAttribute(Constants.REQ_ATTR_CONTENT_PAGE, jspPath(pageName)); } /** @@ -249,7 +249,7 @@ * @see Constants#REQ_ATTR_NAVIGATION */ protected void setNavigationMenu(HttpServletRequest req, String jspName) { - req.setAttribute(Constants.REQ_ATTR_NAVIGATION, Functions.jspPath(jspName)); + req.setAttribute(Constants.REQ_ATTR_NAVIGATION, jspPath(jspName)); } /** @@ -259,7 +259,7 @@ */ protected void setRedirectLocation(HttpServletRequest req, String location) { if (location.startsWith("./")) { - location = location.replaceFirst("\\./", Functions.baseHref(req)); + location = location.replaceFirst("\\./", baseHref(req)); } req.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, location); } @@ -277,7 +277,7 @@ * @param stylesheet the name of the stylesheet */ public void setStylesheet(HttpServletRequest req, String stylesheet) { - req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Functions.enforceExt(stylesheet, ".css")); + req.setAttribute(Constants.REQ_ATTR_STYLESHEET, enforceExt(stylesheet, ".css")); } /** @@ -384,12 +384,32 @@ req.getRequestDispatcher(SITE_JSP).forward(req, resp); } + protected Optional availableLanguages() { + return Optional.ofNullable(getServletContext().getInitParameter(Constants.CTX_ATTR_LANGUAGES)).map((x) -> x.split("\\s*,\\s*")); + } + + private static String baseHref(HttpServletRequest req) { + return String.format("%s://%s:%d%s/", + req.getScheme(), + req.getServerName(), + req.getServerPort(), + req.getContextPath()); + } + + private static String enforceExt(String filename, String ext) { + return filename.endsWith(ext) ? filename : filename + ext; + } + + private static String jspPath(String filename) { + return enforceExt(Constants.JSP_PATH_PREFIX + filename, ".jsp"); + } + private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // choose the requested language as session language (if available) or fall back to english, otherwise HttpSession session = req.getSession(); if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) { - Optional> availableLanguages = Functions.availableLanguages(getServletContext()).map(Arrays::asList); + Optional> availableLanguages = availableLanguages().map(Arrays::asList); Optional reqLocale = Optional.of(req.getLocale()); Locale sessionLocale = reqLocale.filter((rl) -> availableLanguages.map((al) -> al.contains(rl.getLanguage())).orElse(false)).orElse(Locale.ENGLISH); session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, sessionLocale); @@ -401,8 +421,8 @@ } // set some internal request attributes - final String fullPath = Functions.fullPath(req); - req.setAttribute(Constants.REQ_ATTR_BASE_HREF, Functions.baseHref(req)); + final String fullPath = req.getServletPath() + Optional.ofNullable(req.getPathInfo()).orElse(""); + req.setAttribute(Constants.REQ_ATTR_BASE_HREF, baseHref(req)); req.setAttribute(Constants.REQ_ATTR_PATH, fullPath); req.setAttribute(Constants.REQ_ATTR_RESOURCE_BUNDLE, getResourceBundleName());