/* * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.web.servlet.mvc.method.annotation; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.server.ServerHttpResponse; import org.springframework.lang.Nullable; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; /** * A specialization of {@link ResponseBodyEmitter} for sending * Server-Sent Events. * * @author Rossen Stoyanchev * @author Juergen Hoeller * @since 4.2 */ public class SseEmitter extends ResponseBodyEmitter { static final MediaType TEXT_PLAIN = new MediaType("text", "plain", StandardCharsets.UTF_8); static final MediaType UTF8_TEXT_EVENTSTREAM = new MediaType("text", "event-stream", StandardCharsets.UTF_8); /** * Create a new SseEmitter instance. */ public SseEmitter() { super(); } /** * Create a SseEmitter with a custom timeout value. *
By default not set in which case the default configured in the MVC * Java Config or the MVC namespace is used, or if that's not set, then the * timeout depends on the default of the underlying server. * @param timeout timeout value in milliseconds * @since 4.2.2 */ public SseEmitter(Long timeout) { super(timeout); } @Override protected void extendResponse(ServerHttpResponse outputMessage) { super.extendResponse(outputMessage); HttpHeaders headers = outputMessage.getHeaders(); if (headers.getContentType() == null) { headers.setContentType(UTF8_TEXT_EVENTSTREAM); } } /** * Send the object formatted as a single SSE "data" line. It's equivalent to: *
* // static import of SseEmitter.* * * SseEmitter emitter = new SseEmitter(); * emitter.send(event().data(myObject)); ** @param object the object to write * @throws IOException raised when an I/O error occurs * @throws java.lang.IllegalStateException wraps any other errors */ @Override public void send(Object object) throws IOException { send(object, null); } /** * Send the object formatted as a single SSE "data" line. It's equivalent to: *
* // static import of SseEmitter.* * * SseEmitter emitter = new SseEmitter(); * emitter.send(event().data(myObject, MediaType.APPLICATION_JSON)); ** @param object the object to write * @param mediaType a MediaType hint for selecting an HttpMessageConverter * @throws IOException raised when an I/O error occurs */ @Override public void send(Object object, @Nullable MediaType mediaType) throws IOException { send(event().data(object, mediaType)); } /** * Send an SSE event prepared with the given builder. For example: *
* // static import of SseEmitter * SseEmitter emitter = new SseEmitter(); * emitter.send(event().name("update").id("1").data(myObject)); ** @param builder a builder for an SSE formatted event. * @throws IOException raised when an I/O error occurs */ public void send(SseEventBuilder builder) throws IOException { Set