Search in sources :

Example 1 with ResolvableMethodParameter

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);
}
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 ResolvableMethodParameter

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);
}
Also used : ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter)

Example 3 with ResolvableMethodParameter

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;
}
Also used : ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter)

Example 4 with ResolvableMethodParameter

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);
}
Also used : ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 5 with ResolvableMethodParameter

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);
}
Also used : MatrixParam(cn.taketoday.web.annotation.MatrixParam) List(java.util.List) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Nullable(cn.taketoday.lang.Nullable)

Aggregations

ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)90 Test (org.junit.jupiter.api.Test)62 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)35 RequestParam (cn.taketoday.web.annotation.RequestParam)32 BindingContext (cn.taketoday.web.BindingContext)29 MethodParameter (cn.taketoday.core.MethodParameter)26 Method (java.lang.reflect.Method)22 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)18 MultiValueMap (cn.taketoday.core.MultiValueMap)14 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)14 Nullable (cn.taketoday.lang.Nullable)13 HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)13 MockMultipartFile (cn.taketoday.web.testfixture.servlet.MockMultipartFile)12 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)11 MultipartFile (cn.taketoday.web.multipart.MultipartFile)11 MockMultipartHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest)11 List (java.util.List)11 ByteArrayHttpMessageConverter (cn.taketoday.http.converter.ByteArrayHttpMessageConverter)10 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)10 MappingJackson2HttpMessageConverter (cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter)10