makes every parameter in a handler method optional

Wed, 13 May 2020 18:40:52 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 13 May 2020 18:40:52 +0200
changeset 42
f962ff9dd44e
parent 41
4f1c026a8aab
child 43
9abf0bf44c7b

makes every parameter in a handler method optional

src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/modules/ErrorModule.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/modules/HomeModule.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/modules/LanguageModule.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/modules/VersionsModule.java file | annotate | diff | comparison | revisions
--- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Wed May 13 18:33:25 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Wed May 13 18:40:52 2020 +0200
@@ -98,7 +98,19 @@
     private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
         try {
             LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName());
-            return (ResponseType) method.invoke(this, req, resp, dao);
+            final var paramTypes = method.getParameterTypes();
+            final var paramValues = new Object[paramTypes.length];
+            for (int i = 0; i < paramTypes.length; i++) {
+                if (paramTypes[i].isAssignableFrom(HttpServletRequest.class)) {
+                    paramValues[i] = req;
+                } else if (paramTypes[i].isAssignableFrom(HttpServletResponse.class)) {
+                    paramValues[i] = resp;
+                }
+                if (paramTypes[i].isAssignableFrom(DataAccessObjects.class)) {
+                    paramValues[i] = dao;
+                }
+            }
+            return (ResponseType) method.invoke(this, paramValues);
         } catch (ReflectiveOperationException | ClassCastException ex) {
             LOG.error("invocation of method {} failed: {}", method.getName(), ex.getMessage());
             LOG.debug("Details: ", ex);
@@ -144,12 +156,13 @@
                         continue;
                     }
 
-                    Class<?>[] params = method.getParameterTypes();
-                    if (params.length == 3
-                            && HttpServletRequest.class.isAssignableFrom(params[0])
-                            && HttpServletResponse.class.isAssignableFrom(params[1])
-                            && DataAccessObjects.class.isAssignableFrom(params[2])) {
-
+                    boolean paramsInjectible = true;
+                    for (var param : method.getParameterTypes()) {
+                        paramsInjectible &= HttpServletRequest.class.isAssignableFrom(param)
+                                || HttpServletResponse.class.isAssignableFrom(param)
+                                || DataAccessObjects.class.isAssignableFrom(param);
+                    }
+                    if (paramsInjectible) {
                         final String requestPath = "/" + mapping.get().requestPath();
 
                         if (mappings
@@ -168,7 +181,7 @@
                                 method.getName()
                         );
                     } else {
-                        LOG.warn("{} is annotated with {} but has the wrong parameters - (HttpServletRequest,HttpServletResponse,DataAccessObjects) required",
+                        LOG.warn("{} is annotated with {} but has the wrong parameters - only HttpServletRequest. HttpServletResponse, and DataAccessObjects are allowed",
                                 method.getName(), RequestMapping.class.getSimpleName()
                         );
                     }
--- a/src/main/java/de/uapcore/lightpit/modules/ErrorModule.java	Wed May 13 18:33:25 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/ErrorModule.java	Wed May 13 18:40:52 2020 +0200
@@ -29,11 +29,9 @@
 package de.uapcore.lightpit.modules;
 
 import de.uapcore.lightpit.*;
-import de.uapcore.lightpit.dao.DataAccessObjects;
 
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
 
 /**
  * Entry point for the application.
@@ -62,17 +60,17 @@
     }
 
     @RequestMapping(requestPath = "404", method = HttpMethod.GET)
-    public ResponseType handle404(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) {
+    public ResponseType handle404(HttpServletRequest req) {
         return handle(req, 404);
     }
 
     @RequestMapping(requestPath = "403", method = HttpMethod.GET)
-    public ResponseType handle403(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) {
+    public ResponseType handle403(HttpServletRequest req) {
         return handle(req, 403);
     }
 
     @RequestMapping(requestPath = "500", method = HttpMethod.GET)
-    public ResponseType handle500(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) {
+    public ResponseType handle500(HttpServletRequest req) {
         return handle(req, 500);
     }
 }
--- a/src/main/java/de/uapcore/lightpit/modules/HomeModule.java	Wed May 13 18:33:25 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/HomeModule.java	Wed May 13 18:40:52 2020 +0200
@@ -29,11 +29,8 @@
 package de.uapcore.lightpit.modules;
 
 import de.uapcore.lightpit.*;
-import de.uapcore.lightpit.dao.DataAccessObjects;
 
 import javax.servlet.annotation.WebServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
 
 /**
  * Entry point for the application.
@@ -50,7 +47,7 @@
 public final class HomeModule extends AbstractLightPITServlet {
 
     @RequestMapping(method = HttpMethod.GET)
-    public ResponseType handle(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) {
+    public ResponseType handle() {
 
         return ResponseType.HTML_FULL;
     }
--- a/src/main/java/de/uapcore/lightpit/modules/LanguageModule.java	Wed May 13 18:33:25 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/LanguageModule.java	Wed May 13 18:40:52 2020 +0200
@@ -29,7 +29,6 @@
 package de.uapcore.lightpit.modules;
 
 import de.uapcore.lightpit.*;
-import de.uapcore.lightpit.dao.DataAccessObjects;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -86,7 +85,7 @@
     }
 
     @RequestMapping(method = HttpMethod.GET)
-    public ResponseType handle(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) {
+    public ResponseType handle(HttpServletRequest req) {
 
         req.setAttribute("languages", languages);
         req.setAttribute("browserLanguage", req.getLocale());
@@ -97,7 +96,7 @@
     }
 
     @RequestMapping(method = HttpMethod.POST)
-    public ResponseType switchLanguage(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) {
+    public ResponseType switchLanguage(HttpServletRequest req, HttpServletResponse resp) {
 
         Optional<Locale> chosenLanguage = Optional.ofNullable(req.getParameter("language"))
                 .map(Locale::forLanguageTag)
@@ -106,6 +105,6 @@
         chosenLanguage.ifPresent((l) -> req.getSession().setAttribute(Constants.SESSION_ATTR_LANGUAGE, l));
         chosenLanguage.ifPresent(resp::setLocale);
 
-        return handle(req, resp, dao);
+        return handle(req);
     }
 }
--- a/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Wed May 13 18:33:25 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Wed May 13 18:40:52 2020 +0200
@@ -34,7 +34,6 @@
 
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
 
 @LightPITModule(
         bundleBaseName = "localization.projects",
@@ -48,7 +47,7 @@
 public final class ProjectsModule extends AbstractLightPITServlet {
 
     @RequestMapping(method = HttpMethod.GET)
-    public ResponseType handle(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) {
+    public ResponseType handle(HttpServletRequest req, DataAccessObjects dao) {
 
         return ResponseType.HTML_FULL;
     }
--- a/src/main/java/de/uapcore/lightpit/modules/VersionsModule.java	Wed May 13 18:33:25 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/VersionsModule.java	Wed May 13 18:40:52 2020 +0200
@@ -33,7 +33,6 @@
 
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
 
 
 @LightPITModule(
@@ -47,7 +46,7 @@
 )
 public final class VersionsModule extends AbstractLightPITServlet {
     @RequestMapping(method = HttpMethod.GET)
-    public ResponseType handle(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) {
+    public ResponseType handle(HttpServletRequest req, DataAccessObjects dao) {
 
         return ResponseType.HTML_FULL;
     }

mercurial