use of cn.taketoday.web.BindingContext in project today-infrastructure by TAKETODAY.
the class RequestPartMethodArgumentResolver method resolveArgument.
@Nullable
@Override
public Object resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
MethodParameter parameter = resolvable.getParameter();
RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class);
boolean isRequired = ((requestPart == null || requestPart.required()) && !parameter.isOptional());
String name = getPartName(parameter, requestPart);
parameter = parameter.nestedIfOptional();
Object arg = null;
Object mpArg = MultipartResolutionDelegate.resolveMultipartArgument(name, parameter, context);
if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
arg = mpArg;
} else {
try {
var inputMessage = new RequestPartServletServerHttpRequest(context, name);
arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
BindingContext binderFactory = context.getBindingContext();
if (binderFactory != null) {
WebDataBinder binder = binderFactory.createBinder(context, arg, name);
if (arg != null) {
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
}
}
binderFactory.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
}
} catch (MissingRequestPartException | MultipartException ex) {
if (isRequired) {
throw ex;
}
}
}
if (arg == null && isRequired) {
if (!context.isMultipart()) {
throw new MultipartException("Current request is not a multipart request");
} else {
throw new MissingRequestPartException(name);
}
}
return adaptArgumentIfNecessary(arg, parameter);
}
use of cn.taketoday.web.BindingContext in project today-infrastructure by TAKETODAY.
the class RequestMappingHandlerAdapter method invokeHandlerMethod.
/**
* Invoke the {@link RequestMapping} handler method preparing a {@link ModelAndView}
* if view resolution is required.
*
* @see #createInvocableHandlerMethod(RequestContext, HandlerMethod)
*/
@Nullable
protected Object invokeHandlerMethod(RequestContext request, HandlerMethod handlerMethod) throws Throwable {
BindingContext bindingContext = createBindingContext(handlerMethod);
ModelFactory modelFactory = getModelFactory(handlerMethod, bindingContext);
// ActionMappingAnnotationHandler handler = annotationHandlerMap.get(handlerMethod);
ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(request, handlerMethod);
invocableMethod.setReturnValueHandlerManager(returnValueHandlerManager);
RedirectModel inputRedirectModel = RequestContextUtils.getInputRedirectModel(request, redirectModelManager);
bindingContext.addAllAttributes(inputRedirectModel);
modelFactory.initModel(request, bindingContext, invocableMethod);
AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request);
asyncWebRequest.setTimeout(asyncRequestTimeout);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.setTaskExecutor(taskExecutor);
asyncManager.setAsyncRequest(asyncWebRequest);
asyncManager.registerCallableInterceptors(callableInterceptors);
asyncManager.registerDeferredResultInterceptors(deferredResultInterceptors);
if (asyncManager.hasConcurrentResult()) {
Object result = asyncManager.getConcurrentResult();
bindingContext = asyncManager.getBindingContext();
asyncManager.clearConcurrentResult();
LogFormatUtils.traceDebug(log, traceOn -> {
String formatted = LogFormatUtils.formatValue(result, !traceOn);
return "Resume with async result [" + formatted + "]";
});
invocableMethod = invocableMethod.wrapConcurrentResult(result);
}
Object returnValue = invocableMethod.invokeAndHandle(request, bindingContext);
if (asyncManager.isConcurrentHandlingStarted()) {
return null;
}
// modelFactory.updateModel(request, bindingContext);
return returnValue;
}
use of cn.taketoday.web.BindingContext in project today-infrastructure by TAKETODAY.
the class ModelAndViewReturnValueHandler method handleModelAndView.
/**
* Resolve {@link ModelAndView} return type
*
* @since 2.3.3
*/
public final void handleModelAndView(RequestContext context, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
if (modelAndView != null) {
View view = modelAndView.getView();
String viewName = modelAndView.getViewName();
BindingContext bindingContext = context.getBindingContext();
bindingContext.addAllAttributes(modelAndView.getModel());
if (viewName != null) {
delegate.renderView(context, viewName);
} else if (view != null) {
delegate.renderView(context, view);
}
}
}
use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.
the class ReactiveTypeHandlerTests method handleValue.
private ResponseBodyEmitter handleValue(Object returnValue, Class<?> asyncType, ResolvableType genericType) throws Exception {
BindingContext mavContainer = new BindingContext();
MethodParameter returnType = on(TestController.class).resolveReturnType(asyncType, genericType);
webRequest.setBindingContext(mavContainer);
return this.handler.handleValue(returnValue, returnType, this.webRequest);
}
use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.
the class ModelFactoryTests method modelAttributeWithBindingDisabled.
@Test
public void modelAttributeWithBindingDisabled() throws Throwable {
BindingContext bindingContext = webRequest.getBindingContext();
ModelFactory modelFactory = createModelFactory("modelAttrWithBindingDisabled");
HandlerMethod handlerMethod = createHandlerMethod("handle");
modelFactory.initModel(this.webRequest, bindingContext, handlerMethod);
assertThat(bindingContext.containsAttribute("foo")).isTrue();
assertThat(bindingContext.isBindingDisabled("foo")).isTrue();
}
Aggregations