use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method missingOptionalMultipartFile.
@Test
public void missingOptionalMultipartFile() throws Exception {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
request.setMethod("POST");
request.setContentType("multipart/form-data");
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
Object actual = resolver.resolveArgument(param, null, webRequest, binderFactory);
assertEquals(Optional.empty(), actual);
}
use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.
the class AbstractRequestAttributesArgumentResolverTests method resolveOptional.
@Test
public void resolveOptional() throws Exception {
WebDataBinder dataBinder = new WebRequestDataBinder(null);
dataBinder.setConversionService(new DefaultConversionService());
WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
given(factory.createBinder(this.webRequest, null, "foo")).willReturn(dataBinder);
MethodParameter param = initMethodParameter(3);
Object actual = testResolveArgument(param, factory);
assertNotNull(actual);
assertEquals(Optional.class, actual.getClass());
assertFalse(((Optional) actual).isPresent());
Foo foo = new Foo();
this.webRequest.setAttribute("foo", foo, getScope());
actual = testResolveArgument(param, factory);
assertNotNull(actual);
assertEquals(Optional.class, actual.getClass());
assertTrue(((Optional) actual).isPresent());
assertSame(foo, ((Optional) actual).get());
}
use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method resolveOptionalMultipartFile.
@Test
public void resolveOptionalMultipartFile() throws Exception {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
request.addFile(expected);
webRequest = new ServletWebRequest(request);
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
assertTrue(result instanceof Optional);
assertEquals("Invalid result", expected, ((Optional<?>) result).get());
}
use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.
the class RequestMappingHandlerAdapter method invokeHandlerMethod.
/**
* Invoke the {@link RequestMapping} handler method preparing a {@link ModelAndView}
* if view resolution is required.
* @since 4.2
* @see #createInvocableHandlerMethod(HandlerMethod)
*/
protected ModelAndView invokeHandlerMethod(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
ServletWebRequest webRequest = new ServletWebRequest(request, response);
try {
WebDataBinderFactory binderFactory = getDataBinderFactory(handlerMethod);
ModelFactory modelFactory = getModelFactory(handlerMethod, binderFactory);
ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
invocableMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
invocableMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
invocableMethod.setDataBinderFactory(binderFactory);
invocableMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));
modelFactory.initModel(webRequest, mavContainer, invocableMethod);
mavContainer.setIgnoreDefaultModelOnRedirect(this.ignoreDefaultModelOnRedirect);
AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response);
asyncWebRequest.setTimeout(this.asyncRequestTimeout);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.setTaskExecutor(this.taskExecutor);
asyncManager.setAsyncWebRequest(asyncWebRequest);
asyncManager.registerCallableInterceptors(this.callableInterceptors);
asyncManager.registerDeferredResultInterceptors(this.deferredResultInterceptors);
if (asyncManager.hasConcurrentResult()) {
Object result = asyncManager.getConcurrentResult();
mavContainer = (ModelAndViewContainer) asyncManager.getConcurrentResultContext()[0];
asyncManager.clearConcurrentResult();
if (logger.isDebugEnabled()) {
logger.debug("Found concurrent result value [" + result + "]");
}
invocableMethod = invocableMethod.wrapConcurrentResult(result);
}
invocableMethod.invokeAndHandle(webRequest, mavContainer);
if (asyncManager.isConcurrentHandlingStarted()) {
return null;
}
return getModelAndView(mavContainer, modelFactory, webRequest);
} finally {
webRequest.requestCompleted();
}
}
use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.
the class PathVariableMethodArgumentResolverTests method wrapEmptyWithOptional.
@Test
public void wrapEmptyWithOptional() throws Exception {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
assertEquals(Optional.empty(), resolver.resolveArgument(paramOptional, mavContainer, webRequest, binderFactory));
}
Aggregations