diff -r 1574965c7dc7 -r 57cfb94ab99f src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java --- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java Wed May 13 21:46:26 2020 +0200 +++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java Thu May 14 22:48:01 2020 +0200 @@ -39,6 +39,7 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; +import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.sql.Connection; @@ -225,6 +226,18 @@ } /** + * @param req the servlet request object + * @param location the location where to redirect + * @see Constants#REQ_ATTR_REDIRECT_LOCATION + */ + public void setRedirectLocation(HttpServletRequest req, String location) { + if (location.startsWith("./")) { + location = location.replaceFirst("\\./", Functions.baseHref(req)); + } + req.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, location); + } + + /** * Specifies the name of an additional stylesheet used by the module. *

* Setting an additional stylesheet is optional, but quite common for HTML @@ -240,6 +253,30 @@ req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Functions.enforceExt(stylesheet, ".css")); } + /** + * Obtains a request parameter of the specified type. + * The specified type must have a single-argument constructor accepting a string to perform conversion. + * The constructor of the specified type may throw an exception on conversion failures. + * + * @param req the servlet request object + * @param clazz the class object of the expected type + * @param name the name of the parameter + * @param the expected type + * @return the parameter value or an empty optional, if no parameter with the specified name was found + */ + public Optional getParameter(HttpServletRequest req, Class clazz, String name) { + final String paramValue = req.getParameter(name); + if (paramValue == null) return Optional.empty(); + if (clazz.equals(String.class)) return Optional.of((T)paramValue); + try { + final Constructor ctor = clazz.getConstructor(String.class); + return Optional.of(ctor.newInstance(paramValue)); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + + } + private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { @@ -287,6 +324,7 @@ } // set some internal request attributes + req.setAttribute(Constants.REQ_ATTR_BASE_HREF, Functions.baseHref(req)); req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req)); Optional.ofNullable(moduleInfo).ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy));