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