Browse Source
Prior to this commit, MVC function endpoints would allow Server Sent Event responses through `ServerResponse.sse()`. While this covers a common use case for streaming responses, other technologies would benefit from a "low-level", unopinionated streaming support. This commit introduces a new `BodyBuilder.stream()` methods that enables such use cases. Developers are in charge of setting the relevant HTTP response headers beforehand, and then can write to the response as raw `String`, `byte[]` or using complex objects and the configured message converters for serialization. Because each streaming protocol has different message separator semantics, it is also the developers' responsibility to flush buffered content to the network once a message has been fully written. Closes gh-32710pull/33525/head
4 changed files with 466 additions and 27 deletions
@ -0,0 +1,223 @@
@@ -0,0 +1,223 @@
|
||||
/* |
||||
* Copyright 2002-2024 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 |
||||
* |
||||
* https://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.function; |
||||
|
||||
import java.io.IOException; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.time.Duration; |
||||
import java.util.List; |
||||
import java.util.function.Consumer; |
||||
|
||||
import jakarta.servlet.http.Cookie; |
||||
import jakarta.servlet.http.HttpServletRequest; |
||||
import jakarta.servlet.http.HttpServletResponse; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.HttpStatusCode; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.http.converter.HttpMessageConverter; |
||||
import org.springframework.http.server.DelegatingServerHttpResponse; |
||||
import org.springframework.http.server.ServerHttpResponse; |
||||
import org.springframework.http.server.ServletServerHttpResponse; |
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.MultiValueMap; |
||||
import org.springframework.web.context.request.async.DeferredResult; |
||||
import org.springframework.web.servlet.ModelAndView; |
||||
|
||||
/** |
||||
* Implementation of {@link ServerResponse} for sending streaming response bodies. |
||||
* |
||||
* @author Brian Clozel |
||||
*/ |
||||
final class StreamingServerResponse extends AbstractServerResponse { |
||||
|
||||
private final Consumer<StreamBuilder> streamConsumer; |
||||
|
||||
@Nullable |
||||
private final Duration timeout; |
||||
|
||||
private StreamingServerResponse(HttpStatusCode statusCode, HttpHeaders headers, MultiValueMap<String, Cookie> cookies, |
||||
Consumer<StreamBuilder> streamConsumer, @Nullable Duration timeout) { |
||||
super(statusCode, headers, cookies); |
||||
this.streamConsumer = streamConsumer; |
||||
this.timeout = timeout; |
||||
} |
||||
|
||||
static ServerResponse create(HttpStatusCode statusCode, HttpHeaders headers, MultiValueMap<String, Cookie> cookies, |
||||
Consumer<StreamBuilder> streamConsumer, @Nullable Duration timeout) { |
||||
Assert.notNull(statusCode, "statusCode must not be null"); |
||||
Assert.notNull(headers, "headers must not be null"); |
||||
Assert.notNull(cookies, "cookies must not be null"); |
||||
Assert.notNull(streamConsumer, "streamConsumer must not be null"); |
||||
return new StreamingServerResponse(statusCode, headers, cookies, streamConsumer, timeout); |
||||
} |
||||
|
||||
@Nullable |
||||
@Override |
||||
protected ModelAndView writeToInternal(HttpServletRequest request, HttpServletResponse response, Context context) throws Exception { |
||||
DeferredResult<?> result; |
||||
if (this.timeout != null) { |
||||
result = new DeferredResult<>(this.timeout.toMillis()); |
||||
} |
||||
else { |
||||
result = new DeferredResult<>(); |
||||
} |
||||
DefaultAsyncServerResponse.writeAsync(request, response, result); |
||||
this.streamConsumer.accept(new DefaultStreamBuilder(response, context, result, this.headers())); |
||||
return null; |
||||
} |
||||
|
||||
private static class DefaultStreamBuilder implements StreamBuilder { |
||||
|
||||
private final ServerHttpResponse outputMessage; |
||||
|
||||
private final DeferredResult<?> deferredResult; |
||||
|
||||
private final List<HttpMessageConverter<?>> messageConverters; |
||||
|
||||
private final HttpHeaders httpHeaders; |
||||
|
||||
private boolean sendFailed; |
||||
|
||||
|
||||
public DefaultStreamBuilder(HttpServletResponse response, Context context, DeferredResult<?> deferredResult, |
||||
HttpHeaders httpHeaders) { |
||||
this.outputMessage = new ServletServerHttpResponse(response); |
||||
this.deferredResult = deferredResult; |
||||
this.messageConverters = context.messageConverters(); |
||||
this.httpHeaders = httpHeaders; |
||||
} |
||||
|
||||
@Override |
||||
public StreamBuilder write(Object object) throws IOException { |
||||
write(object, null); |
||||
return this; |
||||
} |
||||
|
||||
@Override |
||||
public StreamBuilder write(Object object, @Nullable MediaType mediaType) throws IOException { |
||||
Assert.notNull(object, "data must not be null"); |
||||
try { |
||||
if (object instanceof byte[] bytes) { |
||||
this.outputMessage.getBody().write(bytes); |
||||
} |
||||
else if (object instanceof String str) { |
||||
this.outputMessage.getBody().write(str.getBytes(StandardCharsets.UTF_8)); |
||||
} |
||||
else { |
||||
writeObject(object, mediaType); |
||||
} |
||||
} |
||||
catch (IOException ex) { |
||||
this.sendFailed = true; |
||||
throw ex; |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
@SuppressWarnings("unchecked") |
||||
private void writeObject(Object data, @Nullable MediaType mediaType) throws IOException { |
||||
Class<?> elementClass = data.getClass(); |
||||
for (HttpMessageConverter<?> converter : this.messageConverters) { |
||||
if (converter.canWrite(elementClass, mediaType)) { |
||||
HttpMessageConverter<Object> objectConverter = (HttpMessageConverter<Object>) converter; |
||||
ServerHttpResponse response = new MutableHeadersServerHttpResponse(this.outputMessage, this.httpHeaders); |
||||
objectConverter.write(data, mediaType, response); |
||||
return; |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void flush() throws IOException { |
||||
if (this.sendFailed) { |
||||
return; |
||||
} |
||||
try { |
||||
this.outputMessage.flush(); |
||||
} |
||||
catch (IOException ex) { |
||||
this.sendFailed = true; |
||||
throw ex; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void error(Throwable t) { |
||||
if (this.sendFailed) { |
||||
return; |
||||
} |
||||
this.deferredResult.setErrorResult(t); |
||||
} |
||||
|
||||
@Override |
||||
public void complete() { |
||||
if (this.sendFailed) { |
||||
return; |
||||
} |
||||
try { |
||||
this.outputMessage.flush(); |
||||
this.deferredResult.setResult(null); |
||||
} |
||||
catch (IOException ex) { |
||||
this.deferredResult.setErrorResult(ex); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public StreamBuilder onTimeout(Runnable onTimeout) { |
||||
this.deferredResult.onTimeout(onTimeout); |
||||
return this; |
||||
} |
||||
|
||||
@Override |
||||
public StreamBuilder onError(Consumer<Throwable> onError) { |
||||
this.deferredResult.onError(onError); |
||||
return this; |
||||
} |
||||
|
||||
@Override |
||||
public StreamBuilder onComplete(Runnable onCompletion) { |
||||
this.deferredResult.onCompletion(onCompletion); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Wrap to silently ignore header changes HttpMessageConverter's that would |
||||
* otherwise cause HttpHeaders to raise exceptions. |
||||
*/ |
||||
private static final class MutableHeadersServerHttpResponse extends DelegatingServerHttpResponse { |
||||
|
||||
private final HttpHeaders mutableHeaders = new HttpHeaders(); |
||||
|
||||
public MutableHeadersServerHttpResponse(ServerHttpResponse delegate, HttpHeaders headers) { |
||||
super(delegate); |
||||
this.mutableHeaders.putAll(delegate.getHeaders()); |
||||
this.mutableHeaders.putAll(headers); |
||||
} |
||||
|
||||
@Override |
||||
public HttpHeaders getHeaders() { |
||||
return this.mutableHeaders; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,131 @@
@@ -0,0 +1,131 @@
|
||||
/* |
||||
* Copyright 2002-2024 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 |
||||
* |
||||
* https://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.function; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.UncheckedIOException; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Collections; |
||||
|
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.http.CacheControl; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
||||
import org.springframework.web.servlet.ModelAndView; |
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest; |
||||
import org.springframework.web.testfixture.servlet.MockHttpServletResponse; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link StreamingServerResponse}. |
||||
* @author Brian Clozel |
||||
*/ |
||||
class StreamingServerResponseTests { |
||||
|
||||
private MockHttpServletRequest mockRequest; |
||||
|
||||
private MockHttpServletResponse mockResponse; |
||||
|
||||
@BeforeEach |
||||
void setUp() { |
||||
this.mockRequest = new MockHttpServletRequest("GET", "https://example.com"); |
||||
this.mockRequest.setAsyncSupported(true); |
||||
this.mockResponse = new MockHttpServletResponse(); |
||||
} |
||||
|
||||
@Test |
||||
void writeSingleString() throws Exception { |
||||
String body = "data: foo bar\n\n"; |
||||
ServerResponse response = ServerResponse.ok() |
||||
.contentType(MediaType.TEXT_EVENT_STREAM) |
||||
.stream(stream -> { |
||||
try { |
||||
stream.write(body).complete(); |
||||
} |
||||
catch (IOException ex) { |
||||
throw new UncheckedIOException(ex); |
||||
} |
||||
}); |
||||
|
||||
ServerResponse.Context context = Collections::emptyList; |
||||
ModelAndView mav = response.writeTo(this.mockRequest, this.mockResponse, context); |
||||
assertThat(mav).isNull(); |
||||
assertThat(this.mockResponse.getContentType()).isEqualTo(MediaType.TEXT_EVENT_STREAM.toString()); |
||||
assertThat(this.mockResponse.getContentAsString()).isEqualTo(body); |
||||
} |
||||
|
||||
@Test |
||||
void writeBytes() throws Exception { |
||||
String body = "data: foo bar\n\n"; |
||||
ServerResponse response = ServerResponse |
||||
.ok() |
||||
.contentType(MediaType.TEXT_EVENT_STREAM) |
||||
.cacheControl(CacheControl.noCache()) |
||||
.stream(stream -> { |
||||
try { |
||||
stream.write(body.getBytes(StandardCharsets.UTF_8)).complete(); |
||||
} |
||||
catch (IOException ex) { |
||||
throw new UncheckedIOException(ex); |
||||
} |
||||
}); |
||||
ServerResponse.Context context = Collections::emptyList; |
||||
ModelAndView mav = response.writeTo(this.mockRequest, this.mockResponse, context); |
||||
assertThat(mav).isNull(); |
||||
assertThat(this.mockResponse.getContentType()).isEqualTo(MediaType.TEXT_EVENT_STREAM.toString()); |
||||
assertThat(this.mockResponse.getContentAsString()).isEqualTo(body); |
||||
} |
||||
|
||||
@Test |
||||
void writeWithConverters() throws Exception { |
||||
ServerResponse response = ServerResponse |
||||
.ok() |
||||
.contentType(MediaType.APPLICATION_NDJSON) |
||||
.cacheControl(CacheControl.noCache()) |
||||
.stream(stream -> { |
||||
try { |
||||
stream.write(new Person("John", 51), MediaType.APPLICATION_JSON) |
||||
.write(new byte[]{'\n'}) |
||||
.flush(); |
||||
stream.write(new Person("Jane", 42), MediaType.APPLICATION_JSON) |
||||
.write(new byte[]{'\n'}) |
||||
.complete(); |
||||
} |
||||
catch (IOException ex) { |
||||
throw new UncheckedIOException(ex); |
||||
} |
||||
}); |
||||
|
||||
ServerResponse.Context context = () -> Collections.singletonList(new MappingJackson2HttpMessageConverter()); |
||||
ModelAndView mav = response.writeTo(this.mockRequest, this.mockResponse, context); |
||||
assertThat(mav).isNull(); |
||||
assertThat(this.mockResponse.getContentType()).isEqualTo(MediaType.APPLICATION_NDJSON.toString()); |
||||
assertThat(this.mockResponse.getContentAsString()).isEqualTo(""" |
||||
{"name":"John","age":51} |
||||
{"name":"Jane","age":42} |
||||
"""); |
||||
} |
||||
|
||||
|
||||
record Person(String name, int age) { |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue