Top Description Inners Fields Constructors Methods
org.springframework.web.servlet.function

pack-priv final Class SseServerResponse

extends AbstractServerResponse
Class Inheritance
Imports
java.io.IOException, .OutputStream, java.nio.charset.StandardCharsets, java.time.Duration, java.util.Collections, .List, java.util.function.Consumer, jakarta.servlet.ServletException, jakarta.servlet.http.Cookie, .HttpServletRequest, .HttpServletResponse, org.springframework.http.CacheControl, .HttpHeaders, .HttpStatus, .MediaType, org.springframework.http.converter.HttpMessageConverter, org.springframework.http.server.DelegatingServerHttpResponse, .ServerHttpResponse, .ServletServerHttpResponse, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, .MultiValueMap, org.springframework.web.context.request.async.DeferredResult, org.springframework.web.servlet.ModelAndView

Implementation of ServerResponse for sending Server-Sent Events.
Authors
Arjen Poutsma, Sebastien Deleuze
Since
5.3.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final Consumer<ServerResponse.SseBuilder>
private final Duration

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ServerResponse
private static HttpHeaders
private static MultiValueMap<String, Cookie>
protected ModelAndView
Inherited from org.springframework.web.servlet.function.AbstractServerResponse:
cookiesheadersrawStatusCodestatusCodewriteTo

Field Detail

sseConsumerback to summary
private final Consumer<ServerResponse.SseBuilder> sseConsumer
timeoutback to summary
private final Duration timeout
Annotations
@Nullable

Constructor Detail

SseServerResponseback to summary
private SseServerResponse(Consumer<ServerResponse.SseBuilder> sseConsumer, Duration timeout)

Method Detail

createback to summary
public static ServerResponse create(Consumer<ServerResponse.SseBuilder> sseConsumer, Duration timeout)
createHeadersback to summary
private static HttpHeaders createHeaders()
emptyCookiesback to summary
private static MultiValueMap<String, Cookie> emptyCookies()
writeToInternalback to summary
protected ModelAndView writeToInternal(HttpServletRequest request, HttpServletResponse response, ServerResponse.Context context) throws ServletException, IOException

Implements abstract org.springframework.web.servlet.function.AbstractServerResponse.writeToInternal.

Annotations
@Nullable
@Override
org.springframework.web.servlet.function back to summary

private final Class SseServerResponse.DefaultSseBuilder

extends Object
implements SseBuilder
Class Inheritance
  • java.lang.Object
  • org.springframework.web.servlet.function.SseServerResponse.DefaultSseBuilder
All Implemented Interfaces
org.springframework.web.servlet.function.ServerResponse.SseBuilder

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
SseServerResponse.DefaultSseBuilder.MutableHeadersServerHttpResponse

Wrap to silently ignore header changes HttpMessageConverter's that would otherwise cause HttpHeaders to raise exceptions.

Field Summary

Modifier and TypeField and Description
private final StringBuilder
private final DeferredResult<?>
private final HttpHeaders
private final List<HttpMessageConverter<?>>
private static final byte[]
private final ServerHttpResponse
private boolean

Constructor Summary

AccessConstructor and Description
public
DefaultSseBuilder(HttpServletResponse response, ServerResponse.Context context, DeferredResult<?> deferredResult, HttpHeaders httpHeaders)

Method Summary

Modifier and TypeMethod and Description
private byte[]
public ServerResponse.SseBuilder
public void
public void
data(Object
the object to send as data
object
)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.data.

Add an SSE "data" line for the given object and sends the built server-sent event to the client.

public void
error(Throwable
the throwable to dispatch
t
)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.error.

Completes the event stream with the given error.

public ServerResponse.SseBuilder
event(String
the event name
eventName
)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.event.

Add an SSE "event" line.

private ServerResponse.SseBuilder
field(String name, String value)

public ServerResponse.SseBuilder
id(String
the event identifier
id
)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.id.

Add an SSE "id" line.

public ServerResponse.SseBuilder
onComplete(Runnable
the callback to invoked on completion
onCompletion
)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.onComplete.

Register a callback to be invoked when the SSE request completes.

public ServerResponse.SseBuilder
onError(Consumer<Throwable>
the callback to invoke on error
onError
)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.onError.

Register a callback to be invoked when an error occurs during SSE processing.

public ServerResponse.SseBuilder
onTimeout(Runnable
the callback to invoke on timeout
onTimeout
)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.onTimeout.

Register a callback to be invoked when an SSE request times out.

public ServerResponse.SseBuilder
retry(Duration
the duration to convert into millis
duration
)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.retry.

Add an SSE "retry" line.

public void
send(Object
the object to send
object
)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.send.

Sends the given object as a server-sent event.

public void
send()

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.send.

Sends the buffered content as a server-sent event, without data.

private void
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

builderback to summary
private final StringBuilder builder
deferredResultback to summary
private final DeferredResult<?> deferredResult
httpHeadersback to summary
private final HttpHeaders httpHeaders
messageConvertersback to summary
private final List<HttpMessageConverter<?>> messageConverters
NL_NLback to summary
private static final byte[] NL_NL
outputMessageback to summary
private final ServerHttpResponse outputMessage
sendFailedback to summary
private boolean sendFailed

Constructor Detail

DefaultSseBuilderback to summary
public DefaultSseBuilder(HttpServletResponse response, ServerResponse.Context context, DeferredResult<?> deferredResult, HttpHeaders httpHeaders)

Method Detail

builderBytesback to summary
private byte[] builderBytes()
commentback to summary
public ServerResponse.SseBuilder comment(String comment)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.comment.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.comment.

Add an SSE comment.

Parameters
comment:String

the comment

Returns:ServerResponse.SseBuilder

this builder

Annotations
@Override
completeback to summary
public void complete()

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.complete.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.complete.

Completes the event stream.

Annotations
@Override
databack to summary
public void data(Object object) throws IOException

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.data.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.data.

Add an SSE "data" line for the given object and sends the built server-sent event to the client. Strings will be sent as UTF-8 encoded bytes, and other objects will be converted into JSON using message converters.

Parameters
object:Object

the object to send as data

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
errorback to summary
public void error(Throwable t)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.error.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.error.

Completes the event stream with the given error.

The throwable is dispatched back into Spring MVC, and passed to its exception handling mechanism. Since the response has been committed by this point, the response status can not change.

Parameters
t:Throwable

the throwable to dispatch

Annotations
@Override
eventback to summary
public ServerResponse.SseBuilder event(String eventName)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.event.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.event.

Add an SSE "event" line.

Parameters
eventName:String

the event name

Returns:ServerResponse.SseBuilder

this builder

Annotations
@Override
fieldback to summary
private ServerResponse.SseBuilder field(String name, String value)
idback to summary
public ServerResponse.SseBuilder id(String id)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.id.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.id.

Add an SSE "id" line.

Parameters
id:String

the event identifier

Returns:ServerResponse.SseBuilder

this builder

Annotations
@Override
onCompleteback to summary
public ServerResponse.SseBuilder onComplete(Runnable onCompletion)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.onComplete.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.onComplete.

Register a callback to be invoked when the SSE request completes.

Parameters
onCompletion:Runnable

the callback to invoked on completion

Returns:ServerResponse.SseBuilder

this builder

Annotations
@Override
onErrorback to summary
public ServerResponse.SseBuilder onError(Consumer<Throwable> onError)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.onError.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.onError.

Register a callback to be invoked when an error occurs during SSE processing.

Parameters
onError:Consumer<Throwable>

the callback to invoke on error

Returns:ServerResponse.SseBuilder

this builder

Annotations
@Override
onTimeoutback to summary
public ServerResponse.SseBuilder onTimeout(Runnable onTimeout)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.onTimeout.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.onTimeout.

Register a callback to be invoked when an SSE request times out.

Parameters
onTimeout:Runnable

the callback to invoke on timeout

Returns:ServerResponse.SseBuilder

this builder

Annotations
@Override
retryback to summary
public ServerResponse.SseBuilder retry(Duration duration)

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.retry.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.retry.

Add an SSE "retry" line.

Parameters
duration:Duration

the duration to convert into millis

Returns:ServerResponse.SseBuilder

this builder

Annotations
@Override
sendback to summary
public void send(Object object) throws IOException

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.send.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.send.

Sends the given object as a server-sent event. Strings will be sent as UTF-8 encoded bytes, and other objects will be converted into JSON using message converters.

This convenience method has the same effect as data(Object).

Parameters
object:Object

the object to send

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
sendback to summary
public void send() throws IOException

Implements org.springframework.web.servlet.function.ServerResponse.SseBuilder.send.

Doc from org.springframework.web.servlet.function.ServerResponse.SseBuilder.send.

Sends the buffered content as a server-sent event, without data. Only the events and comments will be sent.

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
writeObjectback to summary
private void writeObject(Object data) throws IOException
Annotations
@SuppressWarnings:unchecked
writeStringback to summary
private void writeString(String string) throws IOException
org.springframework.web.servlet.function back to summary

private final Class SseServerResponse.DefaultSseBuilder.MutableHeadersServerHttpResponse

extends DelegatingServerHttpResponse
Class Inheritance

Wrap to silently ignore header changes HttpMessageConverter's that would otherwise cause HttpHeaders to raise exceptions.

Field Summary

Modifier and TypeField and Description
private final HttpHeaders

Constructor Summary

AccessConstructor and Description
public

Field Detail

mutableHeadersback to summary
private final HttpHeaders mutableHeaders

Constructor Detail

MutableHeadersServerHttpResponseback to summary
public MutableHeadersServerHttpResponse(ServerHttpResponse delegate, HttpHeaders headers)

Method Detail

getHeadersback to summary
public HttpHeaders getHeaders()

Overrides org.springframework.http.server.DelegatingServerHttpResponse.getHeaders.

Implements org.springframework.http.HttpMessage.getHeaders.

Doc from org.springframework.http.HttpMessage.getHeaders.

Return the headers of this message.

Returns:HttpHeaders

a corresponding HttpHeaders object (never null)

Annotations
@Override