
com.github.gkutiel.flip.web.MethodInvokerDefault Maven / Gradle / Ivy
package com.github.gkutiel.flip.web;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.github.gkutiel.flip.utils.Utils;
import com.github.gkutiel.flip.web.Flipper.RedirectException;
class MethodInvokerDefault extends MethodInvoker {
MethodInvokerDefault(final Class> clazz, final Method method) {
super(clazz, method);
}
private Map actualParams(final HttpServletRequest req) {
return actualParams(req, method);
}
private Map actualParams(final HttpServletRequest req, final Method method) {
final Map actualParams = new HashMap<>();
final Map parameterMap = req.getParameterMap();
for (final Entry> formalParam : Utils.Reflection.getFormalParams(method).entrySet()) {
final String paramName = formalParam.getKey();
final Class> paramType = formalParam.getValue();
actualParams.put(paramName, ParseParamService.parse(paramType, parameterMap.get(paramName)));
}
return actualParams;
}
@Override
Object invoke(final HttpServletRequest req, final HttpServletResponse res) {
try {
final Object instance = getInstance(req, res);
invokeBefore(req, instance);
return Utils.Reflection.invoke(method, instance, actualParams(req));
} catch (IllegalAccessException | IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (final InvocationTargetException e) {
final Throwable cause = e.getCause();
if (cause instanceof RedirectException) throw (RedirectException) cause;
throw new RuntimeException(cause);
}
}
private void invokeBefore(final HttpServletRequest req, final Object instance) throws IllegalAccessException, InvocationTargetException {
Method before = null;
Class> c = clazz;
while (c != null && before == null) {
before = Utils.Reflection.findFirstMethod(c, "before");
c = c.getSuperclass();
}
if (before != null) Utils.Reflection.invoke(before, instance, actualParams(req, before));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy