Search in sources :

Example 11 with MultiValueMap

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

the class MultipartHttpMessageWriter method writeMultipart.

private Mono<Void> writeMultipart(MultiValueMap<String, ?> map, ReactiveHttpOutputMessage outputMessage, @Nullable MediaType mediaType, Map<String, Object> hints) {
    byte[] boundary = generateMultipartBoundary();
    mediaType = getMultipartMediaType(mediaType, boundary);
    outputMessage.getHeaders().setContentType(mediaType);
    LogFormatUtils.traceDebug(logger, traceOn -> Hints.getLogPrefix(hints) + "Encoding " + (isEnableLoggingRequestDetails() ? LogFormatUtils.formatValue(map, !traceOn) : "parts " + map.keySet() + " (content masked)"));
    DataBufferFactory bufferFactory = outputMessage.bufferFactory();
    Flux<DataBuffer> body = Flux.fromIterable(map.entrySet()).concatMap(entry -> encodePartValues(boundary, entry.getKey(), entry.getValue(), bufferFactory)).concatWith(generateLastLine(boundary, bufferFactory)).doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
    if (logger.isDebugEnabled()) {
        body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
    }
    return outputMessage.writeWith(body);
}
Also used : Arrays(java.util.Arrays) Assert(cn.taketoday.lang.Assert) ResolvableTypeProvider(cn.taketoday.core.ResolvableTypeProvider) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Resource(cn.taketoday.core.io.Resource) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CodecException(cn.taketoday.core.codec.CodecException) LogFormatUtils(cn.taketoday.util.LogFormatUtils) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HttpHeaders(cn.taketoday.http.HttpHeaders) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) Publisher(org.reactivestreams.Publisher) ResolvableType(cn.taketoday.core.ResolvableType) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) Mono(reactor.core.publisher.Mono) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Flux(reactor.core.publisher.Flux) List(java.util.List) Nullable(cn.taketoday.lang.Nullable) Optional(java.util.Optional) HttpEntity(cn.taketoday.http.HttpEntity) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Example 12 with MultiValueMap

use of cn.taketoday.core.MultiValueMap 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);
}
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 13 with MultiValueMap

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

the class FormHttpMessageReaderTests method readFormError.

@Test
public void readFormError() {
    DataBuffer fooBuffer = stringBuffer("name=value");
    Flux<DataBuffer> body = Flux.just(fooBuffer).concatWith(Flux.error(new RuntimeException()));
    MockServerHttpRequest request = request(body);
    Flux<MultiValueMap<String, String>> result = this.reader.read(null, request, null);
    StepVerifier.create(result).expectError().verify();
}
Also used : MockServerHttpRequest(cn.taketoday.http.server.reactive.MockServerHttpRequest) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 14 with MultiValueMap

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

the class BodyInsertersTests method fromFormDataMap.

@Test
public void fromFormDataMap() {
    MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    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);
    BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> inserter = BodyInserters.fromFormData(body);
    MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com"));
    Mono<Void> result = inserter.insert(request, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(request.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        assertThat(resultBytes).isEqualTo("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8));
    }).expectComplete().verify();
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 15 with MultiValueMap

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

the class RequestResponseBodyMethodProcessorTests method resolveArgumentRawTypeFromParameterizedType.

@Test
public void resolveArgumentRawTypeFromParameterizedType() throws Throwable {
    String content = "fruit=apple&vegetable=kale";
    this.servletRequest.setMethod("GET");
    this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
    this.servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new AllEncompassingFormHttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    @SuppressWarnings("unchecked") MultiValueMap<String, String> result = (MultiValueMap<String, String>) processor.resolveArgument(request, paramMultiValueMap);
    assertThat(result).isNotNull();
    assertThat(result.getFirst("fruit")).isEqualTo("apple");
    assertThat(result.getFirst("vegetable")).isEqualTo("kale");
}
Also used : HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) 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