Search in sources :

Example 1 with MimeType

use of cn.taketoday.util.MimeType in project today-infrastructure by TAKETODAY.

the class DefaultClientResponse method createException.

@Override
public Mono<WebClientResponseException> createException() {
    return bodyToMono(byte[].class).defaultIfEmpty(EMPTY).onErrorReturn(ex -> !(ex instanceof Error), EMPTY).map(bodyBytes -> {
        HttpRequest request = this.requestSupplier.get();
        Charset charset = headers().contentType().map(MimeType::getCharset).orElse(null);
        int statusCode = rawStatusCode();
        HttpStatus httpStatus = HttpStatus.resolve(statusCode);
        if (httpStatus != null) {
            return WebClientResponseException.create(statusCode, httpStatus.getReasonPhrase(), headers().asHttpHeaders(), bodyBytes, charset, request);
        } else {
            return new UnknownHttpStatusCodeException(statusCode, headers().asHttpHeaders(), bodyBytes, charset, request);
        }
    });
}
Also used : Supplier(java.util.function.Supplier) ResponseCookie(cn.taketoday.http.ResponseCookie) OptionalLong(java.util.OptionalLong) HttpHeaders(cn.taketoday.http.HttpHeaders) ClientHttpResponse(cn.taketoday.http.client.reactive.ClientHttpResponse) Charset(java.nio.charset.Charset) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) TypeReference(cn.taketoday.core.TypeReference) BodyExtractor(cn.taketoday.web.reactive.function.BodyExtractor) HttpRequest(cn.taketoday.http.HttpRequest) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) Mono(reactor.core.publisher.Mono) ResponseEntity(cn.taketoday.http.ResponseEntity) BodyExtractors(cn.taketoday.web.reactive.function.BodyExtractors) Flux(reactor.core.publisher.Flux) List(java.util.List) Optional(java.util.Optional) HttpStatus(cn.taketoday.http.HttpStatus) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) MimeType(cn.taketoday.util.MimeType) HttpRequest(cn.taketoday.http.HttpRequest) HttpStatus(cn.taketoday.http.HttpStatus) Charset(java.nio.charset.Charset)

Example 2 with MimeType

use of cn.taketoday.util.MimeType in project today-infrastructure by TAKETODAY.

the class Jackson2JsonDecoder method processInput.

@Override
protected Flux<DataBuffer> processInput(Publisher<DataBuffer> input, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
    Flux<DataBuffer> flux = Flux.from(input);
    if (mimeType == null) {
        return flux;
    }
    // Jackson asynchronous parser only supports UTF-8
    Charset charset = mimeType.getCharset();
    if (charset == null || StandardCharsets.UTF_8.equals(charset) || StandardCharsets.US_ASCII.equals(charset)) {
        return flux;
    }
    // Potentially, the memory consumption of this conversion could be improved by using CharBuffers instead
    // of allocating Strings, but that would require refactoring the buffer tokenization code from StringDecoder
    MimeType textMimeType = new MimeType(MimeTypeUtils.TEXT_PLAIN, charset);
    Flux<String> decoded = STRING_DECODER.decode(input, STRING_TYPE, textMimeType, null);
    return decoded.map(s -> DefaultDataBufferFactory.sharedInstance.wrap(s.getBytes(StandardCharsets.UTF_8)));
}
Also used : Charset(java.nio.charset.Charset) MimeType(cn.taketoday.util.MimeType) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Example 3 with MimeType

use of cn.taketoday.util.MimeType in project today-infrastructure by TAKETODAY.

the class Jackson2JsonEncoderTests method encodeAscii.

@Test
public void encodeAscii() {
    Mono<Object> input = Mono.just(new Pojo("foo", "bar"));
    testEncode(input, ResolvableType.fromClass(Pojo.class), step -> step.consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}")).verifyComplete(), new MimeType("application", "json", StandardCharsets.US_ASCII), null);
}
Also used : Pojo(cn.taketoday.http.codec.Pojo) MimeType(cn.taketoday.util.MimeType) Test(org.junit.jupiter.api.Test)

Example 4 with MimeType

use of cn.taketoday.util.MimeType in project today-infrastructure by TAKETODAY.

the class Jackson2JsonEncoderTests method canEncodeWithCustomMimeType.

// SPR-15866
@Test
public void canEncodeWithCustomMimeType() {
    MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8);
    Jackson2JsonEncoder encoder = new Jackson2JsonEncoder(new ObjectMapper(), textJavascript);
    assertThat(encoder.getEncodableMimeTypes()).isEqualTo(Collections.singletonList(textJavascript));
}
Also used : MimeType(cn.taketoday.util.MimeType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 5 with MimeType

use of cn.taketoday.util.MimeType in project today-infrastructure by TAKETODAY.

the class Jackson2JsonDecoderTests method canDecodeWithProvidedMimeType.

// SPR-15866
@Test
public void canDecodeWithProvidedMimeType() {
    MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8);
    Jackson2JsonDecoder decoder = new Jackson2JsonDecoder(new ObjectMapper(), textJavascript);
    assertThat(decoder.getDecodableMimeTypes()).isEqualTo(Collections.singletonList(textJavascript));
}
Also used : MimeType(cn.taketoday.util.MimeType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Aggregations

MimeType (cn.taketoday.util.MimeType)25 Test (org.junit.jupiter.api.Test)18 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Msg (cn.taketoday.protobuf.Msg)4 SecondMsg (cn.taketoday.protobuf.SecondMsg)4 Charset (java.nio.charset.Charset)3 ResolvableType (cn.taketoday.core.ResolvableType)2 ClassPathResource (cn.taketoday.core.io.ClassPathResource)2 Resource (cn.taketoday.core.io.Resource)2 ReactiveHttpOutputMessage (cn.taketoday.http.ReactiveHttpOutputMessage)2 Pojo (cn.taketoday.http.codec.Pojo)2 ProtobufDecoder (cn.taketoday.http.codec.protobuf.ProtobufDecoder)2 ProtobufEncoder (cn.taketoday.http.codec.protobuf.ProtobufEncoder)2 Message (com.google.protobuf.Message)2 MultiValueMap (cn.taketoday.core.MultiValueMap)1 TypeReference (cn.taketoday.core.TypeReference)1 Hints (cn.taketoday.core.codec.Hints)1 DataBufferUtils (cn.taketoday.core.io.buffer.DataBufferUtils)1 HttpHeaders (cn.taketoday.http.HttpHeaders)1