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

changeset 131
67df332e3146
parent 130
7ef369744fd1
child 145
6d2d69fd1c12
--- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Thu Oct 15 18:36:05 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Thu Oct 15 20:02:30 2020 +0200
@@ -194,12 +194,15 @@
                         try {
                             PathPattern pathPattern = new PathPattern(mapping.get().requestPath());
 
-                            if (mappings
-                                    .computeIfAbsent(mapping.get().method(), k -> new HashMap<>())
-                                    .putIfAbsent(pathPattern, method) != null) {
-                                LOG.warn("{} {} has multiple mappings",
+                            final var methodMappings = mappings.computeIfAbsent(mapping.get().method(), k -> new HashMap<>());
+                            final var currentMapping = methodMappings.putIfAbsent(pathPattern, method);
+                            if (currentMapping != null) {
+                                LOG.warn("Cannot map {} {} to {} in class {} - this would override the mapping to {}",
                                         mapping.get().method(),
-                                        mapping.get().requestPath()
+                                        mapping.get().requestPath(),
+                                        method.getName(),
+                                        getClass().getSimpleName(),
+                                        currentMapping.getName()
                                 );
                             }
 
@@ -296,6 +299,32 @@
         req.setAttribute(Constants.REQ_ATTR_VIEWMODEL, viewModel);
     }
 
+    private <T> Optional<T> parseParameter(String paramValue, Class<T> clazz) {
+        if (paramValue == null) return Optional.empty();
+        if (clazz.equals(Boolean.class)) {
+            if (paramValue.toLowerCase().equals("false") || paramValue.equals("0")) {
+                return Optional.of((T) Boolean.FALSE);
+            } else {
+                return Optional.of((T) Boolean.TRUE);
+            }
+        }
+        if (clazz.equals(String.class)) return Optional.of((T) paramValue);
+        if (java.sql.Date.class.isAssignableFrom(clazz)) {
+            try {
+                return Optional.of((T) java.sql.Date.valueOf(paramValue));
+            } catch (IllegalArgumentException ex) {
+                return Optional.empty();
+            }
+        }
+        try {
+            final Constructor<T> ctor = clazz.getConstructor(String.class);
+            return Optional.of(ctor.newInstance(paramValue));
+        } catch (ReflectiveOperationException e) {
+            // does not type check and is not convertible - treat as if the parameter was never set
+            return Optional.empty();
+        }
+    }
+
     /**
      * Obtains a request parameter of the specified type.
      * The specified type must have a single-argument constructor accepting a string to perform conversion.
@@ -322,30 +351,7 @@
             }
             return Optional.of(array);
         } else {
-            final String paramValue = req.getParameter(name);
-            if (paramValue == null) return Optional.empty();
-            if (clazz.equals(Boolean.class)) {
-                if (paramValue.toLowerCase().equals("false") || paramValue.equals("0")) {
-                    return Optional.of((T) Boolean.FALSE);
-                } else {
-                    return Optional.of((T) Boolean.TRUE);
-                }
-            }
-            if (clazz.equals(String.class)) return Optional.of((T) paramValue);
-            if (java.sql.Date.class.isAssignableFrom(clazz)) {
-                try {
-                    return Optional.of((T) java.sql.Date.valueOf(paramValue));
-                } catch (IllegalArgumentException ex) {
-                    return Optional.empty();
-                }
-            }
-            try {
-                final Constructor<T> ctor = clazz.getConstructor(String.class);
-                return Optional.of(ctor.newInstance(paramValue));
-            } catch (ReflectiveOperationException e) {
-                // does not type check and is not convertible - treat as if the parameter was never set
-                return Optional.empty();
-            }
+            return parseParameter(req.getParameter(name), clazz);
         }
     }
 

mercurial