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

changeset 47
57cfb94ab99f
parent 45
cc7f082c5ef3
child 53
6a8498291606
     1.1 --- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Wed May 13 21:46:26 2020 +0200
     1.2 +++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Thu May 14 22:48:01 2020 +0200
     1.3 @@ -39,6 +39,7 @@
     1.4  import javax.servlet.http.HttpServletResponse;
     1.5  import javax.servlet.http.HttpSession;
     1.6  import java.io.IOException;
     1.7 +import java.lang.reflect.Constructor;
     1.8  import java.lang.reflect.Method;
     1.9  import java.lang.reflect.Modifier;
    1.10  import java.sql.Connection;
    1.11 @@ -225,6 +226,18 @@
    1.12      }
    1.13  
    1.14      /**
    1.15 +     * @param req      the servlet request object
    1.16 +     * @param location the location where to redirect
    1.17 +     * @see Constants#REQ_ATTR_REDIRECT_LOCATION
    1.18 +     */
    1.19 +    public void setRedirectLocation(HttpServletRequest req, String location) {
    1.20 +        if (location.startsWith("./")) {
    1.21 +            location = location.replaceFirst("\\./", Functions.baseHref(req));
    1.22 +        }
    1.23 +        req.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, location);
    1.24 +    }
    1.25 +
    1.26 +    /**
    1.27       * Specifies the name of an additional stylesheet used by the module.
    1.28       * <p>
    1.29       * Setting an additional stylesheet is optional, but quite common for HTML
    1.30 @@ -240,6 +253,30 @@
    1.31          req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Functions.enforceExt(stylesheet, ".css"));
    1.32      }
    1.33  
    1.34 +    /**
    1.35 +     * Obtains a request parameter of the specified type.
    1.36 +     * The specified type must have a single-argument constructor accepting a string to perform conversion.
    1.37 +     * The constructor of the specified type may throw an exception on conversion failures.
    1.38 +     *
    1.39 +     * @param req the servlet request object
    1.40 +     * @param clazz the class object of the expected type
    1.41 +     * @param name the name of the parameter
    1.42 +     * @param <T> the expected type
    1.43 +     * @return the parameter value or an empty optional, if no parameter with the specified name was found
    1.44 +     */
    1.45 +    public<T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) {
    1.46 +        final String paramValue = req.getParameter(name);
    1.47 +        if (paramValue == null) return Optional.empty();
    1.48 +        if (clazz.equals(String.class)) return Optional.of((T)paramValue);
    1.49 +        try {
    1.50 +            final Constructor<T> ctor = clazz.getConstructor(String.class);
    1.51 +            return Optional.of(ctor.newInstance(paramValue));
    1.52 +        } catch (ReflectiveOperationException e) {
    1.53 +            throw new RuntimeException(e);
    1.54 +        }
    1.55 +
    1.56 +    }
    1.57 +
    1.58      private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp)
    1.59              throws IOException, ServletException {
    1.60  
    1.61 @@ -287,6 +324,7 @@
    1.62          }
    1.63  
    1.64          // set some internal request attributes
    1.65 +        req.setAttribute(Constants.REQ_ATTR_BASE_HREF, Functions.baseHref(req));
    1.66          req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req));
    1.67          Optional.ofNullable(moduleInfo).ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy));
    1.68  

mercurial