org.springframework.web.method.support.InvocableHandlerMethod Java Examples

The following examples show how to use org.springframework.web.method.support.InvocableHandlerMethod. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModelFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invoke model attribute methods to populate the model.
 * Attributes are added only if not already present in the model.
 */
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer container)
		throws Exception {

	while (!this.modelMethods.isEmpty()) {
		InvocableHandlerMethod modelMethod = getNextModelMethod(container).getHandlerMethod();
		ModelAttribute ann = modelMethod.getMethodAnnotation(ModelAttribute.class);
		if (container.containsAttribute(ann.name())) {
			if (!ann.binding()) {
				container.setBindingDisabled(ann.name());
			}
			continue;
		}

		Object returnValue = modelMethod.invokeForRequest(request, container);
		if (!modelMethod.isVoid()){
			String returnValueName = getNameForReturnValue(returnValue, modelMethod.getReturnType());
			if (!ann.binding()) {
				container.setBindingDisabled(returnValueName);
			}
			if (!container.containsAttribute(returnValueName)) {
				container.addAttribute(returnValueName, returnValue);
			}
		}
	}
}
 
Example #2
Source File: ModelFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Invoke model attribute methods to populate the model.
 * Attributes are added only if not already present in the model.
 */
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer mavContainer)
		throws Exception {

	while (!this.modelMethods.isEmpty()) {
		InvocableHandlerMethod attrMethod = getNextModelMethod(mavContainer).getHandlerMethod();
		String modelName = attrMethod.getMethodAnnotation(ModelAttribute.class).value();
		if (mavContainer.containsAttribute(modelName)) {
			continue;
		}

		Object returnValue = attrMethod.invokeForRequest(request, mavContainer);

		if (!attrMethod.isVoid()){
			String returnValueName = getNameForReturnValue(returnValue, attrMethod.getReturnType());
			if (!mavContainer.containsAttribute(returnValueName)) {
				mavContainer.addAttribute(returnValueName, returnValue);
			}
		}
	}
}
 
Example #3
Source File: ModelFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
public ModelMethod(InvocableHandlerMethod handlerMethod) {
	this.handlerMethod = handlerMethod;
	for (MethodParameter parameter : handlerMethod.getMethodParameters()) {
		if (parameter.hasParameterAnnotation(ModelAttribute.class)) {
			this.dependencies.add(getNameForParameter(parameter));
		}
	}
}
 
Example #4
Source File: RequestMappingHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private InvocableHandlerMethod createInitBinderMethod(Object bean, Method method) {
	InvocableHandlerMethod binderMethod = new InvocableHandlerMethod(bean, method);
	if (this.initBinderArgumentResolvers != null) {
		binderMethod.setHandlerMethodArgumentResolvers(this.initBinderArgumentResolvers);
	}
	binderMethod.setDataBinderFactory(new DefaultDataBinderFactory(this.webBindingInitializer));
	binderMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
	return binderMethod;
}
 
Example #5
Source File: RequestMappingHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private InvocableHandlerMethod createModelAttributeMethod(WebDataBinderFactory factory, Object bean, Method method) {
	InvocableHandlerMethod attrMethod = new InvocableHandlerMethod(bean, method);
	if (this.argumentResolvers != null) {
		attrMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
	}
	attrMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
	attrMethod.setDataBinderFactory(factory);
	return attrMethod;
}
 
Example #6
Source File: InitBinderDataBinderFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private WebDataBinderFactory createBinderFactory(String methodName, Class<?>... parameterTypes)
		throws Exception {

	Object handler = new InitBinderHandler();
	Method method = handler.getClass().getMethod(methodName, parameterTypes);

	InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(handler, method);
	handlerMethod.setHandlerMethodArgumentResolvers(argumentResolvers);
	handlerMethod.setDataBinderFactory(new DefaultDataBinderFactory(null));
	handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());

	return new InitBinderDataBinderFactory(Arrays.asList(handlerMethod), bindingInitializer);
}
 
Example #7
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	TestMockServletContext servletContext = new TestMockServletContext();
	appContext = new XmlWebApplicationContext();
	appContext.setServletContext(servletContext);
	LocaleContextHolder.setLocale(Locale.US);

	String attributeName = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
	appContext.getServletContext().setAttribute(attributeName, appContext);

	handler = new TestController();
	handlerMethod = new InvocableHandlerMethod(handler, TestController.class.getMethod("testBind",
			Date.class, Double.class, TestBean.class, BindingResult.class));
}
 
Example #8
Source File: ModelFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new instance with the given {@code @ModelAttribute} methods.
 * @param handlerMethods the {@code @ModelAttribute} methods to invoke
 * @param binderFactory for preparation of {@link BindingResult} attributes
 * @param attributeHandler for access to session attributes
 */
public ModelFactory(@Nullable List<InvocableHandlerMethod> handlerMethods,
		WebDataBinderFactory binderFactory, SessionAttributesHandler attributeHandler) {

	if (handlerMethods != null) {
		for (InvocableHandlerMethod handlerMethod : handlerMethods) {
			this.modelMethods.add(new ModelMethod(handlerMethod));
		}
	}
	this.dataBinderFactory = binderFactory;
	this.sessionAttributesHandler = attributeHandler;
}
 
Example #9
Source File: ModelFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Invoke model attribute methods to populate the model.
 * Attributes are added only if not already present in the model.
 */
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer container)
		throws Exception {

	while (!this.modelMethods.isEmpty()) {
		InvocableHandlerMethod modelMethod = getNextModelMethod(container).getHandlerMethod();
		ModelAttribute ann = modelMethod.getMethodAnnotation(ModelAttribute.class);
		Assert.state(ann != null, "No ModelAttribute annotation");
		if (container.containsAttribute(ann.name())) {
			if (!ann.binding()) {
				container.setBindingDisabled(ann.name());
			}
			continue;
		}

		Object returnValue = modelMethod.invokeForRequest(request, container);
		if (!modelMethod.isVoid()){
			String returnValueName = getNameForReturnValue(returnValue, modelMethod.getReturnType());
			if (!ann.binding()) {
				container.setBindingDisabled(returnValueName);
			}
			if (!container.containsAttribute(returnValueName)) {
				container.addAttribute(returnValueName, returnValue);
			}
		}
	}
}
 
Example #10
Source File: ModelFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public ModelMethod(InvocableHandlerMethod handlerMethod) {
	this.handlerMethod = handlerMethod;
	for (MethodParameter parameter : handlerMethod.getMethodParameters()) {
		if (parameter.hasParameterAnnotation(ModelAttribute.class)) {
			this.dependencies.add(getNameForParameter(parameter));
		}
	}
}
 
Example #11
Source File: RequestMappingInfoHandlerMappingTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void testHttpOptions(String requestURI, String allowHeader) throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", requestURI);
	HandlerMethod handlerMethod = getHandler(request);

	ServletWebRequest webRequest = new ServletWebRequest(request);
	ModelAndViewContainer mavContainer = new ModelAndViewContainer();
	Object result = new InvocableHandlerMethod(handlerMethod).invokeForRequest(webRequest, mavContainer);

	assertNotNull(result);
	assertEquals(HttpHeaders.class, result.getClass());
	assertEquals(allowHeader, ((HttpHeaders) result).getFirst("Allow"));
}
 
Example #12
Source File: ModelFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private ModelMethod(InvocableHandlerMethod handlerMethod) {
	this.handlerMethod = handlerMethod;
	for (MethodParameter parameter : handlerMethod.getMethodParameters()) {
		if (parameter.hasParameterAnnotation(ModelAttribute.class)) {
			this.dependencies.add(getNameForParameter(parameter));
		}
	}
}
 
Example #13
Source File: RequestMappingInfoHandlerMappingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void testHttpOptions(String requestURI, String allowHeader) throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", requestURI);
	HandlerMethod handlerMethod = getHandler(request);

	ServletWebRequest webRequest = new ServletWebRequest(request);
	ModelAndViewContainer mavContainer = new ModelAndViewContainer();
	Object result = new InvocableHandlerMethod(handlerMethod).invokeForRequest(webRequest, mavContainer);

	assertNotNull(result);
	assertEquals(HttpHeaders.class, result.getClass());
	assertEquals(allowHeader, ((HttpHeaders) result).getFirst("Allow"));
}
 
Example #14
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	TestMockServletContext servletContext = new TestMockServletContext();
	appContext = new XmlWebApplicationContext();
	appContext.setServletContext(servletContext);
	LocaleContextHolder.setLocale(Locale.US);

	String attributeName = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
	appContext.getServletContext().setAttribute(attributeName, appContext);

	handler = new TestController();
	handlerMethod = new InvocableHandlerMethod(handler, TestController.class.getMethod("testBind",
			Date.class, Double.class, TestBean.class, BindingResult.class));
}
 
Example #15
Source File: ModelFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new instance with the given {@code @ModelAttribute} methods.
 * @param handlerMethods the {@code @ModelAttribute} methods to invoke
 * @param binderFactory for preparation of {@link BindingResult} attributes
 * @param attributeHandler for access to session attributes
 */
public ModelFactory(@Nullable List<InvocableHandlerMethod> handlerMethods,
		WebDataBinderFactory binderFactory, SessionAttributesHandler attributeHandler) {

	if (handlerMethods != null) {
		for (InvocableHandlerMethod handlerMethod : handlerMethods) {
			this.modelMethods.add(new ModelMethod(handlerMethod));
		}
	}
	this.dataBinderFactory = binderFactory;
	this.sessionAttributesHandler = attributeHandler;
}
 
Example #16
Source File: ModelFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Invoke model attribute methods to populate the model.
 * Attributes are added only if not already present in the model.
 */
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer container)
		throws Exception {

	while (!this.modelMethods.isEmpty()) {
		InvocableHandlerMethod modelMethod = getNextModelMethod(container).getHandlerMethod();
		ModelAttribute ann = modelMethod.getMethodAnnotation(ModelAttribute.class);
		Assert.state(ann != null, "No ModelAttribute annotation");
		if (container.containsAttribute(ann.name())) {
			if (!ann.binding()) {
				container.setBindingDisabled(ann.name());
			}
			continue;
		}

		Object returnValue = modelMethod.invokeForRequest(request, container);
		if (!modelMethod.isVoid()){
			String returnValueName = getNameForReturnValue(returnValue, modelMethod.getReturnType());
			if (!ann.binding()) {
				container.setBindingDisabled(returnValueName);
			}
			if (!container.containsAttribute(returnValueName)) {
				container.addAttribute(returnValueName, returnValue);
			}
		}
	}
}
 
Example #17
Source File: InitBinderDataBinderFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize a WebDataBinder with {@code @InitBinder} methods.
 * If the {@code @InitBinder} annotation specifies attributes names, it is
 * invoked only if the names include the target object name.
 * @throws Exception if one of the invoked @{@link InitBinder} methods fail.
 */
@Override
public void initBinder(WebDataBinder binder, NativeWebRequest request) throws Exception {
	for (InvocableHandlerMethod binderMethod : this.binderMethods) {
		if (isBinderMethodApplicable(binderMethod, binder)) {
			Object returnValue = binderMethod.invokeForRequest(request, null, binder);
			if (returnValue != null) {
				throw new IllegalStateException(
						"@InitBinder methods should return void: " + binderMethod);
			}
		}
	}
}
 
Example #18
Source File: ModelFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new instance with the given {@code @ModelAttribute} methods.
 * @param invocableMethods the {@code @ModelAttribute} methods to invoke
 * @param dataBinderFactory for preparation of {@link BindingResult} attributes
 * @param sessionAttributesHandler for access to session attributes
 */
public ModelFactory(List<InvocableHandlerMethod> invocableMethods, WebDataBinderFactory dataBinderFactory,
		SessionAttributesHandler sessionAttributesHandler) {

	if (invocableMethods != null) {
		for (InvocableHandlerMethod method : invocableMethods) {
			this.modelMethods.add(new ModelMethod(method));
		}
	}
	this.dataBinderFactory = dataBinderFactory;
	this.sessionAttributesHandler = sessionAttributesHandler;
}
 
Example #19
Source File: InitBinderDataBinderFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new InitBinderDataBinderFactory instance.
 * @param binderMethods {@code @InitBinder} methods
 * @param initializer for global data binder initialization
 */
public InitBinderDataBinderFactory(@Nullable List<InvocableHandlerMethod> binderMethods,
		@Nullable WebBindingInitializer initializer) {

	super(initializer);
	this.binderMethods = (binderMethods != null ? binderMethods : Collections.emptyList());
}
 
Example #20
Source File: InitBinderDataBinderFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Initialize a WebDataBinder with {@code @InitBinder} methods.
 * <p>If the {@code @InitBinder} annotation specifies attributes names,
 * it is invoked only if the names include the target object name.
 * @throws Exception if one of the invoked @{@link InitBinder} methods fails
 * @see #isBinderMethodApplicable
 */
@Override
public void initBinder(WebDataBinder dataBinder, NativeWebRequest request) throws Exception {
	for (InvocableHandlerMethod binderMethod : this.binderMethods) {
		if (isBinderMethodApplicable(binderMethod, dataBinder)) {
			Object returnValue = binderMethod.invokeForRequest(request, null, dataBinder);
			if (returnValue != null) {
				throw new IllegalStateException(
						"@InitBinder methods must not return a value (should be void): " + binderMethod);
			}
		}
	}
}
 
Example #21
Source File: ModelFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private ModelFactory createModelFactory(String methodName, Class<?>... parameterTypes) throws Exception {
	HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
	resolvers.addResolver(new ModelMethodProcessor());

	InvocableHandlerMethod modelMethod = createHandlerMethod(methodName, parameterTypes);
	modelMethod.setHandlerMethodArgumentResolvers(resolvers);
	modelMethod.setDataBinderFactory(null);
	modelMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());

	return new ModelFactory(Collections.singletonList(modelMethod), null, this.attributeHandler);
}
 
Example #22
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	TestMockServletContext servletContext = new TestMockServletContext();
	appContext = new GenericWebApplicationContext();
	appContext.setServletContext(servletContext);
	LocaleContextHolder.setLocale(Locale.US);

	String attributeName = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
	appContext.getServletContext().setAttribute(attributeName, appContext);

	handler = new TestController();
	Method method = TestController.class.getMethod("testBind", Date.class, Double.class, TestBean.class, BindingResult.class);
	handlerMethod = new InvocableHandlerMethod(handler, method);
}
 
Example #23
Source File: ModelFactoryOrderingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void runTest(Object controller) throws Exception {
	HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
	resolvers.addResolver(new ModelAttributeMethodProcessor(false));
	resolvers.addResolver(new ModelMethodProcessor());
	WebDataBinderFactory dataBinderFactory = new DefaultDataBinderFactory(null);

	Class<?> type = controller.getClass();
	Set<Method> methods = MethodIntrospector.selectMethods(type, METHOD_FILTER);
	List<InvocableHandlerMethod> modelMethods = new ArrayList<>();
	for (Method method : methods) {
		InvocableHandlerMethod modelMethod = new InvocableHandlerMethod(controller, method);
		modelMethod.setHandlerMethodArgumentResolvers(resolvers);
		modelMethod.setDataBinderFactory(dataBinderFactory);
		modelMethods.add(modelMethod);
	}
	Collections.shuffle(modelMethods);

	SessionAttributesHandler sessionHandler = new SessionAttributesHandler(type, this.sessionAttributeStore);
	ModelFactory factory = new ModelFactory(modelMethods, dataBinderFactory, sessionHandler);
	factory.initModel(this.webRequest, this.mavContainer, new HandlerMethod(controller, "handle"));
	if (logger.isDebugEnabled()) {
		StringBuilder sb = new StringBuilder();
		for (String name : getInvokedMethods()) {
			sb.append(" >> ").append(name);
		}
		logger.debug(sb);
	}
}
 
Example #24
Source File: InitBinderDataBinderFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private WebDataBinderFactory createFactory(String methodName, Class<?>... parameterTypes)
		throws Exception {

	Object handler = new InitBinderHandler();
	Method method = handler.getClass().getMethod(methodName, parameterTypes);

	InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(handler, method);
	handlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
	handlerMethod.setDataBinderFactory(new DefaultDataBinderFactory(null));
	handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());

	return new InitBinderDataBinderFactory(
			Collections.singletonList(handlerMethod), this.bindingInitializer);
}
 
Example #25
Source File: RequestMappingHandlerAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private InvocableHandlerMethod createModelAttributeMethod(WebDataBinderFactory factory, Object bean, Method method) {
	InvocableHandlerMethod attrMethod = new InvocableHandlerMethod(bean, method);
	attrMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
	attrMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
	attrMethod.setDataBinderFactory(factory);
	return attrMethod;
}
 
Example #26
Source File: RequestMappingHandlerAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private InvocableHandlerMethod createInitBinderMethod(Object bean, Method method) {
	InvocableHandlerMethod binderMethod = new InvocableHandlerMethod(bean, method);
	binderMethod.setHandlerMethodArgumentResolvers(this.initBinderArgumentResolvers);
	binderMethod.setDataBinderFactory(new DefaultDataBinderFactory(this.webBindingInitializer));
	binderMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
	return binderMethod;
}
 
Example #27
Source File: ModelFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new instance with the given {@code @ModelAttribute} methods.
 * @param handlerMethods the {@code @ModelAttribute} methods to invoke
 * @param binderFactory for preparation of {@link BindingResult} attributes
 * @param attributeHandler for access to session attributes
 */
public ModelFactory(List<InvocableHandlerMethod> handlerMethods,
		WebDataBinderFactory binderFactory, SessionAttributesHandler attributeHandler) {

	if (handlerMethods != null) {
		for (InvocableHandlerMethod handlerMethod : handlerMethods) {
			this.modelMethods.add(new ModelMethod(handlerMethod));
		}
	}
	this.dataBinderFactory = binderFactory;
	this.sessionAttributesHandler = attributeHandler;
}
 
Example #28
Source File: ModelFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ModelMethod(InvocableHandlerMethod handlerMethod) {
	this.handlerMethod = handlerMethod;
	for (MethodParameter parameter : handlerMethod.getMethodParameters()) {
		if (parameter.hasParameterAnnotation(ModelAttribute.class)) {
			this.dependencies.add(getNameForParameter(parameter));
		}
	}
}
 
Example #29
Source File: RequestMappingHandlerAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private InvocableHandlerMethod createInitBinderMethod(Object bean, Method method) {
	InvocableHandlerMethod binderMethod = new InvocableHandlerMethod(bean, method);
	binderMethod.setHandlerMethodArgumentResolvers(this.initBinderArgumentResolvers);
	binderMethod.setDataBinderFactory(new DefaultDataBinderFactory(this.webBindingInitializer));
	binderMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
	return binderMethod;
}
 
Example #30
Source File: RequestMappingHandlerAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private InvocableHandlerMethod createModelAttributeMethod(WebDataBinderFactory factory, Object bean, Method method) {
	InvocableHandlerMethod attrMethod = new InvocableHandlerMethod(bean, method);
	attrMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
	attrMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
	attrMethod.setDataBinderFactory(factory);
	return attrMethod;
}