Search in sources :

Example 11 with DefaultMultiValueMap

use of cn.taketoday.core.DefaultMultiValueMap in project today-infrastructure 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 12 with DefaultMultiValueMap

use of cn.taketoday.core.DefaultMultiValueMap in project today-infrastructure 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 13 with DefaultMultiValueMap

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

the class FormHttpMessageWriterTests method writeForm.

@Test
public void writeForm() {
    MultiValueMap<String, String> body = new DefaultMultiValueMap<>(new LinkedHashMap<>());
    body.set("name 1", "value 1");
    body.add("name 2", "value 2+1");
    body.add("name 2", "value 2+2");
    body.add("name 3", null);
    MockServerHttpResponse response = new MockServerHttpResponse(this.bufferFactory);
    this.writer.write(Mono.just(body), null, MediaType.APPLICATION_FORM_URLENCODED, response, null).block();
    String expected = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
    StepVerifier.create(response.getBody()).consumeNextWith(stringConsumer(expected)).expectComplete().verify();
    HttpHeaders headers = response.getHeaders();
    assertThat(headers.getContentType().toString()).isEqualTo("application/x-www-form-urlencoded;charset=UTF-8");
    assertThat(headers.getContentLength()).isEqualTo(expected.length());
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) MockServerHttpResponse(cn.taketoday.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 14 with DefaultMultiValueMap

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

the class FormHttpMessageConverterTests method writeMultipartOrder.

// SPR-13309
@Test
public void writeMultipartOrder() throws Exception {
    MyBean myBean = new MyBean();
    myBean.setString("foo");
    MultiValueMap<String, Object> parts = new DefaultMultiValueMap<>();
    parts.add("part1", myBean);
    HttpHeaders entityHeaders = HttpHeaders.create();
    entityHeaders.setContentType(APPLICATION_JSON);
    HttpEntity<MyBean> entity = new HttpEntity<>(myBean, entityHeaders);
    parts.add("part2", entity);
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    this.converter.setMultipartCharset(StandardCharsets.UTF_8);
    this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);
    MediaType contentType = outputMessage.getHeaders().getContentType();
    assertThat(contentType.getParameter("boundary")).as("No boundary found").isNotNull();
    // see if Commons FileUpload can read what we wrote
    FileItemFactory fileItemFactory = new DiskFileItemFactory();
    FileUpload fileUpload = new FileUpload(fileItemFactory);
    RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
    List<FileItem> items = fileUpload.parseRequest(requestContext);
    assertThat(items.size()).isEqualTo(2);
    FileItem item = items.get(0);
    assertThat(item.isFormField()).isTrue();
    assertThat(item.getFieldName()).isEqualTo("part1");
    assertThat(item.getString()).isEqualTo("{\"string\":\"foo\"}");
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) HttpEntity(cn.taketoday.http.HttpEntity) MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) DiskFileItemFactory(org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.tomcat.util.http.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.tomcat.util.http.fileupload.FileItem) MediaType(cn.taketoday.http.MediaType) RequestContext(org.apache.tomcat.util.http.fileupload.RequestContext) FileUpload(org.apache.tomcat.util.http.fileupload.FileUpload) 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