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

changeset 42
f962ff9dd44e
parent 40
276ef00a336d
child 43
9abf0bf44c7b
equal deleted inserted replaced
41:4f1c026a8aab 42:f962ff9dd44e
96 } 96 }
97 97
98 private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException { 98 private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
99 try { 99 try {
100 LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName()); 100 LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName());
101 return (ResponseType) method.invoke(this, req, resp, dao); 101 final var paramTypes = method.getParameterTypes();
102 final var paramValues = new Object[paramTypes.length];
103 for (int i = 0; i < paramTypes.length; i++) {
104 if (paramTypes[i].isAssignableFrom(HttpServletRequest.class)) {
105 paramValues[i] = req;
106 } else if (paramTypes[i].isAssignableFrom(HttpServletResponse.class)) {
107 paramValues[i] = resp;
108 }
109 if (paramTypes[i].isAssignableFrom(DataAccessObjects.class)) {
110 paramValues[i] = dao;
111 }
112 }
113 return (ResponseType) method.invoke(this, paramValues);
102 } catch (ReflectiveOperationException | ClassCastException ex) { 114 } catch (ReflectiveOperationException | ClassCastException ex) {
103 LOG.error("invocation of method {} failed: {}", method.getName(), ex.getMessage()); 115 LOG.error("invocation of method {} failed: {}", method.getName(), ex.getMessage());
104 LOG.debug("Details: ", ex); 116 LOG.debug("Details: ", ex);
105 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 117 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
106 return ResponseType.NONE; 118 return ResponseType.NONE;
142 method.getName(), RequestMapping.class.getSimpleName() 154 method.getName(), RequestMapping.class.getSimpleName()
143 ); 155 );
144 continue; 156 continue;
145 } 157 }
146 158
147 Class<?>[] params = method.getParameterTypes(); 159 boolean paramsInjectible = true;
148 if (params.length == 3 160 for (var param : method.getParameterTypes()) {
149 && HttpServletRequest.class.isAssignableFrom(params[0]) 161 paramsInjectible &= HttpServletRequest.class.isAssignableFrom(param)
150 && HttpServletResponse.class.isAssignableFrom(params[1]) 162 || HttpServletResponse.class.isAssignableFrom(param)
151 && DataAccessObjects.class.isAssignableFrom(params[2])) { 163 || DataAccessObjects.class.isAssignableFrom(param);
152 164 }
165 if (paramsInjectible) {
153 final String requestPath = "/" + mapping.get().requestPath(); 166 final String requestPath = "/" + mapping.get().requestPath();
154 167
155 if (mappings 168 if (mappings
156 .computeIfAbsent(mapping.get().method(), k -> new HashMap<>()) 169 .computeIfAbsent(mapping.get().method(), k -> new HashMap<>())
157 .putIfAbsent(requestPath, method) != null) { 170 .putIfAbsent(requestPath, method) != null) {
166 requestPath, 179 requestPath,
167 getClass().getSimpleName(), 180 getClass().getSimpleName(),
168 method.getName() 181 method.getName()
169 ); 182 );
170 } else { 183 } else {
171 LOG.warn("{} is annotated with {} but has the wrong parameters - (HttpServletRequest,HttpServletResponse,DataAccessObjects) required", 184 LOG.warn("{} is annotated with {} but has the wrong parameters - only HttpServletRequest. HttpServletResponse, and DataAccessObjects are allowed",
172 method.getName(), RequestMapping.class.getSimpleName() 185 method.getName(), RequestMapping.class.getSimpleName()
173 ); 186 );
174 } 187 }
175 } 188 }
176 } 189 }

mercurial