Search in sources :

Example 1 with RequestPart

use of cn.taketoday.web.annotation.RequestPart 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 RequestPart

use of cn.taketoday.web.annotation.RequestPart in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method supportsParameter.

@Test
public void supportsParameter() {
    resolver = new RequestParamMethodArgumentResolver(null, true);
    ResolvableMethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annot(requestParam().name("name")).arg(Map.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
    assertThat(resolver.supportsParameter(param)).isFalse();
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annot(requestPart()).arg(MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isFalse();
    param = this.testMethod.annot(requestParam()).arg(String.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isTrue();
    resolver = new RequestParamMethodArgumentResolver(null, false);
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertThat(resolver.supportsParameter(param)).isFalse();
    param = this.testMethod.annotPresent(RequestPart.class).arg(MultipartFile.class);
    assertThat(resolver.supportsParameter(param)).isFalse();
}
Also used : MockMultipartFile(cn.taketoday.web.testfixture.servlet.MockMultipartFile) MultipartFile(cn.taketoday.web.multipart.MultipartFile) Optional(java.util.Optional) RequestPart(cn.taketoday.web.annotation.RequestPart) MvcAnnotationPredicates.requestPart(cn.taketoday.web.bind.resolver.MvcAnnotationPredicates.requestPart) Part(jakarta.servlet.http.Part) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) List(java.util.List) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with RequestPart

use of cn.taketoday.web.annotation.RequestPart in project today-framework 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());
            validateIfApplicable(context, parameter, arg);
        } 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 : 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)

Aggregations

RequestPart (cn.taketoday.web.annotation.RequestPart)3 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)3 MethodParameter (cn.taketoday.core.MethodParameter)2 Nullable (cn.taketoday.lang.Nullable)2 MultipartException (cn.taketoday.web.bind.MultipartException)2 BindingContext (cn.taketoday.web.BindingContext)1 MethodArgumentNotValidException (cn.taketoday.web.bind.MethodArgumentNotValidException)1 WebDataBinder (cn.taketoday.web.bind.WebDataBinder)1 MvcAnnotationPredicates.requestPart (cn.taketoday.web.bind.resolver.MvcAnnotationPredicates.requestPart)1 MultipartFile (cn.taketoday.web.multipart.MultipartFile)1 MockMultipartFile (cn.taketoday.web.testfixture.servlet.MockMultipartFile)1 MockPart (cn.taketoday.web.testfixture.servlet.MockPart)1 Part (jakarta.servlet.http.Part)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Test (org.junit.jupiter.api.Test)1