Search in sources :

Example 1 with MatrixParam

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

Example 2 with MatrixParam

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

the class MatrixParamMapParameterResolvingStrategy method resolveArgument.

@Nullable
@Override
public Object resolveArgument(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)

Example 3 with MatrixParam

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

the class MatrixParamParameterResolvingStrategy method resolveName.

@Nullable
@Override
protected Object resolveName(String name, ResolvableMethodParameter resolvable, RequestContext request) throws Exception {
    Map<String, MultiValueMap<String, String>> pathParameters = request.getMatchingMetadata().getMatrixVariables();
    if (CollectionUtils.isEmpty(pathParameters)) {
        return null;
    }
    MethodParameter parameter = resolvable.getParameter();
    MatrixParam ann = parameter.getParameterAnnotation(MatrixParam.class);
    Assert.state(ann != null, "No MatrixVariable annotation");
    String pathVar = ann.pathVar();
    List<String> paramValues = null;
    if (!pathVar.equals(Constant.DEFAULT_NONE)) {
        if (pathParameters.containsKey(pathVar)) {
            paramValues = pathParameters.get(pathVar).get(name);
        }
    } else {
        boolean found = false;
        paramValues = new ArrayList<>();
        for (MultiValueMap<String, String> params : pathParameters.values()) {
            if (params.containsKey(name)) {
                if (found) {
                    String paramType = parameter.getNestedParameterType().getName();
                    throw new RequestBindingException("Found more than one match for URI path parameter '" + name + "' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.");
                }
                paramValues.addAll(params.get(name));
                found = true;
            }
        }
    }
    if (CollectionUtils.isEmpty(paramValues)) {
        return null;
    } else if (paramValues.size() == 1) {
        return paramValues.get(0);
    } else {
        return paramValues;
    }
}
Also used : MatrixParam(cn.taketoday.web.annotation.MatrixParam) RequestBindingException(cn.taketoday.web.bind.RequestBindingException) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) MultiValueMap(cn.taketoday.core.MultiValueMap) Nullable(cn.taketoday.lang.Nullable)

Example 4 with MatrixParam

use of cn.taketoday.web.annotation.MatrixParam in project today-infrastructure by TAKETODAY.

the class MatrixParamParameterResolvingStrategy method resolveName.

@Nullable
@Override
protected Object resolveName(String name, ResolvableMethodParameter resolvable, RequestContext request) throws Exception {
    Map<String, MultiValueMap<String, String>> pathParameters = request.getMatchingMetadata().getMatrixVariables();
    if (CollectionUtils.isEmpty(pathParameters)) {
        return null;
    }
    MethodParameter parameter = resolvable.getParameter();
    MatrixParam ann = parameter.getParameterAnnotation(MatrixParam.class);
    Assert.state(ann != null, "No MatrixVariable annotation");
    String pathVar = ann.pathVar();
    List<String> paramValues = null;
    if (!pathVar.equals(Constant.DEFAULT_NONE)) {
        if (pathParameters.containsKey(pathVar)) {
            paramValues = pathParameters.get(pathVar).get(name);
        }
    } else {
        boolean found = false;
        paramValues = new ArrayList<>();
        for (MultiValueMap<String, String> params : pathParameters.values()) {
            if (params.containsKey(name)) {
                if (found) {
                    String paramType = parameter.getNestedParameterType().getName();
                    throw new RequestBindingException("Found more than one match for URI path parameter '" + name + "' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.");
                }
                paramValues.addAll(params.get(name));
                found = true;
            }
        }
    }
    if (CollectionUtils.isEmpty(paramValues)) {
        return null;
    } else if (paramValues.size() == 1) {
        return paramValues.get(0);
    } else {
        return paramValues;
    }
}
Also used : MatrixParam(cn.taketoday.web.annotation.MatrixParam) RequestBindingException(cn.taketoday.web.bind.RequestBindingException) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) MultiValueMap(cn.taketoday.core.MultiValueMap) Nullable(cn.taketoday.lang.Nullable)

Example 5 with MatrixParam

use of cn.taketoday.web.annotation.MatrixParam in project today-infrastructure by TAKETODAY.

the class MatrixParamMapParameterResolvingStrategy method resolveArgument.

@Nullable
@Override
public Object resolveArgument(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

MethodParameter (cn.taketoday.core.MethodParameter)5 MultiValueMap (cn.taketoday.core.MultiValueMap)5 Nullable (cn.taketoday.lang.Nullable)5 MatrixParam (cn.taketoday.web.annotation.MatrixParam)5 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)5 List (java.util.List)3 Map (java.util.Map)3 RequestBindingException (cn.taketoday.web.bind.RequestBindingException)2