Search in sources :

Example 6 with DefaultMultiValueMap

use of cn.taketoday.core.DefaultMultiValueMap 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 7 with DefaultMultiValueMap

use of cn.taketoday.core.DefaultMultiValueMap in project today-framework by TAKETODAY.

the class Jackson2ObjectMapperBuilder method configure.

/**
 * Configure an existing {@link ObjectMapper} instance with this builder's
 * settings. This can be applied to any number of {@code ObjectMappers}.
 *
 * @param objectMapper the ObjectMapper to configure
 */
public void configure(ObjectMapper objectMapper) {
    Assert.notNull(objectMapper, "ObjectMapper must not be null");
    DefaultMultiValueMap<Object, Module> modulesToRegister = MultiValueMap.fromLinkedHashMap();
    if (this.findModulesViaServiceLoader) {
        for (Module module : ObjectMapper.findModules(this.moduleClassLoader)) {
            registerModule(module, modulesToRegister);
        }
    } else if (this.findWellKnownModules) {
        registerWellKnownModulesIfAvailable(modulesToRegister);
    }
    if (this.modules != null) {
        for (Module module : modules) {
            registerModule(module, modulesToRegister);
        }
    }
    if (this.moduleClasses != null) {
        for (Class<? extends Module> moduleClass : this.moduleClasses) {
            registerModule(BeanUtils.newInstance(moduleClass), modulesToRegister);
        }
    }
    List<Module> modules = new ArrayList<>();
    for (List<Module> nestedModules : modulesToRegister.values()) {
        modules.addAll(nestedModules);
    }
    objectMapper.registerModules(modules);
    if (this.dateFormat != null) {
        objectMapper.setDateFormat(this.dateFormat);
    }
    if (this.locale != null) {
        objectMapper.setLocale(this.locale);
    }
    if (this.timeZone != null) {
        objectMapper.setTimeZone(this.timeZone);
    }
    if (this.annotationIntrospector != null) {
        objectMapper.setAnnotationIntrospector(this.annotationIntrospector);
    }
    if (this.propertyNamingStrategy != null) {
        objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy);
    }
    if (this.defaultTyping != null) {
        objectMapper.setDefaultTyping(this.defaultTyping);
    }
    if (this.serializationInclusion != null) {
        objectMapper.setDefaultPropertyInclusion(this.serializationInclusion);
    }
    if (this.filters != null) {
        objectMapper.setFilterProvider(this.filters);
    }
    this.mixIns.forEach(objectMapper::addMixIn);
    if (!this.serializers.isEmpty() || !this.deserializers.isEmpty()) {
        SimpleModule module = new SimpleModule();
        addSerializers(module);
        addDeserializers(module);
        objectMapper.registerModule(module);
    }
    this.visibilities.forEach(objectMapper::setVisibility);
    for (Map.Entry<Object, Boolean> entry : features.entrySet()) {
        Object feature = entry.getKey();
        Boolean enabled = entry.getValue();
        configureFeature(objectMapper, feature, enabled);
    }
    customizeDefaultFeatures(objectMapper);
    if (this.handlerInstantiator != null) {
        objectMapper.setHandlerInstantiator(this.handlerInstantiator);
    } else if (this.applicationContext != null) {
        objectMapper.setHandlerInstantiator(new BeanFactoryHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));
    }
    if (this.configurer != null) {
        this.configurer.accept(objectMapper);
    }
}
Also used : ArrayList(java.util.ArrayList) Module(com.fasterxml.jackson.databind.Module) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JacksonXmlModule(com.fasterxml.jackson.dataformat.xml.JacksonXmlModule) Map(java.util.Map) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) LinkedHashMap(java.util.LinkedHashMap) MultiValueMap(cn.taketoday.core.MultiValueMap) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 8 with DefaultMultiValueMap

use of cn.taketoday.core.DefaultMultiValueMap in project today-framework by TAKETODAY.

the class MultipartBodyBuilderTests method builder.

@Test
public void builder() {
    MultipartBodyBuilder builder = new MultipartBodyBuilder();
    MultiValueMap<String, String> multipartData = new DefaultMultiValueMap<>();
    multipartData.add("form field", "form value");
    builder.part("key", multipartData).header("foo", "bar");
    Resource logo = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg");
    builder.part("logo", logo).header("baz", "qux");
    HttpHeaders entityHeaders = HttpHeaders.create();
    entityHeaders.add("foo", "bar");
    HttpEntity<String> entity = new HttpEntity<>("body", entityHeaders);
    builder.part("entity", entity).header("baz", "qux");
    Publisher<String> publisher = Flux.just("foo", "bar", "baz");
    builder.asyncPart("publisherClass", publisher, String.class).header("baz", "qux");
    builder.asyncPart("publisherPtr", publisher, new TypeReference<String>() {
    }).header("baz", "qux");
    MultiValueMap<String, HttpEntity<?>> result = builder.build();
    assertThat(result.size()).isEqualTo(5);
    HttpEntity<?> resultEntity = result.getFirst("key");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(multipartData);
    assertThat(resultEntity.getHeaders().getFirst("foo")).isEqualTo("bar");
    resultEntity = result.getFirst("logo");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(logo);
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("entity");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo("body");
    assertThat(resultEntity.getHeaders().getFirst("foo")).isEqualTo("bar");
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("publisherClass");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(publisher);
    assertThat(((PublisherEntity<?, ?>) resultEntity).getResolvableType()).isEqualTo(ResolvableType.fromClass(String.class));
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("publisherPtr");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(publisher);
    assertThat(((PublisherEntity<?, ?>) resultEntity).getResolvableType()).isEqualTo(ResolvableType.fromClass(String.class));
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) HttpEntity(cn.taketoday.http.HttpEntity) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) TypeReference(cn.taketoday.core.TypeReference) PublisherEntity(cn.taketoday.http.client.MultipartBodyBuilder.PublisherEntity) Test(org.junit.jupiter.api.Test)

Example 9 with DefaultMultiValueMap

use of cn.taketoday.core.DefaultMultiValueMap in project today-framework by TAKETODAY.

the class AbstractDataBinderParameterResolver method resolveName.

@Nullable
@Override
protected Object resolveName(String name, ResolvableMethodParameter resolvable, RequestContext context) throws Exception {
    final int parameterNameLength = name.length();
    // prepare property values
    final Map<String, String[]> parameters = context.getParameters();
    final DefaultMultiValueMap<String, PropertyValue> propertyValues = new DefaultMultiValueMap<>();
    for (final Map.Entry<String, String[]> entry : parameters.entrySet()) {
        final String[] paramValues = entry.getValue();
        if (ObjectUtils.isNotEmpty(paramValues)) {
            final String requestParameterName = entry.getKey();
            // users[key].userName=TODAY&users[key].age=20
            if (requestParameterName.startsWith(name) && requestParameterName.charAt(parameterNameLength) == '[') {
                // userList[0].name  '.' 's index
                final int separatorIndex = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(requestParameterName);
                final String property = requestParameterName.substring(separatorIndex + 1);
                final int closeKey = requestParameterName.indexOf(']');
                final String key = requestParameterName.substring(parameterNameLength + 1, closeKey);
                final PropertyValue propertyValue = new PropertyValue(property, paramValues[0]);
                propertyValues.add(key, propertyValue);
            }
        }
    }
    return doBind(propertyValues, resolvable);
}
Also used : DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) PropertyValue(cn.taketoday.beans.PropertyValue) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) Nullable(cn.taketoday.lang.Nullable)

Example 10 with DefaultMultiValueMap

use of cn.taketoday.core.DefaultMultiValueMap in project today-framework by TAKETODAY.

the class FormHttpMessageConverterTests method writeMultipartCharset.

@Test
public void writeMultipartCharset() throws Exception {
    MultiValueMap<String, Object> parts = new DefaultMultiValueMap<>();
    Resource logo = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg");
    parts.add("logo", logo);
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    this.converter.write(parts, MULTIPART_FORM_DATA, outputMessage);
    MediaType contentType = outputMessage.getHeaders().getContentType();
    Map<String, String> parameters = contentType.getParameters();
    assertThat(parameters).containsOnlyKeys("boundary");
    this.converter.setCharset(StandardCharsets.ISO_8859_1);
    outputMessage = new MockHttpOutputMessage();
    this.converter.write(parts, MULTIPART_FORM_DATA, outputMessage);
    parameters = outputMessage.getHeaders().getContentType().getParameters();
    assertThat(parameters).containsOnlyKeys("boundary", "charset");
    assertThat(parameters).containsEntry("charset", "ISO-8859-1");
}
Also used : DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) MediaType(cn.taketoday.http.MediaType) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultMultiValueMap (cn.taketoday.core.DefaultMultiValueMap)14 Test (org.junit.jupiter.api.Test)8 MultiValueMap (cn.taketoday.core.MultiValueMap)6 HttpHeaders (cn.taketoday.http.HttpHeaders)6 Map (java.util.Map)6 ClassPathResource (cn.taketoday.core.io.ClassPathResource)4 Resource (cn.taketoday.core.io.Resource)4 HttpEntity (cn.taketoday.http.HttpEntity)4 MediaType (cn.taketoday.http.MediaType)4 MockHttpOutputMessage (cn.taketoday.http.MockHttpOutputMessage)4 PropertyValue (cn.taketoday.beans.PropertyValue)2 TypeReference (cn.taketoday.core.TypeReference)2 HttpCookie (cn.taketoday.http.HttpCookie)2 PublisherEntity (cn.taketoday.http.client.MultipartBodyBuilder.PublisherEntity)2 MockServerHttpResponse (cn.taketoday.http.server.reactive.MockServerHttpResponse)2 Nullable (cn.taketoday.lang.Nullable)2 Module (com.fasterxml.jackson.databind.Module)2 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)2 JacksonXmlModule (com.fasterxml.jackson.dataformat.xml.JacksonXmlModule)2 Cookie (io.netty.handler.codec.http.cookie.Cookie)2