All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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) {
		final Map actualParams = new HashMap<>();
		final Map parameterMap = req.getParameterMap();
		for (final Entry> formalParam : Utils.Reflection.getFormalParams(this.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 Map actualParams = this.actualParams(req);
			final Object instance = this.getInstance(req, res);

			return Utils.Reflection.invoke(this.method, instance, actualParams);
		} 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);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy