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

The following examples show how to use org.springframework.web.method.support.InvocableHandlerMethod. These examples are extracted from open source projects. 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 Project: lams   Source File: ModelFactory.java    License: 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 Project: spring4-understanding   Source File: ModelFactory.java    License: 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
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 4
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
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 6
Source Project: spring-analysis-note   Source File: MvcNamespaceTests.java    License: 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 7
Source Project: spring-analysis-note   Source File: ModelFactory.java    License: 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 8
Source Project: spring-analysis-note   Source File: ModelFactory.java    License: 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 9
Source Project: spring-analysis-note   Source File: ModelFactory.java    License: 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 10
Source Project: spring-analysis-note   Source File: InitBinderDataBinderFactory.java    License: 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 11
Source Project: spring-analysis-note   Source File: InitBinderDataBinderFactory.java    License: 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 12
Source Project: spring-analysis-note   Source File: ModelFactoryTests.java    License: 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 13
Source Project: spring-analysis-note   Source File: ModelFactoryOrderingTests.java    License: 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 14
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 15
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 16
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 17
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 18
Source Project: java-technology-stack   Source File: MvcNamespaceTests.java    License: 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 19
Source Project: java-technology-stack   Source File: ModelFactory.java    License: 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 20
Source Project: java-technology-stack   Source File: ModelFactory.java    License: 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 21
Source Project: java-technology-stack   Source File: ModelFactory.java    License: 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 22
/**
 * 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 23
/**
 * 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 24
Source Project: java-technology-stack   Source File: ModelFactoryTests.java    License: 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 25
Source Project: java-technology-stack   Source File: ModelFactoryOrderingTests.java    License: 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 26
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 27
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 28
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 29
Source Project: lams   Source File: ModelFactory.java    License: 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 30
Source Project: lams   Source File: ModelFactory.java    License: 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));
		}
	}
}