@Override public Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) { MediaType contentType = getContentType(message); return this.decoder.decode(message.getBody(), elementType, contentType, hints); }
@Override public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) { return this.decoder.canDecode(elementType, mediaType); }
private Mono<?> decodeData(String data, ResolvableType dataType, Map<String, Object> hints) { if (String.class == dataType.resolve()) { return Mono.just(data.substring(0, data.length() - 1)); } if (this.decoder == null) { return Mono.error(new CodecException("No SSE decoder configured and the data is not String.")); } byte[] bytes = data.getBytes(StandardCharsets.UTF_8); Mono<DataBuffer> input = Mono.just(bufferFactory.wrap(bytes)); return this.decoder.decodeToMono(input, dataType, MediaType.TEXT_EVENT_STREAM, hints); }
@SuppressWarnings("unchecked") private void assertStringDecoder(Decoder<?> decoder, boolean textOnly) { assertEquals(StringDecoder.class, decoder.getClass()); assertTrue(decoder.canDecode(forClass(String.class), MimeTypeUtils.TEXT_PLAIN)); assertEquals(!textOnly, decoder.canDecode(forClass(String.class), MediaType.TEXT_EVENT_STREAM)); Flux<String> flux = (Flux<String>) decoder.decode( Flux.just(new DefaultDataBufferFactory().wrap("line1\nline2".getBytes(StandardCharsets.UTF_8))), ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()); assertEquals(Arrays.asList("line1", "line2"), flux.collectList().block(Duration.ZERO)); }
public Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) { MediaType contentType = getContentType(message); return this.decoder.decode(message.getBody(), elementType, contentType, hints); public Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) { MediaType contentType = getContentType(message); return this.decoder.decodeToMono(message.getBody(), elementType, contentType, hints);
/** * Create an instance wrapping the given {@link Decoder}. */ public DecoderHttpMessageReader(Decoder<T> decoder) { Assert.notNull(decoder, "Decoder is required"); initLogger(decoder); this.decoder = decoder; this.mediaTypes = MediaType.asMediaTypes(decoder.getDecodableMimeTypes()); }
@SuppressWarnings("unchecked") private void assertStringDecoder(Decoder<?> decoder, boolean textOnly) { assertEquals(StringDecoder.class, decoder.getClass()); assertTrue(decoder.canDecode(forClass(String.class), MimeTypeUtils.TEXT_PLAIN)); assertEquals(!textOnly, decoder.canDecode(forClass(String.class), MediaType.TEXT_EVENT_STREAM)); Flux<String> decoded = (Flux<String>) decoder.decode( Flux.just(new DefaultDataBufferFactory().wrap("line1\nline2".getBytes(StandardCharsets.UTF_8))), ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()); assertEquals(Arrays.asList("line1", "line2"), decoded.collectList().block(Duration.ZERO)); }
/** * Create an instance wrapping the given {@link Decoder}. */ public DecoderHttpMessageReader(Decoder<T> decoder) { Assert.notNull(decoder, "Decoder is required"); this.decoder = decoder; this.mediaTypes = MediaType.asMediaTypes(decoder.getDecodableMimeTypes()); initLogger(decoder); }
@Override public Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) { MediaType contentType = getContentType(message); return this.decoder.decodeToMono(message.getBody(), elementType, contentType, hints); }
@Override public Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) { MediaType contentType = getContentType(message); return this.decoder.decode(message.getBody(), elementType, contentType, hints); }
@Override public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) { return this.decoder.canDecode(elementType, mediaType); }
/** * Create an instance wrapping the given {@link Decoder}. */ public DecoderHttpMessageReader(Decoder<T> decoder) { Assert.notNull(decoder, "Decoder is required"); this.decoder = decoder; this.mediaTypes = MediaType.asMediaTypes(decoder.getDecodableMimeTypes()); }
private Mono<?> decodeData(String data, ResolvableType dataType, Map<String, Object> hints) { if (String.class == dataType.resolve()) { return Mono.just(data.substring(0, data.length() - 1)); } if (this.decoder == null) { return Mono.error(new CodecException("No SSE decoder configured and the data is not String.")); } byte[] bytes = data.getBytes(StandardCharsets.UTF_8); Mono<DataBuffer> input = Mono.just(bufferFactory.wrap(bytes)); return this.decoder.decodeToMono(input, dataType, MediaType.TEXT_EVENT_STREAM, hints); }
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { Flux<T> result = (Flux<T>) this.decoder.decode(input, outputType, mimeType, hints); StepVerifier.FirstStep<T> step = StepVerifier.create(result); stepConsumer.accept(step);
private void assertStringDecoder(Decoder<?> decoder, boolean textOnly) { assertEquals(StringDecoder.class, decoder.getClass()); assertTrue(decoder.canDecode(ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN)); assertEquals(!textOnly, decoder.canDecode(ResolvableType.forClass(String.class), MediaType.TEXT_EVENT_STREAM)); }
/** * Create an instance wrapping the given {@link Decoder}. */ public DecoderHttpMessageReader(Decoder<T> decoder) { Assert.notNull(decoder, "Decoder is required"); this.decoder = decoder; this.mediaTypes = MediaType.asMediaTypes(decoder.getDecodableMimeTypes()); initLogger(decoder); }
@Override public Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) { MediaType contentType = getContentType(message); return this.decoder.decodeToMono(message.getBody(), elementType, contentType, hints); }
/** * Test a {@link Decoder#decode decode} scenario where the input stream is empty. * The output is expected to be empty as well. * * @param outputType the desired output type * @param mimeType the mime type to use for decoding. May be {@code null}. * @param hints the hints used for decoding. May be {@code null}. */ protected void testDecodeEmpty(ResolvableType outputType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { Flux<DataBuffer> input = Flux.empty(); Flux<?> result = this.decoder.decode(input, outputType, mimeType, hints); StepVerifier.create(result) .verifyComplete(); }
@Test public void defaultsOffCustomReaders() { Decoder<?> customDecoder1 = mock(Decoder.class); Decoder<?> customDecoder2 = mock(Decoder.class); when(customDecoder1.canDecode(ResolvableType.forClass(Object.class), null)).thenReturn(false); when(customDecoder2.canDecode(ResolvableType.forClass(Object.class), null)).thenReturn(true); HttpMessageReader<?> customReader1 = mock(HttpMessageReader.class); HttpMessageReader<?> customReader2 = mock(HttpMessageReader.class); when(customReader1.canRead(ResolvableType.forClass(Object.class), null)).thenReturn(false); when(customReader2.canRead(ResolvableType.forClass(Object.class), null)).thenReturn(true); this.configurer.customCodecs().decoder(customDecoder1); this.configurer.customCodecs().decoder(customDecoder2); this.configurer.customCodecs().reader(customReader1); this.configurer.customCodecs().reader(customReader2); this.configurer.registerDefaults(false); List<HttpMessageReader<?>> readers = this.configurer.getReaders(); assertEquals(4, readers.size()); assertSame(customDecoder1, getNextDecoder(readers)); assertSame(customReader1, readers.get(this.index.getAndIncrement())); assertSame(customDecoder2, getNextDecoder(readers)); assertSame(customReader2, readers.get(this.index.getAndIncrement())); }
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { Mono<T> result = (Mono<T>) this.decoder.decodeToMono(input, outputType, mimeType, hints); StepVerifier.FirstStep<T> step = StepVerifier.create(result); stepConsumer.accept(step);