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

changeset 83
24a3596b8f98
parent 80
27a25f32048e
child 86
0a658e53177c
equal deleted inserted replaced
82:4ec7f2600c83 83:24a3596b8f98
37 import javax.servlet.http.HttpServlet; 37 import javax.servlet.http.HttpServlet;
38 import javax.servlet.http.HttpServletRequest; 38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse; 39 import javax.servlet.http.HttpServletResponse;
40 import javax.servlet.http.HttpSession; 40 import javax.servlet.http.HttpSession;
41 import java.io.IOException; 41 import java.io.IOException;
42 import java.lang.reflect.Constructor; 42 import java.lang.reflect.*;
43 import java.lang.reflect.InvocationTargetException;
44 import java.lang.reflect.Method;
45 import java.lang.reflect.Modifier;
46 import java.sql.Connection; 43 import java.sql.Connection;
47 import java.sql.SQLException; 44 import java.sql.SQLException;
48 import java.util.*; 45 import java.util.*;
49 import java.util.function.Function; 46 import java.util.function.Function;
50 47
278 * @param name the name of the parameter 275 * @param name the name of the parameter
279 * @param <T> the expected type 276 * @param <T> the expected type
280 * @return the parameter value or an empty optional, if no parameter with the specified name was found 277 * @return the parameter value or an empty optional, if no parameter with the specified name was found
281 */ 278 */
282 protected <T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) { 279 protected <T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) {
283 final String paramValue = req.getParameter(name); 280 if (clazz.isArray()) {
284 if (paramValue == null) return Optional.empty(); 281 final String[] paramValues = req.getParameterValues(name);
285 if (clazz.equals(Boolean.class)) { 282 int len = paramValues == null ? 0 : paramValues.length;
286 if (paramValue.toLowerCase().equals("false") || paramValue.equals("0")) { 283 final var array = (T) Array.newInstance(clazz.getComponentType(), len);
287 return Optional.of((T)Boolean.FALSE); 284 for (int i = 0 ; i < len ; i++) {
288 } else { 285 try {
289 return Optional.of((T)Boolean.TRUE); 286 final Constructor<?> ctor = clazz.getComponentType().getConstructor(String.class);
290 } 287 Array.set(array, i, ctor.newInstance(paramValues[i]));
291 } 288 } catch (ReflectiveOperationException e) {
292 if (clazz.equals(String.class)) return Optional.of((T) paramValue); 289 throw new RuntimeException(e);
293 if (java.sql.Date.class.isAssignableFrom(clazz)) { 290 }
291 }
292 return Optional.of(array);
293 } else {
294 final String paramValue = req.getParameter(name);
295 if (paramValue == null) return Optional.empty();
296 if (clazz.equals(Boolean.class)) {
297 if (paramValue.toLowerCase().equals("false") || paramValue.equals("0")) {
298 return Optional.of((T) Boolean.FALSE);
299 } else {
300 return Optional.of((T) Boolean.TRUE);
301 }
302 }
303 if (clazz.equals(String.class)) return Optional.of((T) paramValue);
304 if (java.sql.Date.class.isAssignableFrom(clazz)) {
305 try {
306 return Optional.of((T) java.sql.Date.valueOf(paramValue));
307 } catch (IllegalArgumentException ex) {
308 return Optional.empty();
309 }
310 }
294 try { 311 try {
295 return Optional.of((T)java.sql.Date.valueOf(paramValue)); 312 final Constructor<T> ctor = clazz.getConstructor(String.class);
296 } catch (IllegalArgumentException ex) { 313 return Optional.of(ctor.newInstance(paramValue));
297 return Optional.empty(); 314 } catch (ReflectiveOperationException e) {
298 } 315 throw new RuntimeException(e);
299 } 316 }
300 try { 317 }
301 final Constructor<T> ctor = clazz.getConstructor(String.class);
302 return Optional.of(ctor.newInstance(paramValue));
303 } catch (ReflectiveOperationException e) {
304 throw new RuntimeException(e);
305 }
306
307 } 318 }
308 319
309 /** 320 /**
310 * Tries to look up an entity with a key obtained from a request parameter. 321 * Tries to look up an entity with a key obtained from a request parameter.
311 * 322 *

mercurial