use of cn.taketoday.web.handler.method.ResolvableMethodParameter 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.handler.method.ResolvableMethodParameter in project today-infrastructure by TAKETODAY.
the class ParameterResolverRegistryTests method testConverterParameterResolver.
private void testConverterParameterResolver(ParameterResolvingRegistry registry, Class<?> type) {
ResolvableMethodParameter test = mockParameter(type, "test");
assertThat(registry.findStrategy(test)).isNotNull().isInstanceOf(ConverterAwareParameterResolver.class);
}
use of cn.taketoday.web.handler.method.ResolvableMethodParameter in project today-infrastructure by TAKETODAY.
the class AnnotationWebSocketDispatcher method resolveParameters.
protected Object[] resolveParameters(WebSocketSession session, WebSocketHandlerMethod handler, Message<?> message, Object... providedArgs) {
ResolvableMethodParameter[] parameters = handler.getParameters();
if (parameters == null) {
return null;
}
Object[] ret = new Object[parameters.length];
int i = 0;
for (ResolvableMethodParameter parameter : parameters) {
Object argument = findProvidedArgument(parameter, providedArgs);
if (argument == null) {
for (EndpointParameterResolver resolver : resolvers) {
if (resolver.supports(parameter)) {
argument = resolver.resolve(session, message, parameter);
break;
}
}
}
ret[i++] = argument;
}
return ret;
}
use of cn.taketoday.web.handler.method.ResolvableMethodParameter in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessor method resolveParameter.
/**
* Throws MethodArgumentNotValidException if validation fails.
*
* @throws HttpMessageNotReadableException if {@link RequestBody#required()}
* is {@code true} and there is no body content or if there is no suitable
* converter to read the content with.
*/
@Nullable
@Override
public Object resolveParameter(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
MethodParameter parameter = resolvable.getParameter();
parameter = parameter.nestedIfOptional();
Object arg = readWithMessageConverters(context, parameter, parameter.getNestedGenericParameterType());
return adaptArgumentIfNecessary(arg, parameter);
}
use of cn.taketoday.web.handler.method.ResolvableMethodParameter in project today-framework by TAKETODAY.
the class MatrixParamMapParameterResolvingStrategy method resolveParameter.
@Nullable
@Override
public Object resolveParameter(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
Map<String, MultiValueMap<String, String>> matrixVariables = context.getMatchingMetadata().getMatrixVariables();
if (CollectionUtils.isEmpty(matrixVariables)) {
return Collections.emptyMap();
}
MultiValueMap<String, String> map = MultiValueMap.fromLinkedHashMap();
MethodParameter parameter = resolvable.getParameter();
MatrixParam ann = parameter.getParameterAnnotation(MatrixParam.class);
Assert.state(ann != null, "No MatrixVariable annotation");
String pathVariable = ann.pathVar();
if (!pathVariable.equals(Constant.DEFAULT_NONE)) {
MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
if (mapForPathVariable == null) {
return Collections.emptyMap();
}
map.putAll(mapForPathVariable);
} else {
for (MultiValueMap<String, String> vars : matrixVariables.values()) {
for (Map.Entry<String, List<String>> entry : vars.entrySet()) {
String name = entry.getKey();
List<String> values = entry.getValue();
for (String value : values) {
map.add(name, value);
}
}
}
}
return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
Aggregations