Top Description Inners Fields Constructors Methods
org.springframework.http.codec

public Class ServerSentEventHttpMessageReader

extends Object
implements HttpMessageReader<Object>
Class Inheritance
All Implemented Interfaces
org.springframework.http.codec.HttpMessageReader
Imports
java.nio.charset.StandardCharsets, java.time.Duration, java.util.Collections, .List, .Map, reactor.core.publisher.Flux, .Mono, org.springframework.core.ResolvableType, org.springframework.core.codec.CodecException, .Decoder, .StringDecoder, org.springframework.core.io.buffer.DataBuffer, .DataBufferLimitException, .DefaultDataBufferFactory, org.springframework.http.MediaType, .ReactiveHttpInputMessage, org.springframework.lang.Nullable

Reader that supports a stream of ServerSentEvents and also plain Objects which is the same as an ServerSentEvent with data only.
Authors
Sebastien Deleuze, Rossen Stoyanchev, Juergen Hoeller
Since
5.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private final Decoder<?>
private final StringDecoder
private static final ResolvableType

Constructor Summary

AccessConstructor and Description
public
ServerSentEventHttpMessageReader()

Constructor without a Decoder.

public
ServerSentEventHttpMessageReader(Decoder<?> decoder)

Constructor with JSON Decoder for decoding to Objects.

Method Summary

Modifier and TypeMethod and Description
private Object
buildEvent(List<String> lines, ResolvableType valueType, boolean shouldWrap, Map<String, Object> hints)

public boolean
canRead(ResolvableType
the type of object to check
elementType
,
MediaType
the media type for the read (possibly null)
mediaType
)

Implements org.springframework.http.codec.HttpMessageReader.canRead.

Whether the given object type is supported by this reader.

private Object
public Decoder<?>
getDecoder()

Return the configured Decoder.

public int
getMaxInMemorySize()

Return the configured byte count limit.

public List<MediaType>
getReadableMediaTypes()

Implements org.springframework.http.codec.HttpMessageReader.getReadableMediaTypes.

Return the list of media types supported by this reader.

private boolean
public Flux<Object>
read(ResolvableType
the type of objects in the stream which must have been previously checked via canRead(ResolvableType, MediaType)
elementType
,
ReactiveHttpInputMessage
the message to read from
message
,
Map<String, Object>
additional information about how to read and decode the input
hints
)

Implements org.springframework.http.codec.HttpMessageReader.read.

Read from the input message and decode to a stream of objects.

public Mono<Object>
readMono(ResolvableType
the type of objects in the stream which must have been previously checked via canRead(ResolvableType, MediaType)
elementType
,
ReactiveHttpInputMessage
the message to read from
message
,
Map<String, Object>
additional information about how to read and decode the input
hints
)

Implements org.springframework.http.codec.HttpMessageReader.readMono.

Read from the input message and decode to a single object.

public void
setMaxInMemorySize(int
the max number of bytes to buffer, or -1 for unlimited
byteCount
)

Configure a limit on the maximum number of bytes per SSE event which are buffered before the event is parsed.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

decoderback to summary
private final Decoder<?> decoder
Annotations
@Nullable
lineDecoderback to summary
private final StringDecoder lineDecoder
STRING_TYPEback to summary
private static final ResolvableType STRING_TYPE

Constructor Detail

ServerSentEventHttpMessageReaderback to summary
public ServerSentEventHttpMessageReader()

Constructor without a Decoder. In this mode only String is supported as the data of an event.

ServerSentEventHttpMessageReaderback to summary
public ServerSentEventHttpMessageReader(Decoder<?> decoder)

Constructor with JSON Decoder for decoding to Objects. Support for decoding to String event data is built-in.

Method Detail

buildEventback to summary
private Object buildEvent(List<String> lines, ResolvableType valueType, boolean shouldWrap, Map<String, Object> hints)
Annotations
@Nullable
canReadback to summary
public boolean canRead(ResolvableType elementType, MediaType mediaType)

Implements org.springframework.http.codec.HttpMessageReader.canRead.

Doc from org.springframework.http.codec.HttpMessageReader.canRead.

Whether the given object type is supported by this reader.

Parameters
elementType:ResolvableType

the type of object to check

mediaType:MediaType

the media type for the read (possibly null)

Returns:boolean

true if readable, false otherwise

Annotations
@Override
decodeDataback to summary
private Object decodeData(StringBuilder data, ResolvableType dataType, Map<String, Object> hints)
Annotations
@Nullable
getDecoderback to summary
public Decoder<?> getDecoder()

Return the configured Decoder.

Annotations
@Nullable
getMaxInMemorySizeback to summary
public int getMaxInMemorySize()

Return the configured byte count limit.

Since
5.1.13
getReadableMediaTypesback to summary
public List<MediaType> getReadableMediaTypes()

Implements org.springframework.http.codec.HttpMessageReader.getReadableMediaTypes.

Doc from org.springframework.http.codec.HttpMessageReader.getReadableMediaTypes.

Return the list of media types supported by this reader. The list may not apply to every possible target element type and calls to this method should typically be guarded via canWrite(elementType, null). The list may also exclude media types supported only for a specific element type. Alternatively, use getReadableMediaTypes(ResolvableType) for a more precise list.

Returns:List<MediaType>

the general list of supported media types

Annotations
@Override
isServerSentEventback to summary
private boolean isServerSentEvent(ResolvableType elementType)
readback to summary
public Flux<Object> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints)

Implements org.springframework.http.codec.HttpMessageReader.read.

Doc from org.springframework.http.codec.HttpMessageReader.read.

Read from the input message and decode to a stream of objects.

Parameters
elementType:ResolvableType

the type of objects in the stream which must have been previously checked via canRead(ResolvableType, MediaType)

message:ReactiveHttpInputMessage

the message to read from

hints:Map<String, Object>

additional information about how to read and decode the input

Returns:Flux<Object>

the decoded stream of elements

Annotations
@Override
readMonoback to summary
public Mono<Object> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints)

Implements org.springframework.http.codec.HttpMessageReader.readMono.

Doc from org.springframework.http.codec.HttpMessageReader.readMono.

Read from the input message and decode to a single object.

Parameters
elementType:ResolvableType

the type of objects in the stream which must have been previously checked via canRead(ResolvableType, MediaType)

message:ReactiveHttpInputMessage

the message to read from

hints:Map<String, Object>

additional information about how to read and decode the input

Returns:Mono<Object>

the decoded object

Annotations
@Override
setMaxInMemorySizeback to summary
public void setMaxInMemorySize(int byteCount)

Configure a limit on the maximum number of bytes per SSE event which are buffered before the event is parsed.

Note that the data decoder, if provided, must also be customized accordingly to raise the limit if necessary in order to be able to parse the data portion of the event.

By default this is set to 256K.

Parameters
byteCount:int

the max number of bytes to buffer, or -1 for unlimited

Since
5.1.13
org.springframework.http.codec back to summary

private Class ServerSentEventHttpMessageReader.LimitTracker

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.http.codec.ServerSentEventHttpMessageReader.LimitTracker

Field Summary

Modifier and TypeField and Description
private int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

accumulatedback to summary
private int accumulated

Constructor Detail

LimitTrackerback to summary
private LimitTracker()

Method Detail

afterLineParsedback to summary
public void afterLineParsed(String line)
raiseLimitExceptionback to summary
private void raiseLimitException()