Java Code Examples for org.springframework.http.MediaType#TEXT_EVENT_STREAM

The following examples show how to use org.springframework.http.MediaType#TEXT_EVENT_STREAM . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BodyInserters.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Inserter to write the given {@code ServerSentEvent} publisher.
 * <p>Alternatively, you can provide event data objects via
 * {@link #fromPublisher(Publisher, Class)}, and set the "Content-Type" to
 * {@link MediaType#TEXT_EVENT_STREAM text/event-stream}.
 * @param eventsPublisher the {@code ServerSentEvent} publisher to write to the response body
 * @param <T> the type of the data elements in the {@link ServerSentEvent}
 * @return the inserter to write a {@code ServerSentEvent} publisher
 * @see <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
 */
// Parameterized for server-side use
public static <T, S extends Publisher<ServerSentEvent<T>>> BodyInserter<S, ServerHttpResponse> fromServerSentEvents(
		S eventsPublisher) {

	Assert.notNull(eventsPublisher, "Publisher must not be null");
	return (serverResponse, context) -> {
		ResolvableType elementType = SSE_TYPE;
		MediaType mediaType = MediaType.TEXT_EVENT_STREAM;
		HttpMessageWriter<ServerSentEvent<T>> writer = findWriter(context, elementType, mediaType);
		return write(eventsPublisher, elementType, mediaType, serverResponse, context, writer);
	};
}
 
Example 2
Source File: BodyInserters.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Inserter to write the given {@code ServerSentEvent} publisher.
 * <p>Alternatively, you can provide event data objects via
 * {@link #fromPublisher(Publisher, Class)}, and set the "Content-Type" to
 * {@link MediaType#TEXT_EVENT_STREAM text/event-stream}.
 * @param eventsPublisher the {@code ServerSentEvent} publisher to write to the response body
 * @param <T> the type of the data elements in the {@link ServerSentEvent}
 * @return the inserter to write a {@code ServerSentEvent} publisher
 * @see <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
 */
// Parameterized for server-side use
public static <T, S extends Publisher<ServerSentEvent<T>>> BodyInserter<S, ServerHttpResponse> fromServerSentEvents(
		S eventsPublisher) {

	Assert.notNull(eventsPublisher, "Publisher must not be null");
	return (serverResponse, context) -> {
		ResolvableType elementType = SSE_TYPE;
		MediaType mediaType = MediaType.TEXT_EVENT_STREAM;
		HttpMessageWriter<ServerSentEvent<T>> writer = findWriter(context, elementType, mediaType);
		return write(eventsPublisher, elementType, mediaType, serverResponse, context, writer);
	};
}