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);
}
});
}
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)));
}
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);
}
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));
}
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));
}
Aggregations