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);
}
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();
}
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);
}
Aggregations