1.1 --- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java Thu Oct 15 18:36:05 2020 +0200 1.2 +++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java Thu Oct 15 20:02:30 2020 +0200 1.3 @@ -194,12 +194,15 @@ 1.4 try { 1.5 PathPattern pathPattern = new PathPattern(mapping.get().requestPath()); 1.6 1.7 - if (mappings 1.8 - .computeIfAbsent(mapping.get().method(), k -> new HashMap<>()) 1.9 - .putIfAbsent(pathPattern, method) != null) { 1.10 - LOG.warn("{} {} has multiple mappings", 1.11 + final var methodMappings = mappings.computeIfAbsent(mapping.get().method(), k -> new HashMap<>()); 1.12 + final var currentMapping = methodMappings.putIfAbsent(pathPattern, method); 1.13 + if (currentMapping != null) { 1.14 + LOG.warn("Cannot map {} {} to {} in class {} - this would override the mapping to {}", 1.15 mapping.get().method(), 1.16 - mapping.get().requestPath() 1.17 + mapping.get().requestPath(), 1.18 + method.getName(), 1.19 + getClass().getSimpleName(), 1.20 + currentMapping.getName() 1.21 ); 1.22 } 1.23 1.24 @@ -296,6 +299,32 @@ 1.25 req.setAttribute(Constants.REQ_ATTR_VIEWMODEL, viewModel); 1.26 } 1.27 1.28 + private <T> Optional<T> parseParameter(String paramValue, Class<T> clazz) { 1.29 + if (paramValue == null) return Optional.empty(); 1.30 + if (clazz.equals(Boolean.class)) { 1.31 + if (paramValue.toLowerCase().equals("false") || paramValue.equals("0")) { 1.32 + return Optional.of((T) Boolean.FALSE); 1.33 + } else { 1.34 + return Optional.of((T) Boolean.TRUE); 1.35 + } 1.36 + } 1.37 + if (clazz.equals(String.class)) return Optional.of((T) paramValue); 1.38 + if (java.sql.Date.class.isAssignableFrom(clazz)) { 1.39 + try { 1.40 + return Optional.of((T) java.sql.Date.valueOf(paramValue)); 1.41 + } catch (IllegalArgumentException ex) { 1.42 + return Optional.empty(); 1.43 + } 1.44 + } 1.45 + try { 1.46 + final Constructor<T> ctor = clazz.getConstructor(String.class); 1.47 + return Optional.of(ctor.newInstance(paramValue)); 1.48 + } catch (ReflectiveOperationException e) { 1.49 + // does not type check and is not convertible - treat as if the parameter was never set 1.50 + return Optional.empty(); 1.51 + } 1.52 + } 1.53 + 1.54 /** 1.55 * Obtains a request parameter of the specified type. 1.56 * The specified type must have a single-argument constructor accepting a string to perform conversion. 1.57 @@ -322,30 +351,7 @@ 1.58 } 1.59 return Optional.of(array); 1.60 } else { 1.61 - final String paramValue = req.getParameter(name); 1.62 - if (paramValue == null) return Optional.empty(); 1.63 - if (clazz.equals(Boolean.class)) { 1.64 - if (paramValue.toLowerCase().equals("false") || paramValue.equals("0")) { 1.65 - return Optional.of((T) Boolean.FALSE); 1.66 - } else { 1.67 - return Optional.of((T) Boolean.TRUE); 1.68 - } 1.69 - } 1.70 - if (clazz.equals(String.class)) return Optional.of((T) paramValue); 1.71 - if (java.sql.Date.class.isAssignableFrom(clazz)) { 1.72 - try { 1.73 - return Optional.of((T) java.sql.Date.valueOf(paramValue)); 1.74 - } catch (IllegalArgumentException ex) { 1.75 - return Optional.empty(); 1.76 - } 1.77 - } 1.78 - try { 1.79 - final Constructor<T> ctor = clazz.getConstructor(String.class); 1.80 - return Optional.of(ctor.newInstance(paramValue)); 1.81 - } catch (ReflectiveOperationException e) { 1.82 - // does not type check and is not convertible - treat as if the parameter was never set 1.83 - return Optional.empty(); 1.84 - } 1.85 + return parseParameter(req.getParameter(name), clazz); 1.86 } 1.87 } 1.88