Search in sources :

Example 21 with MultiValueMap

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;
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) HttpCookie(cn.taketoday.http.HttpCookie) Set(java.util.Set) HttpCookie(cn.taketoday.http.HttpCookie) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap)

Example 22 with MultiValueMap

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;
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) BeanMetadata(cn.taketoday.beans.BeanMetadata) RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) List(java.util.List) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap)

Example 23 with MultiValueMap

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;
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) BeanMetadata(cn.taketoday.beans.BeanMetadata) RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) List(java.util.List) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableType(cn.taketoday.core.ResolvableType) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap)

Example 24 with MultiValueMap

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);
}
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 25 with MultiValueMap

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"));
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Aggregations

MultiValueMap (cn.taketoday.core.MultiValueMap)46 Test (org.junit.jupiter.api.Test)27 Map (java.util.Map)17 List (java.util.List)13 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)12 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)10 Nullable (cn.taketoday.lang.Nullable)9 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)8 HttpHeaders (cn.taketoday.http.HttpHeaders)8 MultipartBodyBuilder (cn.taketoday.http.client.MultipartBodyBuilder)8 MethodParameter (cn.taketoday.core.MethodParameter)7 Resource (cn.taketoday.core.io.Resource)6 MatrixParam (cn.taketoday.web.annotation.MatrixParam)5 ArrayList (java.util.ArrayList)5 BeanMetadata (cn.taketoday.beans.BeanMetadata)4 PropertyValue (cn.taketoday.beans.PropertyValue)4 PropertyValues (cn.taketoday.beans.PropertyValues)4 DefaultMultiValueMap (cn.taketoday.core.DefaultMultiValueMap)4 ResolvableType (cn.taketoday.core.ResolvableType)4 TypeDescriptor (cn.taketoday.core.TypeDescriptor)4