Skip to content

Commit

Permalink
Insert StringMessageConverter for SSE
Browse files Browse the repository at this point in the history
Closes gh-24465
  • Loading branch information
rstoyanchev committed Feb 5, 2020
1 parent 65c8a10 commit 9aea101
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand All @@ -18,6 +18,8 @@

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

Expand All @@ -33,6 +35,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -60,6 +63,8 @@ public class ResponseBodyEmitterReturnValueHandler implements HandlerMethodRetur

private final List<HttpMessageConverter<?>> messageConverters;

private final List<HttpMessageConverter<?>> sseMessageConverters;

private final ReactiveTypeHandler reactiveHandler;


Expand All @@ -72,6 +77,7 @@ public class ResponseBodyEmitterReturnValueHandler implements HandlerMethodRetur
public ResponseBodyEmitterReturnValueHandler(List<HttpMessageConverter<?>> messageConverters) {
Assert.notEmpty(messageConverters, "HttpMessageConverter List must not be empty");
this.messageConverters = messageConverters;
this.sseMessageConverters = initSseConverters(messageConverters);
this.reactiveHandler = new ReactiveTypeHandler();
}

Expand All @@ -88,9 +94,22 @@ public ResponseBodyEmitterReturnValueHandler(List<HttpMessageConverter<?>> messa

Assert.notEmpty(messageConverters, "HttpMessageConverter List must not be empty");
this.messageConverters = messageConverters;
this.sseMessageConverters = initSseConverters(messageConverters);
this.reactiveHandler = new ReactiveTypeHandler(registry, executor, manager);
}

private static List<HttpMessageConverter<?>> initSseConverters(List<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> converter : converters) {
if (converter.canWrite(String.class, MediaType.TEXT_PLAIN)) {
return converters;
}
}
List<HttpMessageConverter<?>> result = new ArrayList<>(converters.size() + 1);
result.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
result.addAll(converters);
return result;
}


@Override
public boolean supportsReturnType(MethodParameter returnType) {
Expand Down Expand Up @@ -186,7 +205,7 @@ public void send(Object data, @Nullable MediaType mediaType) throws IOException

@SuppressWarnings("unchecked")
private <T> void sendInternal(T data, @Nullable MediaType mediaType) throws IOException {
for (HttpMessageConverter<?> converter : ResponseBodyEmitterReturnValueHandler.this.messageConverters) {
for (HttpMessageConverter<?> converter : ResponseBodyEmitterReturnValueHandler.this.sseMessageConverters) {
if (converter.canWrite(data.getClass(), mediaType)) {
((HttpMessageConverter<T>) converter).write(data, mediaType, this.outputMessage);
this.outputMessage.flush();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand All @@ -16,7 +16,6 @@

package org.springframework.web.servlet.mvc.method.annotation;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -31,7 +30,6 @@
import org.springframework.core.ResolvableType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
Expand Down Expand Up @@ -72,8 +70,8 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
@BeforeEach
public void setup() throws Exception {

List<HttpMessageConverter<?>> converters = Arrays.asList(
new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter());
List<HttpMessageConverter<?>> converters =
Collections.singletonList(new MappingJackson2HttpMessageConverter());

this.handler = new ResponseBodyEmitterReturnValueHandler(converters);
this.request = new MockHttpServletRequest();
Expand Down

0 comments on commit 9aea101

Please sign in to comment.