Search in sources :

Example 1 with BindingContext

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);
}
Also used : BindingContext(cn.taketoday.web.BindingContext) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) WebDataBinder(cn.taketoday.web.bind.WebDataBinder) RequestPart(cn.taketoday.web.annotation.RequestPart) MultipartException(cn.taketoday.web.bind.MultipartException) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 2 with BindingContext

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;
}
Also used : WebAsyncManager(cn.taketoday.web.context.async.WebAsyncManager) RedirectModel(cn.taketoday.web.view.RedirectModel) BindingContext(cn.taketoday.web.BindingContext) AsyncWebRequest(cn.taketoday.web.context.async.AsyncWebRequest) Nullable(cn.taketoday.lang.Nullable)

Example 3 with BindingContext

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);
        }
    }
}
Also used : BindingContext(cn.taketoday.web.BindingContext) View(cn.taketoday.web.view.View) ModelAndView(cn.taketoday.web.view.ModelAndView)

Example 4 with BindingContext

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);
}
Also used : BindingContext(cn.taketoday.web.BindingContext) MethodParameter(cn.taketoday.core.MethodParameter)

Example 5 with BindingContext

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();
}
Also used : BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Aggregations

BindingContext (cn.taketoday.web.BindingContext)75 Test (org.junit.jupiter.api.Test)53 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)31 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)30 RequestParam (cn.taketoday.web.annotation.RequestParam)23 ConfigurableWebBindingInitializer (cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer)17 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)13 DefaultConversionService (cn.taketoday.core.conversion.support.DefaultConversionService)11 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)11 RequestContextDataBinder (cn.taketoday.web.bind.RequestContextDataBinder)9 MockMultipartFile (cn.taketoday.web.testfixture.servlet.MockMultipartFile)9 MockMultipartHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest)9 MultipartFile (cn.taketoday.web.multipart.MultipartFile)8 DefaultFormattingConversionService (cn.taketoday.format.support.DefaultFormattingConversionService)7 Nullable (cn.taketoday.lang.Nullable)7 Optional (java.util.Optional)7 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)6 MethodParameter (cn.taketoday.core.MethodParameter)6 MockPart (cn.taketoday.web.testfixture.servlet.MockPart)6 RequestPart (cn.taketoday.web.annotation.RequestPart)5