use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.
the class ReactorServerHttpRequest method initCookies.
@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
DefaultMultiValueMap<String, HttpCookie> cookies = MultiValueMap.fromLinkedHashMap();
for (Map.Entry<CharSequence, Set<Cookie>> entry : request.cookies().entrySet()) {
CharSequence name = entry.getKey();
for (Cookie cookie : entry.getValue()) {
HttpCookie httpCookie = new HttpCookie(name.toString(), cookie.value());
cookies.add(name.toString(), httpCookie);
}
}
return cookies;
}
use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.
the class DataBinderCollectionParameterResolver method doBind.
/**
* @return Collection object
* @throws ParameterIndexExceededException {@code valueIndex} exceed {@link #maxValueIndex}
* @throws ParameterFormatException {@code valueIndex} number format error
* @see #createCollection(MultiValueMap, ResolvableMethodParameter)
*/
@Override
@SuppressWarnings({ "rawtypes" })
protected Object doBind(MultiValueMap<String, PropertyValue> propertyValues, ResolvableMethodParameter resolvable) {
Collection<Object> collection = createCollection(propertyValues, resolvable);
boolean isList = collection instanceof List;
int maxValueIndex = getMaxValueIndex();
Class<?> parameterClass = getComponentType(resolvable);
BeanMetadata parameterMetadata = BeanMetadata.from(parameterClass);
for (Map.Entry<String, List<PropertyValue>> entry : propertyValues.entrySet()) {
Object rootObject = parameterMetadata.newInstance();
RequestContextDataBinder dataBinder = new RequestContextDataBinder(rootObject, resolvable.getName());
dataBinder.setAutoGrowCollectionLimit(maxValueIndex);
List<PropertyValue> propertyValueList = entry.getValue();
dataBinder.bind(new PropertyValues(propertyValueList));
if (isList) {
try {
String key = entry.getKey();
int valueIndex = Integer.parseInt(key);
if (valueIndex > maxValueIndex) {
throw new ParameterIndexExceededException(resolvable.getParameter());
}
CollectionUtils.setValue((List) collection, valueIndex, rootObject);
} catch (NumberFormatException e) {
throw new ParameterFormatException(resolvable.getParameter(), e);
}
} else {
collection.add(rootObject);
}
}
return collection;
}
use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.
the class DataBinderMapParameterResolver method doBind.
/**
* Resolve {@link Map} parameter.
*/
@Override
protected Object doBind(MultiValueMap<String, PropertyValue> propertyValues, ResolvableMethodParameter resolvable) {
MethodParameter parameter = resolvable.getParameter();
Map<String, Object> map = CollectionUtils.createMap(parameter.getParameterType(), propertyValues.size());
ResolvableType generic = resolvable.getResolvableType().asMap().getGeneric(1);
Class<?> parameterClass = generic.resolve();
BeanMetadata parameterMetadata = BeanMetadata.from(parameterClass);
for (Map.Entry<String, List<PropertyValue>> entry : propertyValues.entrySet()) {
Object rootObject = parameterMetadata.newInstance();
RequestContextDataBinder dataBinder = new RequestContextDataBinder(rootObject, resolvable.getName());
List<PropertyValue> propertyValueList = entry.getValue();
dataBinder.bind(new PropertyValues(propertyValueList));
map.put(entry.getKey(), rootObject);
}
return map;
}
use of cn.taketoday.core.MultiValueMap 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.core.MultiValueMap in project today-framework by TAKETODAY.
the class MapToMapConverterTests method mapToMultiValueMap.
@Test
@SuppressWarnings("unchecked")
void mapToMultiValueMap() throws Exception {
DefaultConversionService.addDefaultConverters(conversionService);
Map<String, Integer> source = new HashMap<>();
source.put("a", 1);
source.put("b", 2);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget"));
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType);
assertThat(converted.size()).isEqualTo(2);
assertThat(converted.get("a")).isEqualTo(Arrays.asList("1"));
assertThat(converted.get("b")).isEqualTo(Arrays.asList("2"));
}
Aggregations