SSE reader support to read full content as a String
The SSE reader is ordered ahead of StringDecoder because with response.decodeToFlux(String.class) we actually want the SSE reader to get involved first based on the content-type. At the same time with response.decodeToMono(String.class) there is nothing the SSE reader can do while the StringDecoder could read the entore content as one String, as long as the server does terminate the stream which can happen in a testing scenario. This commit updates ServerSentEventHttpMessageReader#readMono in to support String.class by delegating to StringDecoder. Since reading to a Mono is an explicit choice there is not much possibility for interfering with decoding to Flux. Issue: SPR-15331
This commit is contained in:
@@ -31,6 +31,7 @@ import reactor.core.publisher.Mono;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.codec.CodecException;
|
||||
import org.springframework.core.codec.Decoder;
|
||||
import org.springframework.core.codec.StringDecoder;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
@@ -57,6 +58,8 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
|
||||
|
||||
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
|
||||
|
||||
private static final StringDecoder stringDecoder = new StringDecoder(false);
|
||||
|
||||
|
||||
private final List<Decoder<?>> dataDecoders;
|
||||
|
||||
@@ -178,6 +181,13 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
|
||||
public Mono<Object> readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage,
|
||||
Map<String, Object> hints) {
|
||||
|
||||
// Let's give StringDecoder a chance since SSE is ordered ahead of it
|
||||
|
||||
if (String.class.equals(elementType.getRawClass())) {
|
||||
Flux<DataBuffer> body = inputMessage.getBody();
|
||||
return stringDecoder.decodeToMono(body, elementType, null, null).cast(Object.class);
|
||||
}
|
||||
|
||||
return Mono.error(new UnsupportedOperationException(
|
||||
"ServerSentEventHttpMessageReader only supports reading stream of events as a Flux"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user